1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include "dc.h"
27 #include "dc_dmub_srv.h"
28 #include "../dmub/dmub_srv.h"
29 #include "dm_helpers.h"
30 #include "dc_hw_types.h"
31 #include "core_types.h"
32 
33 #define CTX dc_dmub_srv->ctx
34 #define DC_LOGGER CTX->logger
35 
36 static void dc_dmub_srv_construct(struct dc_dmub_srv *dc_srv, struct dc *dc,
37 				  struct dmub_srv *dmub)
38 {
39 	dc_srv->dmub = dmub;
40 	dc_srv->ctx = dc->ctx;
41 }
42 
43 struct dc_dmub_srv *dc_dmub_srv_create(struct dc *dc, struct dmub_srv *dmub)
44 {
45 	struct dc_dmub_srv *dc_srv =
46 		kzalloc(sizeof(struct dc_dmub_srv), GFP_KERNEL);
47 
48 	if (dc_srv == NULL) {
49 		BREAK_TO_DEBUGGER();
50 		return NULL;
51 	}
52 
53 	dc_dmub_srv_construct(dc_srv, dc, dmub);
54 
55 	return dc_srv;
56 }
57 
58 void dc_dmub_srv_destroy(struct dc_dmub_srv **dmub_srv)
59 {
60 	if (*dmub_srv) {
61 		kfree(*dmub_srv);
62 		*dmub_srv = NULL;
63 	}
64 }
65 
66 void dc_dmub_srv_cmd_queue(struct dc_dmub_srv *dc_dmub_srv,
67 			   union dmub_rb_cmd *cmd)
68 {
69 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
70 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
71 	enum dmub_status status;
72 
73 	status = dmub_srv_cmd_queue(dmub, cmd);
74 	if (status == DMUB_STATUS_OK)
75 		return;
76 
77 	if (status != DMUB_STATUS_QUEUE_FULL)
78 		goto error;
79 
80 	/* Execute and wait for queue to become empty again. */
81 	dc_dmub_srv_cmd_execute(dc_dmub_srv);
82 	dc_dmub_srv_wait_idle(dc_dmub_srv);
83 
84 	/* Requeue the command. */
85 	status = dmub_srv_cmd_queue(dmub, cmd);
86 	if (status == DMUB_STATUS_OK)
87 		return;
88 
89 error:
90 	DC_ERROR("Error queuing DMUB command: status=%d\n", status);
91 	dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
92 }
93 
94 void dc_dmub_srv_cmd_execute(struct dc_dmub_srv *dc_dmub_srv)
95 {
96 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
97 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
98 	enum dmub_status status;
99 
100 	status = dmub_srv_cmd_execute(dmub);
101 	if (status != DMUB_STATUS_OK) {
102 		DC_ERROR("Error starting DMUB execution: status=%d\n", status);
103 		dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
104 	}
105 }
106 
107 void dc_dmub_srv_wait_idle(struct dc_dmub_srv *dc_dmub_srv)
108 {
109 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
110 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
111 	enum dmub_status status;
112 
113 	status = dmub_srv_wait_for_idle(dmub, 100000);
114 	if (status != DMUB_STATUS_OK) {
115 		DC_ERROR("Error waiting for DMUB idle: status=%d\n", status);
116 		dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
117 	}
118 }
119 
120 void dc_dmub_srv_clear_inbox0_ack(struct dc_dmub_srv *dmub_srv)
121 {
122 	struct dmub_srv *dmub = dmub_srv->dmub;
123 	struct dc_context *dc_ctx = dmub_srv->ctx;
124 	enum dmub_status status = DMUB_STATUS_OK;
125 
126 	status = dmub_srv_clear_inbox0_ack(dmub);
127 	if (status != DMUB_STATUS_OK) {
128 		DC_ERROR("Error clearing INBOX0 ack: status=%d\n", status);
129 		dc_dmub_srv_log_diagnostic_data(dmub_srv);
130 	}
131 }
132 
133 void dc_dmub_srv_wait_for_inbox0_ack(struct dc_dmub_srv *dmub_srv)
134 {
135 	struct dmub_srv *dmub = dmub_srv->dmub;
136 	struct dc_context *dc_ctx = dmub_srv->ctx;
137 	enum dmub_status status = DMUB_STATUS_OK;
138 
139 	status = dmub_srv_wait_for_inbox0_ack(dmub, 100000);
140 	if (status != DMUB_STATUS_OK) {
141 		DC_ERROR("Error waiting for INBOX0 HW Lock Ack\n");
142 		dc_dmub_srv_log_diagnostic_data(dmub_srv);
143 	}
144 }
145 
146 void dc_dmub_srv_send_inbox0_cmd(struct dc_dmub_srv *dmub_srv,
147 		union dmub_inbox0_data_register data)
148 {
149 	struct dmub_srv *dmub = dmub_srv->dmub;
150 	struct dc_context *dc_ctx = dmub_srv->ctx;
151 	enum dmub_status status = DMUB_STATUS_OK;
152 
153 	status = dmub_srv_send_inbox0_cmd(dmub, data);
154 	if (status != DMUB_STATUS_OK) {
155 		DC_ERROR("Error sending INBOX0 cmd\n");
156 		dc_dmub_srv_log_diagnostic_data(dmub_srv);
157 	}
158 }
159 
160 bool dc_dmub_srv_cmd_with_reply_data(struct dc_dmub_srv *dc_dmub_srv, union dmub_rb_cmd *cmd)
161 {
162 	struct dmub_srv *dmub;
163 	enum dmub_status status;
164 
165 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
166 		return false;
167 
168 	dmub = dc_dmub_srv->dmub;
169 
170 	status = dmub_srv_cmd_with_reply_data(dmub, cmd);
171 	if (status != DMUB_STATUS_OK) {
172 		DC_LOG_DEBUG("No reply for DMUB command: status=%d\n", status);
173 		return false;
174 	}
175 
176 	return true;
177 }
178 
179 void dc_dmub_srv_wait_phy_init(struct dc_dmub_srv *dc_dmub_srv)
180 {
181 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
182 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
183 	enum dmub_status status;
184 
185 	for (;;) {
186 		/* Wait up to a second for PHY init. */
187 		status = dmub_srv_wait_for_phy_init(dmub, 1000000);
188 		if (status == DMUB_STATUS_OK)
189 			/* Initialization OK */
190 			break;
191 
192 		DC_ERROR("DMCUB PHY init failed: status=%d\n", status);
193 		ASSERT(0);
194 
195 		if (status != DMUB_STATUS_TIMEOUT)
196 			/*
197 			 * Server likely initialized or we don't have
198 			 * DMCUB HW support - this won't end.
199 			 */
200 			break;
201 
202 		/* Continue spinning so we don't hang the ASIC. */
203 	}
204 }
205 
206 bool dc_dmub_srv_notify_stream_mask(struct dc_dmub_srv *dc_dmub_srv,
207 				    unsigned int stream_mask)
208 {
209 	struct dmub_srv *dmub;
210 	const uint32_t timeout = 30;
211 
212 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
213 		return false;
214 
215 	dmub = dc_dmub_srv->dmub;
216 
217 	return dmub_srv_send_gpint_command(
218 		       dmub, DMUB_GPINT__IDLE_OPT_NOTIFY_STREAM_MASK,
219 		       stream_mask, timeout) == DMUB_STATUS_OK;
220 }
221 
222 bool dc_dmub_srv_is_restore_required(struct dc_dmub_srv *dc_dmub_srv)
223 {
224 	struct dmub_srv *dmub;
225 	struct dc_context *dc_ctx;
226 	union dmub_fw_boot_status boot_status;
227 	enum dmub_status status;
228 
229 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
230 		return false;
231 
232 	dmub = dc_dmub_srv->dmub;
233 	dc_ctx = dc_dmub_srv->ctx;
234 
235 	status = dmub_srv_get_fw_boot_status(dmub, &boot_status);
236 	if (status != DMUB_STATUS_OK) {
237 		DC_ERROR("Error querying DMUB boot status: error=%d\n", status);
238 		return false;
239 	}
240 
241 	return boot_status.bits.restore_required;
242 }
243 
244 bool dc_dmub_srv_get_dmub_outbox0_msg(const struct dc *dc, struct dmcub_trace_buf_entry *entry)
245 {
246 	struct dmub_srv *dmub = dc->ctx->dmub_srv->dmub;
247 	return dmub_srv_get_outbox0_msg(dmub, entry);
248 }
249 
250 void dc_dmub_trace_event_control(struct dc *dc, bool enable)
251 {
252 	dm_helpers_dmub_outbox_interrupt_control(dc->ctx, enable);
253 }
254 
255 void dc_dmub_srv_drr_update_cmd(struct dc *dc, uint32_t tg_inst, uint32_t vtotal_min, uint32_t vtotal_max)
256 {
257 	union dmub_rb_cmd cmd = { 0 };
258 
259 	cmd.drr_update.header.type = DMUB_CMD__FW_ASSISTED_MCLK_SWITCH;
260 	cmd.drr_update.header.sub_type = DMUB_CMD__FAMS_DRR_UPDATE;
261 	cmd.drr_update.dmub_optc_state_req.v_total_max = vtotal_max;
262 	cmd.drr_update.dmub_optc_state_req.v_total_min = vtotal_min;
263 	cmd.drr_update.dmub_optc_state_req.tg_inst = tg_inst;
264 
265 	cmd.drr_update.header.payload_bytes = sizeof(cmd.drr_update) - sizeof(cmd.drr_update.header);
266 
267 	// Send the command to the DMCUB.
268 	dc_dmub_srv_cmd_queue(dc->ctx->dmub_srv, &cmd);
269 	dc_dmub_srv_cmd_execute(dc->ctx->dmub_srv);
270 	dc_dmub_srv_wait_idle(dc->ctx->dmub_srv);
271 }
272 
273 uint8_t dc_dmub_srv_get_pipes_for_stream(struct dc *dc, struct dc_stream_state *stream)
274 {
275 	uint8_t pipes = 0;
276 	int i = 0;
277 
278 	for (i = 0; i < MAX_PIPES; i++) {
279 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
280 
281 		if (pipe->stream == stream && pipe->stream_res.tg)
282 			pipes = i;
283 	}
284 	return pipes;
285 }
286 
287 int dc_dmub_srv_get_timing_generator_offset(struct dc *dc, struct dc_stream_state *stream)
288 {
289 	int  tg_inst = 0;
290 	int i = 0;
291 
292 	for (i = 0; i < MAX_PIPES; i++) {
293 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
294 
295 		if (pipe->stream == stream && pipe->stream_res.tg) {
296 			tg_inst = pipe->stream_res.tg->inst;
297 			break;
298 		}
299 	}
300 	return tg_inst;
301 }
302 
303 bool dc_dmub_srv_p_state_delegate(struct dc *dc, bool should_manage_pstate, struct dc_state *context)
304 {
305 	union dmub_rb_cmd cmd = { 0 };
306 	struct dmub_cmd_fw_assisted_mclk_switch_config *config_data = &cmd.fw_assisted_mclk_switch.config_data;
307 	int i = 0;
308 	int ramp_up_num_steps = 1; // TODO: Ramp is currently disabled. Reenable it.
309 	uint8_t visual_confirm_enabled = dc->debug.visual_confirm == VISUAL_CONFIRM_FAMS;
310 
311 	if (dc == NULL)
312 		return false;
313 
314 	// Format command.
315 	cmd.fw_assisted_mclk_switch.header.type = DMUB_CMD__FW_ASSISTED_MCLK_SWITCH;
316 	cmd.fw_assisted_mclk_switch.header.sub_type = DMUB_CMD__FAMS_SETUP_FW_CTRL;
317 	cmd.fw_assisted_mclk_switch.config_data.fams_enabled = should_manage_pstate;
318 	cmd.fw_assisted_mclk_switch.config_data.visual_confirm_enabled = visual_confirm_enabled;
319 
320 	for (i = 0; context && i < context->stream_count; i++) {
321 		struct dc_stream_state *stream = context->streams[i];
322 		uint8_t min_refresh_in_hz = (stream->timing.min_refresh_in_uhz + 999999) / 1000000;
323 		int  tg_inst = dc_dmub_srv_get_timing_generator_offset(dc, stream);
324 
325 		config_data->pipe_data[tg_inst].pix_clk_100hz = stream->timing.pix_clk_100hz;
326 		config_data->pipe_data[tg_inst].min_refresh_in_hz = min_refresh_in_hz;
327 		config_data->pipe_data[tg_inst].max_ramp_step = ramp_up_num_steps;
328 		config_data->pipe_data[tg_inst].pipes = dc_dmub_srv_get_pipes_for_stream(dc, stream);
329 	}
330 
331 	cmd.fw_assisted_mclk_switch.header.payload_bytes =
332 		sizeof(cmd.fw_assisted_mclk_switch) - sizeof(cmd.fw_assisted_mclk_switch.header);
333 
334 	// Send the command to the DMCUB.
335 	dc_dmub_srv_cmd_queue(dc->ctx->dmub_srv, &cmd);
336 	dc_dmub_srv_cmd_execute(dc->ctx->dmub_srv);
337 	dc_dmub_srv_wait_idle(dc->ctx->dmub_srv);
338 
339 	return true;
340 }
341 
342 void dc_dmub_srv_query_caps_cmd(struct dmub_srv *dmub)
343 {
344 	union dmub_rb_cmd cmd = { 0 };
345 	enum dmub_status status;
346 
347 	if (!dmub) {
348 		return;
349 	}
350 
351 	memset(&cmd, 0, sizeof(cmd));
352 
353 	/* Prepare fw command */
354 	cmd.query_feature_caps.header.type = DMUB_CMD__QUERY_FEATURE_CAPS;
355 	cmd.query_feature_caps.header.sub_type = 0;
356 	cmd.query_feature_caps.header.ret_status = 1;
357 	cmd.query_feature_caps.header.payload_bytes = sizeof(struct dmub_cmd_query_feature_caps_data);
358 
359 	/* Send command to fw */
360 	status = dmub_srv_cmd_with_reply_data(dmub, &cmd);
361 
362 	ASSERT(status == DMUB_STATUS_OK);
363 
364 	/* If command was processed, copy feature caps to dmub srv */
365 	if (status == DMUB_STATUS_OK &&
366 	    cmd.query_feature_caps.header.ret_status == 0) {
367 		memcpy(&dmub->feature_caps,
368 		       &cmd.query_feature_caps.query_feature_caps_data,
369 		       sizeof(struct dmub_feature_caps));
370 	}
371 }
372 
373 /**
374  * ***********************************************************************************************
375  * populate_subvp_cmd_drr_info: Helper to populate DRR pipe info for the DMCUB subvp command
376  *
377  * Populate the DMCUB SubVP command with DRR pipe info. All the information required for calculating
378  * the SubVP + DRR microschedule is populated here.
379  *
380  * High level algorithm:
381  * 1. Get timing for SubVP pipe, phantom pipe, and DRR pipe
382  * 2. Calculate the min and max vtotal which supports SubVP + DRR microschedule
383  * 3. Populate the drr_info with the min and max supported vtotal values
384  *
385  * @param [in] dc: current dc state
386  * @param [in] subvp_pipe: pipe_ctx for the SubVP pipe
387  * @param [in] vblank_pipe: pipe_ctx for the DRR pipe
388  * @param [in] pipe_data: Pipe data which stores the VBLANK/DRR info
389  *
390  * @return: void
391  *
392  * ***********************************************************************************************
393  */
394 static void populate_subvp_cmd_drr_info(struct dc *dc,
395 		struct pipe_ctx *subvp_pipe,
396 		struct pipe_ctx *vblank_pipe,
397 		struct dmub_cmd_fw_assisted_mclk_switch_pipe_data_v2 *pipe_data)
398 {
399 	struct dc_crtc_timing *main_timing = &subvp_pipe->stream->timing;
400 	struct dc_crtc_timing *phantom_timing = &subvp_pipe->stream->mall_stream_config.paired_stream->timing;
401 	struct dc_crtc_timing *drr_timing = &vblank_pipe->stream->timing;
402 	int16_t drr_frame_us = 0;
403 	int16_t min_drr_supported_us = 0;
404 	int16_t max_drr_supported_us = 0;
405 	int16_t max_drr_vblank_us = 0;
406 	int16_t max_drr_mallregion_us = 0;
407 	int16_t mall_region_us = 0;
408 	int16_t prefetch_us = 0;
409 	int16_t subvp_active_us = 0;
410 	int16_t drr_active_us = 0;
411 	int16_t min_vtotal_supported = 0;
412 	int16_t max_vtotal_supported = 0;
413 
414 	pipe_data->pipe_config.vblank_data.drr_info.drr_in_use = true;
415 	pipe_data->pipe_config.vblank_data.drr_info.use_ramping = false; // for now don't use ramping
416 	pipe_data->pipe_config.vblank_data.drr_info.drr_window_size_ms = 4; // hardcode 4ms DRR window for now
417 
418 	drr_frame_us = drr_timing->v_total * drr_timing->h_total /
419 			(double)(drr_timing->pix_clk_100hz * 100) * 1000000;
420 	// P-State allow width and FW delays already included phantom_timing->v_addressable
421 	mall_region_us = phantom_timing->v_addressable * phantom_timing->h_total /
422 			(double)(phantom_timing->pix_clk_100hz * 100) * 1000000;
423 	min_drr_supported_us = drr_frame_us + mall_region_us + SUBVP_DRR_MARGIN_US;
424 	min_vtotal_supported = drr_timing->pix_clk_100hz * 100 * ((double)min_drr_supported_us / 1000000) /
425 			(double)drr_timing->h_total;
426 
427 	prefetch_us = (phantom_timing->v_total - phantom_timing->v_front_porch) * phantom_timing->h_total /
428 			(double)(phantom_timing->pix_clk_100hz * 100) * 1000000 +
429 			dc->caps.subvp_prefetch_end_to_mall_start_us;
430 	subvp_active_us = main_timing->v_addressable * main_timing->h_total /
431 			(double)(main_timing->pix_clk_100hz * 100) * 1000000;
432 	drr_active_us = drr_timing->v_addressable * drr_timing->h_total /
433 			(double)(drr_timing->pix_clk_100hz * 100) * 1000000;
434 	max_drr_vblank_us = (double)(subvp_active_us - prefetch_us - drr_active_us) / 2 + drr_active_us;
435 	max_drr_mallregion_us = subvp_active_us - prefetch_us - mall_region_us;
436 	max_drr_supported_us = max_drr_vblank_us > max_drr_mallregion_us ? max_drr_vblank_us : max_drr_mallregion_us;
437 	max_vtotal_supported = drr_timing->pix_clk_100hz * 100 * ((double)max_drr_supported_us / 1000000) /
438 			(double)drr_timing->h_total;
439 
440 	pipe_data->pipe_config.vblank_data.drr_info.min_vtotal_supported = min_vtotal_supported;
441 	pipe_data->pipe_config.vblank_data.drr_info.max_vtotal_supported = max_vtotal_supported;
442 }
443 
444 /**
445  * ***********************************************************************************************
446  * populate_subvp_cmd_vblank_pipe_info: Helper to populate VBLANK pipe info for the DMUB subvp command
447  *
448  * Populate the DMCUB SubVP command with VBLANK pipe info. All the information required to calculate
449  * the microschedule for SubVP + VBLANK case is stored in the pipe_data (subvp_data and vblank_data).
450  * Also check if the VBLANK pipe is a DRR display -- if it is make a call to populate drr_info.
451  *
452  * @param [in] dc: current dc state
453  * @param [in] context: new dc state
454  * @param [in] cmd: DMUB cmd to be populated with SubVP info
455  * @param [in] vblank_pipe: pipe_ctx for the VBLANK pipe
456  * @param [in] cmd_pipe_index: index for the pipe array in DMCUB SubVP cmd
457  *
458  * @return: void
459  *
460  * ***********************************************************************************************
461  */
462 static void populate_subvp_cmd_vblank_pipe_info(struct dc *dc,
463 		struct dc_state *context,
464 		union dmub_rb_cmd *cmd,
465 		struct pipe_ctx *vblank_pipe,
466 		uint8_t cmd_pipe_index)
467 {
468 	uint32_t i;
469 	struct pipe_ctx *pipe = NULL;
470 	struct dmub_cmd_fw_assisted_mclk_switch_pipe_data_v2 *pipe_data =
471 			&cmd->fw_assisted_mclk_switch_v2.config_data.pipe_data[cmd_pipe_index];
472 
473 	// Find the SubVP pipe
474 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
475 		pipe = &context->res_ctx.pipe_ctx[i];
476 
477 		// We check for master pipe, but it shouldn't matter since we only need
478 		// the pipe for timing info (stream should be same for any pipe splits)
479 		if (!pipe->stream || !pipe->plane_state || pipe->top_pipe || pipe->prev_odm_pipe)
480 			continue;
481 
482 		// Find the SubVP pipe
483 		if (pipe->stream->mall_stream_config.type == SUBVP_MAIN)
484 			break;
485 	}
486 
487 	pipe_data->mode = VBLANK;
488 	pipe_data->pipe_config.vblank_data.pix_clk_100hz = vblank_pipe->stream->timing.pix_clk_100hz;
489 	pipe_data->pipe_config.vblank_data.vblank_start = vblank_pipe->stream->timing.v_total -
490 							vblank_pipe->stream->timing.v_front_porch;
491 	pipe_data->pipe_config.vblank_data.vtotal = vblank_pipe->stream->timing.v_total;
492 	pipe_data->pipe_config.vblank_data.htotal = vblank_pipe->stream->timing.h_total;
493 	pipe_data->pipe_config.vblank_data.vblank_pipe_index = vblank_pipe->pipe_idx;
494 	pipe_data->pipe_config.vblank_data.vstartup_start = vblank_pipe->pipe_dlg_param.vstartup_start;
495 	pipe_data->pipe_config.vblank_data.vblank_end =
496 			vblank_pipe->stream->timing.v_total - vblank_pipe->stream->timing.v_front_porch - vblank_pipe->stream->timing.v_addressable;
497 
498 	if (vblank_pipe->stream->ignore_msa_timing_param)
499 		populate_subvp_cmd_drr_info(dc, pipe, vblank_pipe, pipe_data);
500 }
501 
502 /**
503  * ***********************************************************************************************
504  * update_subvp_prefetch_end_to_mall_start: Helper for SubVP + SubVP case
505  *
506  * For SubVP + SubVP, we use a single vertical interrupt to start the microschedule for both
507  * SubVP pipes. In order for this to work correctly, the MALL REGION of both SubVP pipes must
508  * start at the same time. This function lengthens the prefetch end to mall start delay of the
509  * SubVP pipe that has the shorter prefetch so that both MALL REGION's will start at the same time.
510  *
511  * @param [in] dc: current dc state
512  * @param [in] context: new dc state
513  * @param [in] cmd: DMUB cmd to be populated with SubVP info
514  * @param [in] subvp_pipes: Array of SubVP pipes (should always be length 2)
515  *
516  * @return: void
517  *
518  * ***********************************************************************************************
519  */
520 static void update_subvp_prefetch_end_to_mall_start(struct dc *dc,
521 		struct dc_state *context,
522 		union dmub_rb_cmd *cmd,
523 		struct pipe_ctx *subvp_pipes[])
524 {
525 	uint32_t subvp0_prefetch_us = 0;
526 	uint32_t subvp1_prefetch_us = 0;
527 	uint32_t prefetch_delta_us = 0;
528 	struct dc_crtc_timing *phantom_timing0 = &subvp_pipes[0]->stream->mall_stream_config.paired_stream->timing;
529 	struct dc_crtc_timing *phantom_timing1 = &subvp_pipes[1]->stream->mall_stream_config.paired_stream->timing;
530 	struct dmub_cmd_fw_assisted_mclk_switch_pipe_data_v2 *pipe_data = NULL;
531 
532 	subvp0_prefetch_us = (phantom_timing0->v_total - phantom_timing0->v_front_porch) * phantom_timing0->h_total /
533 				(double)(phantom_timing0->pix_clk_100hz * 100) * 1000000 + dc->caps.subvp_prefetch_end_to_mall_start_us;
534 	subvp1_prefetch_us = (phantom_timing1->v_total - phantom_timing1->v_front_porch) * phantom_timing1->h_total /
535 					(double)(phantom_timing1->pix_clk_100hz * 100) * 1000000 + dc->caps.subvp_prefetch_end_to_mall_start_us;
536 
537 	// Whichever SubVP PIPE has the smaller prefetch (including the prefetch end to mall start time)
538 	// should increase it's prefetch time to match the other
539 	if (subvp0_prefetch_us > subvp1_prefetch_us) {
540 		pipe_data = &cmd->fw_assisted_mclk_switch_v2.config_data.pipe_data[1];
541 		prefetch_delta_us = subvp0_prefetch_us - subvp1_prefetch_us;
542 		pipe_data->pipe_config.subvp_data.prefetch_to_mall_start_lines =
543 					(((double)(dc->caps.subvp_prefetch_end_to_mall_start_us + prefetch_delta_us) / 1000000) *
544 					(phantom_timing1->pix_clk_100hz * 100) + phantom_timing1->h_total - 1) /
545 					(double)phantom_timing1->h_total;
546 	} else if (subvp1_prefetch_us >  subvp0_prefetch_us) {
547 		pipe_data = &cmd->fw_assisted_mclk_switch_v2.config_data.pipe_data[0];
548 		prefetch_delta_us = subvp1_prefetch_us - subvp0_prefetch_us;
549 		pipe_data->pipe_config.subvp_data.prefetch_to_mall_start_lines =
550 					(((double)(dc->caps.subvp_prefetch_end_to_mall_start_us + prefetch_delta_us) / 1000000) *
551 					(phantom_timing0->pix_clk_100hz * 100) + phantom_timing0->h_total - 1) /
552 					(double)phantom_timing0->h_total;
553 	}
554 }
555 
556 /**
557  * ***************************************************************************************
558  * setup_subvp_dmub_command: Helper to populate the SubVP pipe info for the DMUB subvp command
559  *
560  * Populate the DMCUB SubVP command with SubVP pipe info. All the information required to
561  * calculate the microschedule for the SubVP pipe is stored in the pipe_data of the DMCUB
562  * SubVP command.
563  *
564  * @param [in] dc: current dc state
565  * @param [in] context: new dc state
566  * @param [in] cmd: DMUB cmd to be populated with SubVP info
567  * @param [in] subvp_pipe: pipe_ctx for the SubVP pipe
568  * @param [in] cmd_pipe_index: index for the pipe array in DMCUB SubVP cmd
569  *
570  * @return: void
571  *
572  * ***************************************************************************************
573  */
574 static void populate_subvp_cmd_pipe_info(struct dc *dc,
575 		struct dc_state *context,
576 		union dmub_rb_cmd *cmd,
577 		struct pipe_ctx *subvp_pipe,
578 		uint8_t cmd_pipe_index)
579 {
580 	uint32_t j;
581 	struct dmub_cmd_fw_assisted_mclk_switch_pipe_data_v2 *pipe_data =
582 			&cmd->fw_assisted_mclk_switch_v2.config_data.pipe_data[cmd_pipe_index];
583 	struct dc_crtc_timing *main_timing = &subvp_pipe->stream->timing;
584 	struct dc_crtc_timing *phantom_timing = &subvp_pipe->stream->mall_stream_config.paired_stream->timing;
585 
586 	pipe_data->mode = SUBVP;
587 	pipe_data->pipe_config.subvp_data.pix_clk_100hz = subvp_pipe->stream->timing.pix_clk_100hz;
588 	pipe_data->pipe_config.subvp_data.htotal = subvp_pipe->stream->timing.h_total;
589 	pipe_data->pipe_config.subvp_data.vtotal = subvp_pipe->stream->timing.v_total;
590 	pipe_data->pipe_config.subvp_data.main_vblank_start =
591 			main_timing->v_total - main_timing->v_front_porch;
592 	pipe_data->pipe_config.subvp_data.main_vblank_end =
593 			main_timing->v_total - main_timing->v_front_porch - main_timing->v_addressable;
594 	pipe_data->pipe_config.subvp_data.mall_region_lines = phantom_timing->v_addressable;
595 	pipe_data->pipe_config.subvp_data.main_pipe_index = subvp_pipe->pipe_idx;
596 
597 	// Prefetch lines is equal to VACTIVE + BP + VSYNC
598 	pipe_data->pipe_config.subvp_data.prefetch_lines =
599 			phantom_timing->v_total - phantom_timing->v_front_porch;
600 
601 	// Round up
602 	pipe_data->pipe_config.subvp_data.prefetch_to_mall_start_lines =
603 			(((double)dc->caps.subvp_prefetch_end_to_mall_start_us / 1000000) *
604 			(phantom_timing->pix_clk_100hz * 100) + phantom_timing->h_total - 1) /
605 			(double)phantom_timing->h_total;
606 	pipe_data->pipe_config.subvp_data.processing_delay_lines =
607 			(((double)dc->caps.subvp_fw_processing_delay_us / 1000000) *
608 			(phantom_timing->pix_clk_100hz * 100) + phantom_timing->h_total - 1) /
609 			(double)phantom_timing->h_total;
610 	// Find phantom pipe index based on phantom stream
611 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
612 		struct pipe_ctx *phantom_pipe = &context->res_ctx.pipe_ctx[j];
613 
614 		if (phantom_pipe->stream == subvp_pipe->stream->mall_stream_config.paired_stream) {
615 			pipe_data->pipe_config.subvp_data.phantom_pipe_index = phantom_pipe->pipe_idx;
616 			break;
617 		}
618 	}
619 }
620 
621 /**
622  * ***************************************************************************************
623  * dc_dmub_setup_subvp_dmub_command: Populate the DMCUB SubVP command
624  *
625  * This function loops through each pipe and populates the DMUB
626  * SubVP CMD info based on the pipe (e.g. SubVP, VBLANK).
627  *
628  * @param [in] dc: current dc state
629  * @param [in] context: new dc state
630  * @param [in] cmd: DMUB cmd to be populated with SubVP info
631  *
632  * @return: void
633  *
634  * ***************************************************************************************
635  */
636 void dc_dmub_setup_subvp_dmub_command(struct dc *dc,
637 		struct dc_state *context,
638 		bool enable)
639 {
640 	uint8_t cmd_pipe_index = 0;
641 	uint32_t i, pipe_idx;
642 	uint8_t subvp_count = 0;
643 	union dmub_rb_cmd cmd;
644 	struct pipe_ctx *subvp_pipes[2];
645 	uint32_t wm_val_refclk = 0;
646 
647 	memset(&cmd, 0, sizeof(cmd));
648 	// FW command for SUBVP
649 	cmd.fw_assisted_mclk_switch_v2.header.type = DMUB_CMD__FW_ASSISTED_MCLK_SWITCH;
650 	cmd.fw_assisted_mclk_switch_v2.header.sub_type = DMUB_CMD__HANDLE_SUBVP_CMD;
651 	cmd.fw_assisted_mclk_switch_v2.header.payload_bytes =
652 			sizeof(cmd.fw_assisted_mclk_switch_v2) - sizeof(cmd.fw_assisted_mclk_switch_v2.header);
653 
654 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
655 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
656 
657 		if (!pipe->stream)
658 			continue;
659 
660 		if (pipe->plane_state && !pipe->top_pipe &&
661 				pipe->stream->mall_stream_config.type == SUBVP_MAIN)
662 			subvp_pipes[subvp_count++] = pipe;
663 	}
664 
665 	if (enable) {
666 		// For each pipe that is a "main" SUBVP pipe, fill in pipe data for DMUB SUBVP cmd
667 		for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
668 			struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
669 
670 			if (!pipe->stream)
671 				continue;
672 
673 			if (pipe->plane_state && pipe->stream->mall_stream_config.paired_stream &&
674 					pipe->stream->mall_stream_config.type == SUBVP_MAIN) {
675 				populate_subvp_cmd_pipe_info(dc, context, &cmd, pipe, cmd_pipe_index++);
676 			} else if (pipe->plane_state && pipe->stream->mall_stream_config.type == SUBVP_NONE) {
677 				// Don't need to check for ActiveDRAMClockChangeMargin < 0, not valid in cases where
678 				// we run through DML without calculating "natural" P-state support
679 				populate_subvp_cmd_vblank_pipe_info(dc, context, &cmd, pipe, cmd_pipe_index++);
680 
681 			}
682 			pipe_idx++;
683 		}
684 		if (subvp_count == 2) {
685 			update_subvp_prefetch_end_to_mall_start(dc, context, &cmd, subvp_pipes);
686 		}
687 		cmd.fw_assisted_mclk_switch_v2.config_data.pstate_allow_width_us = dc->caps.subvp_pstate_allow_width_us;
688 		cmd.fw_assisted_mclk_switch_v2.config_data.vertical_int_margin_us = dc->caps.subvp_vertical_int_margin_us;
689 
690 		// Store the original watermark value for this SubVP config so we can lower it when the
691 		// MCLK switch starts
692 		wm_val_refclk = context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns *
693 				dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000 / 1000;
694 
695 		cmd.fw_assisted_mclk_switch_v2.config_data.watermark_a_cache = wm_val_refclk < 0xFFFF ? wm_val_refclk : 0xFFFF;
696 	}
697 	dc_dmub_srv_cmd_queue(dc->ctx->dmub_srv, &cmd);
698 	dc_dmub_srv_cmd_execute(dc->ctx->dmub_srv);
699 	dc_dmub_srv_wait_idle(dc->ctx->dmub_srv);
700 }
701 
702 bool dc_dmub_srv_get_diagnostic_data(struct dc_dmub_srv *dc_dmub_srv, struct dmub_diagnostic_data *diag_data)
703 {
704 	if (!dc_dmub_srv || !dc_dmub_srv->dmub || !diag_data)
705 		return false;
706 	return dmub_srv_get_diagnostic_data(dc_dmub_srv->dmub, diag_data);
707 }
708 
709 void dc_dmub_srv_log_diagnostic_data(struct dc_dmub_srv *dc_dmub_srv)
710 {
711 	struct dmub_diagnostic_data diag_data = {0};
712 
713 	if (!dc_dmub_srv || !dc_dmub_srv->dmub) {
714 		DC_LOG_ERROR("%s: invalid parameters.", __func__);
715 		return;
716 	}
717 
718 	if (!dc_dmub_srv_get_diagnostic_data(dc_dmub_srv, &diag_data)) {
719 		DC_LOG_ERROR("%s: dc_dmub_srv_get_diagnostic_data failed.", __func__);
720 		return;
721 	}
722 
723 	DC_LOG_DEBUG(
724 		"DMCUB STATE\n"
725 		"    dmcub_version      : %08x\n"
726 		"    scratch  [0]       : %08x\n"
727 		"    scratch  [1]       : %08x\n"
728 		"    scratch  [2]       : %08x\n"
729 		"    scratch  [3]       : %08x\n"
730 		"    scratch  [4]       : %08x\n"
731 		"    scratch  [5]       : %08x\n"
732 		"    scratch  [6]       : %08x\n"
733 		"    scratch  [7]       : %08x\n"
734 		"    scratch  [8]       : %08x\n"
735 		"    scratch  [9]       : %08x\n"
736 		"    scratch [10]       : %08x\n"
737 		"    scratch [11]       : %08x\n"
738 		"    scratch [12]       : %08x\n"
739 		"    scratch [13]       : %08x\n"
740 		"    scratch [14]       : %08x\n"
741 		"    scratch [15]       : %08x\n"
742 		"    pc                 : %08x\n"
743 		"    unk_fault_addr     : %08x\n"
744 		"    inst_fault_addr    : %08x\n"
745 		"    data_fault_addr    : %08x\n"
746 		"    inbox1_rptr        : %08x\n"
747 		"    inbox1_wptr        : %08x\n"
748 		"    inbox1_size        : %08x\n"
749 		"    inbox0_rptr        : %08x\n"
750 		"    inbox0_wptr        : %08x\n"
751 		"    inbox0_size        : %08x\n"
752 		"    is_enabled         : %d\n"
753 		"    is_soft_reset      : %d\n"
754 		"    is_secure_reset    : %d\n"
755 		"    is_traceport_en    : %d\n"
756 		"    is_cw0_en          : %d\n"
757 		"    is_cw6_en          : %d\n",
758 		diag_data.dmcub_version,
759 		diag_data.scratch[0],
760 		diag_data.scratch[1],
761 		diag_data.scratch[2],
762 		diag_data.scratch[3],
763 		diag_data.scratch[4],
764 		diag_data.scratch[5],
765 		diag_data.scratch[6],
766 		diag_data.scratch[7],
767 		diag_data.scratch[8],
768 		diag_data.scratch[9],
769 		diag_data.scratch[10],
770 		diag_data.scratch[11],
771 		diag_data.scratch[12],
772 		diag_data.scratch[13],
773 		diag_data.scratch[14],
774 		diag_data.scratch[15],
775 		diag_data.pc,
776 		diag_data.undefined_address_fault_addr,
777 		diag_data.inst_fetch_fault_addr,
778 		diag_data.data_write_fault_addr,
779 		diag_data.inbox1_rptr,
780 		diag_data.inbox1_wptr,
781 		diag_data.inbox1_size,
782 		diag_data.inbox0_rptr,
783 		diag_data.inbox0_wptr,
784 		diag_data.inbox0_size,
785 		diag_data.is_dmcub_enabled,
786 		diag_data.is_dmcub_soft_reset,
787 		diag_data.is_dmcub_secure_reset,
788 		diag_data.is_traceport_en,
789 		diag_data.is_cw0_enabled,
790 		diag_data.is_cw6_enabled);
791 }
792