xref: /linux/drivers/gpu/drm/amd/display/dc/clk_mgr/dcn21/rn_clk_mgr_vbios_smu.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 /*
2  * Copyright 2012-16 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 "core_types.h"
27 #include "clk_mgr_internal.h"
28 #include "reg_helper.h"
29 #include "dm_helpers.h"
30 
31 #include "rn_clk_mgr_vbios_smu.h"
32 
33 #include <linux/delay.h>
34 
35 #include "renoir_ip_offset.h"
36 
37 #include "mp/mp_12_0_0_offset.h"
38 #include "mp/mp_12_0_0_sh_mask.h"
39 
40 #define REG(reg_name) \
41 	(MP0_BASE.instance[0].segment[mm ## reg_name ## _BASE_IDX] + mm ## reg_name)
42 
43 #define FN(reg_name, field) \
44 	FD(reg_name##__##field)
45 
46 #include "logger_types.h"
47 #undef DC_LOGGER
48 #define DC_LOGGER \
49 	CTX->logger
50 #define smu_print(str, ...) {DC_LOG_SMU(str, ##__VA_ARGS__); }
51 
52 #define VBIOSSMC_MSG_TestMessage                  0x1
53 #define VBIOSSMC_MSG_GetSmuVersion                0x2
54 #define VBIOSSMC_MSG_PowerUpGfx                   0x3
55 #define VBIOSSMC_MSG_SetDispclkFreq               0x4
56 #define VBIOSSMC_MSG_SetDprefclkFreq              0x5
57 #define VBIOSSMC_MSG_PowerDownGfx                 0x6
58 #define VBIOSSMC_MSG_SetDppclkFreq                0x7
59 #define VBIOSSMC_MSG_SetHardMinDcfclkByFreq       0x8
60 #define VBIOSSMC_MSG_SetMinDeepSleepDcfclk        0x9
61 #define VBIOSSMC_MSG_SetPhyclkVoltageByFreq       0xA
62 #define VBIOSSMC_MSG_GetFclkFrequency             0xB
63 #define VBIOSSMC_MSG_SetDisplayCount              0xC
64 #define VBIOSSMC_MSG_EnableTmdp48MHzRefclkPwrDown 0xD
65 #define VBIOSSMC_MSG_UpdatePmeRestore             0xE
66 #define VBIOSSMC_MSG_IsPeriodicRetrainingDisabled 0xF
67 
68 #define VBIOSSMC_Status_BUSY                      0x0
69 #define VBIOSSMC_Result_OK                        0x1
70 #define VBIOSSMC_Result_Failed                    0xFF
71 #define VBIOSSMC_Result_UnknownCmd                0xFE
72 #define VBIOSSMC_Result_CmdRejectedPrereq         0xFD
73 #define VBIOSSMC_Result_CmdRejectedBusy           0xFC
74 
75 /*
76  * Function to be used instead of REG_WAIT macro because the wait ends when
77  * the register is NOT EQUAL to zero, and because the translation in msg_if.h
78  * won't work with REG_WAIT.
79  */
rn_smu_wait_for_response(struct clk_mgr_internal * clk_mgr,unsigned int delay_us,unsigned int max_retries)80 static uint32_t rn_smu_wait_for_response(struct clk_mgr_internal *clk_mgr, unsigned int delay_us, unsigned int max_retries)
81 {
82 	uint32_t res_val = VBIOSSMC_Status_BUSY;
83 
84 	do {
85 		res_val = REG_READ(MP1_SMN_C2PMSG_91);
86 		if (res_val != VBIOSSMC_Status_BUSY)
87 			break;
88 
89 		if (delay_us >= 1000)
90 			msleep(delay_us/1000);
91 		else if (delay_us > 0)
92 			udelay(delay_us);
93 	} while (max_retries--);
94 
95 	return res_val;
96 }
97 
98 
rn_vbios_smu_send_msg_with_param(struct clk_mgr_internal * clk_mgr,unsigned int msg_id,unsigned int param)99 static int rn_vbios_smu_send_msg_with_param(struct clk_mgr_internal *clk_mgr,
100 					    unsigned int msg_id,
101 					    unsigned int param)
102 {
103 	uint32_t result;
104 
105 	result = rn_smu_wait_for_response(clk_mgr, 10, 200000);
106 
107 	if (result != VBIOSSMC_Result_OK)
108 		smu_print("SMU Response was not OK. SMU response after wait received is: %d\n", result);
109 
110 	if (result == VBIOSSMC_Status_BUSY) {
111 		return -1;
112 	}
113 
114 	/* First clear response register */
115 	REG_WRITE(MP1_SMN_C2PMSG_91, VBIOSSMC_Status_BUSY);
116 
117 	/* Set the parameter register for the SMU message, unit is Mhz */
118 	REG_WRITE(MP1_SMN_C2PMSG_83, param);
119 
120 	/* Trigger the message transaction by writing the message ID */
121 	REG_WRITE(MP1_SMN_C2PMSG_67, msg_id);
122 
123 	result = rn_smu_wait_for_response(clk_mgr, 10, 200000);
124 
125 	if (IS_SMU_TIMEOUT(result)) {
126 		ASSERT(0);
127 		dm_helpers_smu_timeout(CTX, msg_id, param, 10 * 200000);
128 	}
129 
130 	/* Actual dispclk set is returned in the parameter register */
131 	return REG_READ(MP1_SMN_C2PMSG_83);
132 }
133 
rn_vbios_smu_get_smu_version(struct clk_mgr_internal * clk_mgr)134 int rn_vbios_smu_get_smu_version(struct clk_mgr_internal *clk_mgr)
135 {
136 	return rn_vbios_smu_send_msg_with_param(
137 			clk_mgr,
138 			VBIOSSMC_MSG_GetSmuVersion,
139 			0);
140 }
141 
142 
rn_vbios_smu_set_dispclk(struct clk_mgr_internal * clk_mgr,int requested_dispclk_khz)143 int rn_vbios_smu_set_dispclk(struct clk_mgr_internal *clk_mgr, int requested_dispclk_khz)
144 {
145 	int actual_dispclk_set_mhz = -1;
146 	struct dc *dc = clk_mgr->base.ctx->dc;
147 	struct dmcu *dmcu = dc->res_pool->dmcu;
148 
149 	/*  Unit of SMU msg parameter is Mhz */
150 	actual_dispclk_set_mhz = rn_vbios_smu_send_msg_with_param(
151 			clk_mgr,
152 			VBIOSSMC_MSG_SetDispclkFreq,
153 			khz_to_mhz_ceil(requested_dispclk_khz));
154 
155 	if (dmcu && dmcu->funcs->is_dmcu_initialized(dmcu)) {
156 		if (clk_mgr->dfs_bypass_disp_clk != actual_dispclk_set_mhz)
157 			dmcu->funcs->set_psr_wait_loop(dmcu,
158 					actual_dispclk_set_mhz / 7);
159 	}
160 
161 	// pmfw always set clock more than or equal requested clock
162 	ASSERT(actual_dispclk_set_mhz >= khz_to_mhz_ceil(requested_dispclk_khz));
163 
164 	return actual_dispclk_set_mhz * 1000;
165 }
166 
rn_vbios_smu_set_hard_min_dcfclk(struct clk_mgr_internal * clk_mgr,int requested_dcfclk_khz)167 int rn_vbios_smu_set_hard_min_dcfclk(struct clk_mgr_internal *clk_mgr, int requested_dcfclk_khz)
168 {
169 	int actual_dcfclk_set_mhz = -1;
170 
171 	if (clk_mgr->smu_ver < 0x370c00)
172 		return actual_dcfclk_set_mhz;
173 
174 	actual_dcfclk_set_mhz = rn_vbios_smu_send_msg_with_param(
175 			clk_mgr,
176 			VBIOSSMC_MSG_SetHardMinDcfclkByFreq,
177 			khz_to_mhz_ceil(requested_dcfclk_khz));
178 
179 	return actual_dcfclk_set_mhz * 1000;
180 }
181 
rn_vbios_smu_set_min_deep_sleep_dcfclk(struct clk_mgr_internal * clk_mgr,int requested_min_ds_dcfclk_khz)182 int rn_vbios_smu_set_min_deep_sleep_dcfclk(struct clk_mgr_internal *clk_mgr, int requested_min_ds_dcfclk_khz)
183 {
184 	int actual_min_ds_dcfclk_mhz = -1;
185 
186 	if (clk_mgr->smu_ver < 0x370c00)
187 		return actual_min_ds_dcfclk_mhz;
188 
189 	actual_min_ds_dcfclk_mhz = rn_vbios_smu_send_msg_with_param(
190 			clk_mgr,
191 			VBIOSSMC_MSG_SetMinDeepSleepDcfclk,
192 			khz_to_mhz_ceil(requested_min_ds_dcfclk_khz));
193 
194 	return actual_min_ds_dcfclk_mhz * 1000;
195 }
196 
rn_vbios_smu_set_phyclk(struct clk_mgr_internal * clk_mgr,int requested_phyclk_khz)197 void rn_vbios_smu_set_phyclk(struct clk_mgr_internal *clk_mgr, int requested_phyclk_khz)
198 {
199 	rn_vbios_smu_send_msg_with_param(
200 			clk_mgr,
201 			VBIOSSMC_MSG_SetPhyclkVoltageByFreq,
202 			khz_to_mhz_ceil(requested_phyclk_khz));
203 }
204 
rn_vbios_smu_set_dppclk(struct clk_mgr_internal * clk_mgr,int requested_dpp_khz)205 int rn_vbios_smu_set_dppclk(struct clk_mgr_internal *clk_mgr, int requested_dpp_khz)
206 {
207 	int actual_dppclk_set_mhz = -1;
208 
209 	actual_dppclk_set_mhz = rn_vbios_smu_send_msg_with_param(
210 			clk_mgr,
211 			VBIOSSMC_MSG_SetDppclkFreq,
212 			khz_to_mhz_ceil(requested_dpp_khz));
213 
214 	ASSERT(actual_dppclk_set_mhz >= khz_to_mhz_ceil(requested_dpp_khz));
215 
216 	return actual_dppclk_set_mhz * 1000;
217 }
218 
rn_vbios_smu_set_dcn_low_power_state(struct clk_mgr_internal * clk_mgr,enum dcn_pwr_state state)219 void rn_vbios_smu_set_dcn_low_power_state(struct clk_mgr_internal *clk_mgr, enum dcn_pwr_state state)
220 {
221 	int disp_count;
222 
223 	if (state == DCN_PWR_STATE_LOW_POWER)
224 		disp_count = 0;
225 	else
226 		disp_count = 1;
227 
228 	rn_vbios_smu_send_msg_with_param(
229 		clk_mgr,
230 		VBIOSSMC_MSG_SetDisplayCount,
231 		disp_count);
232 }
233 
rn_vbios_smu_enable_48mhz_tmdp_refclk_pwrdwn(struct clk_mgr_internal * clk_mgr,bool enable)234 void rn_vbios_smu_enable_48mhz_tmdp_refclk_pwrdwn(struct clk_mgr_internal *clk_mgr, bool enable)
235 {
236 	rn_vbios_smu_send_msg_with_param(
237 			clk_mgr,
238 			VBIOSSMC_MSG_EnableTmdp48MHzRefclkPwrDown,
239 			enable);
240 }
241 
rn_vbios_smu_enable_pme_wa(struct clk_mgr_internal * clk_mgr)242 void rn_vbios_smu_enable_pme_wa(struct clk_mgr_internal *clk_mgr)
243 {
244 	rn_vbios_smu_send_msg_with_param(
245 			clk_mgr,
246 			VBIOSSMC_MSG_UpdatePmeRestore,
247 			0);
248 }
249 
rn_vbios_smu_is_periodic_retraining_disabled(struct clk_mgr_internal * clk_mgr)250 int rn_vbios_smu_is_periodic_retraining_disabled(struct clk_mgr_internal *clk_mgr)
251 {
252 	return rn_vbios_smu_send_msg_with_param(
253 			clk_mgr,
254 			VBIOSSMC_MSG_IsPeriodicRetrainingDisabled,
255 			1);	// if PMFW doesn't support this message, assume retraining is disabled
256 				// so we only use most optimal watermark if we know retraining is enabled.
257 }
258