1 /*
2  * Copyright 2020 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include "dcn30_clk_mgr_smu_msg.h"
27 
28 #include "clk_mgr_internal.h"
29 #include "reg_helper.h"
30 #include "dm_helpers.h"
31 
32 #include "dalsmc.h"
33 #include "dcn30_smu11_driver_if.h"
34 
35 #define mmDAL_MSG_REG  0x1628A
36 #define mmDAL_ARG_REG  0x16273
37 #define mmDAL_RESP_REG 0x16274
38 
39 #define REG(reg_name) \
40 	mm ## reg_name
41 
42 #include "logger_types.h"
43 #undef DC_LOGGER
44 #define DC_LOGGER \
45 	CTX->logger
46 #define smu_print(str, ...) {DC_LOG_SMU(str, ##__VA_ARGS__); }
47 
48 
49 /*
50  * Function to be used instead of REG_WAIT macro because the wait ends when
51  * the register is NOT EQUAL to zero, and because the translation in msg_if.h
52  * won't work with REG_WAIT.
53  */
dcn30_smu_wait_for_response(struct clk_mgr_internal * clk_mgr,unsigned int delay_us,unsigned int max_retries)54 static uint32_t dcn30_smu_wait_for_response(struct clk_mgr_internal *clk_mgr, unsigned int delay_us, unsigned int max_retries)
55 {
56 	const uint32_t initial_max_retries = max_retries;
57 	uint32_t reg = 0;
58 
59 	do {
60 		reg = REG_READ(DAL_RESP_REG);
61 		if (reg)
62 			break;
63 
64 		if (delay_us >= 1000)
65 			msleep(delay_us/1000);
66 		else if (delay_us > 0)
67 			udelay(delay_us);
68 	} while (max_retries--);
69 
70 	/* handle DALSMC_Result_CmdRejectedBusy? */
71 
72 	TRACE_SMU_DELAY(delay_us * (initial_max_retries - max_retries), clk_mgr->base.ctx);
73 
74 	return reg;
75 }
76 
dcn30_smu_send_msg_with_param(struct clk_mgr_internal * clk_mgr,uint32_t msg_id,uint32_t param_in,uint32_t * param_out)77 static bool dcn30_smu_send_msg_with_param(struct clk_mgr_internal *clk_mgr, uint32_t msg_id, uint32_t param_in, uint32_t *param_out)
78 {
79 	uint32_t result;
80 	/* Wait for response register to be ready */
81 	dcn30_smu_wait_for_response(clk_mgr, 10, 200000);
82 
83 	/* Clear response register */
84 	REG_WRITE(DAL_RESP_REG, 0);
85 
86 	/* Set the parameter register for the SMU message */
87 	REG_WRITE(DAL_ARG_REG, param_in);
88 
89 	/* Trigger the message transaction by writing the message ID */
90 	REG_WRITE(DAL_MSG_REG, msg_id);
91 
92 	TRACE_SMU_MSG(msg_id, param_in, clk_mgr->base.ctx);
93 
94 	result = dcn30_smu_wait_for_response(clk_mgr, 10, 200000);
95 
96 	if (IS_SMU_TIMEOUT(result)) {
97 		dm_helpers_smu_timeout(CTX, msg_id, param_in, 10 * 200000);
98 	}
99 
100 	/* Wait for response */
101 	if (result == DALSMC_Result_OK) {
102 		if (param_out)
103 			*param_out = REG_READ(DAL_ARG_REG);
104 
105 		return true;
106 	}
107 
108 	return false;
109 }
110 
111 /* Test message should return input + 1 */
dcn30_smu_test_message(struct clk_mgr_internal * clk_mgr,uint32_t input)112 bool dcn30_smu_test_message(struct clk_mgr_internal *clk_mgr, uint32_t input)
113 {
114 	uint32_t response = 0;
115 
116 	smu_print("SMU Test message: %d\n", input);
117 
118 	if (dcn30_smu_send_msg_with_param(clk_mgr,
119 			DALSMC_MSG_TestMessage, input, &response))
120 		if (response == input + 1)
121 			return true;
122 
123 	return false;
124 }
125 
dcn30_smu_get_smu_version(struct clk_mgr_internal * clk_mgr,unsigned int * version)126 bool dcn30_smu_get_smu_version(struct clk_mgr_internal *clk_mgr, unsigned int *version)
127 {
128 	smu_print("SMU Get SMU version\n");
129 
130 	if (dcn30_smu_send_msg_with_param(clk_mgr,
131 			DALSMC_MSG_GetSmuVersion, 0, version)) {
132 
133 		smu_print("SMU version: %d\n", *version);
134 
135 		return true;
136 	}
137 
138 	return false;
139 }
140 
141 /* Message output should match SMU11_DRIVER_IF_VERSION in smu11_driver_if.h */
dcn30_smu_check_driver_if_version(struct clk_mgr_internal * clk_mgr)142 bool dcn30_smu_check_driver_if_version(struct clk_mgr_internal *clk_mgr)
143 {
144 	uint32_t response = 0;
145 
146 	smu_print("SMU Check driver if version\n");
147 
148 	if (dcn30_smu_send_msg_with_param(clk_mgr,
149 			DALSMC_MSG_GetDriverIfVersion, 0, &response)) {
150 
151 		smu_print("SMU driver if version: %d\n", response);
152 
153 		if (response == SMU11_DRIVER_IF_VERSION)
154 			return true;
155 	}
156 
157 	return false;
158 }
159 
160 /* Message output should match DALSMC_VERSION in dalsmc.h */
dcn30_smu_check_msg_header_version(struct clk_mgr_internal * clk_mgr)161 bool dcn30_smu_check_msg_header_version(struct clk_mgr_internal *clk_mgr)
162 {
163 	uint32_t response = 0;
164 
165 	smu_print("SMU Check msg header version\n");
166 
167 	if (dcn30_smu_send_msg_with_param(clk_mgr,
168 			DALSMC_MSG_GetMsgHeaderVersion, 0, &response)) {
169 
170 		smu_print("SMU msg header version: %d\n", response);
171 
172 		if (response == DALSMC_VERSION)
173 			return true;
174 	}
175 
176 	return false;
177 }
178 
dcn30_smu_set_dram_addr_high(struct clk_mgr_internal * clk_mgr,uint32_t addr_high)179 void dcn30_smu_set_dram_addr_high(struct clk_mgr_internal *clk_mgr, uint32_t addr_high)
180 {
181 	smu_print("SMU Set DRAM addr high: %d\n", addr_high);
182 
183 	dcn30_smu_send_msg_with_param(clk_mgr,
184 			DALSMC_MSG_SetDalDramAddrHigh, addr_high, NULL);
185 }
186 
dcn30_smu_set_dram_addr_low(struct clk_mgr_internal * clk_mgr,uint32_t addr_low)187 void dcn30_smu_set_dram_addr_low(struct clk_mgr_internal *clk_mgr, uint32_t addr_low)
188 {
189 	smu_print("SMU Set DRAM addr low: %d\n", addr_low);
190 
191 	dcn30_smu_send_msg_with_param(clk_mgr,
192 			DALSMC_MSG_SetDalDramAddrLow, addr_low, NULL);
193 }
194 
dcn30_smu_transfer_wm_table_smu_2_dram(struct clk_mgr_internal * clk_mgr)195 void dcn30_smu_transfer_wm_table_smu_2_dram(struct clk_mgr_internal *clk_mgr)
196 {
197 	smu_print("SMU Transfer WM table SMU 2 DRAM\n");
198 
199 	dcn30_smu_send_msg_with_param(clk_mgr,
200 			DALSMC_MSG_TransferTableSmu2Dram, TABLE_WATERMARKS, NULL);
201 }
202 
dcn30_smu_transfer_wm_table_dram_2_smu(struct clk_mgr_internal * clk_mgr)203 void dcn30_smu_transfer_wm_table_dram_2_smu(struct clk_mgr_internal *clk_mgr)
204 {
205 	smu_print("SMU Transfer WM table DRAM 2 SMU\n");
206 
207 	dcn30_smu_send_msg_with_param(clk_mgr,
208 			DALSMC_MSG_TransferTableDram2Smu, TABLE_WATERMARKS, NULL);
209 }
210 
211 /* Returns the actual frequency that was set in MHz, 0 on failure */
dcn30_smu_set_hard_min_by_freq(struct clk_mgr_internal * clk_mgr,uint32_t clk,uint16_t freq_mhz)212 unsigned int dcn30_smu_set_hard_min_by_freq(struct clk_mgr_internal *clk_mgr, uint32_t clk, uint16_t freq_mhz)
213 {
214 	uint32_t response = 0;
215 
216 	/* bits 23:16 for clock type, lower 16 bits for frequency in MHz */
217 	uint32_t param = (clk << 16) | freq_mhz;
218 
219 	smu_print("SMU Set hard min by freq: clk = %d, freq_mhz = %d MHz\n", clk, freq_mhz);
220 
221 	dcn30_smu_send_msg_with_param(clk_mgr,
222 			DALSMC_MSG_SetHardMinByFreq, param, &response);
223 
224 	smu_print("SMU Frequency set = %d MHz\n", response);
225 
226 	return response;
227 }
228 
229 /* Returns the actual frequency that was set in MHz, 0 on failure */
dcn30_smu_set_hard_max_by_freq(struct clk_mgr_internal * clk_mgr,uint32_t clk,uint16_t freq_mhz)230 unsigned int dcn30_smu_set_hard_max_by_freq(struct clk_mgr_internal *clk_mgr, uint32_t clk, uint16_t freq_mhz)
231 {
232 	uint32_t response = 0;
233 
234 	/* bits 23:16 for clock type, lower 16 bits for frequency in MHz */
235 	uint32_t param = (clk << 16) | freq_mhz;
236 
237 	smu_print("SMU Set hard max by freq: clk = %d, freq_mhz = %d MHz\n", clk, freq_mhz);
238 
239 	dcn30_smu_send_msg_with_param(clk_mgr,
240 			DALSMC_MSG_SetHardMaxByFreq, param, &response);
241 
242 	smu_print("SMU Frequency set = %d MHz\n", response);
243 
244 	return response;
245 }
246 
247 /*
248  * Frequency in MHz returned in lower 16 bits for valid DPM level
249  *
250  * Call with dpm_level = 0xFF to query features, return value will be:
251  *     Bits 7:0 - number of DPM levels
252  *     Bit   28 - 1 = auto DPM on
253  *     Bit   29 - 1 = sweep DPM on
254  *     Bit   30 - 1 = forced DPM on
255  *     Bit   31 - 0 = discrete, 1 = fine-grained
256  *
257  * With fine-grained DPM, only min and max frequencies will be reported
258  *
259  * Returns 0 on failure
260  */
dcn30_smu_get_dpm_freq_by_index(struct clk_mgr_internal * clk_mgr,uint32_t clk,uint8_t dpm_level)261 unsigned int dcn30_smu_get_dpm_freq_by_index(struct clk_mgr_internal *clk_mgr, uint32_t clk, uint8_t dpm_level)
262 {
263 	uint32_t response = 0;
264 
265 	/* bits 23:16 for clock type, lower 8 bits for DPM level */
266 	uint32_t param = (clk << 16) | dpm_level;
267 
268 	smu_print("SMU Get dpm freq by index: clk = %d, dpm_level = %d\n", clk, dpm_level);
269 
270 	dcn30_smu_send_msg_with_param(clk_mgr,
271 			DALSMC_MSG_GetDpmFreqByIndex, param, &response);
272 
273 	smu_print("SMU dpm freq: %d MHz\n", response);
274 
275 	return response;
276 }
277 
278 /* Returns the max DPM frequency in DC mode in MHz, 0 on failure */
dcn30_smu_get_dc_mode_max_dpm_freq(struct clk_mgr_internal * clk_mgr,uint32_t clk)279 unsigned int dcn30_smu_get_dc_mode_max_dpm_freq(struct clk_mgr_internal *clk_mgr, uint32_t clk)
280 {
281 	uint32_t response = 0;
282 
283 	/* bits 23:16 for clock type */
284 	uint32_t param = clk << 16;
285 
286 	smu_print("SMU Get DC mode max DPM freq: clk = %d\n", clk);
287 
288 	dcn30_smu_send_msg_with_param(clk_mgr,
289 			DALSMC_MSG_GetDcModeMaxDpmFreq, param, &response);
290 
291 	smu_print("SMU DC mode max DMP freq: %d MHz\n", response);
292 
293 	return response;
294 }
295 
dcn30_smu_set_min_deep_sleep_dcef_clk(struct clk_mgr_internal * clk_mgr,uint32_t freq_mhz)296 void dcn30_smu_set_min_deep_sleep_dcef_clk(struct clk_mgr_internal *clk_mgr, uint32_t freq_mhz)
297 {
298 	smu_print("SMU Set min deep sleep dcef clk: freq_mhz = %d MHz\n", freq_mhz);
299 
300 	dcn30_smu_send_msg_with_param(clk_mgr,
301 			DALSMC_MSG_SetMinDeepSleepDcefclk, freq_mhz, NULL);
302 }
303 
dcn30_smu_set_num_of_displays(struct clk_mgr_internal * clk_mgr,uint32_t num_displays)304 void dcn30_smu_set_num_of_displays(struct clk_mgr_internal *clk_mgr, uint32_t num_displays)
305 {
306 	smu_print("SMU Set num of displays: num_displays = %d\n", num_displays);
307 
308 	dcn30_smu_send_msg_with_param(clk_mgr,
309 			DALSMC_MSG_NumOfDisplays, num_displays, NULL);
310 }
311 
dcn30_smu_set_display_refresh_from_mall(struct clk_mgr_internal * clk_mgr,bool enable,uint8_t cache_timer_delay,uint8_t cache_timer_scale)312 void dcn30_smu_set_display_refresh_from_mall(struct clk_mgr_internal *clk_mgr, bool enable, uint8_t cache_timer_delay, uint8_t cache_timer_scale)
313 {
314 	/* bits 8:7 for cache timer scale, bits 6:1 for cache timer delay, bit 0 = 1 for enable, = 0 for disable */
315 	uint32_t param = (cache_timer_scale << 7) | (cache_timer_delay << 1) | (enable ? 1 : 0);
316 
317 	smu_print("SMU Set display refresh from mall: enable = %d, cache_timer_delay = %d, cache_timer_scale = %d\n",
318 		enable, cache_timer_delay, cache_timer_scale);
319 
320 	dcn30_smu_send_msg_with_param(clk_mgr,
321 			DALSMC_MSG_SetDisplayRefreshFromMall, param, NULL);
322 }
323 
dcn30_smu_set_external_client_df_cstate_allow(struct clk_mgr_internal * clk_mgr,bool enable)324 void dcn30_smu_set_external_client_df_cstate_allow(struct clk_mgr_internal *clk_mgr, bool enable)
325 {
326 	smu_print("SMU Set external client df cstate allow: enable = %d\n", enable);
327 
328 	dcn30_smu_send_msg_with_param(clk_mgr,
329 			DALSMC_MSG_SetExternalClientDfCstateAllow, enable ? 1 : 0, NULL);
330 }
331 
dcn30_smu_set_pme_workaround(struct clk_mgr_internal * clk_mgr)332 void dcn30_smu_set_pme_workaround(struct clk_mgr_internal *clk_mgr)
333 {
334 	smu_print("SMU Set PME workaround\n");
335 
336 	dcn30_smu_send_msg_with_param(clk_mgr,
337 	DALSMC_MSG_BacoAudioD3PME, 0, NULL);
338 }
339