xref: /linux/drivers/gpu/drm/amd/display/dc/dce/dmub_replay.c (revision f82480fafedf622541276d48a3b4fed20ce5d866)
1 // SPDX-License-Identifier: MIT
2 //
3 // Copyright 2024 Advanced Micro Devices, Inc.
4 
5 #include "dc.h"
6 #include "link_service.h"
7 #include "dc_dmub_srv.h"
8 #include "dmub/dmub_srv.h"
9 #include "core_types.h"
10 #include "dmub_replay.h"
11 
12 #define DC_TRACE_LEVEL_MESSAGE(...) /* do nothing */
13 
14 #define MAX_PIPES 6
15 
16 #define GPINT_RETRY_NUM 20
17 
18 static const uint8_t DP_SINK_DEVICE_STR_ID_1[] = {7, 1, 8, 7, 3};
19 static const uint8_t DP_SINK_DEVICE_STR_ID_2[] = {7, 1, 8, 7, 5};
20 
21 /*
22  * Get Replay state from firmware.
23  */
24 static void dmub_replay_get_state(struct dmub_replay *dmub, enum replay_state *state, uint8_t panel_inst)
25 {
26 	uint32_t retry_count = 0;
27 
28 	do {
29 		// Send gpint command and wait for ack
30 		if (!dc_wake_and_execute_gpint(dmub->ctx, DMUB_GPINT__GET_REPLAY_STATE, panel_inst,
31 					       (uint32_t *)state, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)) {
32 			// Return invalid state when GPINT times out
33 			*state = REPLAY_STATE_INVALID;
34 		}
35 	} while (++retry_count <= 1000 && *state == REPLAY_STATE_INVALID);
36 
37 	// Assert if max retry hit
38 	if (retry_count >= 1000 && *state == REPLAY_STATE_INVALID) {
39 		ASSERT(0);
40 		/* To-do: Add retry fail log */
41 	}
42 }
43 
44 /*
45  * Enable/Disable Replay.
46  */
47 static void dmub_replay_enable(struct dmub_replay *dmub, bool enable, bool wait, uint8_t panel_inst)
48 {
49 	union dmub_rb_cmd cmd;
50 	struct dc_context *dc = dmub->ctx;
51 	uint32_t retry_count;
52 	enum replay_state state = REPLAY_STATE_0;
53 
54 	memset(&cmd, 0, sizeof(cmd));
55 	cmd.replay_enable.header.type = DMUB_CMD__REPLAY;
56 	cmd.replay_enable.data.panel_inst = panel_inst;
57 
58 	cmd.replay_enable.header.sub_type = DMUB_CMD__REPLAY_ENABLE;
59 	if (enable)
60 		cmd.replay_enable.data.enable = REPLAY_ENABLE;
61 	else
62 		cmd.replay_enable.data.enable = REPLAY_DISABLE;
63 
64 	cmd.replay_enable.header.payload_bytes = sizeof(struct dmub_rb_cmd_replay_enable_data);
65 
66 	dc_wake_and_execute_dmub_cmd(dc, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
67 
68 	/* Below loops 1000 x 500us = 500 ms.
69 	 *  Exit REPLAY may need to wait 1-2 frames to power up. Timeout after at
70 	 *  least a few frames. Should never hit the max retry assert below.
71 	 */
72 	if (wait) {
73 		for (retry_count = 0; retry_count <= 1000; retry_count++) {
74 			dmub_replay_get_state(dmub, &state, panel_inst);
75 
76 			if (enable) {
77 				if (state != REPLAY_STATE_0)
78 					break;
79 			} else {
80 				if (state == REPLAY_STATE_0)
81 					break;
82 			}
83 
84 			/* must *not* be fsleep - this can be called from high irq levels */
85 			udelay(500);
86 		}
87 
88 		/* assert if max retry hit */
89 		if (retry_count >= 1000)
90 			ASSERT(0);
91 	}
92 }
93 
94 /*
95  * Set REPLAY power optimization flags.
96  */
97 static void dmub_replay_set_power_opt(struct dmub_replay *dmub, unsigned int power_opt, uint8_t panel_inst)
98 {
99 	union dmub_rb_cmd cmd;
100 	struct dc_context *dc = dmub->ctx;
101 
102 	memset(&cmd, 0, sizeof(cmd));
103 	cmd.replay_set_power_opt.header.type = DMUB_CMD__REPLAY;
104 	cmd.replay_set_power_opt.header.sub_type = DMUB_CMD__SET_REPLAY_POWER_OPT;
105 	cmd.replay_set_power_opt.header.payload_bytes = sizeof(struct dmub_cmd_replay_set_power_opt_data);
106 	cmd.replay_set_power_opt.replay_set_power_opt_data.power_opt = power_opt;
107 	cmd.replay_set_power_opt.replay_set_power_opt_data.panel_inst = panel_inst;
108 
109 	dc_wake_and_execute_dmub_cmd(dc, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
110 }
111 
112 /*
113  * Setup Replay by programming phy registers and sending replay hw context values to firmware.
114  */
115 static bool dmub_replay_copy_settings(struct dmub_replay *dmub,
116 	struct dc_link *link,
117 	struct replay_context *replay_context,
118 	uint8_t panel_inst)
119 {
120 	union dmub_rb_cmd cmd;
121 	struct dc_context *dc = dmub->ctx;
122 	struct dmub_cmd_replay_copy_settings_data *copy_settings_data
123 		= &cmd.replay_copy_settings.replay_copy_settings_data;
124 	struct pipe_ctx *pipe_ctx = NULL;
125 	struct resource_context *res_ctx = &link->ctx->dc->current_state->res_ctx;
126 	int i = 0;
127 
128 	for (i = 0; i < MAX_PIPES; i++) {
129 		if (res_ctx &&
130 			res_ctx->pipe_ctx[i].stream &&
131 			res_ctx->pipe_ctx[i].stream->link &&
132 			res_ctx->pipe_ctx[i].stream->link == link &&
133 			res_ctx->pipe_ctx[i].stream->link->connector_signal == SIGNAL_TYPE_EDP) {
134 			pipe_ctx = &res_ctx->pipe_ctx[i];
135 			//TODO: refactor for multi edp support
136 			break;
137 		}
138 	}
139 
140 	if (!pipe_ctx)
141 		return false;
142 
143 	memset(&cmd, 0, sizeof(cmd));
144 	cmd.replay_copy_settings.header.type = DMUB_CMD__REPLAY;
145 	cmd.replay_copy_settings.header.sub_type = DMUB_CMD__REPLAY_COPY_SETTINGS;
146 	cmd.replay_copy_settings.header.payload_bytes = sizeof(struct dmub_cmd_replay_copy_settings_data);
147 
148 	// HW insts
149 	copy_settings_data->aux_inst				= replay_context->aux_inst;
150 	copy_settings_data->digbe_inst				= replay_context->digbe_inst;
151 	copy_settings_data->digfe_inst				= replay_context->digfe_inst;
152 
153 	if (pipe_ctx->plane_res.dpp)
154 		copy_settings_data->dpp_inst			= pipe_ctx->plane_res.dpp->inst;
155 	else
156 		copy_settings_data->dpp_inst			= 0;
157 	if (pipe_ctx->stream_res.tg)
158 		copy_settings_data->otg_inst			= pipe_ctx->stream_res.tg->inst;
159 	else
160 		copy_settings_data->otg_inst			= 0;
161 
162 	copy_settings_data->dpphy_inst				= link->link_enc->transmitter;
163 
164 	// Misc
165 	copy_settings_data->line_time_in_ns			= replay_context->line_time_in_ns;
166 	copy_settings_data->panel_inst				= panel_inst;
167 	copy_settings_data->debug.u32All			= link->replay_settings.config.debug_flags;
168 	copy_settings_data->pixel_deviation_per_line		= link->dpcd_caps.pr_info.pixel_deviation_per_line;
169 	copy_settings_data->max_deviation_line			= link->dpcd_caps.pr_info.max_deviation_line;
170 	copy_settings_data->smu_optimizations_en		= link->replay_settings.replay_smu_opt_enable;
171 	copy_settings_data->replay_timing_sync_supported = link->replay_settings.config.replay_timing_sync_supported;
172 	copy_settings_data->replay_support_fast_resync_in_ultra_sleep_mode = link->replay_settings.config.replay_support_fast_resync_in_ultra_sleep_mode;
173 
174 	copy_settings_data->debug.bitfields.enable_ips_visual_confirm = dc->dc->debug.enable_ips_visual_confirm;
175 
176 	copy_settings_data->flags.u32All = 0;
177 	copy_settings_data->flags.bitfields.fec_enable_status = (link->fec_state == dc_link_fec_enabled);
178 	copy_settings_data->flags.bitfields.dsc_enable_status = (pipe_ctx->stream->timing.flags.DSC == 1);
179 	// WA for PSRSU+DSC on specific TCON, if DSC is enabled, force PSRSU as ffu mode(full frame update)
180 	if (((link->dpcd_caps.fec_cap.bits.FEC_CAPABLE &&
181 		!link->dc->debug.disable_fec) &&
182 		(link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_SUPPORT &&
183 		!link->panel_config.dsc.disable_dsc_edp &&
184 		link->dc->caps.edp_dsc_support)) &&
185 		link->dpcd_caps.sink_dev_id == DP_DEVICE_ID_38EC11 &&
186 		(!memcmp(link->dpcd_caps.sink_dev_id_str, DP_SINK_DEVICE_STR_ID_1,
187 			sizeof(DP_SINK_DEVICE_STR_ID_1)) ||
188 		!memcmp(link->dpcd_caps.sink_dev_id_str, DP_SINK_DEVICE_STR_ID_2,
189 			sizeof(DP_SINK_DEVICE_STR_ID_2))))
190 		copy_settings_data->flags.bitfields.force_wakeup_by_tps3 = 1;
191 	else
192 		copy_settings_data->flags.bitfields.force_wakeup_by_tps3 = 0;
193 
194 	copy_settings_data->flags.bitfields.alpm_mode = (enum dmub_alpm_mode)link->replay_settings.config.alpm_mode;
195 	if (link->replay_settings.config.alpm_mode == DC_ALPM_AUXLESS) {
196 		copy_settings_data->auxless_alpm_data.lfps_setup_ns = dc->dc->debug.auxless_alpm_lfps_setup_ns;
197 		copy_settings_data->auxless_alpm_data.lfps_period_ns = dc->dc->debug.auxless_alpm_lfps_period_ns;
198 		copy_settings_data->auxless_alpm_data.lfps_silence_ns = dc->dc->debug.auxless_alpm_lfps_silence_ns;
199 		copy_settings_data->auxless_alpm_data.lfps_t1_t2_override_us =
200 			dc->dc->debug.auxless_alpm_lfps_t1t2_us;
201 		copy_settings_data->auxless_alpm_data.lfps_t1_t2_offset_us =
202 			dc->dc->debug.auxless_alpm_lfps_t1t2_offset_us;
203 		copy_settings_data->auxless_alpm_data.lttpr_count = link->dc->link_srv->dp_get_lttpr_count(link);
204 	}
205 
206 	dc_wake_and_execute_dmub_cmd(dc, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
207 
208 	return true;
209 }
210 
211 /*
212  * Set coasting vtotal.
213  */
214 static void dmub_replay_set_coasting_vtotal(struct dmub_replay *dmub,
215 		uint32_t coasting_vtotal,
216 		uint8_t panel_inst,
217 		uint16_t frame_skip_number)
218 {
219 	(void)panel_inst;
220 	union dmub_rb_cmd cmd;
221 	struct dc_context *dc = dmub->ctx;
222 	struct dmub_rb_cmd_replay_set_coasting_vtotal *pCmd = NULL;
223 
224 	pCmd = &(cmd.replay_set_coasting_vtotal);
225 
226 	memset(&cmd, 0, sizeof(cmd));
227 	pCmd->header.type = DMUB_CMD__REPLAY;
228 	pCmd->header.sub_type = DMUB_CMD__REPLAY_SET_COASTING_VTOTAL;
229 	pCmd->header.payload_bytes = sizeof(struct dmub_cmd_replay_set_coasting_vtotal_data);
230 	pCmd->replay_set_coasting_vtotal_data.coasting_vtotal = (coasting_vtotal & 0xFFFF);
231 	pCmd->replay_set_coasting_vtotal_data.coasting_vtotal_high = (coasting_vtotal & 0xFFFF0000) >> 16;
232 	pCmd->replay_set_coasting_vtotal_data.frame_skip_number = frame_skip_number;
233 
234 	dc_wake_and_execute_dmub_cmd(dc, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
235 }
236 
237 /*
238  * Get Replay residency from firmware.
239  */
240 static void dmub_replay_residency(struct dmub_replay *dmub, uint8_t panel_inst,
241 	uint32_t *residency, const bool is_start, enum pr_residency_mode mode)
242 {
243 	uint16_t param = (uint16_t)(panel_inst << 8);
244 	uint32_t i = 0;
245 
246 	switch (mode) {
247 	case PR_RESIDENCY_MODE_PHY:
248 		param |= REPLAY_RESIDENCY_FIELD_MODE_PHY;
249 		break;
250 	case PR_RESIDENCY_MODE_ALPM:
251 		param |= REPLAY_RESIDENCY_FIELD_MODE_ALPM;
252 		break;
253 	case PR_RESIDENCY_MODE_IPS2:
254 		param |= REPLAY_RESIDENCY_REVISION_1;
255 		param |= REPLAY_RESIDENCY_FIELD_MODE2_IPS;
256 		break;
257 	case PR_RESIDENCY_MODE_FRAME_CNT:
258 		param |= REPLAY_RESIDENCY_REVISION_1;
259 		param |= REPLAY_RESIDENCY_FIELD_MODE2_FRAME_CNT;
260 		break;
261 	case PR_RESIDENCY_MODE_ENABLEMENT_PERIOD:
262 		param |= REPLAY_RESIDENCY_REVISION_1;
263 		param |= REPLAY_RESIDENCY_FIELD_MODE2_EN_PERIOD;
264 		break;
265 	default:
266 		break;
267 	}
268 
269 	if (is_start)
270 		param |= REPLAY_RESIDENCY_ENABLE;
271 
272 	for (i = 0; i < GPINT_RETRY_NUM; i++) {
273 		// Send gpint command and wait for ack
274 		if (dc_wake_and_execute_gpint(dmub->ctx, DMUB_GPINT__REPLAY_RESIDENCY, param,
275 			residency, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY))
276 			return;
277 
278 		udelay(100);
279 	}
280 
281 	// it means gpint retry many times
282 	*residency = 0;
283 }
284 
285 /*
286  * Set REPLAY power optimization flags and coasting vtotal.
287  */
288 static void dmub_replay_set_power_opt_and_coasting_vtotal(struct dmub_replay *dmub,
289 		unsigned int power_opt, uint8_t panel_inst, uint32_t coasting_vtotal, uint16_t frame_skip_number)
290 {
291 	union dmub_rb_cmd cmd;
292 	struct dc_context *dc = dmub->ctx;
293 	struct dmub_rb_cmd_replay_set_power_opt_and_coasting_vtotal *pCmd = NULL;
294 
295 	pCmd = &(cmd.replay_set_power_opt_and_coasting_vtotal);
296 
297 	memset(&cmd, 0, sizeof(cmd));
298 	pCmd->header.type = DMUB_CMD__REPLAY;
299 	pCmd->header.sub_type = DMUB_CMD__REPLAY_SET_POWER_OPT_AND_COASTING_VTOTAL;
300 	pCmd->header.payload_bytes =
301 			sizeof(struct dmub_rb_cmd_replay_set_power_opt_and_coasting_vtotal) -
302 			sizeof(struct dmub_cmd_header);
303 	pCmd->replay_set_power_opt_data.power_opt = power_opt;
304 	pCmd->replay_set_power_opt_data.panel_inst = panel_inst;
305 	pCmd->replay_set_coasting_vtotal_data.coasting_vtotal = (coasting_vtotal & 0xFFFF);
306 	pCmd->replay_set_coasting_vtotal_data.coasting_vtotal_high = (coasting_vtotal & 0xFFFF0000) >> 16;
307 	pCmd->replay_set_coasting_vtotal_data.frame_skip_number = frame_skip_number;
308 
309 	dc_wake_and_execute_dmub_cmd(dc, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
310 }
311 
312 /*
313  * send Replay general cmd to DMUB.
314  */
315 static void dmub_replay_send_cmd(struct dmub_replay *dmub,
316 		enum replay_FW_Message_type msg, union dmub_replay_cmd_set *cmd_element)
317 {
318 	union dmub_rb_cmd cmd;
319 	struct dc_context *ctx = NULL;
320 
321 	if (dmub == NULL || cmd_element == NULL)
322 		return;
323 
324 	ctx = dmub->ctx;
325 	if (ctx != NULL) {
326 
327 		if (msg != Replay_Msg_Not_Support) {
328 			memset(&cmd, 0, sizeof(cmd));
329 			//Header
330 			cmd.replay_set_timing_sync.header.type = DMUB_CMD__REPLAY;
331 		} else
332 			return;
333 	} else
334 		return;
335 
336 	switch (msg) {
337 	case Replay_Set_Timing_Sync_Supported:
338 		//Header
339 		cmd.replay_set_timing_sync.header.sub_type =
340 			DMUB_CMD__REPLAY_SET_TIMING_SYNC_SUPPORTED;
341 		cmd.replay_set_timing_sync.header.payload_bytes =
342 			sizeof(struct dmub_rb_cmd_replay_set_timing_sync) -
343 			sizeof(struct dmub_cmd_header);
344 		//Cmd Body
345 		cmd.replay_set_timing_sync.replay_set_timing_sync_data.panel_inst =
346 						cmd_element->sync_data.panel_inst;
347 		cmd.replay_set_timing_sync.replay_set_timing_sync_data.timing_sync_supported =
348 						cmd_element->sync_data.timing_sync_supported;
349 		break;
350 	case Replay_Set_Residency_Frameupdate_Timer:
351 		//Header
352 		cmd.replay_set_frameupdate_timer.header.sub_type =
353 			DMUB_CMD__REPLAY_SET_RESIDENCY_FRAMEUPDATE_TIMER;
354 		cmd.replay_set_frameupdate_timer.header.payload_bytes =
355 			sizeof(struct dmub_rb_cmd_replay_set_frameupdate_timer) -
356 			sizeof(struct dmub_cmd_header);
357 		//Cmd Body
358 		cmd.replay_set_frameupdate_timer.data.panel_inst =
359 						cmd_element->panel_inst;
360 		cmd.replay_set_frameupdate_timer.data.enable =
361 						cmd_element->timer_data.enable;
362 		cmd.replay_set_frameupdate_timer.data.frameupdate_count =
363 						cmd_element->timer_data.frameupdate_count;
364 		break;
365 	case Replay_Set_Pseudo_VTotal:
366 		//Header
367 		cmd.replay_set_pseudo_vtotal.header.sub_type =
368 			DMUB_CMD__REPLAY_SET_PSEUDO_VTOTAL;
369 		cmd.replay_set_pseudo_vtotal.header.payload_bytes =
370 			sizeof(struct dmub_rb_cmd_replay_set_pseudo_vtotal) -
371 			sizeof(struct dmub_cmd_header);
372 		//Cmd Body
373 		cmd.replay_set_pseudo_vtotal.data.panel_inst =
374 			cmd_element->pseudo_vtotal_data.panel_inst;
375 		cmd.replay_set_pseudo_vtotal.data.vtotal =
376 			cmd_element->pseudo_vtotal_data.vtotal;
377 		break;
378 	case Replay_Disabled_Adaptive_Sync_SDP:
379 		//Header
380 		cmd.replay_disabled_adaptive_sync_sdp.header.sub_type =
381 			DMUB_CMD__REPLAY_DISABLED_ADAPTIVE_SYNC_SDP;
382 		cmd.replay_disabled_adaptive_sync_sdp.header.payload_bytes =
383 			sizeof(struct dmub_rb_cmd_replay_disabled_adaptive_sync_sdp) -
384 			sizeof(struct dmub_cmd_header);
385 		//Cmd Body
386 		cmd.replay_disabled_adaptive_sync_sdp.data.panel_inst =
387 			cmd_element->disabled_adaptive_sync_sdp_data.panel_inst;
388 		cmd.replay_disabled_adaptive_sync_sdp.data.force_disabled =
389 			cmd_element->disabled_adaptive_sync_sdp_data.force_disabled;
390 		break;
391 	case Replay_Set_General_Cmd:
392 		//Header
393 		cmd.replay_set_general_cmd.header.sub_type =
394 			DMUB_CMD__REPLAY_SET_GENERAL_CMD;
395 		cmd.replay_set_general_cmd.header.payload_bytes =
396 			sizeof(struct dmub_rb_cmd_replay_set_general_cmd) -
397 			sizeof(struct dmub_cmd_header);
398 		//Cmd Body
399 		cmd.replay_set_general_cmd.data.panel_inst =
400 			cmd_element->set_general_cmd_data.panel_inst;
401 		cmd.replay_set_general_cmd.data.subtype =
402 			cmd_element->set_general_cmd_data.subtype;
403 		cmd.replay_set_general_cmd.data.param1 =
404 			cmd_element->set_general_cmd_data.param1;
405 		cmd.replay_set_general_cmd.data.param2 =
406 			cmd_element->set_general_cmd_data.param2;
407 		break;
408 	case Replay_Msg_Not_Support:
409 	default:
410 		return;
411 		break;
412 	}
413 
414 	dc_wake_and_execute_dmub_cmd(ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
415 }
416 
417 static const struct dmub_replay_funcs replay_funcs = {
418 	.replay_copy_settings				= dmub_replay_copy_settings,
419 	.replay_enable					= dmub_replay_enable,
420 	.replay_get_state				= dmub_replay_get_state,
421 	.replay_set_power_opt				= dmub_replay_set_power_opt,
422 	.replay_set_coasting_vtotal			= dmub_replay_set_coasting_vtotal,
423 	.replay_residency				= dmub_replay_residency,
424 	.replay_set_power_opt_and_coasting_vtotal	= dmub_replay_set_power_opt_and_coasting_vtotal,
425 	.replay_send_cmd				= dmub_replay_send_cmd,
426 };
427 
428 /*
429  * Construct Replay object.
430  */
431 static void dmub_replay_construct(struct dmub_replay *replay, struct dc_context *ctx)
432 {
433 	replay->ctx = ctx;
434 	replay->funcs = &replay_funcs;
435 }
436 
437 /*
438  * Allocate and initialize Replay object.
439  */
440 struct dmub_replay *dmub_replay_create(struct dc_context *ctx)
441 {
442 	struct dmub_replay *replay = kzalloc_obj(struct dmub_replay);
443 
444 	if (replay == NULL) {
445 		BREAK_TO_DEBUGGER();
446 		return NULL;
447 	}
448 
449 	dmub_replay_construct(replay, ctx);
450 
451 	return replay;
452 }
453 
454 /*
455  * Deallocate Replay object.
456  */
457 void dmub_replay_destroy(struct dmub_replay **dmub)
458 {
459 	kfree(*dmub);
460 	*dmub = NULL;
461 }
462