1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2016 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  * Author: Huang Rui <ray.huang@amd.com>
23b843c749SSergey Zigachev  *
24b843c749SSergey Zigachev  */
25b843c749SSergey Zigachev #include "pp_debug.h"
26b843c749SSergey Zigachev #include <linux/types.h>
27b843c749SSergey Zigachev #include <linux/kernel.h>
28b843c749SSergey Zigachev #include <linux/slab.h>
29b843c749SSergey Zigachev #include <linux/gfp.h>
30b843c749SSergey Zigachev 
31b843c749SSergey Zigachev #include "smumgr.h"
32b843c749SSergey Zigachev #include "iceland_smumgr.h"
33b843c749SSergey Zigachev 
34b843c749SSergey Zigachev #include "ppsmc.h"
35b843c749SSergey Zigachev 
36b843c749SSergey Zigachev #include "cgs_common.h"
37b843c749SSergey Zigachev 
38b843c749SSergey Zigachev #include "smu7_dyn_defaults.h"
39b843c749SSergey Zigachev #include "smu7_hwmgr.h"
40b843c749SSergey Zigachev #include "hardwaremanager.h"
41b843c749SSergey Zigachev #include "ppatomctrl.h"
42b843c749SSergey Zigachev #include "atombios.h"
43b843c749SSergey Zigachev #include "pppcielanes.h"
44b843c749SSergey Zigachev #include "pp_endian.h"
45b843c749SSergey Zigachev #include "processpptables.h"
46b843c749SSergey Zigachev 
47b843c749SSergey Zigachev 
48b843c749SSergey Zigachev #include "smu/smu_7_1_1_d.h"
49b843c749SSergey Zigachev #include "smu/smu_7_1_1_sh_mask.h"
50b843c749SSergey Zigachev #include "smu71_discrete.h"
51b843c749SSergey Zigachev 
52b843c749SSergey Zigachev #include "smu_ucode_xfer_vi.h"
53b843c749SSergey Zigachev #include "gmc/gmc_8_1_d.h"
54b843c749SSergey Zigachev #include "gmc/gmc_8_1_sh_mask.h"
55b843c749SSergey Zigachev #include "bif/bif_5_0_d.h"
56b843c749SSergey Zigachev #include "bif/bif_5_0_sh_mask.h"
57b843c749SSergey Zigachev #include "dce/dce_10_0_d.h"
58b843c749SSergey Zigachev #include "dce/dce_10_0_sh_mask.h"
59b843c749SSergey Zigachev 
60b843c749SSergey Zigachev 
61b843c749SSergey Zigachev #define ICELAND_SMC_SIZE               0x20000
62b843c749SSergey Zigachev 
63b843c749SSergey Zigachev #define POWERTUNE_DEFAULT_SET_MAX    1
64b843c749SSergey Zigachev #define MC_CG_ARB_FREQ_F1           0x0b
65b843c749SSergey Zigachev #define VDDC_VDDCI_DELTA            200
66b843c749SSergey Zigachev 
67b843c749SSergey Zigachev #define DEVICE_ID_VI_ICELAND_M_6900	0x6900
68b843c749SSergey Zigachev #define DEVICE_ID_VI_ICELAND_M_6901	0x6901
69b843c749SSergey Zigachev #define DEVICE_ID_VI_ICELAND_M_6902	0x6902
70b843c749SSergey Zigachev #define DEVICE_ID_VI_ICELAND_M_6903	0x6903
71b843c749SSergey Zigachev 
72b843c749SSergey Zigachev static const struct iceland_pt_defaults defaults_iceland = {
73b843c749SSergey Zigachev 	/*
74b843c749SSergey Zigachev 	 * sviLoadLIneEn, SviLoadLineVddC, TDC_VDDC_ThrottleReleaseLimitPerc,
75b843c749SSergey Zigachev 	 * TDC_MAWt, TdcWaterfallCtl, DTEAmbientTempBase, DisplayCac, BAPM_TEMP_GRADIENT
76b843c749SSergey Zigachev 	 */
77b843c749SSergey Zigachev 	1, 0xF, 0xFD, 0x19, 5, 45, 0, 0xB0000,
78b843c749SSergey Zigachev 	{ 0x79,  0x253, 0x25D, 0xAE,  0x72,  0x80,  0x83,  0x86,  0x6F,  0xC8,  0xC9,  0xC9,  0x2F,  0x4D,  0x61  },
79b843c749SSergey Zigachev 	{ 0x17C, 0x172, 0x180, 0x1BC, 0x1B3, 0x1BD, 0x206, 0x200, 0x203, 0x25D, 0x25A, 0x255, 0x2C3, 0x2C5, 0x2B4 }
80b843c749SSergey Zigachev };
81b843c749SSergey Zigachev 
82b843c749SSergey Zigachev /* 35W - XT, XTL */
83b843c749SSergey Zigachev static const struct iceland_pt_defaults defaults_icelandxt = {
84b843c749SSergey Zigachev 	/*
85b843c749SSergey Zigachev 	 * sviLoadLIneEn, SviLoadLineVddC,
86b843c749SSergey Zigachev 	 * TDC_VDDC_ThrottleReleaseLimitPerc, TDC_MAWt,
87b843c749SSergey Zigachev 	 * TdcWaterfallCtl, DTEAmbientTempBase, DisplayCac,
88b843c749SSergey Zigachev 	 * BAPM_TEMP_GRADIENT
89b843c749SSergey Zigachev 	 */
90b843c749SSergey Zigachev 	1, 0xF, 0xFD, 0x19, 5, 45, 0, 0x0,
91b843c749SSergey Zigachev 	{ 0xA7,  0x0, 0x0, 0xB5,  0x0, 0x0, 0x9F,  0x0, 0x0, 0xD6,  0x0, 0x0, 0xD7,  0x0, 0x0},
92b843c749SSergey Zigachev 	{ 0x1EA, 0x0, 0x0, 0x224, 0x0, 0x0, 0x25E, 0x0, 0x0, 0x28E, 0x0, 0x0, 0x2AB, 0x0, 0x0}
93b843c749SSergey Zigachev };
94b843c749SSergey Zigachev 
95b843c749SSergey Zigachev /* 25W - PRO, LE */
96b843c749SSergey Zigachev static const struct iceland_pt_defaults defaults_icelandpro = {
97b843c749SSergey Zigachev 	/*
98b843c749SSergey Zigachev 	 * sviLoadLIneEn, SviLoadLineVddC,
99b843c749SSergey Zigachev 	 * TDC_VDDC_ThrottleReleaseLimitPerc, TDC_MAWt,
100b843c749SSergey Zigachev 	 * TdcWaterfallCtl, DTEAmbientTempBase, DisplayCac,
101b843c749SSergey Zigachev 	 * BAPM_TEMP_GRADIENT
102b843c749SSergey Zigachev 	 */
103b843c749SSergey Zigachev 	1, 0xF, 0xFD, 0x19, 5, 45, 0, 0x0,
104b843c749SSergey Zigachev 	{ 0xB7,  0x0, 0x0, 0xC3,  0x0, 0x0, 0xB5,  0x0, 0x0, 0xEA,  0x0, 0x0, 0xE6,  0x0, 0x0},
105b843c749SSergey Zigachev 	{ 0x1EA, 0x0, 0x0, 0x224, 0x0, 0x0, 0x25E, 0x0, 0x0, 0x28E, 0x0, 0x0, 0x2AB, 0x0, 0x0}
106b843c749SSergey Zigachev };
107b843c749SSergey Zigachev 
iceland_start_smc(struct pp_hwmgr * hwmgr)108b843c749SSergey Zigachev static int iceland_start_smc(struct pp_hwmgr *hwmgr)
109b843c749SSergey Zigachev {
110b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
111b843c749SSergey Zigachev 				  SMC_SYSCON_RESET_CNTL, rst_reg, 0);
112b843c749SSergey Zigachev 
113b843c749SSergey Zigachev 	return 0;
114b843c749SSergey Zigachev }
115b843c749SSergey Zigachev 
iceland_reset_smc(struct pp_hwmgr * hwmgr)116b843c749SSergey Zigachev static void iceland_reset_smc(struct pp_hwmgr *hwmgr)
117b843c749SSergey Zigachev {
118b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
119b843c749SSergey Zigachev 				  SMC_SYSCON_RESET_CNTL,
120b843c749SSergey Zigachev 				  rst_reg, 1);
121b843c749SSergey Zigachev }
122b843c749SSergey Zigachev 
123b843c749SSergey Zigachev 
iceland_stop_smc_clock(struct pp_hwmgr * hwmgr)124b843c749SSergey Zigachev static void iceland_stop_smc_clock(struct pp_hwmgr *hwmgr)
125b843c749SSergey Zigachev {
126b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
127b843c749SSergey Zigachev 				  SMC_SYSCON_CLOCK_CNTL_0,
128b843c749SSergey Zigachev 				  ck_disable, 1);
129b843c749SSergey Zigachev }
130b843c749SSergey Zigachev 
iceland_start_smc_clock(struct pp_hwmgr * hwmgr)131b843c749SSergey Zigachev static void iceland_start_smc_clock(struct pp_hwmgr *hwmgr)
132b843c749SSergey Zigachev {
133b843c749SSergey Zigachev 	PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
134b843c749SSergey Zigachev 				  SMC_SYSCON_CLOCK_CNTL_0,
135b843c749SSergey Zigachev 				  ck_disable, 0);
136b843c749SSergey Zigachev }
137b843c749SSergey Zigachev 
iceland_smu_start_smc(struct pp_hwmgr * hwmgr)138b843c749SSergey Zigachev static int iceland_smu_start_smc(struct pp_hwmgr *hwmgr)
139b843c749SSergey Zigachev {
140b843c749SSergey Zigachev 	/* set smc instruct start point at 0x0 */
141b843c749SSergey Zigachev 	smu7_program_jump_on_start(hwmgr);
142b843c749SSergey Zigachev 
143b843c749SSergey Zigachev 	/* enable smc clock */
144b843c749SSergey Zigachev 	iceland_start_smc_clock(hwmgr);
145b843c749SSergey Zigachev 
146b843c749SSergey Zigachev 	/* de-assert reset */
147b843c749SSergey Zigachev 	iceland_start_smc(hwmgr);
148b843c749SSergey Zigachev 
149b843c749SSergey Zigachev 	PHM_WAIT_INDIRECT_FIELD(hwmgr, SMC_IND, FIRMWARE_FLAGS,
150b843c749SSergey Zigachev 				 INTERRUPTS_ENABLED, 1);
151b843c749SSergey Zigachev 
152b843c749SSergey Zigachev 	return 0;
153b843c749SSergey Zigachev }
154b843c749SSergey Zigachev 
155b843c749SSergey Zigachev 
iceland_upload_smc_firmware_data(struct pp_hwmgr * hwmgr,uint32_t length,const uint8_t * src,uint32_t limit,uint32_t start_addr)156b843c749SSergey Zigachev static int iceland_upload_smc_firmware_data(struct pp_hwmgr *hwmgr,
157b843c749SSergey Zigachev 					uint32_t length, const uint8_t *src,
158b843c749SSergey Zigachev 					uint32_t limit, uint32_t start_addr)
159b843c749SSergey Zigachev {
160b843c749SSergey Zigachev 	uint32_t byte_count = length;
161b843c749SSergey Zigachev 	uint32_t data;
162b843c749SSergey Zigachev 
163b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((limit >= byte_count), "SMC address is beyond the SMC RAM area.", return -EINVAL);
164b843c749SSergey Zigachev 
165b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmSMC_IND_INDEX_0, start_addr);
166b843c749SSergey Zigachev 	PHM_WRITE_FIELD(hwmgr->device, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 1);
167b843c749SSergey Zigachev 
168b843c749SSergey Zigachev 	while (byte_count >= 4) {
169b843c749SSergey Zigachev 		data = src[0] * 0x1000000 + src[1] * 0x10000 + src[2] * 0x100 + src[3];
170b843c749SSergey Zigachev 		cgs_write_register(hwmgr->device, mmSMC_IND_DATA_0, data);
171b843c749SSergey Zigachev 		src += 4;
172b843c749SSergey Zigachev 		byte_count -= 4;
173b843c749SSergey Zigachev 	}
174b843c749SSergey Zigachev 
175b843c749SSergey Zigachev 	PHM_WRITE_FIELD(hwmgr->device, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 0);
176b843c749SSergey Zigachev 
177b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == byte_count), "SMC size must be divisible by 4.", return -EINVAL);
178b843c749SSergey Zigachev 
179b843c749SSergey Zigachev 	return 0;
180b843c749SSergey Zigachev }
181b843c749SSergey Zigachev 
182b843c749SSergey Zigachev 
iceland_smu_upload_firmware_image(struct pp_hwmgr * hwmgr)183b843c749SSergey Zigachev static int iceland_smu_upload_firmware_image(struct pp_hwmgr *hwmgr)
184b843c749SSergey Zigachev {
185b843c749SSergey Zigachev 	uint32_t val;
186b843c749SSergey Zigachev 	struct cgs_firmware_info info = {0};
187b843c749SSergey Zigachev 
188b843c749SSergey Zigachev 	if (hwmgr == NULL || hwmgr->device == NULL)
189b843c749SSergey Zigachev 		return -EINVAL;
190b843c749SSergey Zigachev 
191b843c749SSergey Zigachev 	/* load SMC firmware */
192b843c749SSergey Zigachev 	cgs_get_firmware_info(hwmgr->device,
193b843c749SSergey Zigachev 		smu7_convert_fw_type_to_cgs(UCODE_ID_SMU), &info);
194b843c749SSergey Zigachev 
195b843c749SSergey Zigachev 	if (info.image_size & 3) {
196b843c749SSergey Zigachev 		pr_err("[ powerplay ] SMC ucode is not 4 bytes aligned\n");
197b843c749SSergey Zigachev 		return -EINVAL;
198b843c749SSergey Zigachev 	}
199b843c749SSergey Zigachev 
200b843c749SSergey Zigachev 	if (info.image_size > ICELAND_SMC_SIZE) {
201b843c749SSergey Zigachev 		pr_err("[ powerplay ] SMC address is beyond the SMC RAM area\n");
202b843c749SSergey Zigachev 		return -EINVAL;
203b843c749SSergey Zigachev 	}
204b843c749SSergey Zigachev 	hwmgr->smu_version = info.version;
205b843c749SSergey Zigachev 	/* wait for smc boot up */
206b843c749SSergey Zigachev 	PHM_WAIT_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND,
207b843c749SSergey Zigachev 					 RCU_UC_EVENTS, boot_seq_done, 0);
208b843c749SSergey Zigachev 
209b843c749SSergey Zigachev 	/* clear firmware interrupt enable flag */
210b843c749SSergey Zigachev 	val = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
211b843c749SSergey Zigachev 				    ixSMC_SYSCON_MISC_CNTL);
212b843c749SSergey Zigachev 	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
213b843c749SSergey Zigachev 			       ixSMC_SYSCON_MISC_CNTL, val | 1);
214b843c749SSergey Zigachev 
215b843c749SSergey Zigachev 	/* stop smc clock */
216b843c749SSergey Zigachev 	iceland_stop_smc_clock(hwmgr);
217b843c749SSergey Zigachev 
218b843c749SSergey Zigachev 	/* reset smc */
219b843c749SSergey Zigachev 	iceland_reset_smc(hwmgr);
220b843c749SSergey Zigachev 	iceland_upload_smc_firmware_data(hwmgr, info.image_size,
221b843c749SSergey Zigachev 				(uint8_t *)info.kptr, ICELAND_SMC_SIZE,
222b843c749SSergey Zigachev 				info.ucode_start_address);
223b843c749SSergey Zigachev 
224b843c749SSergey Zigachev 	return 0;
225b843c749SSergey Zigachev }
226b843c749SSergey Zigachev 
iceland_request_smu_load_specific_fw(struct pp_hwmgr * hwmgr,uint32_t firmwareType)227b843c749SSergey Zigachev static int iceland_request_smu_load_specific_fw(struct pp_hwmgr *hwmgr,
228b843c749SSergey Zigachev 						uint32_t firmwareType)
229b843c749SSergey Zigachev {
230b843c749SSergey Zigachev 	return 0;
231b843c749SSergey Zigachev }
232b843c749SSergey Zigachev 
iceland_start_smu(struct pp_hwmgr * hwmgr)233b843c749SSergey Zigachev static int iceland_start_smu(struct pp_hwmgr *hwmgr)
234b843c749SSergey Zigachev {
235b843c749SSergey Zigachev 	int result;
236b843c749SSergey Zigachev 
237b843c749SSergey Zigachev 	result = iceland_smu_upload_firmware_image(hwmgr);
238b843c749SSergey Zigachev 	if (result)
239b843c749SSergey Zigachev 		return result;
240b843c749SSergey Zigachev 	result = iceland_smu_start_smc(hwmgr);
241b843c749SSergey Zigachev 	if (result)
242b843c749SSergey Zigachev 		return result;
243b843c749SSergey Zigachev 
244b843c749SSergey Zigachev 	if (!smu7_is_smc_ram_running(hwmgr)) {
245b843c749SSergey Zigachev 		pr_info("smu not running, upload firmware again \n");
246b843c749SSergey Zigachev 		result = iceland_smu_upload_firmware_image(hwmgr);
247b843c749SSergey Zigachev 		if (result)
248b843c749SSergey Zigachev 			return result;
249b843c749SSergey Zigachev 
250b843c749SSergey Zigachev 		result = iceland_smu_start_smc(hwmgr);
251b843c749SSergey Zigachev 		if (result)
252b843c749SSergey Zigachev 			return result;
253b843c749SSergey Zigachev 	}
254b843c749SSergey Zigachev 
255b843c749SSergey Zigachev 	result = smu7_request_smu_load_fw(hwmgr);
256b843c749SSergey Zigachev 
257b843c749SSergey Zigachev 	return result;
258b843c749SSergey Zigachev }
259b843c749SSergey Zigachev 
iceland_smu_init(struct pp_hwmgr * hwmgr)260b843c749SSergey Zigachev static int iceland_smu_init(struct pp_hwmgr *hwmgr)
261b843c749SSergey Zigachev {
262b843c749SSergey Zigachev 	struct iceland_smumgr *iceland_priv = NULL;
263b843c749SSergey Zigachev 
264b843c749SSergey Zigachev 	iceland_priv = kzalloc(sizeof(struct iceland_smumgr), GFP_KERNEL);
265b843c749SSergey Zigachev 
266b843c749SSergey Zigachev 	if (iceland_priv == NULL)
267b843c749SSergey Zigachev 		return -ENOMEM;
268b843c749SSergey Zigachev 
269b843c749SSergey Zigachev 	hwmgr->smu_backend = iceland_priv;
270b843c749SSergey Zigachev 
271b843c749SSergey Zigachev 	if (smu7_init(hwmgr)) {
272b843c749SSergey Zigachev 		kfree(iceland_priv);
273b843c749SSergey Zigachev 		return -EINVAL;
274b843c749SSergey Zigachev 	}
275b843c749SSergey Zigachev 
276b843c749SSergey Zigachev 	return 0;
277b843c749SSergey Zigachev }
278b843c749SSergey Zigachev 
279b843c749SSergey Zigachev 
iceland_initialize_power_tune_defaults(struct pp_hwmgr * hwmgr)280b843c749SSergey Zigachev static void iceland_initialize_power_tune_defaults(struct pp_hwmgr *hwmgr)
281b843c749SSergey Zigachev {
282b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
283b843c749SSergey Zigachev 	struct amdgpu_device *adev = hwmgr->adev;
284b843c749SSergey Zigachev 	uint32_t dev_id;
285b843c749SSergey Zigachev 
286b843c749SSergey Zigachev 	dev_id = adev->pdev->device;
287b843c749SSergey Zigachev 
288b843c749SSergey Zigachev 	switch (dev_id) {
289b843c749SSergey Zigachev 	case DEVICE_ID_VI_ICELAND_M_6900:
290b843c749SSergey Zigachev 	case DEVICE_ID_VI_ICELAND_M_6903:
291b843c749SSergey Zigachev 		smu_data->power_tune_defaults = &defaults_icelandxt;
292b843c749SSergey Zigachev 		break;
293b843c749SSergey Zigachev 
294b843c749SSergey Zigachev 	case DEVICE_ID_VI_ICELAND_M_6901:
295b843c749SSergey Zigachev 	case DEVICE_ID_VI_ICELAND_M_6902:
296b843c749SSergey Zigachev 		smu_data->power_tune_defaults = &defaults_icelandpro;
297b843c749SSergey Zigachev 		break;
298b843c749SSergey Zigachev 	default:
299b843c749SSergey Zigachev 		smu_data->power_tune_defaults = &defaults_iceland;
300b843c749SSergey Zigachev 		pr_warn("Unknown V.I. Device ID.\n");
301b843c749SSergey Zigachev 		break;
302b843c749SSergey Zigachev 	}
303b843c749SSergey Zigachev 	return;
304b843c749SSergey Zigachev }
305b843c749SSergey Zigachev 
iceland_populate_svi_load_line(struct pp_hwmgr * hwmgr)306b843c749SSergey Zigachev static int iceland_populate_svi_load_line(struct pp_hwmgr *hwmgr)
307b843c749SSergey Zigachev {
308b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
309b843c749SSergey Zigachev 	const struct iceland_pt_defaults *defaults = smu_data->power_tune_defaults;
310b843c749SSergey Zigachev 
311b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineEn = defaults->svi_load_line_en;
312b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineVddC = defaults->svi_load_line_vddc;
313b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineTrimVddC = 3;
314b843c749SSergey Zigachev 	smu_data->power_tune_table.SviLoadLineOffsetVddC = 0;
315b843c749SSergey Zigachev 
316b843c749SSergey Zigachev 	return 0;
317b843c749SSergey Zigachev }
318b843c749SSergey Zigachev 
iceland_populate_tdc_limit(struct pp_hwmgr * hwmgr)319b843c749SSergey Zigachev static int iceland_populate_tdc_limit(struct pp_hwmgr *hwmgr)
320b843c749SSergey Zigachev {
321b843c749SSergey Zigachev 	uint16_t tdc_limit;
322b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
323b843c749SSergey Zigachev 	const struct iceland_pt_defaults *defaults = smu_data->power_tune_defaults;
324b843c749SSergey Zigachev 
325b843c749SSergey Zigachev 	tdc_limit = (uint16_t)(hwmgr->dyn_state.cac_dtp_table->usTDC * 256);
326b843c749SSergey Zigachev 	smu_data->power_tune_table.TDC_VDDC_PkgLimit =
327b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(tdc_limit);
328b843c749SSergey Zigachev 	smu_data->power_tune_table.TDC_VDDC_ThrottleReleaseLimitPerc =
329b843c749SSergey Zigachev 			defaults->tdc_vddc_throttle_release_limit_perc;
330b843c749SSergey Zigachev 	smu_data->power_tune_table.TDC_MAWt = defaults->tdc_mawt;
331b843c749SSergey Zigachev 
332b843c749SSergey Zigachev 	return 0;
333b843c749SSergey Zigachev }
334b843c749SSergey Zigachev 
iceland_populate_dw8(struct pp_hwmgr * hwmgr,uint32_t fuse_table_offset)335b843c749SSergey Zigachev static int iceland_populate_dw8(struct pp_hwmgr *hwmgr, uint32_t fuse_table_offset)
336b843c749SSergey Zigachev {
337b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
338b843c749SSergey Zigachev 	const struct iceland_pt_defaults *defaults = smu_data->power_tune_defaults;
339b843c749SSergey Zigachev 	uint32_t temp;
340b843c749SSergey Zigachev 
341b843c749SSergey Zigachev 	if (smu7_read_smc_sram_dword(hwmgr,
342b843c749SSergey Zigachev 			fuse_table_offset +
343b843c749SSergey Zigachev 			offsetof(SMU71_Discrete_PmFuses, TdcWaterfallCtl),
344b843c749SSergey Zigachev 			(uint32_t *)&temp, SMC_RAM_END))
345b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false,
346b843c749SSergey Zigachev 				"Attempt to read PmFuses.DW6 (SviLoadLineEn) from SMC Failed!",
347b843c749SSergey Zigachev 				return -EINVAL);
348b843c749SSergey Zigachev 	else
349b843c749SSergey Zigachev 		smu_data->power_tune_table.TdcWaterfallCtl = defaults->tdc_waterfall_ctl;
350b843c749SSergey Zigachev 
351b843c749SSergey Zigachev 	return 0;
352b843c749SSergey Zigachev }
353b843c749SSergey Zigachev 
iceland_populate_temperature_scaler(struct pp_hwmgr * hwmgr)354b843c749SSergey Zigachev static int iceland_populate_temperature_scaler(struct pp_hwmgr *hwmgr)
355b843c749SSergey Zigachev {
356b843c749SSergey Zigachev 	return 0;
357b843c749SSergey Zigachev }
358b843c749SSergey Zigachev 
iceland_populate_gnb_lpml(struct pp_hwmgr * hwmgr)359b843c749SSergey Zigachev static int iceland_populate_gnb_lpml(struct pp_hwmgr *hwmgr)
360b843c749SSergey Zigachev {
361b843c749SSergey Zigachev 	int i;
362b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
363b843c749SSergey Zigachev 
364b843c749SSergey Zigachev 	/* Currently not used. Set all to zero. */
365b843c749SSergey Zigachev 	for (i = 0; i < 8; i++)
366b843c749SSergey Zigachev 		smu_data->power_tune_table.GnbLPML[i] = 0;
367b843c749SSergey Zigachev 
368b843c749SSergey Zigachev 	return 0;
369b843c749SSergey Zigachev }
370b843c749SSergey Zigachev 
iceland_populate_bapm_vddc_base_leakage_sidd(struct pp_hwmgr * hwmgr)371b843c749SSergey Zigachev static int iceland_populate_bapm_vddc_base_leakage_sidd(struct pp_hwmgr *hwmgr)
372b843c749SSergey Zigachev {
373b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
374b843c749SSergey Zigachev 	uint16_t HiSidd = smu_data->power_tune_table.BapmVddCBaseLeakageHiSidd;
375b843c749SSergey Zigachev 	uint16_t LoSidd = smu_data->power_tune_table.BapmVddCBaseLeakageLoSidd;
376b843c749SSergey Zigachev 	struct phm_cac_tdp_table *cac_table = hwmgr->dyn_state.cac_dtp_table;
377b843c749SSergey Zigachev 
378b843c749SSergey Zigachev 	HiSidd = (uint16_t)(cac_table->usHighCACLeakage / 100 * 256);
379b843c749SSergey Zigachev 	LoSidd = (uint16_t)(cac_table->usLowCACLeakage / 100 * 256);
380b843c749SSergey Zigachev 
381b843c749SSergey Zigachev 	smu_data->power_tune_table.BapmVddCBaseLeakageHiSidd =
382b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(HiSidd);
383b843c749SSergey Zigachev 	smu_data->power_tune_table.BapmVddCBaseLeakageLoSidd =
384b843c749SSergey Zigachev 			CONVERT_FROM_HOST_TO_SMC_US(LoSidd);
385b843c749SSergey Zigachev 
386b843c749SSergey Zigachev 	return 0;
387b843c749SSergey Zigachev }
388b843c749SSergey Zigachev 
iceland_populate_bapm_vddc_vid_sidd(struct pp_hwmgr * hwmgr)389b843c749SSergey Zigachev static int iceland_populate_bapm_vddc_vid_sidd(struct pp_hwmgr *hwmgr)
390b843c749SSergey Zigachev {
391b843c749SSergey Zigachev 	int i;
392b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
393b843c749SSergey Zigachev 	uint8_t *hi_vid = smu_data->power_tune_table.BapmVddCVidHiSidd;
394b843c749SSergey Zigachev 	uint8_t *lo_vid = smu_data->power_tune_table.BapmVddCVidLoSidd;
395b843c749SSergey Zigachev 
396b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(NULL != hwmgr->dyn_state.cac_leakage_table,
397b843c749SSergey Zigachev 			    "The CAC Leakage table does not exist!", return -EINVAL);
398b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(hwmgr->dyn_state.cac_leakage_table->count <= 8,
399b843c749SSergey Zigachev 			    "There should never be more than 8 entries for BapmVddcVid!!!", return -EINVAL);
400b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(hwmgr->dyn_state.cac_leakage_table->count == hwmgr->dyn_state.vddc_dependency_on_sclk->count,
401b843c749SSergey Zigachev 			    "CACLeakageTable->count and VddcDependencyOnSCLk->count not equal", return -EINVAL);
402b843c749SSergey Zigachev 
403b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_EVV)) {
404b843c749SSergey Zigachev 		for (i = 0; (uint32_t) i < hwmgr->dyn_state.cac_leakage_table->count; i++) {
405b843c749SSergey Zigachev 			lo_vid[i] = convert_to_vid(hwmgr->dyn_state.cac_leakage_table->entries[i].Vddc1);
406b843c749SSergey Zigachev 			hi_vid[i] = convert_to_vid(hwmgr->dyn_state.cac_leakage_table->entries[i].Vddc2);
407b843c749SSergey Zigachev 		}
408b843c749SSergey Zigachev 	} else {
409b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(false, "Iceland should always support EVV", return -EINVAL);
410b843c749SSergey Zigachev 	}
411b843c749SSergey Zigachev 
412b843c749SSergey Zigachev 	return 0;
413b843c749SSergey Zigachev }
414b843c749SSergey Zigachev 
iceland_populate_vddc_vid(struct pp_hwmgr * hwmgr)415b843c749SSergey Zigachev static int iceland_populate_vddc_vid(struct pp_hwmgr *hwmgr)
416b843c749SSergey Zigachev {
417b843c749SSergey Zigachev 	int i;
418b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
419b843c749SSergey Zigachev 	uint8_t *vid = smu_data->power_tune_table.VddCVid;
420b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
421b843c749SSergey Zigachev 
422b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(data->vddc_voltage_table.count <= 8,
423b843c749SSergey Zigachev 		"There should never be more than 8 entries for VddcVid!!!",
424b843c749SSergey Zigachev 		return -EINVAL);
425b843c749SSergey Zigachev 
426b843c749SSergey Zigachev 	for (i = 0; i < (int)data->vddc_voltage_table.count; i++) {
427b843c749SSergey Zigachev 		vid[i] = convert_to_vid(data->vddc_voltage_table.entries[i].value);
428b843c749SSergey Zigachev 	}
429b843c749SSergey Zigachev 
430b843c749SSergey Zigachev 	return 0;
431b843c749SSergey Zigachev }
432b843c749SSergey Zigachev 
433b843c749SSergey Zigachev 
434b843c749SSergey Zigachev 
iceland_populate_pm_fuses(struct pp_hwmgr * hwmgr)435b843c749SSergey Zigachev static int iceland_populate_pm_fuses(struct pp_hwmgr *hwmgr)
436b843c749SSergey Zigachev {
437b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
438b843c749SSergey Zigachev 	uint32_t pm_fuse_table_offset;
439b843c749SSergey Zigachev 
440b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
441b843c749SSergey Zigachev 			PHM_PlatformCaps_PowerContainment)) {
442b843c749SSergey Zigachev 		if (smu7_read_smc_sram_dword(hwmgr,
443b843c749SSergey Zigachev 				SMU71_FIRMWARE_HEADER_LOCATION +
444b843c749SSergey Zigachev 				offsetof(SMU71_Firmware_Header, PmFuseTable),
445b843c749SSergey Zigachev 				&pm_fuse_table_offset, SMC_RAM_END))
446b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
447b843c749SSergey Zigachev 					"Attempt to get pm_fuse_table_offset Failed!",
448b843c749SSergey Zigachev 					return -EINVAL);
449b843c749SSergey Zigachev 
450b843c749SSergey Zigachev 		/* DW0 - DW3 */
451b843c749SSergey Zigachev 		if (iceland_populate_bapm_vddc_vid_sidd(hwmgr))
452b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
453b843c749SSergey Zigachev 					"Attempt to populate bapm vddc vid Failed!",
454b843c749SSergey Zigachev 					return -EINVAL);
455b843c749SSergey Zigachev 
456b843c749SSergey Zigachev 		/* DW4 - DW5 */
457b843c749SSergey Zigachev 		if (iceland_populate_vddc_vid(hwmgr))
458b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
459b843c749SSergey Zigachev 					"Attempt to populate vddc vid Failed!",
460b843c749SSergey Zigachev 					return -EINVAL);
461b843c749SSergey Zigachev 
462b843c749SSergey Zigachev 		/* DW6 */
463b843c749SSergey Zigachev 		if (iceland_populate_svi_load_line(hwmgr))
464b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
465b843c749SSergey Zigachev 					"Attempt to populate SviLoadLine Failed!",
466b843c749SSergey Zigachev 					return -EINVAL);
467b843c749SSergey Zigachev 		/* DW7 */
468b843c749SSergey Zigachev 		if (iceland_populate_tdc_limit(hwmgr))
469b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
470b843c749SSergey Zigachev 					"Attempt to populate TDCLimit Failed!", return -EINVAL);
471b843c749SSergey Zigachev 		/* DW8 */
472b843c749SSergey Zigachev 		if (iceland_populate_dw8(hwmgr, pm_fuse_table_offset))
473b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
474b843c749SSergey Zigachev 					"Attempt to populate TdcWaterfallCtl, "
475b843c749SSergey Zigachev 					"LPMLTemperature Min and Max Failed!",
476b843c749SSergey Zigachev 					return -EINVAL);
477b843c749SSergey Zigachev 
478b843c749SSergey Zigachev 		/* DW9-DW12 */
479b843c749SSergey Zigachev 		if (0 != iceland_populate_temperature_scaler(hwmgr))
480b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
481b843c749SSergey Zigachev 					"Attempt to populate LPMLTemperatureScaler Failed!",
482b843c749SSergey Zigachev 					return -EINVAL);
483b843c749SSergey Zigachev 
484b843c749SSergey Zigachev 		/* DW13-DW16 */
485b843c749SSergey Zigachev 		if (iceland_populate_gnb_lpml(hwmgr))
486b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
487b843c749SSergey Zigachev 					"Attempt to populate GnbLPML Failed!",
488b843c749SSergey Zigachev 					return -EINVAL);
489b843c749SSergey Zigachev 
490b843c749SSergey Zigachev 		/* DW18 */
491b843c749SSergey Zigachev 		if (iceland_populate_bapm_vddc_base_leakage_sidd(hwmgr))
492b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
493b843c749SSergey Zigachev 					"Attempt to populate BapmVddCBaseLeakage Hi and Lo Sidd Failed!",
494b843c749SSergey Zigachev 					return -EINVAL);
495b843c749SSergey Zigachev 
496b843c749SSergey Zigachev 		if (smu7_copy_bytes_to_smc(hwmgr, pm_fuse_table_offset,
497b843c749SSergey Zigachev 				(uint8_t *)&smu_data->power_tune_table,
498b843c749SSergey Zigachev 				sizeof(struct SMU71_Discrete_PmFuses), SMC_RAM_END))
499b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(false,
500b843c749SSergey Zigachev 					"Attempt to download PmFuseTable Failed!",
501b843c749SSergey Zigachev 					return -EINVAL);
502b843c749SSergey Zigachev 	}
503b843c749SSergey Zigachev 	return 0;
504b843c749SSergey Zigachev }
505b843c749SSergey Zigachev 
iceland_get_dependency_volt_by_clk(struct pp_hwmgr * hwmgr,struct phm_clock_voltage_dependency_table * allowed_clock_voltage_table,uint32_t clock,uint32_t * vol)506b843c749SSergey Zigachev static int iceland_get_dependency_volt_by_clk(struct pp_hwmgr *hwmgr,
507b843c749SSergey Zigachev 	struct phm_clock_voltage_dependency_table *allowed_clock_voltage_table,
508b843c749SSergey Zigachev 	uint32_t clock, uint32_t *vol)
509b843c749SSergey Zigachev {
510b843c749SSergey Zigachev 	uint32_t i = 0;
511b843c749SSergey Zigachev 
512b843c749SSergey Zigachev 	/* clock - voltage dependency table is empty table */
513b843c749SSergey Zigachev 	if (allowed_clock_voltage_table->count == 0)
514b843c749SSergey Zigachev 		return -EINVAL;
515b843c749SSergey Zigachev 
516b843c749SSergey Zigachev 	for (i = 0; i < allowed_clock_voltage_table->count; i++) {
517b843c749SSergey Zigachev 		/* find first sclk bigger than request */
518b843c749SSergey Zigachev 		if (allowed_clock_voltage_table->entries[i].clk >= clock) {
519b843c749SSergey Zigachev 			*vol = allowed_clock_voltage_table->entries[i].v;
520b843c749SSergey Zigachev 			return 0;
521b843c749SSergey Zigachev 		}
522b843c749SSergey Zigachev 	}
523b843c749SSergey Zigachev 
524b843c749SSergey Zigachev 	/* sclk is bigger than max sclk in the dependence table */
525b843c749SSergey Zigachev 	*vol = allowed_clock_voltage_table->entries[i - 1].v;
526b843c749SSergey Zigachev 
527b843c749SSergey Zigachev 	return 0;
528b843c749SSergey Zigachev }
529b843c749SSergey Zigachev 
iceland_get_std_voltage_value_sidd(struct pp_hwmgr * hwmgr,pp_atomctrl_voltage_table_entry * tab,uint16_t * hi,uint16_t * lo)530b843c749SSergey Zigachev static int iceland_get_std_voltage_value_sidd(struct pp_hwmgr *hwmgr,
531b843c749SSergey Zigachev 		pp_atomctrl_voltage_table_entry *tab, uint16_t *hi,
532b843c749SSergey Zigachev 		uint16_t *lo)
533b843c749SSergey Zigachev {
534b843c749SSergey Zigachev 	uint16_t v_index;
535b843c749SSergey Zigachev 	bool vol_found = false;
536b843c749SSergey Zigachev 	*hi = tab->value * VOLTAGE_SCALE;
537b843c749SSergey Zigachev 	*lo = tab->value * VOLTAGE_SCALE;
538b843c749SSergey Zigachev 
539b843c749SSergey Zigachev 	/* SCLK/VDDC Dependency Table has to exist. */
540b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(NULL != hwmgr->dyn_state.vddc_dependency_on_sclk,
541b843c749SSergey Zigachev 			"The SCLK/VDDC Dependency Table does not exist.",
542b843c749SSergey Zigachev 			return -EINVAL);
543b843c749SSergey Zigachev 
544b843c749SSergey Zigachev 	if (NULL == hwmgr->dyn_state.cac_leakage_table) {
545b843c749SSergey Zigachev 		pr_warn("CAC Leakage Table does not exist, using vddc.\n");
546b843c749SSergey Zigachev 		return 0;
547b843c749SSergey Zigachev 	}
548b843c749SSergey Zigachev 
549b843c749SSergey Zigachev 	/*
550b843c749SSergey Zigachev 	 * Since voltage in the sclk/vddc dependency table is not
551b843c749SSergey Zigachev 	 * necessarily in ascending order because of ELB voltage
552b843c749SSergey Zigachev 	 * patching, loop through entire list to find exact voltage.
553b843c749SSergey Zigachev 	 */
554b843c749SSergey Zigachev 	for (v_index = 0; (uint32_t)v_index < hwmgr->dyn_state.vddc_dependency_on_sclk->count; v_index++) {
555b843c749SSergey Zigachev 		if (tab->value == hwmgr->dyn_state.vddc_dependency_on_sclk->entries[v_index].v) {
556b843c749SSergey Zigachev 			vol_found = true;
557b843c749SSergey Zigachev 			if ((uint32_t)v_index < hwmgr->dyn_state.cac_leakage_table->count) {
558b843c749SSergey Zigachev 				*lo = hwmgr->dyn_state.cac_leakage_table->entries[v_index].Vddc * VOLTAGE_SCALE;
559b843c749SSergey Zigachev 				*hi = (uint16_t)(hwmgr->dyn_state.cac_leakage_table->entries[v_index].Leakage * VOLTAGE_SCALE);
560b843c749SSergey Zigachev 			} else {
561b843c749SSergey Zigachev 				pr_warn("Index from SCLK/VDDC Dependency Table exceeds the CAC Leakage Table index, using maximum index from CAC table.\n");
562b843c749SSergey Zigachev 				*lo = hwmgr->dyn_state.cac_leakage_table->entries[hwmgr->dyn_state.cac_leakage_table->count - 1].Vddc * VOLTAGE_SCALE;
563b843c749SSergey Zigachev 				*hi = (uint16_t)(hwmgr->dyn_state.cac_leakage_table->entries[hwmgr->dyn_state.cac_leakage_table->count - 1].Leakage * VOLTAGE_SCALE);
564b843c749SSergey Zigachev 			}
565b843c749SSergey Zigachev 			break;
566b843c749SSergey Zigachev 		}
567b843c749SSergey Zigachev 	}
568b843c749SSergey Zigachev 
569b843c749SSergey Zigachev 	/*
570b843c749SSergey Zigachev 	 * If voltage is not found in the first pass, loop again to
571b843c749SSergey Zigachev 	 * find the best match, equal or higher value.
572b843c749SSergey Zigachev 	 */
573b843c749SSergey Zigachev 	if (!vol_found) {
574b843c749SSergey Zigachev 		for (v_index = 0; (uint32_t)v_index < hwmgr->dyn_state.vddc_dependency_on_sclk->count; v_index++) {
575b843c749SSergey Zigachev 			if (tab->value <= hwmgr->dyn_state.vddc_dependency_on_sclk->entries[v_index].v) {
576b843c749SSergey Zigachev 				vol_found = true;
577b843c749SSergey Zigachev 				if ((uint32_t)v_index < hwmgr->dyn_state.cac_leakage_table->count) {
578b843c749SSergey Zigachev 					*lo = hwmgr->dyn_state.cac_leakage_table->entries[v_index].Vddc * VOLTAGE_SCALE;
579b843c749SSergey Zigachev 					*hi = (uint16_t)(hwmgr->dyn_state.cac_leakage_table->entries[v_index].Leakage) * VOLTAGE_SCALE;
580b843c749SSergey Zigachev 				} else {
581b843c749SSergey Zigachev 					pr_warn("Index from SCLK/VDDC Dependency Table exceeds the CAC Leakage Table index in second look up, using maximum index from CAC table.");
582b843c749SSergey Zigachev 					*lo = hwmgr->dyn_state.cac_leakage_table->entries[hwmgr->dyn_state.cac_leakage_table->count - 1].Vddc * VOLTAGE_SCALE;
583b843c749SSergey Zigachev 					*hi = (uint16_t)(hwmgr->dyn_state.cac_leakage_table->entries[hwmgr->dyn_state.cac_leakage_table->count - 1].Leakage * VOLTAGE_SCALE);
584b843c749SSergey Zigachev 				}
585b843c749SSergey Zigachev 				break;
586b843c749SSergey Zigachev 			}
587b843c749SSergey Zigachev 		}
588b843c749SSergey Zigachev 
589b843c749SSergey Zigachev 		if (!vol_found)
590b843c749SSergey Zigachev 			pr_warn("Unable to get std_vddc from SCLK/VDDC Dependency Table, using vddc.\n");
591b843c749SSergey Zigachev 	}
592b843c749SSergey Zigachev 
593b843c749SSergey Zigachev 	return 0;
594b843c749SSergey Zigachev }
595b843c749SSergey Zigachev 
iceland_populate_smc_voltage_table(struct pp_hwmgr * hwmgr,pp_atomctrl_voltage_table_entry * tab,SMU71_Discrete_VoltageLevel * smc_voltage_tab)596b843c749SSergey Zigachev static int iceland_populate_smc_voltage_table(struct pp_hwmgr *hwmgr,
597b843c749SSergey Zigachev 		pp_atomctrl_voltage_table_entry *tab,
598b843c749SSergey Zigachev 		SMU71_Discrete_VoltageLevel *smc_voltage_tab)
599b843c749SSergey Zigachev {
600b843c749SSergey Zigachev 	int result;
601b843c749SSergey Zigachev 
602b843c749SSergey Zigachev 	result = iceland_get_std_voltage_value_sidd(hwmgr, tab,
603b843c749SSergey Zigachev 			&smc_voltage_tab->StdVoltageHiSidd,
604b843c749SSergey Zigachev 			&smc_voltage_tab->StdVoltageLoSidd);
605b843c749SSergey Zigachev 	if (0 != result) {
606b843c749SSergey Zigachev 		smc_voltage_tab->StdVoltageHiSidd = tab->value * VOLTAGE_SCALE;
607b843c749SSergey Zigachev 		smc_voltage_tab->StdVoltageLoSidd = tab->value * VOLTAGE_SCALE;
608b843c749SSergey Zigachev 	}
609b843c749SSergey Zigachev 
610b843c749SSergey Zigachev 	smc_voltage_tab->Voltage = PP_HOST_TO_SMC_US(tab->value * VOLTAGE_SCALE);
611b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(smc_voltage_tab->StdVoltageHiSidd);
612b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(smc_voltage_tab->StdVoltageHiSidd);
613b843c749SSergey Zigachev 
614b843c749SSergey Zigachev 	return 0;
615b843c749SSergey Zigachev }
616b843c749SSergey Zigachev 
iceland_populate_smc_vddc_table(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)617b843c749SSergey Zigachev static int iceland_populate_smc_vddc_table(struct pp_hwmgr *hwmgr,
618b843c749SSergey Zigachev 			SMU71_Discrete_DpmTable *table)
619b843c749SSergey Zigachev {
620b843c749SSergey Zigachev 	unsigned int count;
621b843c749SSergey Zigachev 	int result;
622b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
623b843c749SSergey Zigachev 
624b843c749SSergey Zigachev 	table->VddcLevelCount = data->vddc_voltage_table.count;
625b843c749SSergey Zigachev 	for (count = 0; count < table->VddcLevelCount; count++) {
626b843c749SSergey Zigachev 		result = iceland_populate_smc_voltage_table(hwmgr,
627b843c749SSergey Zigachev 				&(data->vddc_voltage_table.entries[count]),
628b843c749SSergey Zigachev 				&(table->VddcLevel[count]));
629b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == result, "do not populate SMC VDDC voltage table", return -EINVAL);
630b843c749SSergey Zigachev 
631b843c749SSergey Zigachev 		/* GPIO voltage control */
632b843c749SSergey Zigachev 		if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->voltage_control)
633b843c749SSergey Zigachev 			table->VddcLevel[count].Smio |= data->vddc_voltage_table.entries[count].smio_low;
634b843c749SSergey Zigachev 		else if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control)
635b843c749SSergey Zigachev 			table->VddcLevel[count].Smio = 0;
636b843c749SSergey Zigachev 	}
637b843c749SSergey Zigachev 
638b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->VddcLevelCount);
639b843c749SSergey Zigachev 
640b843c749SSergey Zigachev 	return 0;
641b843c749SSergey Zigachev }
642b843c749SSergey Zigachev 
iceland_populate_smc_vdd_ci_table(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)643b843c749SSergey Zigachev static int iceland_populate_smc_vdd_ci_table(struct pp_hwmgr *hwmgr,
644b843c749SSergey Zigachev 			SMU71_Discrete_DpmTable *table)
645b843c749SSergey Zigachev {
646b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
647b843c749SSergey Zigachev 	uint32_t count;
648b843c749SSergey Zigachev 	int result;
649b843c749SSergey Zigachev 
650b843c749SSergey Zigachev 	table->VddciLevelCount = data->vddci_voltage_table.count;
651b843c749SSergey Zigachev 
652b843c749SSergey Zigachev 	for (count = 0; count < table->VddciLevelCount; count++) {
653b843c749SSergey Zigachev 		result = iceland_populate_smc_voltage_table(hwmgr,
654b843c749SSergey Zigachev 				&(data->vddci_voltage_table.entries[count]),
655b843c749SSergey Zigachev 				&(table->VddciLevel[count]));
656b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(result == 0, "do not populate SMC VDDCI voltage table", return -EINVAL);
657b843c749SSergey Zigachev 		if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control)
658b843c749SSergey Zigachev 			table->VddciLevel[count].Smio |= data->vddci_voltage_table.entries[count].smio_low;
659b843c749SSergey Zigachev 		else
660b843c749SSergey Zigachev 			table->VddciLevel[count].Smio |= 0;
661b843c749SSergey Zigachev 	}
662b843c749SSergey Zigachev 
663b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->VddciLevelCount);
664b843c749SSergey Zigachev 
665b843c749SSergey Zigachev 	return 0;
666b843c749SSergey Zigachev }
667b843c749SSergey Zigachev 
iceland_populate_smc_mvdd_table(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)668b843c749SSergey Zigachev static int iceland_populate_smc_mvdd_table(struct pp_hwmgr *hwmgr,
669b843c749SSergey Zigachev 			SMU71_Discrete_DpmTable *table)
670b843c749SSergey Zigachev {
671b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
672b843c749SSergey Zigachev 	uint32_t count;
673b843c749SSergey Zigachev 	int result;
674b843c749SSergey Zigachev 
675b843c749SSergey Zigachev 	table->MvddLevelCount = data->mvdd_voltage_table.count;
676b843c749SSergey Zigachev 
677b843c749SSergey Zigachev 	for (count = 0; count < table->VddciLevelCount; count++) {
678b843c749SSergey Zigachev 		result = iceland_populate_smc_voltage_table(hwmgr,
679b843c749SSergey Zigachev 				&(data->mvdd_voltage_table.entries[count]),
680b843c749SSergey Zigachev 				&table->MvddLevel[count]);
681b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(result == 0, "do not populate SMC mvdd voltage table", return -EINVAL);
682b843c749SSergey Zigachev 		if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->mvdd_control)
683b843c749SSergey Zigachev 			table->MvddLevel[count].Smio |= data->mvdd_voltage_table.entries[count].smio_low;
684b843c749SSergey Zigachev 		else
685b843c749SSergey Zigachev 			table->MvddLevel[count].Smio |= 0;
686b843c749SSergey Zigachev 	}
687b843c749SSergey Zigachev 
688b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->MvddLevelCount);
689b843c749SSergey Zigachev 
690b843c749SSergey Zigachev 	return 0;
691b843c749SSergey Zigachev }
692b843c749SSergey Zigachev 
693b843c749SSergey Zigachev 
iceland_populate_smc_voltage_tables(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)694b843c749SSergey Zigachev static int iceland_populate_smc_voltage_tables(struct pp_hwmgr *hwmgr,
695b843c749SSergey Zigachev 	SMU71_Discrete_DpmTable *table)
696b843c749SSergey Zigachev {
697b843c749SSergey Zigachev 	int result;
698b843c749SSergey Zigachev 
699b843c749SSergey Zigachev 	result = iceland_populate_smc_vddc_table(hwmgr, table);
700b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
701b843c749SSergey Zigachev 			"can not populate VDDC voltage table to SMC", return -EINVAL);
702b843c749SSergey Zigachev 
703b843c749SSergey Zigachev 	result = iceland_populate_smc_vdd_ci_table(hwmgr, table);
704b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
705b843c749SSergey Zigachev 			"can not populate VDDCI voltage table to SMC", return -EINVAL);
706b843c749SSergey Zigachev 
707b843c749SSergey Zigachev 	result = iceland_populate_smc_mvdd_table(hwmgr, table);
708b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
709b843c749SSergey Zigachev 			"can not populate MVDD voltage table to SMC", return -EINVAL);
710b843c749SSergey Zigachev 
711b843c749SSergey Zigachev 	return 0;
712b843c749SSergey Zigachev }
713b843c749SSergey Zigachev 
iceland_populate_ulv_level(struct pp_hwmgr * hwmgr,struct SMU71_Discrete_Ulv * state)714b843c749SSergey Zigachev static int iceland_populate_ulv_level(struct pp_hwmgr *hwmgr,
715b843c749SSergey Zigachev 		struct SMU71_Discrete_Ulv *state)
716b843c749SSergey Zigachev {
717b843c749SSergey Zigachev 	uint32_t voltage_response_time, ulv_voltage;
718b843c749SSergey Zigachev 	int result;
719b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
720b843c749SSergey Zigachev 
721b843c749SSergey Zigachev 	state->CcPwrDynRm = 0;
722b843c749SSergey Zigachev 	state->CcPwrDynRm1 = 0;
723b843c749SSergey Zigachev 
724b843c749SSergey Zigachev 	result = pp_tables_get_response_times(hwmgr, &voltage_response_time, &ulv_voltage);
725b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == result), "can not get ULV voltage value", return result;);
726b843c749SSergey Zigachev 
727b843c749SSergey Zigachev 	if (ulv_voltage == 0) {
728b843c749SSergey Zigachev 		data->ulv_supported = false;
729b843c749SSergey Zigachev 		return 0;
730b843c749SSergey Zigachev 	}
731b843c749SSergey Zigachev 
732b843c749SSergey Zigachev 	if (data->voltage_control != SMU7_VOLTAGE_CONTROL_BY_SVID2) {
733b843c749SSergey Zigachev 		/* use minimum voltage if ulv voltage in pptable is bigger than minimum voltage */
734b843c749SSergey Zigachev 		if (ulv_voltage > hwmgr->dyn_state.vddc_dependency_on_sclk->entries[0].v)
735b843c749SSergey Zigachev 			state->VddcOffset = 0;
736b843c749SSergey Zigachev 		else
737b843c749SSergey Zigachev 			/* used in SMIO Mode. not implemented for now. this is backup only for CI. */
738b843c749SSergey Zigachev 			state->VddcOffset = (uint16_t)(hwmgr->dyn_state.vddc_dependency_on_sclk->entries[0].v - ulv_voltage);
739b843c749SSergey Zigachev 	} else {
740b843c749SSergey Zigachev 		/* use minimum voltage if ulv voltage in pptable is bigger than minimum voltage */
741b843c749SSergey Zigachev 		if (ulv_voltage > hwmgr->dyn_state.vddc_dependency_on_sclk->entries[0].v)
742b843c749SSergey Zigachev 			state->VddcOffsetVid = 0;
743b843c749SSergey Zigachev 		else  /* used in SVI2 Mode */
744b843c749SSergey Zigachev 			state->VddcOffsetVid = (uint8_t)(
745b843c749SSergey Zigachev 					(hwmgr->dyn_state.vddc_dependency_on_sclk->entries[0].v - ulv_voltage)
746b843c749SSergey Zigachev 						* VOLTAGE_VID_OFFSET_SCALE2
747b843c749SSergey Zigachev 						/ VOLTAGE_VID_OFFSET_SCALE1);
748b843c749SSergey Zigachev 	}
749b843c749SSergey Zigachev 	state->VddcPhase = 1;
750b843c749SSergey Zigachev 
751b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm);
752b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm1);
753b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(state->VddcOffset);
754b843c749SSergey Zigachev 
755b843c749SSergey Zigachev 	return 0;
756b843c749SSergey Zigachev }
757b843c749SSergey Zigachev 
iceland_populate_ulv_state(struct pp_hwmgr * hwmgr,SMU71_Discrete_Ulv * ulv_level)758b843c749SSergey Zigachev static int iceland_populate_ulv_state(struct pp_hwmgr *hwmgr,
759b843c749SSergey Zigachev 		 SMU71_Discrete_Ulv *ulv_level)
760b843c749SSergey Zigachev {
761b843c749SSergey Zigachev 	return iceland_populate_ulv_level(hwmgr, ulv_level);
762b843c749SSergey Zigachev }
763b843c749SSergey Zigachev 
iceland_populate_smc_link_level(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)764b843c749SSergey Zigachev static int iceland_populate_smc_link_level(struct pp_hwmgr *hwmgr, SMU71_Discrete_DpmTable *table)
765b843c749SSergey Zigachev {
766b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
767b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &data->dpm_table;
768b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
769b843c749SSergey Zigachev 	uint32_t i;
770b843c749SSergey Zigachev 
771b843c749SSergey Zigachev 	/* Index (dpm_table->pcie_speed_table.count) is reserved for PCIE boot level. */
772b843c749SSergey Zigachev 	for (i = 0; i <= dpm_table->pcie_speed_table.count; i++) {
773b843c749SSergey Zigachev 		table->LinkLevel[i].PcieGenSpeed  =
774b843c749SSergey Zigachev 			(uint8_t)dpm_table->pcie_speed_table.dpm_levels[i].value;
775b843c749SSergey Zigachev 		table->LinkLevel[i].PcieLaneCount =
776b843c749SSergey Zigachev 			(uint8_t)encode_pcie_lane_width(dpm_table->pcie_speed_table.dpm_levels[i].param1);
777b843c749SSergey Zigachev 		table->LinkLevel[i].EnabledForActivity =
778b843c749SSergey Zigachev 			1;
779b843c749SSergey Zigachev 		table->LinkLevel[i].SPC =
780b843c749SSergey Zigachev 			(uint8_t)(data->pcie_spc_cap & 0xff);
781b843c749SSergey Zigachev 		table->LinkLevel[i].DownThreshold =
782b843c749SSergey Zigachev 			PP_HOST_TO_SMC_UL(5);
783b843c749SSergey Zigachev 		table->LinkLevel[i].UpThreshold =
784b843c749SSergey Zigachev 			PP_HOST_TO_SMC_UL(30);
785b843c749SSergey Zigachev 	}
786b843c749SSergey Zigachev 
787b843c749SSergey Zigachev 	smu_data->smc_state_table.LinkLevelCount =
788b843c749SSergey Zigachev 		(uint8_t)dpm_table->pcie_speed_table.count;
789b843c749SSergey Zigachev 	data->dpm_level_enable_mask.pcie_dpm_enable_mask =
790b843c749SSergey Zigachev 		phm_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
791b843c749SSergey Zigachev 
792b843c749SSergey Zigachev 	return 0;
793b843c749SSergey Zigachev }
794b843c749SSergey Zigachev 
iceland_calculate_sclk_params(struct pp_hwmgr * hwmgr,uint32_t engine_clock,SMU71_Discrete_GraphicsLevel * sclk)795b843c749SSergey Zigachev static int iceland_calculate_sclk_params(struct pp_hwmgr *hwmgr,
796b843c749SSergey Zigachev 		uint32_t engine_clock, SMU71_Discrete_GraphicsLevel *sclk)
797b843c749SSergey Zigachev {
798b843c749SSergey Zigachev 	const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
799b843c749SSergey Zigachev 	pp_atomctrl_clock_dividers_vi dividers;
800b843c749SSergey Zigachev 	uint32_t spll_func_cntl            = data->clock_registers.vCG_SPLL_FUNC_CNTL;
801b843c749SSergey Zigachev 	uint32_t spll_func_cntl_3          = data->clock_registers.vCG_SPLL_FUNC_CNTL_3;
802b843c749SSergey Zigachev 	uint32_t spll_func_cntl_4          = data->clock_registers.vCG_SPLL_FUNC_CNTL_4;
803b843c749SSergey Zigachev 	uint32_t cg_spll_spread_spectrum   = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM;
804b843c749SSergey Zigachev 	uint32_t cg_spll_spread_spectrum_2 = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2;
805b843c749SSergey Zigachev 	uint32_t    reference_clock;
806b843c749SSergey Zigachev 	uint32_t reference_divider;
807b843c749SSergey Zigachev 	uint32_t fbdiv;
808b843c749SSergey Zigachev 	int result;
809b843c749SSergey Zigachev 
810b843c749SSergey Zigachev 	/* get the engine clock dividers for this clock value*/
811b843c749SSergey Zigachev 	result = atomctrl_get_engine_pll_dividers_vi(hwmgr, engine_clock,  &dividers);
812b843c749SSergey Zigachev 
813b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(result == 0,
814b843c749SSergey Zigachev 		"Error retrieving Engine Clock dividers from VBIOS.", return result);
815b843c749SSergey Zigachev 
816b843c749SSergey Zigachev 	/* To get FBDIV we need to multiply this by 16384 and divide it by Fref.*/
817b843c749SSergey Zigachev 	reference_clock = atomctrl_get_reference_clock(hwmgr);
818b843c749SSergey Zigachev 
819b843c749SSergey Zigachev 	reference_divider = 1 + dividers.uc_pll_ref_div;
820b843c749SSergey Zigachev 
821b843c749SSergey Zigachev 	/* low 14 bits is fraction and high 12 bits is divider*/
822b843c749SSergey Zigachev 	fbdiv = dividers.ul_fb_div.ul_fb_divider & 0x3FFFFFF;
823b843c749SSergey Zigachev 
824b843c749SSergey Zigachev 	/* SPLL_FUNC_CNTL setup*/
825b843c749SSergey Zigachev 	spll_func_cntl = PHM_SET_FIELD(spll_func_cntl,
826b843c749SSergey Zigachev 		CG_SPLL_FUNC_CNTL, SPLL_REF_DIV, dividers.uc_pll_ref_div);
827b843c749SSergey Zigachev 	spll_func_cntl = PHM_SET_FIELD(spll_func_cntl,
828b843c749SSergey Zigachev 		CG_SPLL_FUNC_CNTL, SPLL_PDIV_A,  dividers.uc_pll_post_div);
829b843c749SSergey Zigachev 
830b843c749SSergey Zigachev 	/* SPLL_FUNC_CNTL_3 setup*/
831b843c749SSergey Zigachev 	spll_func_cntl_3 = PHM_SET_FIELD(spll_func_cntl_3,
832b843c749SSergey Zigachev 		CG_SPLL_FUNC_CNTL_3, SPLL_FB_DIV, fbdiv);
833b843c749SSergey Zigachev 
834b843c749SSergey Zigachev 	/* set to use fractional accumulation*/
835b843c749SSergey Zigachev 	spll_func_cntl_3 = PHM_SET_FIELD(spll_func_cntl_3,
836b843c749SSergey Zigachev 		CG_SPLL_FUNC_CNTL_3, SPLL_DITHEN, 1);
837b843c749SSergey Zigachev 
838b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
839b843c749SSergey Zigachev 			PHM_PlatformCaps_EngineSpreadSpectrumSupport)) {
840b843c749SSergey Zigachev 		pp_atomctrl_internal_ss_info ss_info;
841b843c749SSergey Zigachev 
842b843c749SSergey Zigachev 		uint32_t vcoFreq = engine_clock * dividers.uc_pll_post_div;
843b843c749SSergey Zigachev 		if (0 == atomctrl_get_engine_clock_spread_spectrum(hwmgr, vcoFreq, &ss_info)) {
844b843c749SSergey Zigachev 			/*
845b843c749SSergey Zigachev 			* ss_info.speed_spectrum_percentage -- in unit of 0.01%
846b843c749SSergey Zigachev 			* ss_info.speed_spectrum_rate -- in unit of khz
847b843c749SSergey Zigachev 			*/
848b843c749SSergey Zigachev 			/* clks = reference_clock * 10 / (REFDIV + 1) / speed_spectrum_rate / 2 */
849b843c749SSergey Zigachev 			uint32_t clkS = reference_clock * 5 / (reference_divider * ss_info.speed_spectrum_rate);
850b843c749SSergey Zigachev 
851b843c749SSergey Zigachev 			/* clkv = 2 * D * fbdiv / NS */
852b843c749SSergey Zigachev 			uint32_t clkV = 4 * ss_info.speed_spectrum_percentage * fbdiv / (clkS * 10000);
853b843c749SSergey Zigachev 
854b843c749SSergey Zigachev 			cg_spll_spread_spectrum =
855b843c749SSergey Zigachev 				PHM_SET_FIELD(cg_spll_spread_spectrum, CG_SPLL_SPREAD_SPECTRUM, CLKS, clkS);
856b843c749SSergey Zigachev 			cg_spll_spread_spectrum =
857b843c749SSergey Zigachev 				PHM_SET_FIELD(cg_spll_spread_spectrum, CG_SPLL_SPREAD_SPECTRUM, SSEN, 1);
858b843c749SSergey Zigachev 			cg_spll_spread_spectrum_2 =
859b843c749SSergey Zigachev 				PHM_SET_FIELD(cg_spll_spread_spectrum_2, CG_SPLL_SPREAD_SPECTRUM_2, CLKV, clkV);
860b843c749SSergey Zigachev 		}
861b843c749SSergey Zigachev 	}
862b843c749SSergey Zigachev 
863b843c749SSergey Zigachev 	sclk->SclkFrequency        = engine_clock;
864b843c749SSergey Zigachev 	sclk->CgSpllFuncCntl3      = spll_func_cntl_3;
865b843c749SSergey Zigachev 	sclk->CgSpllFuncCntl4      = spll_func_cntl_4;
866b843c749SSergey Zigachev 	sclk->SpllSpreadSpectrum   = cg_spll_spread_spectrum;
867b843c749SSergey Zigachev 	sclk->SpllSpreadSpectrum2  = cg_spll_spread_spectrum_2;
868b843c749SSergey Zigachev 	sclk->SclkDid              = (uint8_t)dividers.pll_post_divider;
869b843c749SSergey Zigachev 
870b843c749SSergey Zigachev 	return 0;
871b843c749SSergey Zigachev }
872b843c749SSergey Zigachev 
iceland_populate_phase_value_based_on_sclk(struct pp_hwmgr * hwmgr,const struct phm_phase_shedding_limits_table * pl,uint32_t sclk,uint32_t * p_shed)873b843c749SSergey Zigachev static int iceland_populate_phase_value_based_on_sclk(struct pp_hwmgr *hwmgr,
874b843c749SSergey Zigachev 				const struct phm_phase_shedding_limits_table *pl,
875b843c749SSergey Zigachev 					uint32_t sclk, uint32_t *p_shed)
876b843c749SSergey Zigachev {
877b843c749SSergey Zigachev 	unsigned int i;
878b843c749SSergey Zigachev 
879b843c749SSergey Zigachev 	/* use the minimum phase shedding */
880b843c749SSergey Zigachev 	*p_shed = 1;
881b843c749SSergey Zigachev 
882b843c749SSergey Zigachev 	for (i = 0; i < pl->count; i++) {
883b843c749SSergey Zigachev 		if (sclk < pl->entries[i].Sclk) {
884b843c749SSergey Zigachev 			*p_shed = i;
885b843c749SSergey Zigachev 			break;
886b843c749SSergey Zigachev 		}
887b843c749SSergey Zigachev 	}
888b843c749SSergey Zigachev 	return 0;
889b843c749SSergey Zigachev }
890b843c749SSergey Zigachev 
iceland_populate_single_graphic_level(struct pp_hwmgr * hwmgr,uint32_t engine_clock,SMU71_Discrete_GraphicsLevel * graphic_level)891b843c749SSergey Zigachev static int iceland_populate_single_graphic_level(struct pp_hwmgr *hwmgr,
892b843c749SSergey Zigachev 						uint32_t engine_clock,
893b843c749SSergey Zigachev 				SMU71_Discrete_GraphicsLevel *graphic_level)
894b843c749SSergey Zigachev {
895b843c749SSergey Zigachev 	int result;
896b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
897b843c749SSergey Zigachev 
898b843c749SSergey Zigachev 	result = iceland_calculate_sclk_params(hwmgr, engine_clock, graphic_level);
899b843c749SSergey Zigachev 
900b843c749SSergey Zigachev 	/* populate graphics levels*/
901b843c749SSergey Zigachev 	result = iceland_get_dependency_volt_by_clk(hwmgr,
902b843c749SSergey Zigachev 		hwmgr->dyn_state.vddc_dependency_on_sclk, engine_clock,
903b843c749SSergey Zigachev 		&graphic_level->MinVddc);
904b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == result),
905b843c749SSergey Zigachev 		"can not find VDDC voltage value for VDDC engine clock dependency table", return result);
906b843c749SSergey Zigachev 
907b843c749SSergey Zigachev 	/* SCLK frequency in units of 10KHz*/
908b843c749SSergey Zigachev 	graphic_level->SclkFrequency = engine_clock;
909b843c749SSergey Zigachev 	graphic_level->MinVddcPhases = 1;
910b843c749SSergey Zigachev 
911b843c749SSergey Zigachev 	if (data->vddc_phase_shed_control)
912b843c749SSergey Zigachev 		iceland_populate_phase_value_based_on_sclk(hwmgr,
913b843c749SSergey Zigachev 				hwmgr->dyn_state.vddc_phase_shed_limits_table,
914b843c749SSergey Zigachev 				engine_clock,
915b843c749SSergey Zigachev 				&graphic_level->MinVddcPhases);
916b843c749SSergey Zigachev 
917b843c749SSergey Zigachev 	/* Indicates maximum activity level for this performance level. 50% for now*/
918b843c749SSergey Zigachev 	graphic_level->ActivityLevel = data->current_profile_setting.sclk_activity;
919b843c749SSergey Zigachev 
920b843c749SSergey Zigachev 	graphic_level->CcPwrDynRm = 0;
921b843c749SSergey Zigachev 	graphic_level->CcPwrDynRm1 = 0;
922b843c749SSergey Zigachev 	/* this level can be used if activity is high enough.*/
923b843c749SSergey Zigachev 	graphic_level->EnabledForActivity = 0;
924b843c749SSergey Zigachev 	/* this level can be used for throttling.*/
925b843c749SSergey Zigachev 	graphic_level->EnabledForThrottle = 1;
926b843c749SSergey Zigachev 	graphic_level->UpHyst = data->current_profile_setting.sclk_up_hyst;
927b843c749SSergey Zigachev 	graphic_level->DownHyst = data->current_profile_setting.sclk_down_hyst;
928b843c749SSergey Zigachev 	graphic_level->VoltageDownHyst = 0;
929b843c749SSergey Zigachev 	graphic_level->PowerThrottle = 0;
930b843c749SSergey Zigachev 
931b843c749SSergey Zigachev 	data->display_timing.min_clock_in_sr =
932b843c749SSergey Zigachev 			hwmgr->display_config->min_core_set_clock_in_sr;
933b843c749SSergey Zigachev 
934b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
935b843c749SSergey Zigachev 			PHM_PlatformCaps_SclkDeepSleep))
936b843c749SSergey Zigachev 		graphic_level->DeepSleepDivId =
937b843c749SSergey Zigachev 				smu7_get_sleep_divider_id_from_clock(engine_clock,
938b843c749SSergey Zigachev 						data->display_timing.min_clock_in_sr);
939b843c749SSergey Zigachev 
940b843c749SSergey Zigachev 	/* Default to slow, highest DPM level will be set to PPSMC_DISPLAY_WATERMARK_LOW later.*/
941b843c749SSergey Zigachev 	graphic_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
942b843c749SSergey Zigachev 
943b843c749SSergey Zigachev 	if (0 == result) {
944b843c749SSergey Zigachev 		graphic_level->MinVddc = PP_HOST_TO_SMC_UL(graphic_level->MinVddc * VOLTAGE_SCALE);
945b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->MinVddcPhases);
946b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->SclkFrequency);
947b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_US(graphic_level->ActivityLevel);
948b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->CgSpllFuncCntl3);
949b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->CgSpllFuncCntl4);
950b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->SpllSpreadSpectrum);
951b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->SpllSpreadSpectrum2);
952b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->CcPwrDynRm);
953b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->CcPwrDynRm1);
954b843c749SSergey Zigachev 	}
955b843c749SSergey Zigachev 
956b843c749SSergey Zigachev 	return result;
957b843c749SSergey Zigachev }
958b843c749SSergey Zigachev 
iceland_populate_all_graphic_levels(struct pp_hwmgr * hwmgr)959b843c749SSergey Zigachev static int iceland_populate_all_graphic_levels(struct pp_hwmgr *hwmgr)
960b843c749SSergey Zigachev {
961b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
962b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
963b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &data->dpm_table;
964b843c749SSergey Zigachev 	uint32_t level_array_adress = smu_data->smu7_data.dpm_table_start +
965b843c749SSergey Zigachev 				offsetof(SMU71_Discrete_DpmTable, GraphicsLevel);
966b843c749SSergey Zigachev 
967b843c749SSergey Zigachev 	uint32_t level_array_size = sizeof(SMU71_Discrete_GraphicsLevel) *
968b843c749SSergey Zigachev 						SMU71_MAX_LEVELS_GRAPHICS;
969b843c749SSergey Zigachev 
970b843c749SSergey Zigachev 	SMU71_Discrete_GraphicsLevel *levels = smu_data->smc_state_table.GraphicsLevel;
971b843c749SSergey Zigachev 
972b843c749SSergey Zigachev 	uint32_t i;
973b843c749SSergey Zigachev 	uint8_t highest_pcie_level_enabled = 0;
974b843c749SSergey Zigachev 	uint8_t lowest_pcie_level_enabled = 0, mid_pcie_level_enabled = 0;
975b843c749SSergey Zigachev 	uint8_t count = 0;
976b843c749SSergey Zigachev 	int result = 0;
977b843c749SSergey Zigachev 
978b843c749SSergey Zigachev 	memset(levels, 0x00, level_array_size);
979b843c749SSergey Zigachev 
980b843c749SSergey Zigachev 	for (i = 0; i < dpm_table->sclk_table.count; i++) {
981b843c749SSergey Zigachev 		result = iceland_populate_single_graphic_level(hwmgr,
982b843c749SSergey Zigachev 					dpm_table->sclk_table.dpm_levels[i].value,
983b843c749SSergey Zigachev 					&(smu_data->smc_state_table.GraphicsLevel[i]));
984b843c749SSergey Zigachev 		if (result != 0)
985b843c749SSergey Zigachev 			return result;
986b843c749SSergey Zigachev 
987b843c749SSergey Zigachev 		/* Making sure only DPM level 0-1 have Deep Sleep Div ID populated. */
988b843c749SSergey Zigachev 		if (i > 1)
989b843c749SSergey Zigachev 			smu_data->smc_state_table.GraphicsLevel[i].DeepSleepDivId = 0;
990b843c749SSergey Zigachev 	}
991b843c749SSergey Zigachev 
992b843c749SSergey Zigachev 	/* Only enable level 0 for now. */
993b843c749SSergey Zigachev 	smu_data->smc_state_table.GraphicsLevel[0].EnabledForActivity = 1;
994b843c749SSergey Zigachev 
995b843c749SSergey Zigachev 	/* set highest level watermark to high */
996b843c749SSergey Zigachev 	if (dpm_table->sclk_table.count > 1)
997b843c749SSergey Zigachev 		smu_data->smc_state_table.GraphicsLevel[dpm_table->sclk_table.count-1].DisplayWatermark =
998b843c749SSergey Zigachev 			PPSMC_DISPLAY_WATERMARK_HIGH;
999b843c749SSergey Zigachev 
1000b843c749SSergey Zigachev 	smu_data->smc_state_table.GraphicsDpmLevelCount =
1001b843c749SSergey Zigachev 		(uint8_t)dpm_table->sclk_table.count;
1002b843c749SSergey Zigachev 	data->dpm_level_enable_mask.sclk_dpm_enable_mask =
1003b843c749SSergey Zigachev 		phm_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
1004b843c749SSergey Zigachev 
1005b843c749SSergey Zigachev 	while ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1006b843c749SSergey Zigachev 				(1 << (highest_pcie_level_enabled + 1))) != 0) {
1007b843c749SSergey Zigachev 		highest_pcie_level_enabled++;
1008b843c749SSergey Zigachev 	}
1009b843c749SSergey Zigachev 
1010b843c749SSergey Zigachev 	while ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1011b843c749SSergey Zigachev 		(1 << lowest_pcie_level_enabled)) == 0) {
1012b843c749SSergey Zigachev 		lowest_pcie_level_enabled++;
1013b843c749SSergey Zigachev 	}
1014b843c749SSergey Zigachev 
1015b843c749SSergey Zigachev 	while ((count < highest_pcie_level_enabled) &&
1016b843c749SSergey Zigachev 			((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1017b843c749SSergey Zigachev 				(1 << (lowest_pcie_level_enabled + 1 + count))) == 0)) {
1018b843c749SSergey Zigachev 		count++;
1019b843c749SSergey Zigachev 	}
1020b843c749SSergey Zigachev 
1021b843c749SSergey Zigachev 	mid_pcie_level_enabled = (lowest_pcie_level_enabled+1+count) < highest_pcie_level_enabled ?
1022b843c749SSergey Zigachev 		(lowest_pcie_level_enabled+1+count) : highest_pcie_level_enabled;
1023b843c749SSergey Zigachev 
1024b843c749SSergey Zigachev 
1025b843c749SSergey Zigachev 	/* set pcieDpmLevel to highest_pcie_level_enabled*/
1026b843c749SSergey Zigachev 	for (i = 2; i < dpm_table->sclk_table.count; i++) {
1027b843c749SSergey Zigachev 		smu_data->smc_state_table.GraphicsLevel[i].pcieDpmLevel = highest_pcie_level_enabled;
1028b843c749SSergey Zigachev 	}
1029b843c749SSergey Zigachev 
1030b843c749SSergey Zigachev 	/* set pcieDpmLevel to lowest_pcie_level_enabled*/
1031b843c749SSergey Zigachev 	smu_data->smc_state_table.GraphicsLevel[0].pcieDpmLevel = lowest_pcie_level_enabled;
1032b843c749SSergey Zigachev 
1033b843c749SSergey Zigachev 	/* set pcieDpmLevel to mid_pcie_level_enabled*/
1034b843c749SSergey Zigachev 	smu_data->smc_state_table.GraphicsLevel[1].pcieDpmLevel = mid_pcie_level_enabled;
1035b843c749SSergey Zigachev 
1036b843c749SSergey Zigachev 	/* level count will send to smc once at init smc table and never change*/
1037b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr, level_array_adress,
1038b843c749SSergey Zigachev 				(uint8_t *)levels, (uint32_t)level_array_size,
1039b843c749SSergey Zigachev 								SMC_RAM_END);
1040b843c749SSergey Zigachev 
1041b843c749SSergey Zigachev 	return result;
1042b843c749SSergey Zigachev }
1043b843c749SSergey Zigachev 
iceland_calculate_mclk_params(struct pp_hwmgr * hwmgr,uint32_t memory_clock,SMU71_Discrete_MemoryLevel * mclk,bool strobe_mode,bool dllStateOn)1044b843c749SSergey Zigachev static int iceland_calculate_mclk_params(
1045b843c749SSergey Zigachev 		struct pp_hwmgr *hwmgr,
1046b843c749SSergey Zigachev 		uint32_t memory_clock,
1047b843c749SSergey Zigachev 		SMU71_Discrete_MemoryLevel *mclk,
1048b843c749SSergey Zigachev 		bool strobe_mode,
1049b843c749SSergey Zigachev 		bool dllStateOn
1050b843c749SSergey Zigachev 		)
1051b843c749SSergey Zigachev {
1052b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1053b843c749SSergey Zigachev 
1054b843c749SSergey Zigachev 	uint32_t  dll_cntl = data->clock_registers.vDLL_CNTL;
1055b843c749SSergey Zigachev 	uint32_t  mclk_pwrmgt_cntl = data->clock_registers.vMCLK_PWRMGT_CNTL;
1056b843c749SSergey Zigachev 	uint32_t  mpll_ad_func_cntl = data->clock_registers.vMPLL_AD_FUNC_CNTL;
1057b843c749SSergey Zigachev 	uint32_t  mpll_dq_func_cntl = data->clock_registers.vMPLL_DQ_FUNC_CNTL;
1058b843c749SSergey Zigachev 	uint32_t  mpll_func_cntl = data->clock_registers.vMPLL_FUNC_CNTL;
1059b843c749SSergey Zigachev 	uint32_t  mpll_func_cntl_1 = data->clock_registers.vMPLL_FUNC_CNTL_1;
1060b843c749SSergey Zigachev 	uint32_t  mpll_func_cntl_2 = data->clock_registers.vMPLL_FUNC_CNTL_2;
1061b843c749SSergey Zigachev 	uint32_t  mpll_ss1 = data->clock_registers.vMPLL_SS1;
1062b843c749SSergey Zigachev 	uint32_t  mpll_ss2 = data->clock_registers.vMPLL_SS2;
1063b843c749SSergey Zigachev 
1064b843c749SSergey Zigachev 	pp_atomctrl_memory_clock_param mpll_param;
1065b843c749SSergey Zigachev 	int result;
1066b843c749SSergey Zigachev 
1067b843c749SSergey Zigachev 	result = atomctrl_get_memory_pll_dividers_si(hwmgr,
1068b843c749SSergey Zigachev 				memory_clock, &mpll_param, strobe_mode);
1069b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1070b843c749SSergey Zigachev 		"Error retrieving Memory Clock Parameters from VBIOS.", return result);
1071b843c749SSergey Zigachev 
1072b843c749SSergey Zigachev 	/* MPLL_FUNC_CNTL setup*/
1073b843c749SSergey Zigachev 	mpll_func_cntl = PHM_SET_FIELD(mpll_func_cntl, MPLL_FUNC_CNTL, BWCTRL, mpll_param.bw_ctrl);
1074b843c749SSergey Zigachev 
1075b843c749SSergey Zigachev 	/* MPLL_FUNC_CNTL_1 setup*/
1076b843c749SSergey Zigachev 	mpll_func_cntl_1  = PHM_SET_FIELD(mpll_func_cntl_1,
1077b843c749SSergey Zigachev 							MPLL_FUNC_CNTL_1, CLKF, mpll_param.mpll_fb_divider.cl_kf);
1078b843c749SSergey Zigachev 	mpll_func_cntl_1  = PHM_SET_FIELD(mpll_func_cntl_1,
1079b843c749SSergey Zigachev 							MPLL_FUNC_CNTL_1, CLKFRAC, mpll_param.mpll_fb_divider.clk_frac);
1080b843c749SSergey Zigachev 	mpll_func_cntl_1  = PHM_SET_FIELD(mpll_func_cntl_1,
1081b843c749SSergey Zigachev 							MPLL_FUNC_CNTL_1, VCO_MODE, mpll_param.vco_mode);
1082b843c749SSergey Zigachev 
1083b843c749SSergey Zigachev 	/* MPLL_AD_FUNC_CNTL setup*/
1084b843c749SSergey Zigachev 	mpll_ad_func_cntl = PHM_SET_FIELD(mpll_ad_func_cntl,
1085b843c749SSergey Zigachev 							MPLL_AD_FUNC_CNTL, YCLK_POST_DIV, mpll_param.mpll_post_divider);
1086b843c749SSergey Zigachev 
1087b843c749SSergey Zigachev 	if (data->is_memory_gddr5) {
1088b843c749SSergey Zigachev 		/* MPLL_DQ_FUNC_CNTL setup*/
1089b843c749SSergey Zigachev 		mpll_dq_func_cntl  = PHM_SET_FIELD(mpll_dq_func_cntl,
1090b843c749SSergey Zigachev 								MPLL_DQ_FUNC_CNTL, YCLK_SEL, mpll_param.yclk_sel);
1091b843c749SSergey Zigachev 		mpll_dq_func_cntl  = PHM_SET_FIELD(mpll_dq_func_cntl,
1092b843c749SSergey Zigachev 								MPLL_DQ_FUNC_CNTL, YCLK_POST_DIV, mpll_param.mpll_post_divider);
1093b843c749SSergey Zigachev 	}
1094b843c749SSergey Zigachev 
1095b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1096b843c749SSergey Zigachev 			PHM_PlatformCaps_MemorySpreadSpectrumSupport)) {
1097b843c749SSergey Zigachev 		/*
1098b843c749SSergey Zigachev 		 ************************************
1099b843c749SSergey Zigachev 		 Fref = Reference Frequency
1100b843c749SSergey Zigachev 		 NF = Feedback divider ratio
1101b843c749SSergey Zigachev 		 NR = Reference divider ratio
1102b843c749SSergey Zigachev 		 Fnom = Nominal VCO output frequency = Fref * NF / NR
1103b843c749SSergey Zigachev 		 Fs = Spreading Rate
1104b843c749SSergey Zigachev 		 D = Percentage down-spread / 2
1105b843c749SSergey Zigachev 		 Fint = Reference input frequency to PFD = Fref / NR
1106b843c749SSergey Zigachev 		 NS = Spreading rate divider ratio = int(Fint / (2 * Fs))
1107b843c749SSergey Zigachev 		 CLKS = NS - 1 = ISS_STEP_NUM[11:0]
1108b843c749SSergey Zigachev 		 NV = D * Fs / Fnom * 4 * ((Fnom/Fref * NR) ^ 2)
1109b843c749SSergey Zigachev 		 CLKV = 65536 * NV = ISS_STEP_SIZE[25:0]
1110b843c749SSergey Zigachev 		 *************************************
1111b843c749SSergey Zigachev 		 */
1112b843c749SSergey Zigachev 		pp_atomctrl_internal_ss_info ss_info;
1113b843c749SSergey Zigachev 		uint32_t freq_nom;
1114b843c749SSergey Zigachev 		uint32_t tmp;
1115b843c749SSergey Zigachev 		uint32_t reference_clock = atomctrl_get_mpll_reference_clock(hwmgr);
1116b843c749SSergey Zigachev 
1117b843c749SSergey Zigachev 		/* for GDDR5 for all modes and DDR3 */
1118b843c749SSergey Zigachev 		if (1 == mpll_param.qdr)
1119b843c749SSergey Zigachev 			freq_nom = memory_clock * 4 * (1 << mpll_param.mpll_post_divider);
1120b843c749SSergey Zigachev 		else
1121b843c749SSergey Zigachev 			freq_nom = memory_clock * 2 * (1 << mpll_param.mpll_post_divider);
1122b843c749SSergey Zigachev 
1123b843c749SSergey Zigachev 		/* tmp = (freq_nom / reference_clock * reference_divider) ^ 2  Note: S.I. reference_divider = 1*/
1124b843c749SSergey Zigachev 		tmp = (freq_nom / reference_clock);
1125b843c749SSergey Zigachev 		tmp = tmp * tmp;
1126b843c749SSergey Zigachev 
1127b843c749SSergey Zigachev 		if (0 == atomctrl_get_memory_clock_spread_spectrum(hwmgr, freq_nom, &ss_info)) {
1128b843c749SSergey Zigachev 			/* ss_info.speed_spectrum_percentage -- in unit of 0.01% */
1129b843c749SSergey Zigachev 			/* ss.Info.speed_spectrum_rate -- in unit of khz */
1130b843c749SSergey Zigachev 			/* CLKS = reference_clock / (2 * speed_spectrum_rate * reference_divider) * 10 */
1131b843c749SSergey Zigachev 			/*     = reference_clock * 5 / speed_spectrum_rate */
1132b843c749SSergey Zigachev 			uint32_t clks = reference_clock * 5 / ss_info.speed_spectrum_rate;
1133b843c749SSergey Zigachev 
1134b843c749SSergey Zigachev 			/* CLKV = 65536 * speed_spectrum_percentage / 2 * spreadSpecrumRate / freq_nom * 4 / 100000 * ((freq_nom / reference_clock) ^ 2) */
1135b843c749SSergey Zigachev 			/*     = 131 * speed_spectrum_percentage * speed_spectrum_rate / 100 * ((freq_nom / reference_clock) ^ 2) / freq_nom */
1136b843c749SSergey Zigachev 			uint32_t clkv =
1137b843c749SSergey Zigachev 				(uint32_t)((((131 * ss_info.speed_spectrum_percentage *
1138b843c749SSergey Zigachev 							ss_info.speed_spectrum_rate) / 100) * tmp) / freq_nom);
1139b843c749SSergey Zigachev 
1140b843c749SSergey Zigachev 			mpll_ss1 = PHM_SET_FIELD(mpll_ss1, MPLL_SS1, CLKV, clkv);
1141b843c749SSergey Zigachev 			mpll_ss2 = PHM_SET_FIELD(mpll_ss2, MPLL_SS2, CLKS, clks);
1142b843c749SSergey Zigachev 		}
1143b843c749SSergey Zigachev 	}
1144b843c749SSergey Zigachev 
1145b843c749SSergey Zigachev 	/* MCLK_PWRMGT_CNTL setup */
1146b843c749SSergey Zigachev 	mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
1147b843c749SSergey Zigachev 		MCLK_PWRMGT_CNTL, DLL_SPEED, mpll_param.dll_speed);
1148b843c749SSergey Zigachev 	mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
1149b843c749SSergey Zigachev 		MCLK_PWRMGT_CNTL, MRDCK0_PDNB, dllStateOn);
1150b843c749SSergey Zigachev 	mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
1151b843c749SSergey Zigachev 		MCLK_PWRMGT_CNTL, MRDCK1_PDNB, dllStateOn);
1152b843c749SSergey Zigachev 
1153b843c749SSergey Zigachev 
1154b843c749SSergey Zigachev 	/* Save the result data to outpupt memory level structure */
1155b843c749SSergey Zigachev 	mclk->MclkFrequency   = memory_clock;
1156b843c749SSergey Zigachev 	mclk->MpllFuncCntl    = mpll_func_cntl;
1157b843c749SSergey Zigachev 	mclk->MpllFuncCntl_1  = mpll_func_cntl_1;
1158b843c749SSergey Zigachev 	mclk->MpllFuncCntl_2  = mpll_func_cntl_2;
1159b843c749SSergey Zigachev 	mclk->MpllAdFuncCntl  = mpll_ad_func_cntl;
1160b843c749SSergey Zigachev 	mclk->MpllDqFuncCntl  = mpll_dq_func_cntl;
1161b843c749SSergey Zigachev 	mclk->MclkPwrmgtCntl  = mclk_pwrmgt_cntl;
1162b843c749SSergey Zigachev 	mclk->DllCntl         = dll_cntl;
1163b843c749SSergey Zigachev 	mclk->MpllSs1         = mpll_ss1;
1164b843c749SSergey Zigachev 	mclk->MpllSs2         = mpll_ss2;
1165b843c749SSergey Zigachev 
1166b843c749SSergey Zigachev 	return 0;
1167b843c749SSergey Zigachev }
1168b843c749SSergey Zigachev 
iceland_get_mclk_frequency_ratio(uint32_t memory_clock,bool strobe_mode)1169b843c749SSergey Zigachev static uint8_t iceland_get_mclk_frequency_ratio(uint32_t memory_clock,
1170b843c749SSergey Zigachev 		bool strobe_mode)
1171b843c749SSergey Zigachev {
1172b843c749SSergey Zigachev 	uint8_t mc_para_index;
1173b843c749SSergey Zigachev 
1174b843c749SSergey Zigachev 	if (strobe_mode) {
1175b843c749SSergey Zigachev 		if (memory_clock < 12500) {
1176b843c749SSergey Zigachev 			mc_para_index = 0x00;
1177b843c749SSergey Zigachev 		} else if (memory_clock > 47500) {
1178b843c749SSergey Zigachev 			mc_para_index = 0x0f;
1179b843c749SSergey Zigachev 		} else {
1180b843c749SSergey Zigachev 			mc_para_index = (uint8_t)((memory_clock - 10000) / 2500);
1181b843c749SSergey Zigachev 		}
1182b843c749SSergey Zigachev 	} else {
1183b843c749SSergey Zigachev 		if (memory_clock < 65000) {
1184b843c749SSergey Zigachev 			mc_para_index = 0x00;
1185b843c749SSergey Zigachev 		} else if (memory_clock > 135000) {
1186b843c749SSergey Zigachev 			mc_para_index = 0x0f;
1187b843c749SSergey Zigachev 		} else {
1188b843c749SSergey Zigachev 			mc_para_index = (uint8_t)((memory_clock - 60000) / 5000);
1189b843c749SSergey Zigachev 		}
1190b843c749SSergey Zigachev 	}
1191b843c749SSergey Zigachev 
1192b843c749SSergey Zigachev 	return mc_para_index;
1193b843c749SSergey Zigachev }
1194b843c749SSergey Zigachev 
iceland_get_ddr3_mclk_frequency_ratio(uint32_t memory_clock)1195b843c749SSergey Zigachev static uint8_t iceland_get_ddr3_mclk_frequency_ratio(uint32_t memory_clock)
1196b843c749SSergey Zigachev {
1197b843c749SSergey Zigachev 	uint8_t mc_para_index;
1198b843c749SSergey Zigachev 
1199b843c749SSergey Zigachev 	if (memory_clock < 10000) {
1200b843c749SSergey Zigachev 		mc_para_index = 0;
1201b843c749SSergey Zigachev 	} else if (memory_clock >= 80000) {
1202b843c749SSergey Zigachev 		mc_para_index = 0x0f;
1203b843c749SSergey Zigachev 	} else {
1204b843c749SSergey Zigachev 		mc_para_index = (uint8_t)((memory_clock - 10000) / 5000 + 1);
1205b843c749SSergey Zigachev 	}
1206b843c749SSergey Zigachev 
1207b843c749SSergey Zigachev 	return mc_para_index;
1208b843c749SSergey Zigachev }
1209b843c749SSergey Zigachev 
iceland_populate_phase_value_based_on_mclk(struct pp_hwmgr * hwmgr,const struct phm_phase_shedding_limits_table * pl,uint32_t memory_clock,uint32_t * p_shed)1210b843c749SSergey Zigachev static int iceland_populate_phase_value_based_on_mclk(struct pp_hwmgr *hwmgr, const struct phm_phase_shedding_limits_table *pl,
1211b843c749SSergey Zigachev 					uint32_t memory_clock, uint32_t *p_shed)
1212b843c749SSergey Zigachev {
1213b843c749SSergey Zigachev 	unsigned int i;
1214b843c749SSergey Zigachev 
1215b843c749SSergey Zigachev 	*p_shed = 1;
1216b843c749SSergey Zigachev 
1217b843c749SSergey Zigachev 	for (i = 0; i < pl->count; i++) {
1218b843c749SSergey Zigachev 		if (memory_clock < pl->entries[i].Mclk) {
1219b843c749SSergey Zigachev 			*p_shed = i;
1220b843c749SSergey Zigachev 			break;
1221b843c749SSergey Zigachev 		}
1222b843c749SSergey Zigachev 	}
1223b843c749SSergey Zigachev 
1224b843c749SSergey Zigachev 	return 0;
1225b843c749SSergey Zigachev }
1226b843c749SSergey Zigachev 
iceland_populate_single_memory_level(struct pp_hwmgr * hwmgr,uint32_t memory_clock,SMU71_Discrete_MemoryLevel * memory_level)1227b843c749SSergey Zigachev static int iceland_populate_single_memory_level(
1228b843c749SSergey Zigachev 		struct pp_hwmgr *hwmgr,
1229b843c749SSergey Zigachev 		uint32_t memory_clock,
1230b843c749SSergey Zigachev 		SMU71_Discrete_MemoryLevel *memory_level
1231b843c749SSergey Zigachev 		)
1232b843c749SSergey Zigachev {
1233b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1234b843c749SSergey Zigachev 	int result = 0;
1235b843c749SSergey Zigachev 	bool dll_state_on;
1236b843c749SSergey Zigachev 	uint32_t mclk_edc_wr_enable_threshold = 40000;
1237b843c749SSergey Zigachev 	uint32_t mclk_edc_enable_threshold = 40000;
1238b843c749SSergey Zigachev 	uint32_t mclk_strobe_mode_threshold = 40000;
1239b843c749SSergey Zigachev 
1240b843c749SSergey Zigachev 	if (hwmgr->dyn_state.vddc_dependency_on_mclk != NULL) {
1241b843c749SSergey Zigachev 		result = iceland_get_dependency_volt_by_clk(hwmgr,
1242b843c749SSergey Zigachev 			hwmgr->dyn_state.vddc_dependency_on_mclk, memory_clock, &memory_level->MinVddc);
1243b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1244b843c749SSergey Zigachev 			"can not find MinVddc voltage value from memory VDDC voltage dependency table", return result);
1245b843c749SSergey Zigachev 	}
1246b843c749SSergey Zigachev 
1247b843c749SSergey Zigachev 	if (data->vddci_control == SMU7_VOLTAGE_CONTROL_NONE) {
1248b843c749SSergey Zigachev 		memory_level->MinVddci = memory_level->MinVddc;
1249b843c749SSergey Zigachev 	} else if (NULL != hwmgr->dyn_state.vddci_dependency_on_mclk) {
1250b843c749SSergey Zigachev 		result = iceland_get_dependency_volt_by_clk(hwmgr,
1251b843c749SSergey Zigachev 				hwmgr->dyn_state.vddci_dependency_on_mclk,
1252b843c749SSergey Zigachev 				memory_clock,
1253b843c749SSergey Zigachev 				&memory_level->MinVddci);
1254b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 == result),
1255b843c749SSergey Zigachev 			"can not find MinVddci voltage value from memory VDDCI voltage dependency table", return result);
1256b843c749SSergey Zigachev 	}
1257b843c749SSergey Zigachev 
1258b843c749SSergey Zigachev 	memory_level->MinVddcPhases = 1;
1259b843c749SSergey Zigachev 
1260b843c749SSergey Zigachev 	if (data->vddc_phase_shed_control) {
1261b843c749SSergey Zigachev 		iceland_populate_phase_value_based_on_mclk(hwmgr, hwmgr->dyn_state.vddc_phase_shed_limits_table,
1262b843c749SSergey Zigachev 				memory_clock, &memory_level->MinVddcPhases);
1263b843c749SSergey Zigachev 	}
1264b843c749SSergey Zigachev 
1265b843c749SSergey Zigachev 	memory_level->EnabledForThrottle = 1;
1266b843c749SSergey Zigachev 	memory_level->EnabledForActivity = 0;
1267b843c749SSergey Zigachev 	memory_level->UpHyst = data->current_profile_setting.mclk_up_hyst;
1268b843c749SSergey Zigachev 	memory_level->DownHyst = data->current_profile_setting.mclk_down_hyst;
1269b843c749SSergey Zigachev 	memory_level->VoltageDownHyst = 0;
1270b843c749SSergey Zigachev 
1271b843c749SSergey Zigachev 	/* Indicates maximum activity level for this performance level.*/
1272b843c749SSergey Zigachev 	memory_level->ActivityLevel = data->current_profile_setting.mclk_activity;
1273b843c749SSergey Zigachev 	memory_level->StutterEnable = 0;
1274b843c749SSergey Zigachev 	memory_level->StrobeEnable = 0;
1275b843c749SSergey Zigachev 	memory_level->EdcReadEnable = 0;
1276b843c749SSergey Zigachev 	memory_level->EdcWriteEnable = 0;
1277b843c749SSergey Zigachev 	memory_level->RttEnable = 0;
1278b843c749SSergey Zigachev 
1279b843c749SSergey Zigachev 	/* default set to low watermark. Highest level will be set to high later.*/
1280b843c749SSergey Zigachev 	memory_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
1281b843c749SSergey Zigachev 
1282b843c749SSergey Zigachev 	data->display_timing.num_existing_displays = hwmgr->display_config->num_display;
1283b843c749SSergey Zigachev 
1284b843c749SSergey Zigachev 	/* stutter mode not support on iceland */
1285b843c749SSergey Zigachev 
1286b843c749SSergey Zigachev 	/* decide strobe mode*/
1287b843c749SSergey Zigachev 	memory_level->StrobeEnable = (mclk_strobe_mode_threshold != 0) &&
1288b843c749SSergey Zigachev 		(memory_clock <= mclk_strobe_mode_threshold);
1289b843c749SSergey Zigachev 
1290b843c749SSergey Zigachev 	/* decide EDC mode and memory clock ratio*/
1291b843c749SSergey Zigachev 	if (data->is_memory_gddr5) {
1292b843c749SSergey Zigachev 		memory_level->StrobeRatio = iceland_get_mclk_frequency_ratio(memory_clock,
1293b843c749SSergey Zigachev 					memory_level->StrobeEnable);
1294b843c749SSergey Zigachev 
1295b843c749SSergey Zigachev 		if ((mclk_edc_enable_threshold != 0) &&
1296b843c749SSergey Zigachev 				(memory_clock > mclk_edc_enable_threshold)) {
1297b843c749SSergey Zigachev 			memory_level->EdcReadEnable = 1;
1298b843c749SSergey Zigachev 		}
1299b843c749SSergey Zigachev 
1300b843c749SSergey Zigachev 		if ((mclk_edc_wr_enable_threshold != 0) &&
1301b843c749SSergey Zigachev 				(memory_clock > mclk_edc_wr_enable_threshold)) {
1302b843c749SSergey Zigachev 			memory_level->EdcWriteEnable = 1;
1303b843c749SSergey Zigachev 		}
1304b843c749SSergey Zigachev 
1305b843c749SSergey Zigachev 		if (memory_level->StrobeEnable) {
1306b843c749SSergey Zigachev 			if (iceland_get_mclk_frequency_ratio(memory_clock, 1) >=
1307b843c749SSergey Zigachev 					((cgs_read_register(hwmgr->device, mmMC_SEQ_MISC7) >> 16) & 0xf))
1308b843c749SSergey Zigachev 				dll_state_on = ((cgs_read_register(hwmgr->device, mmMC_SEQ_MISC5) >> 1) & 0x1) ? 1 : 0;
1309b843c749SSergey Zigachev 			else
1310b843c749SSergey Zigachev 				dll_state_on = ((cgs_read_register(hwmgr->device, mmMC_SEQ_MISC6) >> 1) & 0x1) ? 1 : 0;
1311b843c749SSergey Zigachev 		} else
1312b843c749SSergey Zigachev 			dll_state_on = data->dll_default_on;
1313b843c749SSergey Zigachev 	} else {
1314b843c749SSergey Zigachev 		memory_level->StrobeRatio =
1315b843c749SSergey Zigachev 			iceland_get_ddr3_mclk_frequency_ratio(memory_clock);
1316b843c749SSergey Zigachev 		dll_state_on = ((cgs_read_register(hwmgr->device, mmMC_SEQ_MISC5) >> 1) & 0x1) ? 1 : 0;
1317b843c749SSergey Zigachev 	}
1318b843c749SSergey Zigachev 
1319b843c749SSergey Zigachev 	result = iceland_calculate_mclk_params(hwmgr,
1320b843c749SSergey Zigachev 		memory_clock, memory_level, memory_level->StrobeEnable, dll_state_on);
1321b843c749SSergey Zigachev 
1322b843c749SSergey Zigachev 	if (0 == result) {
1323b843c749SSergey Zigachev 		memory_level->MinVddc = PP_HOST_TO_SMC_UL(memory_level->MinVddc * VOLTAGE_SCALE);
1324b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MinVddcPhases);
1325b843c749SSergey Zigachev 		memory_level->MinVddci = PP_HOST_TO_SMC_UL(memory_level->MinVddci * VOLTAGE_SCALE);
1326b843c749SSergey Zigachev 		memory_level->MinMvdd = PP_HOST_TO_SMC_UL(memory_level->MinMvdd * VOLTAGE_SCALE);
1327b843c749SSergey Zigachev 		/* MCLK frequency in units of 10KHz*/
1328b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MclkFrequency);
1329b843c749SSergey Zigachev 		/* Indicates maximum activity level for this performance level.*/
1330b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_US(memory_level->ActivityLevel);
1331b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllFuncCntl);
1332b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllFuncCntl_1);
1333b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllFuncCntl_2);
1334b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllAdFuncCntl);
1335b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllDqFuncCntl);
1336b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MclkPwrmgtCntl);
1337b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->DllCntl);
1338b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllSs1);
1339b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllSs2);
1340b843c749SSergey Zigachev 	}
1341b843c749SSergey Zigachev 
1342b843c749SSergey Zigachev 	return result;
1343b843c749SSergey Zigachev }
1344b843c749SSergey Zigachev 
iceland_populate_all_memory_levels(struct pp_hwmgr * hwmgr)1345b843c749SSergey Zigachev static int iceland_populate_all_memory_levels(struct pp_hwmgr *hwmgr)
1346b843c749SSergey Zigachev {
1347b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1348b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
1349b843c749SSergey Zigachev 	struct smu7_dpm_table *dpm_table = &data->dpm_table;
1350b843c749SSergey Zigachev 	int result;
1351b843c749SSergey Zigachev 
1352b843c749SSergey Zigachev 	/* populate MCLK dpm table to SMU7 */
1353b843c749SSergey Zigachev 	uint32_t level_array_adress = smu_data->smu7_data.dpm_table_start + offsetof(SMU71_Discrete_DpmTable, MemoryLevel);
1354b843c749SSergey Zigachev 	uint32_t level_array_size = sizeof(SMU71_Discrete_MemoryLevel) * SMU71_MAX_LEVELS_MEMORY;
1355b843c749SSergey Zigachev 	SMU71_Discrete_MemoryLevel *levels = smu_data->smc_state_table.MemoryLevel;
1356b843c749SSergey Zigachev 	uint32_t i;
1357b843c749SSergey Zigachev 
1358b843c749SSergey Zigachev 	memset(levels, 0x00, level_array_size);
1359b843c749SSergey Zigachev 
1360b843c749SSergey Zigachev 	for (i = 0; i < dpm_table->mclk_table.count; i++) {
1361b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((0 != dpm_table->mclk_table.dpm_levels[i].value),
1362b843c749SSergey Zigachev 			"can not populate memory level as memory clock is zero", return -EINVAL);
1363b843c749SSergey Zigachev 		result = iceland_populate_single_memory_level(hwmgr, dpm_table->mclk_table.dpm_levels[i].value,
1364b843c749SSergey Zigachev 			&(smu_data->smc_state_table.MemoryLevel[i]));
1365b843c749SSergey Zigachev 		if (0 != result) {
1366b843c749SSergey Zigachev 			return result;
1367b843c749SSergey Zigachev 		}
1368b843c749SSergey Zigachev 	}
1369b843c749SSergey Zigachev 
1370b843c749SSergey Zigachev 	/* Only enable level 0 for now.*/
1371b843c749SSergey Zigachev 	smu_data->smc_state_table.MemoryLevel[0].EnabledForActivity = 1;
1372b843c749SSergey Zigachev 
1373b843c749SSergey Zigachev 	/*
1374b843c749SSergey Zigachev 	* in order to prevent MC activity from stutter mode to push DPM up.
1375b843c749SSergey Zigachev 	* the UVD change complements this by putting the MCLK in a higher state
1376b843c749SSergey Zigachev 	* by default such that we are not effected by up threshold or and MCLK DPM latency.
1377b843c749SSergey Zigachev 	*/
1378b843c749SSergey Zigachev 	smu_data->smc_state_table.MemoryLevel[0].ActivityLevel = 0x1F;
1379b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(smu_data->smc_state_table.MemoryLevel[0].ActivityLevel);
1380b843c749SSergey Zigachev 
1381b843c749SSergey Zigachev 	smu_data->smc_state_table.MemoryDpmLevelCount = (uint8_t)dpm_table->mclk_table.count;
1382b843c749SSergey Zigachev 	data->dpm_level_enable_mask.mclk_dpm_enable_mask = phm_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
1383b843c749SSergey Zigachev 	/* set highest level watermark to high*/
1384b843c749SSergey Zigachev 	smu_data->smc_state_table.MemoryLevel[dpm_table->mclk_table.count-1].DisplayWatermark = PPSMC_DISPLAY_WATERMARK_HIGH;
1385b843c749SSergey Zigachev 
1386b843c749SSergey Zigachev 	/* level count will send to smc once at init smc table and never change*/
1387b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr,
1388b843c749SSergey Zigachev 		level_array_adress, (uint8_t *)levels, (uint32_t)level_array_size,
1389b843c749SSergey Zigachev 		SMC_RAM_END);
1390b843c749SSergey Zigachev 
1391b843c749SSergey Zigachev 	return result;
1392b843c749SSergey Zigachev }
1393b843c749SSergey Zigachev 
iceland_populate_mvdd_value(struct pp_hwmgr * hwmgr,uint32_t mclk,SMU71_Discrete_VoltageLevel * voltage)1394b843c749SSergey Zigachev static int iceland_populate_mvdd_value(struct pp_hwmgr *hwmgr, uint32_t mclk,
1395b843c749SSergey Zigachev 					SMU71_Discrete_VoltageLevel *voltage)
1396b843c749SSergey Zigachev {
1397b843c749SSergey Zigachev 	const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1398b843c749SSergey Zigachev 
1399b843c749SSergey Zigachev 	uint32_t i = 0;
1400b843c749SSergey Zigachev 
1401b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE != data->mvdd_control) {
1402b843c749SSergey Zigachev 		/* find mvdd value which clock is more than request */
1403b843c749SSergey Zigachev 		for (i = 0; i < hwmgr->dyn_state.mvdd_dependency_on_mclk->count; i++) {
1404b843c749SSergey Zigachev 			if (mclk <= hwmgr->dyn_state.mvdd_dependency_on_mclk->entries[i].clk) {
1405b843c749SSergey Zigachev 				/* Always round to higher voltage. */
1406b843c749SSergey Zigachev 				voltage->Voltage = data->mvdd_voltage_table.entries[i].value;
1407b843c749SSergey Zigachev 				break;
1408b843c749SSergey Zigachev 			}
1409b843c749SSergey Zigachev 		}
1410b843c749SSergey Zigachev 
1411b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(i < hwmgr->dyn_state.mvdd_dependency_on_mclk->count,
1412b843c749SSergey Zigachev 			"MVDD Voltage is outside the supported range.", return -EINVAL);
1413b843c749SSergey Zigachev 
1414b843c749SSergey Zigachev 	} else {
1415b843c749SSergey Zigachev 		return -EINVAL;
1416b843c749SSergey Zigachev 	}
1417b843c749SSergey Zigachev 
1418b843c749SSergey Zigachev 	return 0;
1419b843c749SSergey Zigachev }
1420b843c749SSergey Zigachev 
iceland_populate_smc_acpi_level(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)1421b843c749SSergey Zigachev static int iceland_populate_smc_acpi_level(struct pp_hwmgr *hwmgr,
1422b843c749SSergey Zigachev 	SMU71_Discrete_DpmTable *table)
1423b843c749SSergey Zigachev {
1424b843c749SSergey Zigachev 	int result = 0;
1425b843c749SSergey Zigachev 	const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1426b843c749SSergey Zigachev 	struct pp_atomctrl_clock_dividers_vi dividers;
1427b843c749SSergey Zigachev 	uint32_t vddc_phase_shed_control = 0;
1428b843c749SSergey Zigachev 
1429b843c749SSergey Zigachev 	SMU71_Discrete_VoltageLevel voltage_level;
1430b843c749SSergey Zigachev 	uint32_t spll_func_cntl    = data->clock_registers.vCG_SPLL_FUNC_CNTL;
1431b843c749SSergey Zigachev 	uint32_t spll_func_cntl_2  = data->clock_registers.vCG_SPLL_FUNC_CNTL_2;
1432b843c749SSergey Zigachev 	uint32_t dll_cntl          = data->clock_registers.vDLL_CNTL;
1433b843c749SSergey Zigachev 	uint32_t mclk_pwrmgt_cntl  = data->clock_registers.vMCLK_PWRMGT_CNTL;
1434b843c749SSergey Zigachev 
1435b843c749SSergey Zigachev 
1436b843c749SSergey Zigachev 	/* The ACPI state should not do DPM on DC (or ever).*/
1437b843c749SSergey Zigachev 	table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
1438b843c749SSergey Zigachev 
1439b843c749SSergey Zigachev 	if (data->acpi_vddc)
1440b843c749SSergey Zigachev 		table->ACPILevel.MinVddc = PP_HOST_TO_SMC_UL(data->acpi_vddc * VOLTAGE_SCALE);
1441b843c749SSergey Zigachev 	else
1442b843c749SSergey Zigachev 		table->ACPILevel.MinVddc = PP_HOST_TO_SMC_UL(data->min_vddc_in_pptable * VOLTAGE_SCALE);
1443b843c749SSergey Zigachev 
1444b843c749SSergey Zigachev 	table->ACPILevel.MinVddcPhases = vddc_phase_shed_control ? 0 : 1;
1445b843c749SSergey Zigachev 	/* assign zero for now*/
1446b843c749SSergey Zigachev 	table->ACPILevel.SclkFrequency = atomctrl_get_reference_clock(hwmgr);
1447b843c749SSergey Zigachev 
1448b843c749SSergey Zigachev 	/* get the engine clock dividers for this clock value*/
1449b843c749SSergey Zigachev 	result = atomctrl_get_engine_pll_dividers_vi(hwmgr,
1450b843c749SSergey Zigachev 		table->ACPILevel.SclkFrequency,  &dividers);
1451b843c749SSergey Zigachev 
1452b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(result == 0,
1453b843c749SSergey Zigachev 		"Error retrieving Engine Clock dividers from VBIOS.", return result);
1454b843c749SSergey Zigachev 
1455b843c749SSergey Zigachev 	/* divider ID for required SCLK*/
1456b843c749SSergey Zigachev 	table->ACPILevel.SclkDid = (uint8_t)dividers.pll_post_divider;
1457b843c749SSergey Zigachev 	table->ACPILevel.DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
1458b843c749SSergey Zigachev 	table->ACPILevel.DeepSleepDivId = 0;
1459b843c749SSergey Zigachev 
1460b843c749SSergey Zigachev 	spll_func_cntl      = PHM_SET_FIELD(spll_func_cntl,
1461b843c749SSergey Zigachev 							CG_SPLL_FUNC_CNTL,   SPLL_PWRON,     0);
1462b843c749SSergey Zigachev 	spll_func_cntl      = PHM_SET_FIELD(spll_func_cntl,
1463b843c749SSergey Zigachev 							CG_SPLL_FUNC_CNTL,   SPLL_RESET,     1);
1464b843c749SSergey Zigachev 	spll_func_cntl_2    = PHM_SET_FIELD(spll_func_cntl_2,
1465b843c749SSergey Zigachev 							CG_SPLL_FUNC_CNTL_2, SCLK_MUX_SEL,   4);
1466b843c749SSergey Zigachev 
1467b843c749SSergey Zigachev 	table->ACPILevel.CgSpllFuncCntl = spll_func_cntl;
1468b843c749SSergey Zigachev 	table->ACPILevel.CgSpllFuncCntl2 = spll_func_cntl_2;
1469b843c749SSergey Zigachev 	table->ACPILevel.CgSpllFuncCntl3 = data->clock_registers.vCG_SPLL_FUNC_CNTL_3;
1470b843c749SSergey Zigachev 	table->ACPILevel.CgSpllFuncCntl4 = data->clock_registers.vCG_SPLL_FUNC_CNTL_4;
1471b843c749SSergey Zigachev 	table->ACPILevel.SpllSpreadSpectrum = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM;
1472b843c749SSergey Zigachev 	table->ACPILevel.SpllSpreadSpectrum2 = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2;
1473b843c749SSergey Zigachev 	table->ACPILevel.CcPwrDynRm = 0;
1474b843c749SSergey Zigachev 	table->ACPILevel.CcPwrDynRm1 = 0;
1475b843c749SSergey Zigachev 
1476b843c749SSergey Zigachev 
1477b843c749SSergey Zigachev 	/* For various features to be enabled/disabled while this level is active.*/
1478b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.Flags);
1479b843c749SSergey Zigachev 	/* SCLK frequency in units of 10KHz*/
1480b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SclkFrequency);
1481b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl);
1482b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl2);
1483b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl3);
1484b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl4);
1485b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SpllSpreadSpectrum);
1486b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SpllSpreadSpectrum2);
1487b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm);
1488b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm1);
1489b843c749SSergey Zigachev 
1490b843c749SSergey Zigachev 	/* table->MemoryACPILevel.MinVddcPhases = table->ACPILevel.MinVddcPhases;*/
1491b843c749SSergey Zigachev 	table->MemoryACPILevel.MinVddc = table->ACPILevel.MinVddc;
1492b843c749SSergey Zigachev 	table->MemoryACPILevel.MinVddcPhases = table->ACPILevel.MinVddcPhases;
1493b843c749SSergey Zigachev 
1494b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE == data->vddci_control)
1495b843c749SSergey Zigachev 		table->MemoryACPILevel.MinVddci = table->MemoryACPILevel.MinVddc;
1496b843c749SSergey Zigachev 	else {
1497b843c749SSergey Zigachev 		if (data->acpi_vddci != 0)
1498b843c749SSergey Zigachev 			table->MemoryACPILevel.MinVddci = PP_HOST_TO_SMC_UL(data->acpi_vddci * VOLTAGE_SCALE);
1499b843c749SSergey Zigachev 		else
1500b843c749SSergey Zigachev 			table->MemoryACPILevel.MinVddci = PP_HOST_TO_SMC_UL(data->min_vddci_in_pptable * VOLTAGE_SCALE);
1501b843c749SSergey Zigachev 	}
1502b843c749SSergey Zigachev 
1503b843c749SSergey Zigachev 	if (0 == iceland_populate_mvdd_value(hwmgr, 0, &voltage_level))
1504b843c749SSergey Zigachev 		table->MemoryACPILevel.MinMvdd =
1505b843c749SSergey Zigachev 			PP_HOST_TO_SMC_UL(voltage_level.Voltage * VOLTAGE_SCALE);
1506b843c749SSergey Zigachev 	else
1507b843c749SSergey Zigachev 		table->MemoryACPILevel.MinMvdd = 0;
1508b843c749SSergey Zigachev 
1509b843c749SSergey Zigachev 	/* Force reset on DLL*/
1510b843c749SSergey Zigachev 	mclk_pwrmgt_cntl    = PHM_SET_FIELD(mclk_pwrmgt_cntl,
1511b843c749SSergey Zigachev 		MCLK_PWRMGT_CNTL, MRDCK0_RESET, 0x1);
1512b843c749SSergey Zigachev 	mclk_pwrmgt_cntl    = PHM_SET_FIELD(mclk_pwrmgt_cntl,
1513b843c749SSergey Zigachev 		MCLK_PWRMGT_CNTL, MRDCK1_RESET, 0x1);
1514b843c749SSergey Zigachev 
1515b843c749SSergey Zigachev 	/* Disable DLL in ACPIState*/
1516b843c749SSergey Zigachev 	mclk_pwrmgt_cntl    = PHM_SET_FIELD(mclk_pwrmgt_cntl,
1517b843c749SSergey Zigachev 		MCLK_PWRMGT_CNTL, MRDCK0_PDNB, 0);
1518b843c749SSergey Zigachev 	mclk_pwrmgt_cntl    = PHM_SET_FIELD(mclk_pwrmgt_cntl,
1519b843c749SSergey Zigachev 		MCLK_PWRMGT_CNTL, MRDCK1_PDNB, 0);
1520b843c749SSergey Zigachev 
1521b843c749SSergey Zigachev 	/* Enable DLL bypass signal*/
1522b843c749SSergey Zigachev 	dll_cntl            = PHM_SET_FIELD(dll_cntl,
1523b843c749SSergey Zigachev 		DLL_CNTL, MRDCK0_BYPASS, 0);
1524b843c749SSergey Zigachev 	dll_cntl            = PHM_SET_FIELD(dll_cntl,
1525b843c749SSergey Zigachev 		DLL_CNTL, MRDCK1_BYPASS, 0);
1526b843c749SSergey Zigachev 
1527b843c749SSergey Zigachev 	table->MemoryACPILevel.DllCntl            =
1528b843c749SSergey Zigachev 		PP_HOST_TO_SMC_UL(dll_cntl);
1529b843c749SSergey Zigachev 	table->MemoryACPILevel.MclkPwrmgtCntl     =
1530b843c749SSergey Zigachev 		PP_HOST_TO_SMC_UL(mclk_pwrmgt_cntl);
1531b843c749SSergey Zigachev 	table->MemoryACPILevel.MpllAdFuncCntl     =
1532b843c749SSergey Zigachev 		PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_AD_FUNC_CNTL);
1533b843c749SSergey Zigachev 	table->MemoryACPILevel.MpllDqFuncCntl     =
1534b843c749SSergey Zigachev 		PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_DQ_FUNC_CNTL);
1535b843c749SSergey Zigachev 	table->MemoryACPILevel.MpllFuncCntl       =
1536b843c749SSergey Zigachev 		PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_FUNC_CNTL);
1537b843c749SSergey Zigachev 	table->MemoryACPILevel.MpllFuncCntl_1     =
1538b843c749SSergey Zigachev 		PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_FUNC_CNTL_1);
1539b843c749SSergey Zigachev 	table->MemoryACPILevel.MpllFuncCntl_2     =
1540b843c749SSergey Zigachev 		PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_FUNC_CNTL_2);
1541b843c749SSergey Zigachev 	table->MemoryACPILevel.MpllSs1            =
1542b843c749SSergey Zigachev 		PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_SS1);
1543b843c749SSergey Zigachev 	table->MemoryACPILevel.MpllSs2            =
1544b843c749SSergey Zigachev 		PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_SS2);
1545b843c749SSergey Zigachev 
1546b843c749SSergey Zigachev 	table->MemoryACPILevel.EnabledForThrottle = 0;
1547b843c749SSergey Zigachev 	table->MemoryACPILevel.EnabledForActivity = 0;
1548b843c749SSergey Zigachev 	table->MemoryACPILevel.UpHyst = 0;
1549b843c749SSergey Zigachev 	table->MemoryACPILevel.DownHyst = 100;
1550b843c749SSergey Zigachev 	table->MemoryACPILevel.VoltageDownHyst = 0;
1551b843c749SSergey Zigachev 	/* Indicates maximum activity level for this performance level.*/
1552b843c749SSergey Zigachev 	table->MemoryACPILevel.ActivityLevel = PP_HOST_TO_SMC_US(data->current_profile_setting.mclk_activity);
1553b843c749SSergey Zigachev 
1554b843c749SSergey Zigachev 	table->MemoryACPILevel.StutterEnable = 0;
1555b843c749SSergey Zigachev 	table->MemoryACPILevel.StrobeEnable = 0;
1556b843c749SSergey Zigachev 	table->MemoryACPILevel.EdcReadEnable = 0;
1557b843c749SSergey Zigachev 	table->MemoryACPILevel.EdcWriteEnable = 0;
1558b843c749SSergey Zigachev 	table->MemoryACPILevel.RttEnable = 0;
1559b843c749SSergey Zigachev 
1560b843c749SSergey Zigachev 	return result;
1561b843c749SSergey Zigachev }
1562b843c749SSergey Zigachev 
iceland_populate_smc_uvd_level(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)1563b843c749SSergey Zigachev static int iceland_populate_smc_uvd_level(struct pp_hwmgr *hwmgr,
1564b843c749SSergey Zigachev 					SMU71_Discrete_DpmTable *table)
1565b843c749SSergey Zigachev {
1566b843c749SSergey Zigachev 	return 0;
1567b843c749SSergey Zigachev }
1568b843c749SSergey Zigachev 
iceland_populate_smc_vce_level(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)1569b843c749SSergey Zigachev static int iceland_populate_smc_vce_level(struct pp_hwmgr *hwmgr,
1570b843c749SSergey Zigachev 		SMU71_Discrete_DpmTable *table)
1571b843c749SSergey Zigachev {
1572b843c749SSergey Zigachev 	return 0;
1573b843c749SSergey Zigachev }
1574b843c749SSergey Zigachev 
iceland_populate_smc_acp_level(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)1575b843c749SSergey Zigachev static int iceland_populate_smc_acp_level(struct pp_hwmgr *hwmgr,
1576b843c749SSergey Zigachev 		SMU71_Discrete_DpmTable *table)
1577b843c749SSergey Zigachev {
1578b843c749SSergey Zigachev 	return 0;
1579b843c749SSergey Zigachev }
1580b843c749SSergey Zigachev 
iceland_populate_memory_timing_parameters(struct pp_hwmgr * hwmgr,uint32_t engine_clock,uint32_t memory_clock,struct SMU71_Discrete_MCArbDramTimingTableEntry * arb_regs)1581b843c749SSergey Zigachev static int iceland_populate_memory_timing_parameters(
1582b843c749SSergey Zigachev 		struct pp_hwmgr *hwmgr,
1583b843c749SSergey Zigachev 		uint32_t engine_clock,
1584b843c749SSergey Zigachev 		uint32_t memory_clock,
1585b843c749SSergey Zigachev 		struct SMU71_Discrete_MCArbDramTimingTableEntry *arb_regs
1586b843c749SSergey Zigachev 		)
1587b843c749SSergey Zigachev {
1588b843c749SSergey Zigachev 	uint32_t dramTiming;
1589b843c749SSergey Zigachev 	uint32_t dramTiming2;
1590b843c749SSergey Zigachev 	uint32_t burstTime;
1591b843c749SSergey Zigachev 	int result;
1592b843c749SSergey Zigachev 
1593b843c749SSergey Zigachev 	result = atomctrl_set_engine_dram_timings_rv770(hwmgr,
1594b843c749SSergey Zigachev 				engine_clock, memory_clock);
1595b843c749SSergey Zigachev 
1596b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(result == 0,
1597b843c749SSergey Zigachev 		"Error calling VBIOS to set DRAM_TIMING.", return result);
1598b843c749SSergey Zigachev 
1599b843c749SSergey Zigachev 	dramTiming  = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING);
1600b843c749SSergey Zigachev 	dramTiming2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2);
1601b843c749SSergey Zigachev 	burstTime = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0);
1602b843c749SSergey Zigachev 
1603b843c749SSergey Zigachev 	arb_regs->McArbDramTiming  = PP_HOST_TO_SMC_UL(dramTiming);
1604b843c749SSergey Zigachev 	arb_regs->McArbDramTiming2 = PP_HOST_TO_SMC_UL(dramTiming2);
1605b843c749SSergey Zigachev 	arb_regs->McArbBurstTime = (uint8_t)burstTime;
1606b843c749SSergey Zigachev 
1607b843c749SSergey Zigachev 	return 0;
1608b843c749SSergey Zigachev }
1609b843c749SSergey Zigachev 
iceland_program_memory_timing_parameters(struct pp_hwmgr * hwmgr)1610b843c749SSergey Zigachev static int iceland_program_memory_timing_parameters(struct pp_hwmgr *hwmgr)
1611b843c749SSergey Zigachev {
1612b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1613b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
1614b843c749SSergey Zigachev 	int result = 0;
1615b843c749SSergey Zigachev 	SMU71_Discrete_MCArbDramTimingTable  arb_regs;
1616b843c749SSergey Zigachev 	uint32_t i, j;
1617b843c749SSergey Zigachev 
1618b843c749SSergey Zigachev 	memset(&arb_regs, 0x00, sizeof(SMU71_Discrete_MCArbDramTimingTable));
1619b843c749SSergey Zigachev 
1620b843c749SSergey Zigachev 	for (i = 0; i < data->dpm_table.sclk_table.count; i++) {
1621b843c749SSergey Zigachev 		for (j = 0; j < data->dpm_table.mclk_table.count; j++) {
1622b843c749SSergey Zigachev 			result = iceland_populate_memory_timing_parameters
1623b843c749SSergey Zigachev 				(hwmgr, data->dpm_table.sclk_table.dpm_levels[i].value,
1624b843c749SSergey Zigachev 				 data->dpm_table.mclk_table.dpm_levels[j].value,
1625b843c749SSergey Zigachev 				 &arb_regs.entries[i][j]);
1626b843c749SSergey Zigachev 
1627b843c749SSergey Zigachev 			if (0 != result) {
1628b843c749SSergey Zigachev 				break;
1629b843c749SSergey Zigachev 			}
1630b843c749SSergey Zigachev 		}
1631b843c749SSergey Zigachev 	}
1632b843c749SSergey Zigachev 
1633b843c749SSergey Zigachev 	if (0 == result) {
1634b843c749SSergey Zigachev 		result = smu7_copy_bytes_to_smc(
1635b843c749SSergey Zigachev 				hwmgr,
1636b843c749SSergey Zigachev 				smu_data->smu7_data.arb_table_start,
1637b843c749SSergey Zigachev 				(uint8_t *)&arb_regs,
1638b843c749SSergey Zigachev 				sizeof(SMU71_Discrete_MCArbDramTimingTable),
1639b843c749SSergey Zigachev 				SMC_RAM_END
1640b843c749SSergey Zigachev 				);
1641b843c749SSergey Zigachev 	}
1642b843c749SSergey Zigachev 
1643b843c749SSergey Zigachev 	return result;
1644b843c749SSergey Zigachev }
1645b843c749SSergey Zigachev 
iceland_populate_smc_boot_level(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * table)1646b843c749SSergey Zigachev static int iceland_populate_smc_boot_level(struct pp_hwmgr *hwmgr,
1647b843c749SSergey Zigachev 			SMU71_Discrete_DpmTable *table)
1648b843c749SSergey Zigachev {
1649b843c749SSergey Zigachev 	int result = 0;
1650b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1651b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
1652b843c749SSergey Zigachev 	table->GraphicsBootLevel = 0;
1653b843c749SSergey Zigachev 	table->MemoryBootLevel = 0;
1654b843c749SSergey Zigachev 
1655b843c749SSergey Zigachev 	/* find boot level from dpm table*/
1656b843c749SSergey Zigachev 	result = phm_find_boot_level(&(data->dpm_table.sclk_table),
1657b843c749SSergey Zigachev 			data->vbios_boot_state.sclk_bootup_value,
1658b843c749SSergey Zigachev 			(uint32_t *)&(smu_data->smc_state_table.GraphicsBootLevel));
1659b843c749SSergey Zigachev 
1660b843c749SSergey Zigachev 	if (0 != result) {
1661b843c749SSergey Zigachev 		smu_data->smc_state_table.GraphicsBootLevel = 0;
1662b843c749SSergey Zigachev 		pr_err("VBIOS did not find boot engine clock value in dependency table. Using Graphics DPM level 0!\n");
1663b843c749SSergey Zigachev 		result = 0;
1664b843c749SSergey Zigachev 	}
1665b843c749SSergey Zigachev 
1666b843c749SSergey Zigachev 	result = phm_find_boot_level(&(data->dpm_table.mclk_table),
1667b843c749SSergey Zigachev 		data->vbios_boot_state.mclk_bootup_value,
1668b843c749SSergey Zigachev 		(uint32_t *)&(smu_data->smc_state_table.MemoryBootLevel));
1669b843c749SSergey Zigachev 
1670b843c749SSergey Zigachev 	if (0 != result) {
1671b843c749SSergey Zigachev 		smu_data->smc_state_table.MemoryBootLevel = 0;
1672b843c749SSergey Zigachev 		pr_err("VBIOS did not find boot engine clock value in dependency table. Using Memory DPM level 0!\n");
1673b843c749SSergey Zigachev 		result = 0;
1674b843c749SSergey Zigachev 	}
1675b843c749SSergey Zigachev 
1676b843c749SSergey Zigachev 	table->BootVddc = data->vbios_boot_state.vddc_bootup_value;
1677b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE == data->vddci_control)
1678b843c749SSergey Zigachev 		table->BootVddci = table->BootVddc;
1679b843c749SSergey Zigachev 	else
1680b843c749SSergey Zigachev 		table->BootVddci = data->vbios_boot_state.vddci_bootup_value;
1681b843c749SSergey Zigachev 
1682b843c749SSergey Zigachev 	table->BootMVdd = data->vbios_boot_state.mvdd_bootup_value;
1683b843c749SSergey Zigachev 
1684b843c749SSergey Zigachev 	return result;
1685b843c749SSergey Zigachev }
1686b843c749SSergey Zigachev 
iceland_populate_mc_reg_address(struct pp_hwmgr * hwmgr,SMU71_Discrete_MCRegisters * mc_reg_table)1687b843c749SSergey Zigachev static int iceland_populate_mc_reg_address(struct pp_hwmgr *hwmgr,
1688b843c749SSergey Zigachev 				 SMU71_Discrete_MCRegisters *mc_reg_table)
1689b843c749SSergey Zigachev {
1690b843c749SSergey Zigachev 	const struct iceland_smumgr *smu_data = (struct iceland_smumgr *)hwmgr->smu_backend;
1691b843c749SSergey Zigachev 
1692b843c749SSergey Zigachev 	uint32_t i, j;
1693b843c749SSergey Zigachev 
1694b843c749SSergey Zigachev 	for (i = 0, j = 0; j < smu_data->mc_reg_table.last; j++) {
1695b843c749SSergey Zigachev 		if (smu_data->mc_reg_table.validflag & 1<<j) {
1696b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE(i < SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE,
1697b843c749SSergey Zigachev 				"Index of mc_reg_table->address[] array out of boundary", return -EINVAL);
1698b843c749SSergey Zigachev 			mc_reg_table->address[i].s0 =
1699b843c749SSergey Zigachev 				PP_HOST_TO_SMC_US(smu_data->mc_reg_table.mc_reg_address[j].s0);
1700b843c749SSergey Zigachev 			mc_reg_table->address[i].s1 =
1701b843c749SSergey Zigachev 				PP_HOST_TO_SMC_US(smu_data->mc_reg_table.mc_reg_address[j].s1);
1702b843c749SSergey Zigachev 			i++;
1703b843c749SSergey Zigachev 		}
1704b843c749SSergey Zigachev 	}
1705b843c749SSergey Zigachev 
1706b843c749SSergey Zigachev 	mc_reg_table->last = (uint8_t)i;
1707b843c749SSergey Zigachev 
1708b843c749SSergey Zigachev 	return 0;
1709b843c749SSergey Zigachev }
1710b843c749SSergey Zigachev 
1711b843c749SSergey Zigachev /*convert register values from driver to SMC format */
iceland_convert_mc_registers(const struct iceland_mc_reg_entry * entry,SMU71_Discrete_MCRegisterSet * data,uint32_t num_entries,uint32_t valid_flag)1712b843c749SSergey Zigachev static void iceland_convert_mc_registers(
1713b843c749SSergey Zigachev 	const struct iceland_mc_reg_entry *entry,
1714b843c749SSergey Zigachev 	SMU71_Discrete_MCRegisterSet *data,
1715b843c749SSergey Zigachev 	uint32_t num_entries, uint32_t valid_flag)
1716b843c749SSergey Zigachev {
1717b843c749SSergey Zigachev 	uint32_t i, j;
1718b843c749SSergey Zigachev 
1719b843c749SSergey Zigachev 	for (i = 0, j = 0; j < num_entries; j++) {
1720b843c749SSergey Zigachev 		if (valid_flag & 1<<j) {
1721b843c749SSergey Zigachev 			data->value[i] = PP_HOST_TO_SMC_UL(entry->mc_data[j]);
1722b843c749SSergey Zigachev 			i++;
1723b843c749SSergey Zigachev 		}
1724b843c749SSergey Zigachev 	}
1725b843c749SSergey Zigachev }
1726b843c749SSergey Zigachev 
iceland_convert_mc_reg_table_entry_to_smc(struct pp_hwmgr * hwmgr,const uint32_t memory_clock,SMU71_Discrete_MCRegisterSet * mc_reg_table_data)1727b843c749SSergey Zigachev static int iceland_convert_mc_reg_table_entry_to_smc(struct pp_hwmgr *hwmgr,
1728b843c749SSergey Zigachev 		const uint32_t memory_clock,
1729b843c749SSergey Zigachev 		SMU71_Discrete_MCRegisterSet *mc_reg_table_data
1730b843c749SSergey Zigachev 		)
1731b843c749SSergey Zigachev {
1732b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
1733b843c749SSergey Zigachev 	uint32_t i = 0;
1734b843c749SSergey Zigachev 
1735b843c749SSergey Zigachev 	for (i = 0; i < smu_data->mc_reg_table.num_entries; i++) {
1736b843c749SSergey Zigachev 		if (memory_clock <=
1737b843c749SSergey Zigachev 			smu_data->mc_reg_table.mc_reg_table_entry[i].mclk_max) {
1738b843c749SSergey Zigachev 			break;
1739b843c749SSergey Zigachev 		}
1740b843c749SSergey Zigachev 	}
1741b843c749SSergey Zigachev 
1742b843c749SSergey Zigachev 	if ((i == smu_data->mc_reg_table.num_entries) && (i > 0))
1743b843c749SSergey Zigachev 		--i;
1744b843c749SSergey Zigachev 
1745b843c749SSergey Zigachev 	iceland_convert_mc_registers(&smu_data->mc_reg_table.mc_reg_table_entry[i],
1746b843c749SSergey Zigachev 				mc_reg_table_data, smu_data->mc_reg_table.last,
1747b843c749SSergey Zigachev 				smu_data->mc_reg_table.validflag);
1748b843c749SSergey Zigachev 
1749b843c749SSergey Zigachev 	return 0;
1750b843c749SSergey Zigachev }
1751b843c749SSergey Zigachev 
iceland_convert_mc_reg_table_to_smc(struct pp_hwmgr * hwmgr,SMU71_Discrete_MCRegisters * mc_regs)1752b843c749SSergey Zigachev static int iceland_convert_mc_reg_table_to_smc(struct pp_hwmgr *hwmgr,
1753b843c749SSergey Zigachev 		SMU71_Discrete_MCRegisters *mc_regs)
1754b843c749SSergey Zigachev {
1755b843c749SSergey Zigachev 	int result = 0;
1756b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1757b843c749SSergey Zigachev 	int res;
1758b843c749SSergey Zigachev 	uint32_t i;
1759b843c749SSergey Zigachev 
1760b843c749SSergey Zigachev 	for (i = 0; i < data->dpm_table.mclk_table.count; i++) {
1761b843c749SSergey Zigachev 		res = iceland_convert_mc_reg_table_entry_to_smc(
1762b843c749SSergey Zigachev 				hwmgr,
1763b843c749SSergey Zigachev 				data->dpm_table.mclk_table.dpm_levels[i].value,
1764b843c749SSergey Zigachev 				&mc_regs->data[i]
1765b843c749SSergey Zigachev 				);
1766b843c749SSergey Zigachev 
1767b843c749SSergey Zigachev 		if (0 != res)
1768b843c749SSergey Zigachev 			result = res;
1769b843c749SSergey Zigachev 	}
1770b843c749SSergey Zigachev 
1771b843c749SSergey Zigachev 	return result;
1772b843c749SSergey Zigachev }
1773b843c749SSergey Zigachev 
iceland_update_and_upload_mc_reg_table(struct pp_hwmgr * hwmgr)1774b843c749SSergey Zigachev static int iceland_update_and_upload_mc_reg_table(struct pp_hwmgr *hwmgr)
1775b843c749SSergey Zigachev {
1776b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
1777b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1778b843c749SSergey Zigachev 	uint32_t address;
1779b843c749SSergey Zigachev 	int32_t result;
1780b843c749SSergey Zigachev 
1781b843c749SSergey Zigachev 	if (0 == (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK))
1782b843c749SSergey Zigachev 		return 0;
1783b843c749SSergey Zigachev 
1784b843c749SSergey Zigachev 
1785b843c749SSergey Zigachev 	memset(&smu_data->mc_regs, 0, sizeof(SMU71_Discrete_MCRegisters));
1786b843c749SSergey Zigachev 
1787b843c749SSergey Zigachev 	result = iceland_convert_mc_reg_table_to_smc(hwmgr, &(smu_data->mc_regs));
1788b843c749SSergey Zigachev 
1789b843c749SSergey Zigachev 	if (result != 0)
1790b843c749SSergey Zigachev 		return result;
1791b843c749SSergey Zigachev 
1792b843c749SSergey Zigachev 
1793b843c749SSergey Zigachev 	address = smu_data->smu7_data.mc_reg_table_start + (uint32_t)offsetof(SMU71_Discrete_MCRegisters, data[0]);
1794b843c749SSergey Zigachev 
1795b843c749SSergey Zigachev 	return  smu7_copy_bytes_to_smc(hwmgr, address,
1796b843c749SSergey Zigachev 				 (uint8_t *)&smu_data->mc_regs.data[0],
1797b843c749SSergey Zigachev 				sizeof(SMU71_Discrete_MCRegisterSet) * data->dpm_table.mclk_table.count,
1798b843c749SSergey Zigachev 				SMC_RAM_END);
1799b843c749SSergey Zigachev }
1800b843c749SSergey Zigachev 
iceland_populate_initial_mc_reg_table(struct pp_hwmgr * hwmgr)1801b843c749SSergey Zigachev static int iceland_populate_initial_mc_reg_table(struct pp_hwmgr *hwmgr)
1802b843c749SSergey Zigachev {
1803b843c749SSergey Zigachev 	int result;
1804b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
1805b843c749SSergey Zigachev 
1806b843c749SSergey Zigachev 	memset(&smu_data->mc_regs, 0x00, sizeof(SMU71_Discrete_MCRegisters));
1807b843c749SSergey Zigachev 	result = iceland_populate_mc_reg_address(hwmgr, &(smu_data->mc_regs));
1808b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1809b843c749SSergey Zigachev 		"Failed to initialize MCRegTable for the MC register addresses!", return result;);
1810b843c749SSergey Zigachev 
1811b843c749SSergey Zigachev 	result = iceland_convert_mc_reg_table_to_smc(hwmgr, &smu_data->mc_regs);
1812b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1813b843c749SSergey Zigachev 		"Failed to initialize MCRegTable for driver state!", return result;);
1814b843c749SSergey Zigachev 
1815b843c749SSergey Zigachev 	return smu7_copy_bytes_to_smc(hwmgr, smu_data->smu7_data.mc_reg_table_start,
1816b843c749SSergey Zigachev 			(uint8_t *)&smu_data->mc_regs, sizeof(SMU71_Discrete_MCRegisters), SMC_RAM_END);
1817b843c749SSergey Zigachev }
1818b843c749SSergey Zigachev 
iceland_populate_smc_initial_state(struct pp_hwmgr * hwmgr)1819b843c749SSergey Zigachev static int iceland_populate_smc_initial_state(struct pp_hwmgr *hwmgr)
1820b843c749SSergey Zigachev {
1821b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1822b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
1823b843c749SSergey Zigachev 	uint8_t count, level;
1824b843c749SSergey Zigachev 
1825b843c749SSergey Zigachev 	count = (uint8_t)(hwmgr->dyn_state.vddc_dependency_on_sclk->count);
1826b843c749SSergey Zigachev 
1827b843c749SSergey Zigachev 	for (level = 0; level < count; level++) {
1828b843c749SSergey Zigachev 		if (hwmgr->dyn_state.vddc_dependency_on_sclk->entries[level].clk
1829b843c749SSergey Zigachev 			 >= data->vbios_boot_state.sclk_bootup_value) {
1830b843c749SSergey Zigachev 			smu_data->smc_state_table.GraphicsBootLevel = level;
1831b843c749SSergey Zigachev 			break;
1832b843c749SSergey Zigachev 		}
1833b843c749SSergey Zigachev 	}
1834b843c749SSergey Zigachev 
1835b843c749SSergey Zigachev 	count = (uint8_t)(hwmgr->dyn_state.vddc_dependency_on_mclk->count);
1836b843c749SSergey Zigachev 
1837b843c749SSergey Zigachev 	for (level = 0; level < count; level++) {
1838b843c749SSergey Zigachev 		if (hwmgr->dyn_state.vddc_dependency_on_mclk->entries[level].clk
1839b843c749SSergey Zigachev 			>= data->vbios_boot_state.mclk_bootup_value) {
1840b843c749SSergey Zigachev 			smu_data->smc_state_table.MemoryBootLevel = level;
1841b843c749SSergey Zigachev 			break;
1842b843c749SSergey Zigachev 		}
1843b843c749SSergey Zigachev 	}
1844b843c749SSergey Zigachev 
1845b843c749SSergey Zigachev 	return 0;
1846b843c749SSergey Zigachev }
1847b843c749SSergey Zigachev 
iceland_populate_bapm_parameters_in_dpm_table(struct pp_hwmgr * hwmgr)1848b843c749SSergey Zigachev static int iceland_populate_bapm_parameters_in_dpm_table(struct pp_hwmgr *hwmgr)
1849b843c749SSergey Zigachev {
1850b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1851b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
1852b843c749SSergey Zigachev 	const struct iceland_pt_defaults *defaults = smu_data->power_tune_defaults;
1853b843c749SSergey Zigachev 	SMU71_Discrete_DpmTable  *dpm_table = &(smu_data->smc_state_table);
1854b843c749SSergey Zigachev 	struct phm_cac_tdp_table *cac_dtp_table = hwmgr->dyn_state.cac_dtp_table;
1855b843c749SSergey Zigachev 	struct phm_ppm_table *ppm = hwmgr->dyn_state.ppm_parameter_table;
1856b843c749SSergey Zigachev 	const uint16_t *def1, *def2;
1857b843c749SSergey Zigachev 	int i, j, k;
1858b843c749SSergey Zigachev 
1859b843c749SSergey Zigachev 
1860b843c749SSergey Zigachev 	/*
1861b843c749SSergey Zigachev 	 * TDP number of fraction bits are changed from 8 to 7 for Iceland
1862b843c749SSergey Zigachev 	 * as requested by SMC team
1863b843c749SSergey Zigachev 	 */
1864b843c749SSergey Zigachev 
1865b843c749SSergey Zigachev 	dpm_table->DefaultTdp = PP_HOST_TO_SMC_US((uint16_t)(cac_dtp_table->usTDP * 256));
1866b843c749SSergey Zigachev 	dpm_table->TargetTdp = PP_HOST_TO_SMC_US((uint16_t)(cac_dtp_table->usConfigurableTDP * 256));
1867b843c749SSergey Zigachev 
1868b843c749SSergey Zigachev 
1869b843c749SSergey Zigachev 	dpm_table->DTETjOffset = 0;
1870b843c749SSergey Zigachev 
1871b843c749SSergey Zigachev 	dpm_table->GpuTjMax = (uint8_t)(data->thermal_temp_setting.temperature_high / PP_TEMPERATURE_UNITS_PER_CENTIGRADES);
1872b843c749SSergey Zigachev 	dpm_table->GpuTjHyst = 8;
1873b843c749SSergey Zigachev 
1874b843c749SSergey Zigachev 	dpm_table->DTEAmbientTempBase = defaults->dte_ambient_temp_base;
1875b843c749SSergey Zigachev 
1876b843c749SSergey Zigachev 	/* The following are for new Iceland Multi-input fan/thermal control */
1877b843c749SSergey Zigachev 	if (NULL != ppm) {
1878b843c749SSergey Zigachev 		dpm_table->PPM_PkgPwrLimit = (uint16_t)ppm->dgpu_tdp * 256 / 1000;
1879b843c749SSergey Zigachev 		dpm_table->PPM_TemperatureLimit = (uint16_t)ppm->tj_max * 256;
1880b843c749SSergey Zigachev 	} else {
1881b843c749SSergey Zigachev 		dpm_table->PPM_PkgPwrLimit = 0;
1882b843c749SSergey Zigachev 		dpm_table->PPM_TemperatureLimit = 0;
1883b843c749SSergey Zigachev 	}
1884b843c749SSergey Zigachev 
1885b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(dpm_table->PPM_PkgPwrLimit);
1886b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(dpm_table->PPM_TemperatureLimit);
1887b843c749SSergey Zigachev 
1888b843c749SSergey Zigachev 	dpm_table->BAPM_TEMP_GRADIENT = PP_HOST_TO_SMC_UL(defaults->bapm_temp_gradient);
1889b843c749SSergey Zigachev 	def1 = defaults->bapmti_r;
1890b843c749SSergey Zigachev 	def2 = defaults->bapmti_rc;
1891b843c749SSergey Zigachev 
1892b843c749SSergey Zigachev 	for (i = 0; i < SMU71_DTE_ITERATIONS; i++) {
1893b843c749SSergey Zigachev 		for (j = 0; j < SMU71_DTE_SOURCES; j++) {
1894b843c749SSergey Zigachev 			for (k = 0; k < SMU71_DTE_SINKS; k++) {
1895b843c749SSergey Zigachev 				dpm_table->BAPMTI_R[i][j][k] = PP_HOST_TO_SMC_US(*def1);
1896b843c749SSergey Zigachev 				dpm_table->BAPMTI_RC[i][j][k] = PP_HOST_TO_SMC_US(*def2);
1897b843c749SSergey Zigachev 				def1++;
1898b843c749SSergey Zigachev 				def2++;
1899b843c749SSergey Zigachev 			}
1900b843c749SSergey Zigachev 		}
1901b843c749SSergey Zigachev 	}
1902b843c749SSergey Zigachev 
1903b843c749SSergey Zigachev 	return 0;
1904b843c749SSergey Zigachev }
1905b843c749SSergey Zigachev 
iceland_populate_smc_svi2_config(struct pp_hwmgr * hwmgr,SMU71_Discrete_DpmTable * tab)1906b843c749SSergey Zigachev static int iceland_populate_smc_svi2_config(struct pp_hwmgr *hwmgr,
1907b843c749SSergey Zigachev 					    SMU71_Discrete_DpmTable *tab)
1908b843c749SSergey Zigachev {
1909b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1910b843c749SSergey Zigachev 
1911b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control)
1912b843c749SSergey Zigachev 		tab->SVI2Enable |= VDDC_ON_SVI2;
1913b843c749SSergey Zigachev 
1914b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control)
1915b843c749SSergey Zigachev 		tab->SVI2Enable |= VDDCI_ON_SVI2;
1916b843c749SSergey Zigachev 	else
1917b843c749SSergey Zigachev 		tab->MergedVddci = 1;
1918b843c749SSergey Zigachev 
1919b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->mvdd_control)
1920b843c749SSergey Zigachev 		tab->SVI2Enable |= MVDD_ON_SVI2;
1921b843c749SSergey Zigachev 
1922b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(tab->SVI2Enable != (VDDC_ON_SVI2 | VDDCI_ON_SVI2 | MVDD_ON_SVI2) &&
1923b843c749SSergey Zigachev 		(tab->SVI2Enable & VDDC_ON_SVI2), "SVI2 domain configuration is incorrect!", return -EINVAL);
1924b843c749SSergey Zigachev 
1925b843c749SSergey Zigachev 	return 0;
1926b843c749SSergey Zigachev }
1927b843c749SSergey Zigachev 
iceland_init_smc_table(struct pp_hwmgr * hwmgr)1928b843c749SSergey Zigachev static int iceland_init_smc_table(struct pp_hwmgr *hwmgr)
1929b843c749SSergey Zigachev {
1930b843c749SSergey Zigachev 	int result;
1931b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
1932b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
1933b843c749SSergey Zigachev 	SMU71_Discrete_DpmTable  *table = &(smu_data->smc_state_table);
1934b843c749SSergey Zigachev 
1935b843c749SSergey Zigachev 
1936b843c749SSergey Zigachev 	iceland_initialize_power_tune_defaults(hwmgr);
1937b843c749SSergey Zigachev 	memset(&(smu_data->smc_state_table), 0x00, sizeof(smu_data->smc_state_table));
1938b843c749SSergey Zigachev 
1939b843c749SSergey Zigachev 	if (SMU7_VOLTAGE_CONTROL_NONE != data->voltage_control) {
1940b843c749SSergey Zigachev 		iceland_populate_smc_voltage_tables(hwmgr, table);
1941b843c749SSergey Zigachev 	}
1942b843c749SSergey Zigachev 
1943b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1944b843c749SSergey Zigachev 			PHM_PlatformCaps_AutomaticDCTransition))
1945b843c749SSergey Zigachev 		table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
1946b843c749SSergey Zigachev 
1947b843c749SSergey Zigachev 
1948b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1949b843c749SSergey Zigachev 			PHM_PlatformCaps_StepVddc))
1950b843c749SSergey Zigachev 		table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
1951b843c749SSergey Zigachev 
1952b843c749SSergey Zigachev 	if (data->is_memory_gddr5)
1953b843c749SSergey Zigachev 		table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
1954b843c749SSergey Zigachev 
1955b843c749SSergey Zigachev 
1956b843c749SSergey Zigachev 	if (data->ulv_supported) {
1957b843c749SSergey Zigachev 		result = iceland_populate_ulv_state(hwmgr, &(smu_data->ulv_setting));
1958b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE(0 == result,
1959b843c749SSergey Zigachev 			"Failed to initialize ULV state!", return result;);
1960b843c749SSergey Zigachev 
1961b843c749SSergey Zigachev 		cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1962b843c749SSergey Zigachev 			ixCG_ULV_PARAMETER, 0x40035);
1963b843c749SSergey Zigachev 	}
1964b843c749SSergey Zigachev 
1965b843c749SSergey Zigachev 	result = iceland_populate_smc_link_level(hwmgr, table);
1966b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1967b843c749SSergey Zigachev 		"Failed to initialize Link Level!", return result;);
1968b843c749SSergey Zigachev 
1969b843c749SSergey Zigachev 	result = iceland_populate_all_graphic_levels(hwmgr);
1970b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1971b843c749SSergey Zigachev 		"Failed to initialize Graphics Level!", return result;);
1972b843c749SSergey Zigachev 
1973b843c749SSergey Zigachev 	result = iceland_populate_all_memory_levels(hwmgr);
1974b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1975b843c749SSergey Zigachev 		"Failed to initialize Memory Level!", return result;);
1976b843c749SSergey Zigachev 
1977b843c749SSergey Zigachev 	result = iceland_populate_smc_acpi_level(hwmgr, table);
1978b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1979b843c749SSergey Zigachev 		"Failed to initialize ACPI Level!", return result;);
1980b843c749SSergey Zigachev 
1981b843c749SSergey Zigachev 	result = iceland_populate_smc_vce_level(hwmgr, table);
1982b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1983b843c749SSergey Zigachev 		"Failed to initialize VCE Level!", return result;);
1984b843c749SSergey Zigachev 
1985b843c749SSergey Zigachev 	result = iceland_populate_smc_acp_level(hwmgr, table);
1986b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1987b843c749SSergey Zigachev 		"Failed to initialize ACP Level!", return result;);
1988b843c749SSergey Zigachev 
1989b843c749SSergey Zigachev 	/* Since only the initial state is completely set up at this point (the other states are just copies of the boot state) we only */
1990b843c749SSergey Zigachev 	/* need to populate the  ARB settings for the initial state. */
1991b843c749SSergey Zigachev 	result = iceland_program_memory_timing_parameters(hwmgr);
1992b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1993b843c749SSergey Zigachev 		"Failed to Write ARB settings for the initial state.", return result;);
1994b843c749SSergey Zigachev 
1995b843c749SSergey Zigachev 	result = iceland_populate_smc_uvd_level(hwmgr, table);
1996b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
1997b843c749SSergey Zigachev 		"Failed to initialize UVD Level!", return result;);
1998b843c749SSergey Zigachev 
1999b843c749SSergey Zigachev 	table->GraphicsBootLevel = 0;
2000b843c749SSergey Zigachev 	table->MemoryBootLevel = 0;
2001b843c749SSergey Zigachev 
2002b843c749SSergey Zigachev 	result = iceland_populate_smc_boot_level(hwmgr, table);
2003b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2004b843c749SSergey Zigachev 		"Failed to initialize Boot Level!", return result;);
2005b843c749SSergey Zigachev 
2006b843c749SSergey Zigachev 	result = iceland_populate_smc_initial_state(hwmgr);
2007b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result, "Failed to initialize Boot State!", return result);
2008b843c749SSergey Zigachev 
2009b843c749SSergey Zigachev 	result = iceland_populate_bapm_parameters_in_dpm_table(hwmgr);
2010b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result, "Failed to populate BAPM Parameters!", return result);
2011b843c749SSergey Zigachev 
2012b843c749SSergey Zigachev 	table->GraphicsVoltageChangeEnable  = 1;
2013b843c749SSergey Zigachev 	table->GraphicsThermThrottleEnable  = 1;
2014b843c749SSergey Zigachev 	table->GraphicsInterval = 1;
2015b843c749SSergey Zigachev 	table->VoltageInterval  = 1;
2016b843c749SSergey Zigachev 	table->ThermalInterval  = 1;
2017b843c749SSergey Zigachev 
2018b843c749SSergey Zigachev 	table->TemperatureLimitHigh =
2019b843c749SSergey Zigachev 		(data->thermal_temp_setting.temperature_high *
2020b843c749SSergey Zigachev 		 SMU7_Q88_FORMAT_CONVERSION_UNIT) / PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
2021b843c749SSergey Zigachev 	table->TemperatureLimitLow =
2022b843c749SSergey Zigachev 		(data->thermal_temp_setting.temperature_low *
2023b843c749SSergey Zigachev 		SMU7_Q88_FORMAT_CONVERSION_UNIT) / PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
2024b843c749SSergey Zigachev 
2025b843c749SSergey Zigachev 	table->MemoryVoltageChangeEnable  = 1;
2026b843c749SSergey Zigachev 	table->MemoryInterval  = 1;
2027b843c749SSergey Zigachev 	table->VoltageResponseTime  = 0;
2028b843c749SSergey Zigachev 	table->PhaseResponseTime  = 0;
2029b843c749SSergey Zigachev 	table->MemoryThermThrottleEnable  = 1;
2030b843c749SSergey Zigachev 	table->PCIeBootLinkLevel = 0;
2031b843c749SSergey Zigachev 	table->PCIeGenInterval = 1;
2032b843c749SSergey Zigachev 
2033b843c749SSergey Zigachev 	result = iceland_populate_smc_svi2_config(hwmgr, table);
2034b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2035b843c749SSergey Zigachev 		"Failed to populate SVI2 setting!", return result);
2036b843c749SSergey Zigachev 
2037b843c749SSergey Zigachev 	table->ThermGpio  = 17;
2038b843c749SSergey Zigachev 	table->SclkStepSize = 0x4000;
2039b843c749SSergey Zigachev 
2040b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SystemFlags);
2041b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMaskVddcVid);
2042b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMaskVddcPhase);
2043b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMaskVddciVid);
2044b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMaskMvddVid);
2045b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_UL(table->SclkStepSize);
2046b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitHigh);
2047b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitLow);
2048b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->VoltageResponseTime);
2049b843c749SSergey Zigachev 	CONVERT_FROM_HOST_TO_SMC_US(table->PhaseResponseTime);
2050b843c749SSergey Zigachev 
2051b843c749SSergey Zigachev 	table->BootVddc = PP_HOST_TO_SMC_US(table->BootVddc * VOLTAGE_SCALE);
2052b843c749SSergey Zigachev 	table->BootVddci = PP_HOST_TO_SMC_US(table->BootVddci * VOLTAGE_SCALE);
2053b843c749SSergey Zigachev 	table->BootMVdd = PP_HOST_TO_SMC_US(table->BootMVdd * VOLTAGE_SCALE);
2054b843c749SSergey Zigachev 
2055b843c749SSergey Zigachev 	/* Upload all dpm data to SMC memory.(dpm level, dpm level count etc) */
2056b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr, smu_data->smu7_data.dpm_table_start +
2057b843c749SSergey Zigachev 										offsetof(SMU71_Discrete_DpmTable, SystemFlags),
2058b843c749SSergey Zigachev 										(uint8_t *)&(table->SystemFlags),
2059b843c749SSergey Zigachev 										sizeof(SMU71_Discrete_DpmTable)-3 * sizeof(SMU71_PIDController),
2060b843c749SSergey Zigachev 										SMC_RAM_END);
2061b843c749SSergey Zigachev 
2062b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2063b843c749SSergey Zigachev 		"Failed to upload dpm data to SMC memory!", return result;);
2064b843c749SSergey Zigachev 
2065b843c749SSergey Zigachev 	/* Upload all ulv setting to SMC memory.(dpm level, dpm level count etc) */
2066b843c749SSergey Zigachev 	result = smu7_copy_bytes_to_smc(hwmgr,
2067b843c749SSergey Zigachev 			smu_data->smu7_data.ulv_setting_starts,
2068b843c749SSergey Zigachev 			(uint8_t *)&(smu_data->ulv_setting),
2069b843c749SSergey Zigachev 			sizeof(SMU71_Discrete_Ulv),
2070b843c749SSergey Zigachev 			SMC_RAM_END);
2071b843c749SSergey Zigachev 
2072b843c749SSergey Zigachev 
2073b843c749SSergey Zigachev 	result = iceland_populate_initial_mc_reg_table(hwmgr);
2074b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == result),
2075b843c749SSergey Zigachev 		"Failed to populate initialize MC Reg table!", return result);
2076b843c749SSergey Zigachev 
2077b843c749SSergey Zigachev 	result = iceland_populate_pm_fuses(hwmgr);
2078b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE(0 == result,
2079b843c749SSergey Zigachev 			"Failed to  populate PM fuses to SMC memory!", return result);
2080b843c749SSergey Zigachev 
2081b843c749SSergey Zigachev 	return 0;
2082b843c749SSergey Zigachev }
2083b843c749SSergey Zigachev 
2084*78973132SSergey Zigachev int iceland_thermal_setup_fan_table(struct pp_hwmgr *hwmgr);
iceland_thermal_setup_fan_table(struct pp_hwmgr * hwmgr)2085b843c749SSergey Zigachev int iceland_thermal_setup_fan_table(struct pp_hwmgr *hwmgr)
2086b843c749SSergey Zigachev {
2087b843c749SSergey Zigachev 	struct smu7_smumgr *smu7_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
2088b843c749SSergey Zigachev 	SMU71_Discrete_FanTable fan_table = { FDO_MODE_HARDWARE };
2089b843c749SSergey Zigachev 	uint32_t duty100;
2090b843c749SSergey Zigachev 	uint32_t t_diff1, t_diff2, pwm_diff1, pwm_diff2;
2091b843c749SSergey Zigachev 	uint16_t fdo_min, slope1, slope2;
2092b843c749SSergey Zigachev 	uint32_t reference_clock;
2093b843c749SSergey Zigachev 	int res;
2094b843c749SSergey Zigachev 	uint64_t tmp64;
2095b843c749SSergey Zigachev 
2096b843c749SSergey Zigachev 	if (!phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_MicrocodeFanControl))
2097b843c749SSergey Zigachev 		return 0;
2098b843c749SSergey Zigachev 
2099b843c749SSergey Zigachev 	if (hwmgr->thermal_controller.fanInfo.bNoFan) {
2100b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2101b843c749SSergey Zigachev 			PHM_PlatformCaps_MicrocodeFanControl);
2102b843c749SSergey Zigachev 		return 0;
2103b843c749SSergey Zigachev 	}
2104b843c749SSergey Zigachev 
2105b843c749SSergey Zigachev 	if (0 == smu7_data->fan_table_start) {
2106b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_MicrocodeFanControl);
2107b843c749SSergey Zigachev 		return 0;
2108b843c749SSergey Zigachev 	}
2109b843c749SSergey Zigachev 
2110b843c749SSergey Zigachev 	duty100 = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, CG_FDO_CTRL1, FMAX_DUTY100);
2111b843c749SSergey Zigachev 
2112b843c749SSergey Zigachev 	if (0 == duty100) {
2113b843c749SSergey Zigachev 		phm_cap_unset(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_MicrocodeFanControl);
2114b843c749SSergey Zigachev 		return 0;
2115b843c749SSergey Zigachev 	}
2116b843c749SSergey Zigachev 
2117b843c749SSergey Zigachev 	tmp64 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMMin * duty100;
2118b843c749SSergey Zigachev 	do_div(tmp64, 10000);
2119b843c749SSergey Zigachev 	fdo_min = (uint16_t)tmp64;
2120b843c749SSergey Zigachev 
2121b843c749SSergey Zigachev 	t_diff1 = hwmgr->thermal_controller.advanceFanControlParameters.usTMed - hwmgr->thermal_controller.advanceFanControlParameters.usTMin;
2122b843c749SSergey Zigachev 	t_diff2 = hwmgr->thermal_controller.advanceFanControlParameters.usTHigh - hwmgr->thermal_controller.advanceFanControlParameters.usTMed;
2123b843c749SSergey Zigachev 
2124b843c749SSergey Zigachev 	pwm_diff1 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMMed - hwmgr->thermal_controller.advanceFanControlParameters.usPWMMin;
2125b843c749SSergey Zigachev 	pwm_diff2 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMHigh - hwmgr->thermal_controller.advanceFanControlParameters.usPWMMed;
2126b843c749SSergey Zigachev 
2127b843c749SSergey Zigachev 	slope1 = (uint16_t)((50 + ((16 * duty100 * pwm_diff1) / t_diff1)) / 100);
2128b843c749SSergey Zigachev 	slope2 = (uint16_t)((50 + ((16 * duty100 * pwm_diff2) / t_diff2)) / 100);
2129b843c749SSergey Zigachev 
2130b843c749SSergey Zigachev 	fan_table.TempMin = cpu_to_be16((50 + hwmgr->thermal_controller.advanceFanControlParameters.usTMin) / 100);
2131b843c749SSergey Zigachev 	fan_table.TempMed = cpu_to_be16((50 + hwmgr->thermal_controller.advanceFanControlParameters.usTMed) / 100);
2132b843c749SSergey Zigachev 	fan_table.TempMax = cpu_to_be16((50 + hwmgr->thermal_controller.advanceFanControlParameters.usTMax) / 100);
2133b843c749SSergey Zigachev 
2134b843c749SSergey Zigachev 	fan_table.Slope1 = cpu_to_be16(slope1);
2135b843c749SSergey Zigachev 	fan_table.Slope2 = cpu_to_be16(slope2);
2136b843c749SSergey Zigachev 
2137b843c749SSergey Zigachev 	fan_table.FdoMin = cpu_to_be16(fdo_min);
2138b843c749SSergey Zigachev 
2139b843c749SSergey Zigachev 	fan_table.HystDown = cpu_to_be16(hwmgr->thermal_controller.advanceFanControlParameters.ucTHyst);
2140b843c749SSergey Zigachev 
2141b843c749SSergey Zigachev 	fan_table.HystUp = cpu_to_be16(1);
2142b843c749SSergey Zigachev 
2143b843c749SSergey Zigachev 	fan_table.HystSlope = cpu_to_be16(1);
2144b843c749SSergey Zigachev 
2145b843c749SSergey Zigachev 	fan_table.TempRespLim = cpu_to_be16(5);
2146b843c749SSergey Zigachev 
2147b843c749SSergey Zigachev 	reference_clock = amdgpu_asic_get_xclk((struct amdgpu_device *)hwmgr->adev);
2148b843c749SSergey Zigachev 
2149b843c749SSergey Zigachev 	fan_table.RefreshPeriod = cpu_to_be32((hwmgr->thermal_controller.advanceFanControlParameters.ulCycleDelay * reference_clock) / 1600);
2150b843c749SSergey Zigachev 
2151b843c749SSergey Zigachev 	fan_table.FdoMax = cpu_to_be16((uint16_t)duty100);
2152b843c749SSergey Zigachev 
2153b843c749SSergey Zigachev 	fan_table.TempSrc = (uint8_t)PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, CG_MULT_THERMAL_CTRL, TEMP_SEL);
2154b843c749SSergey Zigachev 
2155b843c749SSergey Zigachev 	/* fan_table.FanControl_GL_Flag = 1; */
2156b843c749SSergey Zigachev 
2157b843c749SSergey Zigachev 	res = smu7_copy_bytes_to_smc(hwmgr, smu7_data->fan_table_start, (uint8_t *)&fan_table, (uint32_t)sizeof(fan_table), SMC_RAM_END);
2158b843c749SSergey Zigachev 
2159b843c749SSergey Zigachev 	return 0;
2160b843c749SSergey Zigachev }
2161b843c749SSergey Zigachev 
2162b843c749SSergey Zigachev 
iceland_program_mem_timing_parameters(struct pp_hwmgr * hwmgr)2163b843c749SSergey Zigachev static int iceland_program_mem_timing_parameters(struct pp_hwmgr *hwmgr)
2164b843c749SSergey Zigachev {
2165b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2166b843c749SSergey Zigachev 
2167b843c749SSergey Zigachev 	if (data->need_update_smu7_dpm_table &
2168b843c749SSergey Zigachev 		(DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_OD_UPDATE_MCLK))
2169b843c749SSergey Zigachev 		return iceland_program_memory_timing_parameters(hwmgr);
2170b843c749SSergey Zigachev 
2171b843c749SSergey Zigachev 	return 0;
2172b843c749SSergey Zigachev }
2173b843c749SSergey Zigachev 
iceland_update_sclk_threshold(struct pp_hwmgr * hwmgr)2174b843c749SSergey Zigachev static int iceland_update_sclk_threshold(struct pp_hwmgr *hwmgr)
2175b843c749SSergey Zigachev {
2176b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2177b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
2178b843c749SSergey Zigachev 
2179b843c749SSergey Zigachev 	int result = 0;
2180b843c749SSergey Zigachev 	uint32_t low_sclk_interrupt_threshold = 0;
2181b843c749SSergey Zigachev 
2182b843c749SSergey Zigachev 	if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2183b843c749SSergey Zigachev 			PHM_PlatformCaps_SclkThrottleLowNotification)
2184b843c749SSergey Zigachev 		&& (data->low_sclk_interrupt_threshold != 0)) {
2185b843c749SSergey Zigachev 		low_sclk_interrupt_threshold =
2186b843c749SSergey Zigachev 				data->low_sclk_interrupt_threshold;
2187b843c749SSergey Zigachev 
2188b843c749SSergey Zigachev 		CONVERT_FROM_HOST_TO_SMC_UL(low_sclk_interrupt_threshold);
2189b843c749SSergey Zigachev 
2190b843c749SSergey Zigachev 		result = smu7_copy_bytes_to_smc(
2191b843c749SSergey Zigachev 				hwmgr,
2192b843c749SSergey Zigachev 				smu_data->smu7_data.dpm_table_start +
2193b843c749SSergey Zigachev 				offsetof(SMU71_Discrete_DpmTable,
2194b843c749SSergey Zigachev 					LowSclkInterruptThreshold),
2195b843c749SSergey Zigachev 				(uint8_t *)&low_sclk_interrupt_threshold,
2196b843c749SSergey Zigachev 				sizeof(uint32_t),
2197b843c749SSergey Zigachev 				SMC_RAM_END);
2198b843c749SSergey Zigachev 	}
2199b843c749SSergey Zigachev 
2200b843c749SSergey Zigachev 	result = iceland_update_and_upload_mc_reg_table(hwmgr);
2201b843c749SSergey Zigachev 
2202b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((0 == result), "Failed to upload MC reg table!", return result);
2203b843c749SSergey Zigachev 
2204b843c749SSergey Zigachev 	result = iceland_program_mem_timing_parameters(hwmgr);
2205b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((result == 0),
2206b843c749SSergey Zigachev 			"Failed to program memory timing parameters!",
2207b843c749SSergey Zigachev 			);
2208b843c749SSergey Zigachev 
2209b843c749SSergey Zigachev 	return result;
2210b843c749SSergey Zigachev }
2211b843c749SSergey Zigachev 
iceland_get_offsetof(uint32_t type,uint32_t member)2212b843c749SSergey Zigachev static uint32_t iceland_get_offsetof(uint32_t type, uint32_t member)
2213b843c749SSergey Zigachev {
2214b843c749SSergey Zigachev 	switch (type) {
2215b843c749SSergey Zigachev 	case SMU_SoftRegisters:
2216b843c749SSergey Zigachev 		switch (member) {
2217b843c749SSergey Zigachev 		case HandshakeDisables:
2218b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, HandshakeDisables);
2219b843c749SSergey Zigachev 		case VoltageChangeTimeout:
2220b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, VoltageChangeTimeout);
2221b843c749SSergey Zigachev 		case AverageGraphicsActivity:
2222b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, AverageGraphicsActivity);
2223b843c749SSergey Zigachev 		case PreVBlankGap:
2224b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, PreVBlankGap);
2225b843c749SSergey Zigachev 		case VBlankTimeout:
2226b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, VBlankTimeout);
2227b843c749SSergey Zigachev 		case UcodeLoadStatus:
2228b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, UcodeLoadStatus);
2229b843c749SSergey Zigachev 		case DRAM_LOG_ADDR_H:
2230b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, DRAM_LOG_ADDR_H);
2231b843c749SSergey Zigachev 		case DRAM_LOG_ADDR_L:
2232b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, DRAM_LOG_ADDR_L);
2233b843c749SSergey Zigachev 		case DRAM_LOG_PHY_ADDR_H:
2234b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, DRAM_LOG_PHY_ADDR_H);
2235b843c749SSergey Zigachev 		case DRAM_LOG_PHY_ADDR_L:
2236b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, DRAM_LOG_PHY_ADDR_L);
2237b843c749SSergey Zigachev 		case DRAM_LOG_BUFF_SIZE:
2238b843c749SSergey Zigachev 			return offsetof(SMU71_SoftRegisters, DRAM_LOG_BUFF_SIZE);
2239b843c749SSergey Zigachev 		}
2240b843c749SSergey Zigachev 		break;
2241b843c749SSergey Zigachev 	case SMU_Discrete_DpmTable:
2242b843c749SSergey Zigachev 		switch (member) {
2243b843c749SSergey Zigachev 		case LowSclkInterruptThreshold:
2244b843c749SSergey Zigachev 			return offsetof(SMU71_Discrete_DpmTable, LowSclkInterruptThreshold);
2245b843c749SSergey Zigachev 		}
2246b843c749SSergey Zigachev 		break;
2247b843c749SSergey Zigachev 	}
2248b843c749SSergey Zigachev 	pr_warn("can't get the offset of type %x member %x\n", type, member);
2249b843c749SSergey Zigachev 	return 0;
2250b843c749SSergey Zigachev }
2251b843c749SSergey Zigachev 
iceland_get_mac_definition(uint32_t value)2252b843c749SSergey Zigachev static uint32_t iceland_get_mac_definition(uint32_t value)
2253b843c749SSergey Zigachev {
2254b843c749SSergey Zigachev 	switch (value) {
2255b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_GRAPHICS:
2256b843c749SSergey Zigachev 		return SMU71_MAX_LEVELS_GRAPHICS;
2257b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_MEMORY:
2258b843c749SSergey Zigachev 		return SMU71_MAX_LEVELS_MEMORY;
2259b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_LINK:
2260b843c749SSergey Zigachev 		return SMU71_MAX_LEVELS_LINK;
2261b843c749SSergey Zigachev 	case SMU_MAX_ENTRIES_SMIO:
2262b843c749SSergey Zigachev 		return SMU71_MAX_ENTRIES_SMIO;
2263b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_VDDC:
2264b843c749SSergey Zigachev 		return SMU71_MAX_LEVELS_VDDC;
2265b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_VDDCI:
2266b843c749SSergey Zigachev 		return SMU71_MAX_LEVELS_VDDCI;
2267b843c749SSergey Zigachev 	case SMU_MAX_LEVELS_MVDD:
2268b843c749SSergey Zigachev 		return SMU71_MAX_LEVELS_MVDD;
2269b843c749SSergey Zigachev 	}
2270b843c749SSergey Zigachev 
2271b843c749SSergey Zigachev 	pr_warn("can't get the mac of %x\n", value);
2272b843c749SSergey Zigachev 	return 0;
2273b843c749SSergey Zigachev }
2274b843c749SSergey Zigachev 
iceland_process_firmware_header(struct pp_hwmgr * hwmgr)2275b843c749SSergey Zigachev static int iceland_process_firmware_header(struct pp_hwmgr *hwmgr)
2276b843c749SSergey Zigachev {
2277b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2278b843c749SSergey Zigachev 	struct smu7_smumgr *smu7_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
2279b843c749SSergey Zigachev 
2280b843c749SSergey Zigachev 	uint32_t tmp;
2281b843c749SSergey Zigachev 	int result;
2282b843c749SSergey Zigachev 	bool error = false;
2283b843c749SSergey Zigachev 
2284b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2285b843c749SSergey Zigachev 				SMU71_FIRMWARE_HEADER_LOCATION +
2286b843c749SSergey Zigachev 				offsetof(SMU71_Firmware_Header, DpmTable),
2287b843c749SSergey Zigachev 				&tmp, SMC_RAM_END);
2288b843c749SSergey Zigachev 
2289b843c749SSergey Zigachev 	if (0 == result) {
2290b843c749SSergey Zigachev 		smu7_data->dpm_table_start = tmp;
2291b843c749SSergey Zigachev 	}
2292b843c749SSergey Zigachev 
2293b843c749SSergey Zigachev 	error |= (0 != result);
2294b843c749SSergey Zigachev 
2295b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2296b843c749SSergey Zigachev 				SMU71_FIRMWARE_HEADER_LOCATION +
2297b843c749SSergey Zigachev 				offsetof(SMU71_Firmware_Header, SoftRegisters),
2298b843c749SSergey Zigachev 				&tmp, SMC_RAM_END);
2299b843c749SSergey Zigachev 
2300b843c749SSergey Zigachev 	if (0 == result) {
2301b843c749SSergey Zigachev 		data->soft_regs_start = tmp;
2302b843c749SSergey Zigachev 		smu7_data->soft_regs_start = tmp;
2303b843c749SSergey Zigachev 	}
2304b843c749SSergey Zigachev 
2305b843c749SSergey Zigachev 	error |= (0 != result);
2306b843c749SSergey Zigachev 
2307b843c749SSergey Zigachev 
2308b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2309b843c749SSergey Zigachev 				SMU71_FIRMWARE_HEADER_LOCATION +
2310b843c749SSergey Zigachev 				offsetof(SMU71_Firmware_Header, mcRegisterTable),
2311b843c749SSergey Zigachev 				&tmp, SMC_RAM_END);
2312b843c749SSergey Zigachev 
2313b843c749SSergey Zigachev 	if (0 == result) {
2314b843c749SSergey Zigachev 		smu7_data->mc_reg_table_start = tmp;
2315b843c749SSergey Zigachev 	}
2316b843c749SSergey Zigachev 
2317b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2318b843c749SSergey Zigachev 				SMU71_FIRMWARE_HEADER_LOCATION +
2319b843c749SSergey Zigachev 				offsetof(SMU71_Firmware_Header, FanTable),
2320b843c749SSergey Zigachev 				&tmp, SMC_RAM_END);
2321b843c749SSergey Zigachev 
2322b843c749SSergey Zigachev 	if (0 == result) {
2323b843c749SSergey Zigachev 		smu7_data->fan_table_start = tmp;
2324b843c749SSergey Zigachev 	}
2325b843c749SSergey Zigachev 
2326b843c749SSergey Zigachev 	error |= (0 != result);
2327b843c749SSergey Zigachev 
2328b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2329b843c749SSergey Zigachev 				SMU71_FIRMWARE_HEADER_LOCATION +
2330b843c749SSergey Zigachev 				offsetof(SMU71_Firmware_Header, mcArbDramTimingTable),
2331b843c749SSergey Zigachev 				&tmp, SMC_RAM_END);
2332b843c749SSergey Zigachev 
2333b843c749SSergey Zigachev 	if (0 == result) {
2334b843c749SSergey Zigachev 		smu7_data->arb_table_start = tmp;
2335b843c749SSergey Zigachev 	}
2336b843c749SSergey Zigachev 
2337b843c749SSergey Zigachev 	error |= (0 != result);
2338b843c749SSergey Zigachev 
2339b843c749SSergey Zigachev 
2340b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2341b843c749SSergey Zigachev 				SMU71_FIRMWARE_HEADER_LOCATION +
2342b843c749SSergey Zigachev 				offsetof(SMU71_Firmware_Header, Version),
2343b843c749SSergey Zigachev 				&tmp, SMC_RAM_END);
2344b843c749SSergey Zigachev 
2345b843c749SSergey Zigachev 	if (0 == result) {
2346b843c749SSergey Zigachev 		hwmgr->microcode_version_info.SMC = tmp;
2347b843c749SSergey Zigachev 	}
2348b843c749SSergey Zigachev 
2349b843c749SSergey Zigachev 	error |= (0 != result);
2350b843c749SSergey Zigachev 
2351b843c749SSergey Zigachev 	result = smu7_read_smc_sram_dword(hwmgr,
2352b843c749SSergey Zigachev 				SMU71_FIRMWARE_HEADER_LOCATION +
2353b843c749SSergey Zigachev 				offsetof(SMU71_Firmware_Header, UlvSettings),
2354b843c749SSergey Zigachev 				&tmp, SMC_RAM_END);
2355b843c749SSergey Zigachev 
2356b843c749SSergey Zigachev 	if (0 == result) {
2357b843c749SSergey Zigachev 		smu7_data->ulv_setting_starts = tmp;
2358b843c749SSergey Zigachev 	}
2359b843c749SSergey Zigachev 
2360b843c749SSergey Zigachev 	error |= (0 != result);
2361b843c749SSergey Zigachev 
2362b843c749SSergey Zigachev 	return error ? 1 : 0;
2363b843c749SSergey Zigachev }
2364b843c749SSergey Zigachev 
2365b843c749SSergey Zigachev /*---------------------------MC----------------------------*/
2366b843c749SSergey Zigachev 
iceland_get_memory_modile_index(struct pp_hwmgr * hwmgr)2367b843c749SSergey Zigachev static uint8_t iceland_get_memory_modile_index(struct pp_hwmgr *hwmgr)
2368b843c749SSergey Zigachev {
2369b843c749SSergey Zigachev 	return (uint8_t) (0xFF & (cgs_read_register(hwmgr->device, mmBIOS_SCRATCH_4) >> 16));
2370b843c749SSergey Zigachev }
2371b843c749SSergey Zigachev 
iceland_check_s0_mc_reg_index(uint16_t in_reg,uint16_t * out_reg)2372b843c749SSergey Zigachev static bool iceland_check_s0_mc_reg_index(uint16_t in_reg, uint16_t *out_reg)
2373b843c749SSergey Zigachev {
2374b843c749SSergey Zigachev 	bool result = true;
2375b843c749SSergey Zigachev 
2376b843c749SSergey Zigachev 	switch (in_reg) {
2377b843c749SSergey Zigachev 	case  mmMC_SEQ_RAS_TIMING:
2378b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_RAS_TIMING_LP;
2379b843c749SSergey Zigachev 		break;
2380b843c749SSergey Zigachev 
2381b843c749SSergey Zigachev 	case  mmMC_SEQ_DLL_STBY:
2382b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_DLL_STBY_LP;
2383b843c749SSergey Zigachev 		break;
2384b843c749SSergey Zigachev 
2385b843c749SSergey Zigachev 	case  mmMC_SEQ_G5PDX_CMD0:
2386b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_G5PDX_CMD0_LP;
2387b843c749SSergey Zigachev 		break;
2388b843c749SSergey Zigachev 
2389b843c749SSergey Zigachev 	case  mmMC_SEQ_G5PDX_CMD1:
2390b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_G5PDX_CMD1_LP;
2391b843c749SSergey Zigachev 		break;
2392b843c749SSergey Zigachev 
2393b843c749SSergey Zigachev 	case  mmMC_SEQ_G5PDX_CTRL:
2394b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_G5PDX_CTRL_LP;
2395b843c749SSergey Zigachev 		break;
2396b843c749SSergey Zigachev 
2397b843c749SSergey Zigachev 	case mmMC_SEQ_CAS_TIMING:
2398b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_CAS_TIMING_LP;
2399b843c749SSergey Zigachev 		break;
2400b843c749SSergey Zigachev 
2401b843c749SSergey Zigachev 	case mmMC_SEQ_MISC_TIMING:
2402b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_MISC_TIMING_LP;
2403b843c749SSergey Zigachev 		break;
2404b843c749SSergey Zigachev 
2405b843c749SSergey Zigachev 	case mmMC_SEQ_MISC_TIMING2:
2406b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_MISC_TIMING2_LP;
2407b843c749SSergey Zigachev 		break;
2408b843c749SSergey Zigachev 
2409b843c749SSergey Zigachev 	case mmMC_SEQ_PMG_DVS_CMD:
2410b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_PMG_DVS_CMD_LP;
2411b843c749SSergey Zigachev 		break;
2412b843c749SSergey Zigachev 
2413b843c749SSergey Zigachev 	case mmMC_SEQ_PMG_DVS_CTL:
2414b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_PMG_DVS_CTL_LP;
2415b843c749SSergey Zigachev 		break;
2416b843c749SSergey Zigachev 
2417b843c749SSergey Zigachev 	case mmMC_SEQ_RD_CTL_D0:
2418b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_RD_CTL_D0_LP;
2419b843c749SSergey Zigachev 		break;
2420b843c749SSergey Zigachev 
2421b843c749SSergey Zigachev 	case mmMC_SEQ_RD_CTL_D1:
2422b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_RD_CTL_D1_LP;
2423b843c749SSergey Zigachev 		break;
2424b843c749SSergey Zigachev 
2425b843c749SSergey Zigachev 	case mmMC_SEQ_WR_CTL_D0:
2426b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_WR_CTL_D0_LP;
2427b843c749SSergey Zigachev 		break;
2428b843c749SSergey Zigachev 
2429b843c749SSergey Zigachev 	case mmMC_SEQ_WR_CTL_D1:
2430b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_WR_CTL_D1_LP;
2431b843c749SSergey Zigachev 		break;
2432b843c749SSergey Zigachev 
2433b843c749SSergey Zigachev 	case mmMC_PMG_CMD_EMRS:
2434b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_PMG_CMD_EMRS_LP;
2435b843c749SSergey Zigachev 		break;
2436b843c749SSergey Zigachev 
2437b843c749SSergey Zigachev 	case mmMC_PMG_CMD_MRS:
2438b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_PMG_CMD_MRS_LP;
2439b843c749SSergey Zigachev 		break;
2440b843c749SSergey Zigachev 
2441b843c749SSergey Zigachev 	case mmMC_PMG_CMD_MRS1:
2442b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_PMG_CMD_MRS1_LP;
2443b843c749SSergey Zigachev 		break;
2444b843c749SSergey Zigachev 
2445b843c749SSergey Zigachev 	case mmMC_SEQ_PMG_TIMING:
2446b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_PMG_TIMING_LP;
2447b843c749SSergey Zigachev 		break;
2448b843c749SSergey Zigachev 
2449b843c749SSergey Zigachev 	case mmMC_PMG_CMD_MRS2:
2450b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_PMG_CMD_MRS2_LP;
2451b843c749SSergey Zigachev 		break;
2452b843c749SSergey Zigachev 
2453b843c749SSergey Zigachev 	case mmMC_SEQ_WR_CTL_2:
2454b843c749SSergey Zigachev 		*out_reg = mmMC_SEQ_WR_CTL_2_LP;
2455b843c749SSergey Zigachev 		break;
2456b843c749SSergey Zigachev 
2457b843c749SSergey Zigachev 	default:
2458b843c749SSergey Zigachev 		result = false;
2459b843c749SSergey Zigachev 		break;
2460b843c749SSergey Zigachev 	}
2461b843c749SSergey Zigachev 
2462b843c749SSergey Zigachev 	return result;
2463b843c749SSergey Zigachev }
2464b843c749SSergey Zigachev 
iceland_set_s0_mc_reg_index(struct iceland_mc_reg_table * table)2465b843c749SSergey Zigachev static int iceland_set_s0_mc_reg_index(struct iceland_mc_reg_table *table)
2466b843c749SSergey Zigachev {
2467b843c749SSergey Zigachev 	uint32_t i;
2468b843c749SSergey Zigachev 	uint16_t address;
2469b843c749SSergey Zigachev 
2470b843c749SSergey Zigachev 	for (i = 0; i < table->last; i++) {
2471b843c749SSergey Zigachev 		table->mc_reg_address[i].s0 =
2472b843c749SSergey Zigachev 			iceland_check_s0_mc_reg_index(table->mc_reg_address[i].s1, &address)
2473b843c749SSergey Zigachev 			? address : table->mc_reg_address[i].s1;
2474b843c749SSergey Zigachev 	}
2475b843c749SSergey Zigachev 	return 0;
2476b843c749SSergey Zigachev }
2477b843c749SSergey Zigachev 
iceland_copy_vbios_smc_reg_table(const pp_atomctrl_mc_reg_table * table,struct iceland_mc_reg_table * ni_table)2478b843c749SSergey Zigachev static int iceland_copy_vbios_smc_reg_table(const pp_atomctrl_mc_reg_table *table,
2479b843c749SSergey Zigachev 					struct iceland_mc_reg_table *ni_table)
2480b843c749SSergey Zigachev {
2481b843c749SSergey Zigachev 	uint8_t i, j;
2482b843c749SSergey Zigachev 
2483b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((table->last <= SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE),
2484b843c749SSergey Zigachev 		"Invalid VramInfo table.", return -EINVAL);
2485b843c749SSergey Zigachev 	PP_ASSERT_WITH_CODE((table->num_entries <= MAX_AC_TIMING_ENTRIES),
2486b843c749SSergey Zigachev 		"Invalid VramInfo table.", return -EINVAL);
2487b843c749SSergey Zigachev 
2488b843c749SSergey Zigachev 	for (i = 0; i < table->last; i++) {
2489b843c749SSergey Zigachev 		ni_table->mc_reg_address[i].s1 = table->mc_reg_address[i].s1;
2490b843c749SSergey Zigachev 	}
2491b843c749SSergey Zigachev 	ni_table->last = table->last;
2492b843c749SSergey Zigachev 
2493b843c749SSergey Zigachev 	for (i = 0; i < table->num_entries; i++) {
2494b843c749SSergey Zigachev 		ni_table->mc_reg_table_entry[i].mclk_max =
2495b843c749SSergey Zigachev 			table->mc_reg_table_entry[i].mclk_max;
2496b843c749SSergey Zigachev 		for (j = 0; j < table->last; j++) {
2497b843c749SSergey Zigachev 			ni_table->mc_reg_table_entry[i].mc_data[j] =
2498b843c749SSergey Zigachev 				table->mc_reg_table_entry[i].mc_data[j];
2499b843c749SSergey Zigachev 		}
2500b843c749SSergey Zigachev 	}
2501b843c749SSergey Zigachev 
2502b843c749SSergey Zigachev 	ni_table->num_entries = table->num_entries;
2503b843c749SSergey Zigachev 
2504b843c749SSergey Zigachev 	return 0;
2505b843c749SSergey Zigachev }
2506b843c749SSergey Zigachev 
iceland_set_mc_special_registers(struct pp_hwmgr * hwmgr,struct iceland_mc_reg_table * table)2507b843c749SSergey Zigachev static int iceland_set_mc_special_registers(struct pp_hwmgr *hwmgr,
2508b843c749SSergey Zigachev 					struct iceland_mc_reg_table *table)
2509b843c749SSergey Zigachev {
2510b843c749SSergey Zigachev 	uint8_t i, j, k;
2511b843c749SSergey Zigachev 	uint32_t temp_reg;
2512b843c749SSergey Zigachev 	struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
2513b843c749SSergey Zigachev 
2514b843c749SSergey Zigachev 	for (i = 0, j = table->last; i < table->last; i++) {
2515b843c749SSergey Zigachev 		PP_ASSERT_WITH_CODE((j < SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE),
2516b843c749SSergey Zigachev 			"Invalid VramInfo table.", return -EINVAL);
2517b843c749SSergey Zigachev 
2518b843c749SSergey Zigachev 		switch (table->mc_reg_address[i].s1) {
2519b843c749SSergey Zigachev 
2520b843c749SSergey Zigachev 		case mmMC_SEQ_MISC1:
2521b843c749SSergey Zigachev 			temp_reg = cgs_read_register(hwmgr->device, mmMC_PMG_CMD_EMRS);
2522b843c749SSergey Zigachev 			table->mc_reg_address[j].s1 = mmMC_PMG_CMD_EMRS;
2523b843c749SSergey Zigachev 			table->mc_reg_address[j].s0 = mmMC_SEQ_PMG_CMD_EMRS_LP;
2524b843c749SSergey Zigachev 			for (k = 0; k < table->num_entries; k++) {
2525b843c749SSergey Zigachev 				table->mc_reg_table_entry[k].mc_data[j] =
2526b843c749SSergey Zigachev 					((temp_reg & 0xffff0000)) |
2527b843c749SSergey Zigachev 					((table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16);
2528b843c749SSergey Zigachev 			}
2529b843c749SSergey Zigachev 			j++;
2530b843c749SSergey Zigachev 
2531b843c749SSergey Zigachev 			PP_ASSERT_WITH_CODE((j < SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE),
2532b843c749SSergey Zigachev 				"Invalid VramInfo table.", return -EINVAL);
2533b843c749SSergey Zigachev 			temp_reg = cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS);
2534b843c749SSergey Zigachev 			table->mc_reg_address[j].s1 = mmMC_PMG_CMD_MRS;
2535b843c749SSergey Zigachev 			table->mc_reg_address[j].s0 = mmMC_SEQ_PMG_CMD_MRS_LP;
2536b843c749SSergey Zigachev 			for (k = 0; k < table->num_entries; k++) {
2537b843c749SSergey Zigachev 				table->mc_reg_table_entry[k].mc_data[j] =
2538b843c749SSergey Zigachev 					(temp_reg & 0xffff0000) |
2539b843c749SSergey Zigachev 					(table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
2540b843c749SSergey Zigachev 
2541b843c749SSergey Zigachev 				if (!data->is_memory_gddr5) {
2542b843c749SSergey Zigachev 					table->mc_reg_table_entry[k].mc_data[j] |= 0x100;
2543b843c749SSergey Zigachev 				}
2544b843c749SSergey Zigachev 			}
2545b843c749SSergey Zigachev 			j++;
2546b843c749SSergey Zigachev 
2547b843c749SSergey Zigachev 			if (!data->is_memory_gddr5) {
2548b843c749SSergey Zigachev 				PP_ASSERT_WITH_CODE((j < SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE),
2549b843c749SSergey Zigachev 					"Invalid VramInfo table.", return -EINVAL);
2550b843c749SSergey Zigachev 				table->mc_reg_address[j].s1 = mmMC_PMG_AUTO_CMD;
2551b843c749SSergey Zigachev 				table->mc_reg_address[j].s0 = mmMC_PMG_AUTO_CMD;
2552b843c749SSergey Zigachev 				for (k = 0; k < table->num_entries; k++) {
2553b843c749SSergey Zigachev 					table->mc_reg_table_entry[k].mc_data[j] =
2554b843c749SSergey Zigachev 						(table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16;
2555b843c749SSergey Zigachev 				}
2556b843c749SSergey Zigachev 				j++;
2557b843c749SSergey Zigachev 			}
2558b843c749SSergey Zigachev 
2559b843c749SSergey Zigachev 			break;
2560b843c749SSergey Zigachev 
2561b843c749SSergey Zigachev 		case mmMC_SEQ_RESERVE_M:
2562b843c749SSergey Zigachev 			temp_reg = cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS1);
2563b843c749SSergey Zigachev 			table->mc_reg_address[j].s1 = mmMC_PMG_CMD_MRS1;
2564b843c749SSergey Zigachev 			table->mc_reg_address[j].s0 = mmMC_SEQ_PMG_CMD_MRS1_LP;
2565b843c749SSergey Zigachev 			for (k = 0; k < table->num_entries; k++) {
2566b843c749SSergey Zigachev 				table->mc_reg_table_entry[k].mc_data[j] =
2567b843c749SSergey Zigachev 					(temp_reg & 0xffff0000) |
2568b843c749SSergey Zigachev 					(table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
2569b843c749SSergey Zigachev 			}
2570b843c749SSergey Zigachev 			j++;
2571b843c749SSergey Zigachev 			break;
2572b843c749SSergey Zigachev 
2573b843c749SSergey Zigachev 		default:
2574b843c749SSergey Zigachev 			break;
2575b843c749SSergey Zigachev 		}
2576b843c749SSergey Zigachev 
2577b843c749SSergey Zigachev 	}
2578b843c749SSergey Zigachev 
2579b843c749SSergey Zigachev 	table->last = j;
2580b843c749SSergey Zigachev 
2581b843c749SSergey Zigachev 	return 0;
2582b843c749SSergey Zigachev }
2583b843c749SSergey Zigachev 
iceland_set_valid_flag(struct iceland_mc_reg_table * table)2584b843c749SSergey Zigachev static int iceland_set_valid_flag(struct iceland_mc_reg_table *table)
2585b843c749SSergey Zigachev {
2586b843c749SSergey Zigachev 	uint8_t i, j;
2587b843c749SSergey Zigachev 	for (i = 0; i < table->last; i++) {
2588b843c749SSergey Zigachev 		for (j = 1; j < table->num_entries; j++) {
2589b843c749SSergey Zigachev 			if (table->mc_reg_table_entry[j-1].mc_data[i] !=
2590b843c749SSergey Zigachev 				table->mc_reg_table_entry[j].mc_data[i]) {
2591b843c749SSergey Zigachev 				table->validflag |= (1<<i);
2592b843c749SSergey Zigachev 				break;
2593b843c749SSergey Zigachev 			}
2594b843c749SSergey Zigachev 		}
2595b843c749SSergey Zigachev 	}
2596b843c749SSergey Zigachev 
2597b843c749SSergey Zigachev 	return 0;
2598b843c749SSergey Zigachev }
2599b843c749SSergey Zigachev 
iceland_initialize_mc_reg_table(struct pp_hwmgr * hwmgr)2600b843c749SSergey Zigachev static int iceland_initialize_mc_reg_table(struct pp_hwmgr *hwmgr)
2601b843c749SSergey Zigachev {
2602b843c749SSergey Zigachev 	int result;
2603b843c749SSergey Zigachev 	struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
2604b843c749SSergey Zigachev 	pp_atomctrl_mc_reg_table *table;
2605b843c749SSergey Zigachev 	struct iceland_mc_reg_table *ni_table = &smu_data->mc_reg_table;
2606b843c749SSergey Zigachev 	uint8_t module_index = iceland_get_memory_modile_index(hwmgr);
2607b843c749SSergey Zigachev 
2608b843c749SSergey Zigachev 	table = kzalloc(sizeof(pp_atomctrl_mc_reg_table), GFP_KERNEL);
2609b843c749SSergey Zigachev 
2610b843c749SSergey Zigachev 	if (NULL == table)
2611b843c749SSergey Zigachev 		return -ENOMEM;
2612b843c749SSergey Zigachev 
2613b843c749SSergey Zigachev 	/* Program additional LP registers that are no longer programmed by VBIOS */
2614b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_RAS_TIMING_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_RAS_TIMING));
2615b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_CAS_TIMING_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_CAS_TIMING));
2616b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_DLL_STBY_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_DLL_STBY));
2617b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_G5PDX_CMD0_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_G5PDX_CMD0));
2618b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_G5PDX_CMD1_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_G5PDX_CMD1));
2619b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_G5PDX_CTRL_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_G5PDX_CTRL));
2620b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_DVS_CMD_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_PMG_DVS_CMD));
2621b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_DVS_CTL_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_PMG_DVS_CTL));
2622b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_MISC_TIMING_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_MISC_TIMING));
2623b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_MISC_TIMING2_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_MISC_TIMING2));
2624b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_CMD_EMRS_LP, cgs_read_register(hwmgr->device, mmMC_PMG_CMD_EMRS));
2625b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_CMD_MRS_LP, cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS));
2626b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_CMD_MRS1_LP, cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS1));
2627b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_WR_CTL_D0_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_WR_CTL_D0));
2628b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_WR_CTL_D1_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_WR_CTL_D1));
2629b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_RD_CTL_D0_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_RD_CTL_D0));
2630b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_RD_CTL_D1_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_RD_CTL_D1));
2631b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_TIMING_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_PMG_TIMING));
2632b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_CMD_MRS2_LP, cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS2));
2633b843c749SSergey Zigachev 	cgs_write_register(hwmgr->device, mmMC_SEQ_WR_CTL_2_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_WR_CTL_2));
2634b843c749SSergey Zigachev 
2635b843c749SSergey Zigachev 	memset(table, 0x00, sizeof(pp_atomctrl_mc_reg_table));
2636b843c749SSergey Zigachev 
2637b843c749SSergey Zigachev 	result = atomctrl_initialize_mc_reg_table(hwmgr, module_index, table);
2638b843c749SSergey Zigachev 
2639b843c749SSergey Zigachev 	if (0 == result)
2640b843c749SSergey Zigachev 		result = iceland_copy_vbios_smc_reg_table(table, ni_table);
2641b843c749SSergey Zigachev 
2642b843c749SSergey Zigachev 	if (0 == result) {
2643b843c749SSergey Zigachev 		iceland_set_s0_mc_reg_index(ni_table);
2644b843c749SSergey Zigachev 		result = iceland_set_mc_special_registers(hwmgr, ni_table);
2645b843c749SSergey Zigachev 	}
2646b843c749SSergey Zigachev 
2647b843c749SSergey Zigachev 	if (0 == result)
2648b843c749SSergey Zigachev 		iceland_set_valid_flag(ni_table);
2649b843c749SSergey Zigachev 
2650b843c749SSergey Zigachev 	kfree(table);
2651b843c749SSergey Zigachev 
2652b843c749SSergey Zigachev 	return result;
2653b843c749SSergey Zigachev }
2654b843c749SSergey Zigachev 
iceland_is_dpm_running(struct pp_hwmgr * hwmgr)2655b843c749SSergey Zigachev static bool iceland_is_dpm_running(struct pp_hwmgr *hwmgr)
2656b843c749SSergey Zigachev {
2657b843c749SSergey Zigachev 	return (1 == PHM_READ_INDIRECT_FIELD(hwmgr->device,
2658b843c749SSergey Zigachev 			CGS_IND_REG__SMC, FEATURE_STATUS, VOLTAGE_CONTROLLER_ON))
2659b843c749SSergey Zigachev 			? true : false;
2660b843c749SSergey Zigachev }
2661b843c749SSergey Zigachev 
2662b843c749SSergey Zigachev const struct pp_smumgr_func iceland_smu_funcs = {
2663b843c749SSergey Zigachev 	.smu_init = &iceland_smu_init,
2664b843c749SSergey Zigachev 	.smu_fini = &smu7_smu_fini,
2665b843c749SSergey Zigachev 	.start_smu = &iceland_start_smu,
2666b843c749SSergey Zigachev 	.check_fw_load_finish = &smu7_check_fw_load_finish,
2667b843c749SSergey Zigachev 	.request_smu_load_fw = &smu7_reload_firmware,
2668b843c749SSergey Zigachev 	.request_smu_load_specific_fw = &iceland_request_smu_load_specific_fw,
2669b843c749SSergey Zigachev 	.send_msg_to_smc = &smu7_send_msg_to_smc,
2670b843c749SSergey Zigachev 	.send_msg_to_smc_with_parameter = &smu7_send_msg_to_smc_with_parameter,
2671b843c749SSergey Zigachev 	.download_pptable_settings = NULL,
2672b843c749SSergey Zigachev 	.upload_pptable_settings = NULL,
2673b843c749SSergey Zigachev 	.get_offsetof = iceland_get_offsetof,
2674b843c749SSergey Zigachev 	.process_firmware_header = iceland_process_firmware_header,
2675b843c749SSergey Zigachev 	.init_smc_table = iceland_init_smc_table,
2676b843c749SSergey Zigachev 	.update_sclk_threshold = iceland_update_sclk_threshold,
2677b843c749SSergey Zigachev 	.thermal_setup_fan_table = iceland_thermal_setup_fan_table,
2678b843c749SSergey Zigachev 	.populate_all_graphic_levels = iceland_populate_all_graphic_levels,
2679b843c749SSergey Zigachev 	.populate_all_memory_levels = iceland_populate_all_memory_levels,
2680b843c749SSergey Zigachev 	.get_mac_definition = iceland_get_mac_definition,
2681b843c749SSergey Zigachev 	.initialize_mc_reg_table = iceland_initialize_mc_reg_table,
2682b843c749SSergey Zigachev 	.is_dpm_running = iceland_is_dpm_running,
2683b843c749SSergey Zigachev };
2684b843c749SSergey Zigachev 
2685