xref: /openbsd/sys/dev/pci/drm/amd/display/dc/dc_dmub_srv.c (revision f005ef32)
1c349dbc7Sjsg /*
2c349dbc7Sjsg  * Copyright 2019 Advanced Micro Devices, Inc.
3c349dbc7Sjsg  *
4c349dbc7Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
5c349dbc7Sjsg  * copy of this software and associated documentation files (the "Software"),
6c349dbc7Sjsg  * to deal in the Software without restriction, including without limitation
7c349dbc7Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c349dbc7Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
9c349dbc7Sjsg  * Software is furnished to do so, subject to the following conditions:
10c349dbc7Sjsg  *
11c349dbc7Sjsg  * The above copyright notice and this permission notice shall be included in
12c349dbc7Sjsg  * all copies or substantial portions of the Software.
13c349dbc7Sjsg  *
14c349dbc7Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15c349dbc7Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16c349dbc7Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17c349dbc7Sjsg  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18c349dbc7Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19c349dbc7Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20c349dbc7Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
21c349dbc7Sjsg  *
22c349dbc7Sjsg  * Authors: AMD
23c349dbc7Sjsg  *
24c349dbc7Sjsg  */
25c349dbc7Sjsg 
26c349dbc7Sjsg #include "dc.h"
27c349dbc7Sjsg #include "dc_dmub_srv.h"
28ad8b1aafSjsg #include "../dmub/dmub_srv.h"
295ca02815Sjsg #include "dm_helpers.h"
301bb76ff1Sjsg #include "dc_hw_types.h"
311bb76ff1Sjsg #include "core_types.h"
321bb76ff1Sjsg #include "../basics/conversion.h"
331bb76ff1Sjsg #include "cursor_reg_cache.h"
34*f005ef32Sjsg #include "resource.h"
355ca02815Sjsg 
365ca02815Sjsg #define CTX dc_dmub_srv->ctx
375ca02815Sjsg #define DC_LOGGER CTX->logger
38c349dbc7Sjsg 
dc_dmub_srv_construct(struct dc_dmub_srv * dc_srv,struct dc * dc,struct dmub_srv * dmub)39c349dbc7Sjsg static void dc_dmub_srv_construct(struct dc_dmub_srv *dc_srv, struct dc *dc,
40c349dbc7Sjsg 				  struct dmub_srv *dmub)
41c349dbc7Sjsg {
42c349dbc7Sjsg 	dc_srv->dmub = dmub;
43c349dbc7Sjsg 	dc_srv->ctx = dc->ctx;
44c349dbc7Sjsg }
45c349dbc7Sjsg 
dc_dmub_srv_create(struct dc * dc,struct dmub_srv * dmub)46c349dbc7Sjsg struct dc_dmub_srv *dc_dmub_srv_create(struct dc *dc, struct dmub_srv *dmub)
47c349dbc7Sjsg {
48c349dbc7Sjsg 	struct dc_dmub_srv *dc_srv =
49c349dbc7Sjsg 		kzalloc(sizeof(struct dc_dmub_srv), GFP_KERNEL);
50c349dbc7Sjsg 
51c349dbc7Sjsg 	if (dc_srv == NULL) {
52c349dbc7Sjsg 		BREAK_TO_DEBUGGER();
53c349dbc7Sjsg 		return NULL;
54c349dbc7Sjsg 	}
55c349dbc7Sjsg 
56c349dbc7Sjsg 	dc_dmub_srv_construct(dc_srv, dc, dmub);
57c349dbc7Sjsg 
58c349dbc7Sjsg 	return dc_srv;
59c349dbc7Sjsg }
60c349dbc7Sjsg 
dc_dmub_srv_destroy(struct dc_dmub_srv ** dmub_srv)61c349dbc7Sjsg void dc_dmub_srv_destroy(struct dc_dmub_srv **dmub_srv)
62c349dbc7Sjsg {
63c349dbc7Sjsg 	if (*dmub_srv) {
64c349dbc7Sjsg 		kfree(*dmub_srv);
65c349dbc7Sjsg 		*dmub_srv = NULL;
66c349dbc7Sjsg 	}
67c349dbc7Sjsg }
68c349dbc7Sjsg 
dc_dmub_srv_wait_idle(struct dc_dmub_srv * dc_dmub_srv)69c349dbc7Sjsg void dc_dmub_srv_wait_idle(struct dc_dmub_srv *dc_dmub_srv)
70c349dbc7Sjsg {
71c349dbc7Sjsg 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
72c349dbc7Sjsg 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
73c349dbc7Sjsg 	enum dmub_status status;
74c349dbc7Sjsg 
75c349dbc7Sjsg 	status = dmub_srv_wait_for_idle(dmub, 100000);
765ca02815Sjsg 	if (status != DMUB_STATUS_OK) {
77c349dbc7Sjsg 		DC_ERROR("Error waiting for DMUB idle: status=%d\n", status);
785ca02815Sjsg 		dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
795ca02815Sjsg 	}
805ca02815Sjsg }
815ca02815Sjsg 
dc_dmub_srv_clear_inbox0_ack(struct dc_dmub_srv * dmub_srv)821bb76ff1Sjsg void dc_dmub_srv_clear_inbox0_ack(struct dc_dmub_srv *dmub_srv)
831bb76ff1Sjsg {
841bb76ff1Sjsg 	struct dmub_srv *dmub = dmub_srv->dmub;
851bb76ff1Sjsg 	struct dc_context *dc_ctx = dmub_srv->ctx;
861bb76ff1Sjsg 	enum dmub_status status = DMUB_STATUS_OK;
871bb76ff1Sjsg 
881bb76ff1Sjsg 	status = dmub_srv_clear_inbox0_ack(dmub);
891bb76ff1Sjsg 	if (status != DMUB_STATUS_OK) {
901bb76ff1Sjsg 		DC_ERROR("Error clearing INBOX0 ack: status=%d\n", status);
911bb76ff1Sjsg 		dc_dmub_srv_log_diagnostic_data(dmub_srv);
921bb76ff1Sjsg 	}
931bb76ff1Sjsg }
941bb76ff1Sjsg 
dc_dmub_srv_wait_for_inbox0_ack(struct dc_dmub_srv * dmub_srv)951bb76ff1Sjsg void dc_dmub_srv_wait_for_inbox0_ack(struct dc_dmub_srv *dmub_srv)
961bb76ff1Sjsg {
971bb76ff1Sjsg 	struct dmub_srv *dmub = dmub_srv->dmub;
981bb76ff1Sjsg 	struct dc_context *dc_ctx = dmub_srv->ctx;
991bb76ff1Sjsg 	enum dmub_status status = DMUB_STATUS_OK;
1001bb76ff1Sjsg 
1011bb76ff1Sjsg 	status = dmub_srv_wait_for_inbox0_ack(dmub, 100000);
1021bb76ff1Sjsg 	if (status != DMUB_STATUS_OK) {
1031bb76ff1Sjsg 		DC_ERROR("Error waiting for INBOX0 HW Lock Ack\n");
1041bb76ff1Sjsg 		dc_dmub_srv_log_diagnostic_data(dmub_srv);
1051bb76ff1Sjsg 	}
1061bb76ff1Sjsg }
1071bb76ff1Sjsg 
dc_dmub_srv_send_inbox0_cmd(struct dc_dmub_srv * dmub_srv,union dmub_inbox0_data_register data)1085ca02815Sjsg void dc_dmub_srv_send_inbox0_cmd(struct dc_dmub_srv *dmub_srv,
1095ca02815Sjsg 		union dmub_inbox0_data_register data)
1105ca02815Sjsg {
1115ca02815Sjsg 	struct dmub_srv *dmub = dmub_srv->dmub;
1121bb76ff1Sjsg 	struct dc_context *dc_ctx = dmub_srv->ctx;
1131bb76ff1Sjsg 	enum dmub_status status = DMUB_STATUS_OK;
1141bb76ff1Sjsg 
1151bb76ff1Sjsg 	status = dmub_srv_send_inbox0_cmd(dmub, data);
1161bb76ff1Sjsg 	if (status != DMUB_STATUS_OK) {
1171bb76ff1Sjsg 		DC_ERROR("Error sending INBOX0 cmd\n");
1181bb76ff1Sjsg 		dc_dmub_srv_log_diagnostic_data(dmub_srv);
1191bb76ff1Sjsg 	}
1205ca02815Sjsg }
1215ca02815Sjsg 
dc_dmub_srv_cmd_run(struct dc_dmub_srv * dc_dmub_srv,union dmub_rb_cmd * cmd,enum dm_dmub_wait_type wait_type)122*f005ef32Sjsg bool dc_dmub_srv_cmd_run(struct dc_dmub_srv *dc_dmub_srv, union dmub_rb_cmd *cmd, enum dm_dmub_wait_type wait_type)
123*f005ef32Sjsg {
124*f005ef32Sjsg 	return dc_dmub_srv_cmd_run_list(dc_dmub_srv, 1, cmd, wait_type);
125*f005ef32Sjsg }
126*f005ef32Sjsg 
dc_dmub_srv_cmd_run_list(struct dc_dmub_srv * dc_dmub_srv,unsigned int count,union dmub_rb_cmd * cmd_list,enum dm_dmub_wait_type wait_type)127*f005ef32Sjsg bool dc_dmub_srv_cmd_run_list(struct dc_dmub_srv *dc_dmub_srv, unsigned int count, union dmub_rb_cmd *cmd_list, enum dm_dmub_wait_type wait_type)
128*f005ef32Sjsg {
129*f005ef32Sjsg 	struct dc_context *dc_ctx;
130*f005ef32Sjsg 	struct dmub_srv *dmub;
131*f005ef32Sjsg 	enum dmub_status status;
132*f005ef32Sjsg 	int i;
133*f005ef32Sjsg 
134*f005ef32Sjsg 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
135*f005ef32Sjsg 		return false;
136*f005ef32Sjsg 
137*f005ef32Sjsg 	dc_ctx = dc_dmub_srv->ctx;
138*f005ef32Sjsg 	dmub = dc_dmub_srv->dmub;
139*f005ef32Sjsg 
140*f005ef32Sjsg 	for (i = 0 ; i < count; i++) {
141*f005ef32Sjsg 		// Queue command
142*f005ef32Sjsg 		status = dmub_srv_cmd_queue(dmub, &cmd_list[i]);
143*f005ef32Sjsg 
144*f005ef32Sjsg 		if (status == DMUB_STATUS_QUEUE_FULL) {
145*f005ef32Sjsg 			/* Execute and wait for queue to become empty again. */
146*f005ef32Sjsg 			dmub_srv_cmd_execute(dmub);
147*f005ef32Sjsg 			dmub_srv_wait_for_idle(dmub, 100000);
148*f005ef32Sjsg 
149*f005ef32Sjsg 			/* Requeue the command. */
150*f005ef32Sjsg 			status = dmub_srv_cmd_queue(dmub, &cmd_list[i]);
151*f005ef32Sjsg 		}
152*f005ef32Sjsg 
153*f005ef32Sjsg 		if (status != DMUB_STATUS_OK) {
154*f005ef32Sjsg 			DC_ERROR("Error queueing DMUB command: status=%d\n", status);
155*f005ef32Sjsg 			dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
156*f005ef32Sjsg 			return false;
157*f005ef32Sjsg 		}
158*f005ef32Sjsg 	}
159*f005ef32Sjsg 
160*f005ef32Sjsg 	status = dmub_srv_cmd_execute(dmub);
161*f005ef32Sjsg 	if (status != DMUB_STATUS_OK) {
162*f005ef32Sjsg 		DC_ERROR("Error starting DMUB execution: status=%d\n", status);
163*f005ef32Sjsg 		dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
164*f005ef32Sjsg 		return false;
165*f005ef32Sjsg 	}
166*f005ef32Sjsg 
167*f005ef32Sjsg 	// Wait for DMUB to process command
168*f005ef32Sjsg 	if (wait_type != DM_DMUB_WAIT_TYPE_NO_WAIT) {
169*f005ef32Sjsg 		status = dmub_srv_wait_for_idle(dmub, 100000);
170*f005ef32Sjsg 
171*f005ef32Sjsg 		if (status != DMUB_STATUS_OK) {
172*f005ef32Sjsg 			DC_LOG_DEBUG("No reply for DMUB command: status=%d\n", status);
173*f005ef32Sjsg 			dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
174*f005ef32Sjsg 			return false;
175*f005ef32Sjsg 		}
176*f005ef32Sjsg 
177*f005ef32Sjsg 		// Copy data back from ring buffer into command
178*f005ef32Sjsg 		if (wait_type == DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY)
179*f005ef32Sjsg 			dmub_rb_get_return_data(&dmub->inbox1_rb, cmd_list);
180*f005ef32Sjsg 	}
181*f005ef32Sjsg 
182*f005ef32Sjsg 	return true;
183*f005ef32Sjsg }
184*f005ef32Sjsg 
dc_dmub_srv_optimized_init_done(struct dc_dmub_srv * dc_dmub_srv)185*f005ef32Sjsg bool dc_dmub_srv_optimized_init_done(struct dc_dmub_srv *dc_dmub_srv)
1865ca02815Sjsg {
1875ca02815Sjsg 	struct dmub_srv *dmub;
188*f005ef32Sjsg 	struct dc_context *dc_ctx;
189*f005ef32Sjsg 	union dmub_fw_boot_status boot_status;
1905ca02815Sjsg 	enum dmub_status status;
1915ca02815Sjsg 
1925ca02815Sjsg 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
1935ca02815Sjsg 		return false;
1945ca02815Sjsg 
1955ca02815Sjsg 	dmub = dc_dmub_srv->dmub;
196*f005ef32Sjsg 	dc_ctx = dc_dmub_srv->ctx;
1975ca02815Sjsg 
198*f005ef32Sjsg 	status = dmub_srv_get_fw_boot_status(dmub, &boot_status);
1995ca02815Sjsg 	if (status != DMUB_STATUS_OK) {
200*f005ef32Sjsg 		DC_ERROR("Error querying DMUB boot status: error=%d\n", status);
2015ca02815Sjsg 		return false;
2025ca02815Sjsg 	}
2035ca02815Sjsg 
204*f005ef32Sjsg 	return boot_status.bits.optimized_init_done;
205c349dbc7Sjsg }
206ad8b1aafSjsg 
dc_dmub_srv_notify_stream_mask(struct dc_dmub_srv * dc_dmub_srv,unsigned int stream_mask)207ad8b1aafSjsg bool dc_dmub_srv_notify_stream_mask(struct dc_dmub_srv *dc_dmub_srv,
208ad8b1aafSjsg 				    unsigned int stream_mask)
209ad8b1aafSjsg {
210ad8b1aafSjsg 	struct dmub_srv *dmub;
211ad8b1aafSjsg 	const uint32_t timeout = 30;
212ad8b1aafSjsg 
213ad8b1aafSjsg 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
214ad8b1aafSjsg 		return false;
215ad8b1aafSjsg 
216ad8b1aafSjsg 	dmub = dc_dmub_srv->dmub;
217ad8b1aafSjsg 
218ad8b1aafSjsg 	return dmub_srv_send_gpint_command(
219ad8b1aafSjsg 		       dmub, DMUB_GPINT__IDLE_OPT_NOTIFY_STREAM_MASK,
220ad8b1aafSjsg 		       stream_mask, timeout) == DMUB_STATUS_OK;
221ad8b1aafSjsg }
2225ca02815Sjsg 
dc_dmub_srv_is_restore_required(struct dc_dmub_srv * dc_dmub_srv)2235ca02815Sjsg bool dc_dmub_srv_is_restore_required(struct dc_dmub_srv *dc_dmub_srv)
2245ca02815Sjsg {
2255ca02815Sjsg 	struct dmub_srv *dmub;
2265ca02815Sjsg 	struct dc_context *dc_ctx;
2275ca02815Sjsg 	union dmub_fw_boot_status boot_status;
2285ca02815Sjsg 	enum dmub_status status;
2295ca02815Sjsg 
2305ca02815Sjsg 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
2315ca02815Sjsg 		return false;
2325ca02815Sjsg 
2335ca02815Sjsg 	dmub = dc_dmub_srv->dmub;
2345ca02815Sjsg 	dc_ctx = dc_dmub_srv->ctx;
2355ca02815Sjsg 
2365ca02815Sjsg 	status = dmub_srv_get_fw_boot_status(dmub, &boot_status);
2375ca02815Sjsg 	if (status != DMUB_STATUS_OK) {
2385ca02815Sjsg 		DC_ERROR("Error querying DMUB boot status: error=%d\n", status);
2395ca02815Sjsg 		return false;
2405ca02815Sjsg 	}
2415ca02815Sjsg 
2425ca02815Sjsg 	return boot_status.bits.restore_required;
2435ca02815Sjsg }
2445ca02815Sjsg 
dc_dmub_srv_get_dmub_outbox0_msg(const struct dc * dc,struct dmcub_trace_buf_entry * entry)2455ca02815Sjsg bool dc_dmub_srv_get_dmub_outbox0_msg(const struct dc *dc, struct dmcub_trace_buf_entry *entry)
2465ca02815Sjsg {
2475ca02815Sjsg 	struct dmub_srv *dmub = dc->ctx->dmub_srv->dmub;
2485ca02815Sjsg 	return dmub_srv_get_outbox0_msg(dmub, entry);
2495ca02815Sjsg }
2505ca02815Sjsg 
dc_dmub_trace_event_control(struct dc * dc,bool enable)2515ca02815Sjsg void dc_dmub_trace_event_control(struct dc *dc, bool enable)
2525ca02815Sjsg {
2535ca02815Sjsg 	dm_helpers_dmub_outbox_interrupt_control(dc->ctx, enable);
2545ca02815Sjsg }
2555ca02815Sjsg 
dc_dmub_srv_drr_update_cmd(struct dc * dc,uint32_t tg_inst,uint32_t vtotal_min,uint32_t vtotal_max)2561bb76ff1Sjsg void dc_dmub_srv_drr_update_cmd(struct dc *dc, uint32_t tg_inst, uint32_t vtotal_min, uint32_t vtotal_max)
2571bb76ff1Sjsg {
2581bb76ff1Sjsg 	union dmub_rb_cmd cmd = { 0 };
2591bb76ff1Sjsg 
2601bb76ff1Sjsg 	cmd.drr_update.header.type = DMUB_CMD__FW_ASSISTED_MCLK_SWITCH;
2611bb76ff1Sjsg 	cmd.drr_update.header.sub_type = DMUB_CMD__FAMS_DRR_UPDATE;
2621bb76ff1Sjsg 	cmd.drr_update.dmub_optc_state_req.v_total_max = vtotal_max;
2631bb76ff1Sjsg 	cmd.drr_update.dmub_optc_state_req.v_total_min = vtotal_min;
2641bb76ff1Sjsg 	cmd.drr_update.dmub_optc_state_req.tg_inst = tg_inst;
2651bb76ff1Sjsg 
2661bb76ff1Sjsg 	cmd.drr_update.header.payload_bytes = sizeof(cmd.drr_update) - sizeof(cmd.drr_update.header);
2671bb76ff1Sjsg 
2681bb76ff1Sjsg 	// Send the command to the DMCUB.
269*f005ef32Sjsg 	dm_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
2701bb76ff1Sjsg }
2711bb76ff1Sjsg 
dc_dmub_srv_set_drr_manual_trigger_cmd(struct dc * dc,uint32_t tg_inst)2721bb76ff1Sjsg void dc_dmub_srv_set_drr_manual_trigger_cmd(struct dc *dc, uint32_t tg_inst)
2731bb76ff1Sjsg {
2741bb76ff1Sjsg 	union dmub_rb_cmd cmd = { 0 };
2751bb76ff1Sjsg 
2761bb76ff1Sjsg 	cmd.drr_update.header.type = DMUB_CMD__FW_ASSISTED_MCLK_SWITCH;
2771bb76ff1Sjsg 	cmd.drr_update.header.sub_type = DMUB_CMD__FAMS_SET_MANUAL_TRIGGER;
2781bb76ff1Sjsg 	cmd.drr_update.dmub_optc_state_req.tg_inst = tg_inst;
2791bb76ff1Sjsg 
2801bb76ff1Sjsg 	cmd.drr_update.header.payload_bytes = sizeof(cmd.drr_update) - sizeof(cmd.drr_update.header);
2811bb76ff1Sjsg 
2821bb76ff1Sjsg 	// Send the command to the DMCUB.
283*f005ef32Sjsg 	dm_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
2841bb76ff1Sjsg }
2851bb76ff1Sjsg 
dc_dmub_srv_get_pipes_for_stream(struct dc * dc,struct dc_stream_state * stream)2861bb76ff1Sjsg static uint8_t dc_dmub_srv_get_pipes_for_stream(struct dc *dc, struct dc_stream_state *stream)
2871bb76ff1Sjsg {
2881bb76ff1Sjsg 	uint8_t pipes = 0;
2891bb76ff1Sjsg 	int i = 0;
2901bb76ff1Sjsg 
2911bb76ff1Sjsg 	for (i = 0; i < MAX_PIPES; i++) {
2921bb76ff1Sjsg 		struct pipe_ctx *pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2931bb76ff1Sjsg 
2941bb76ff1Sjsg 		if (pipe->stream == stream && pipe->stream_res.tg)
2951bb76ff1Sjsg 			pipes = i;
2961bb76ff1Sjsg 	}
2971bb76ff1Sjsg 	return pipes;
2981bb76ff1Sjsg }
2991bb76ff1Sjsg 
dc_dmub_srv_populate_fams_pipe_info(struct dc * dc,struct dc_state * context,struct pipe_ctx * head_pipe,struct dmub_cmd_fw_assisted_mclk_switch_pipe_data * fams_pipe_data)300*f005ef32Sjsg static void dc_dmub_srv_populate_fams_pipe_info(struct dc *dc, struct dc_state *context,
301*f005ef32Sjsg 		struct pipe_ctx *head_pipe,
302*f005ef32Sjsg 		struct dmub_cmd_fw_assisted_mclk_switch_pipe_data *fams_pipe_data)
3031bb76ff1Sjsg {
304*f005ef32Sjsg 	int j;
305*f005ef32Sjsg 	int pipe_idx = 0;
3061bb76ff1Sjsg 
307*f005ef32Sjsg 	fams_pipe_data->pipe_index[pipe_idx++] = head_pipe->plane_res.hubp->inst;
308*f005ef32Sjsg 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
309*f005ef32Sjsg 		struct pipe_ctx *split_pipe = &context->res_ctx.pipe_ctx[j];
3101bb76ff1Sjsg 
311*f005ef32Sjsg 		if (split_pipe->stream == head_pipe->stream && (split_pipe->top_pipe || split_pipe->prev_odm_pipe)) {
312*f005ef32Sjsg 			fams_pipe_data->pipe_index[pipe_idx++] = split_pipe->plane_res.hubp->inst;
3131bb76ff1Sjsg 		}
3141bb76ff1Sjsg 	}
315*f005ef32Sjsg 	fams_pipe_data->pipe_count = pipe_idx;
3161bb76ff1Sjsg }
3171bb76ff1Sjsg 
dc_dmub_srv_p_state_delegate(struct dc * dc,bool should_manage_pstate,struct dc_state * context)3181bb76ff1Sjsg bool dc_dmub_srv_p_state_delegate(struct dc *dc, bool should_manage_pstate, struct dc_state *context)
3191bb76ff1Sjsg {
3201bb76ff1Sjsg 	union dmub_rb_cmd cmd = { 0 };
3211bb76ff1Sjsg 	struct dmub_cmd_fw_assisted_mclk_switch_config *config_data = &cmd.fw_assisted_mclk_switch.config_data;
322*f005ef32Sjsg 	int i = 0, k = 0;
3231bb76ff1Sjsg 	int ramp_up_num_steps = 1; // TODO: Ramp is currently disabled. Reenable it.
3241bb76ff1Sjsg 	uint8_t visual_confirm_enabled;
325*f005ef32Sjsg 	int pipe_idx = 0;
3261bb76ff1Sjsg 
3271bb76ff1Sjsg 	if (dc == NULL)
3281bb76ff1Sjsg 		return false;
3291bb76ff1Sjsg 
3301bb76ff1Sjsg 	visual_confirm_enabled = dc->debug.visual_confirm == VISUAL_CONFIRM_FAMS;
3311bb76ff1Sjsg 
3321bb76ff1Sjsg 	// Format command.
3331bb76ff1Sjsg 	cmd.fw_assisted_mclk_switch.header.type = DMUB_CMD__FW_ASSISTED_MCLK_SWITCH;
3341bb76ff1Sjsg 	cmd.fw_assisted_mclk_switch.header.sub_type = DMUB_CMD__FAMS_SETUP_FW_CTRL;
3351bb76ff1Sjsg 	cmd.fw_assisted_mclk_switch.config_data.fams_enabled = should_manage_pstate;
3361bb76ff1Sjsg 	cmd.fw_assisted_mclk_switch.config_data.visual_confirm_enabled = visual_confirm_enabled;
3371bb76ff1Sjsg 
338*f005ef32Sjsg 	if (should_manage_pstate) {
339*f005ef32Sjsg 		for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
340*f005ef32Sjsg 			struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3411bb76ff1Sjsg 
342*f005ef32Sjsg 			if (!pipe->stream)
343*f005ef32Sjsg 				continue;
344*f005ef32Sjsg 
345*f005ef32Sjsg 			/* If FAMS is being used to support P-State and there is a stream
346*f005ef32Sjsg 			 * that does not use FAMS, we are in an FPO + VActive scenario.
347*f005ef32Sjsg 			 * Assign vactive stretch margin in this case.
348*f005ef32Sjsg 			 */
349*f005ef32Sjsg 			if (!pipe->stream->fpo_in_use) {
350*f005ef32Sjsg 				cmd.fw_assisted_mclk_switch.config_data.vactive_stretch_margin_us = dc->debug.fpo_vactive_margin_us;
351*f005ef32Sjsg 				break;
352*f005ef32Sjsg 			}
353*f005ef32Sjsg 			pipe_idx++;
354*f005ef32Sjsg 		}
3551bb76ff1Sjsg 	}
3561bb76ff1Sjsg 
357*f005ef32Sjsg 	for (i = 0, k = 0; context && i < dc->res_pool->pipe_count; i++) {
358*f005ef32Sjsg 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
359*f005ef32Sjsg 
360*f005ef32Sjsg 		if (resource_is_pipe_type(pipe, OTG_MASTER) && pipe->stream->fpo_in_use) {
361*f005ef32Sjsg 			struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
362*f005ef32Sjsg 			uint8_t min_refresh_in_hz = (pipe->stream->timing.min_refresh_in_uhz + 999999) / 1000000;
363*f005ef32Sjsg 
364*f005ef32Sjsg 			config_data->pipe_data[k].pix_clk_100hz = pipe->stream->timing.pix_clk_100hz;
365*f005ef32Sjsg 			config_data->pipe_data[k].min_refresh_in_hz = min_refresh_in_hz;
366*f005ef32Sjsg 			config_data->pipe_data[k].max_ramp_step = ramp_up_num_steps;
367*f005ef32Sjsg 			config_data->pipe_data[k].pipes = dc_dmub_srv_get_pipes_for_stream(dc, pipe->stream);
368*f005ef32Sjsg 			dc_dmub_srv_populate_fams_pipe_info(dc, context, pipe, &config_data->pipe_data[k]);
369*f005ef32Sjsg 			k++;
370*f005ef32Sjsg 		}
371*f005ef32Sjsg 	}
3721bb76ff1Sjsg 	cmd.fw_assisted_mclk_switch.header.payload_bytes =
3731bb76ff1Sjsg 		sizeof(cmd.fw_assisted_mclk_switch) - sizeof(cmd.fw_assisted_mclk_switch.header);
3741bb76ff1Sjsg 
3751bb76ff1Sjsg 	// Send the command to the DMCUB.
376*f005ef32Sjsg 	dm_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
3771bb76ff1Sjsg 
3781bb76ff1Sjsg 	return true;
3791bb76ff1Sjsg }
3801bb76ff1Sjsg 
dc_dmub_srv_query_caps_cmd(struct dc_dmub_srv * dc_dmub_srv)381*f005ef32Sjsg void dc_dmub_srv_query_caps_cmd(struct dc_dmub_srv *dc_dmub_srv)
3821bb76ff1Sjsg {
3831bb76ff1Sjsg 	union dmub_rb_cmd cmd = { 0 };
3841bb76ff1Sjsg 
385*f005ef32Sjsg 	if (dc_dmub_srv->ctx->dc->debug.dmcub_emulation)
3861bb76ff1Sjsg 		return;
3871bb76ff1Sjsg 
3881bb76ff1Sjsg 	memset(&cmd, 0, sizeof(cmd));
3891bb76ff1Sjsg 
3901bb76ff1Sjsg 	/* Prepare fw command */
3911bb76ff1Sjsg 	cmd.query_feature_caps.header.type = DMUB_CMD__QUERY_FEATURE_CAPS;
3921bb76ff1Sjsg 	cmd.query_feature_caps.header.sub_type = 0;
3931bb76ff1Sjsg 	cmd.query_feature_caps.header.ret_status = 1;
3941bb76ff1Sjsg 	cmd.query_feature_caps.header.payload_bytes = sizeof(struct dmub_cmd_query_feature_caps_data);
3951bb76ff1Sjsg 
3961bb76ff1Sjsg 	/* If command was processed, copy feature caps to dmub srv */
397*f005ef32Sjsg 	if (dm_execute_dmub_cmd(dc_dmub_srv->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY) &&
3981bb76ff1Sjsg 	    cmd.query_feature_caps.header.ret_status == 0) {
399*f005ef32Sjsg 		memcpy(&dc_dmub_srv->dmub->feature_caps,
4001bb76ff1Sjsg 		       &cmd.query_feature_caps.query_feature_caps_data,
4011bb76ff1Sjsg 		       sizeof(struct dmub_feature_caps));
4021bb76ff1Sjsg 	}
4031bb76ff1Sjsg }
4041bb76ff1Sjsg 
dc_dmub_srv_get_visual_confirm_color_cmd(struct dc * dc,struct pipe_ctx * pipe_ctx)4051bb76ff1Sjsg void dc_dmub_srv_get_visual_confirm_color_cmd(struct dc *dc, struct pipe_ctx *pipe_ctx)
4061bb76ff1Sjsg {
4071bb76ff1Sjsg 	union dmub_rb_cmd cmd = { 0 };
4081bb76ff1Sjsg 	unsigned int panel_inst = 0;
4091bb76ff1Sjsg 
4101bb76ff1Sjsg 	dc_get_edp_link_panel_inst(dc, pipe_ctx->stream->link, &panel_inst);
4111bb76ff1Sjsg 
4121bb76ff1Sjsg 	memset(&cmd, 0, sizeof(cmd));
4131bb76ff1Sjsg 
4141bb76ff1Sjsg 	// Prepare fw command
4151bb76ff1Sjsg 	cmd.visual_confirm_color.header.type = DMUB_CMD__GET_VISUAL_CONFIRM_COLOR;
4161bb76ff1Sjsg 	cmd.visual_confirm_color.header.sub_type = 0;
4171bb76ff1Sjsg 	cmd.visual_confirm_color.header.ret_status = 1;
4181bb76ff1Sjsg 	cmd.visual_confirm_color.header.payload_bytes = sizeof(struct dmub_cmd_visual_confirm_color_data);
4191bb76ff1Sjsg 	cmd.visual_confirm_color.visual_confirm_color_data.visual_confirm_color.panel_inst = panel_inst;
4201bb76ff1Sjsg 
4211bb76ff1Sjsg 	// If command was processed, copy feature caps to dmub srv
422*f005ef32Sjsg 	if (dm_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY) &&
4231bb76ff1Sjsg 		cmd.visual_confirm_color.header.ret_status == 0) {
4241bb76ff1Sjsg 		memcpy(&dc->ctx->dmub_srv->dmub->visual_confirm_color,
4251bb76ff1Sjsg 			&cmd.visual_confirm_color.visual_confirm_color_data,
4261bb76ff1Sjsg 			sizeof(struct dmub_visual_confirm_color));
4271bb76ff1Sjsg 	}
4281bb76ff1Sjsg }
4291bb76ff1Sjsg 
4301bb76ff1Sjsg /**
431*f005ef32Sjsg  * populate_subvp_cmd_drr_info - Helper to populate DRR pipe info for the DMCUB subvp command
4321bb76ff1Sjsg  *
433*f005ef32Sjsg  * @dc: [in] current dc state
434*f005ef32Sjsg  * @subvp_pipe: [in] pipe_ctx for the SubVP pipe
435*f005ef32Sjsg  * @vblank_pipe: [in] pipe_ctx for the DRR pipe
436*f005ef32Sjsg  * @pipe_data: [in] Pipe data which stores the VBLANK/DRR info
437*f005ef32Sjsg  *
438*f005ef32Sjsg  * Populate the DMCUB SubVP command with DRR pipe info. All the information
439*f005ef32Sjsg  * required for calculating the SubVP + DRR microschedule is populated here.
4401bb76ff1Sjsg  *
4411bb76ff1Sjsg  * High level algorithm:
4421bb76ff1Sjsg  * 1. Get timing for SubVP pipe, phantom pipe, and DRR pipe
4431bb76ff1Sjsg  * 2. Calculate the min and max vtotal which supports SubVP + DRR microschedule
4441bb76ff1Sjsg  * 3. Populate the drr_info with the min and max supported vtotal values
4451bb76ff1Sjsg  */
populate_subvp_cmd_drr_info(struct dc * dc,struct pipe_ctx * subvp_pipe,struct pipe_ctx * vblank_pipe,struct dmub_cmd_fw_assisted_mclk_switch_pipe_data_v2 * pipe_data)4461bb76ff1Sjsg static void populate_subvp_cmd_drr_info(struct dc *dc,
4471bb76ff1Sjsg 		struct pipe_ctx *subvp_pipe,
4481bb76ff1Sjsg 		struct pipe_ctx *vblank_pipe,
4491bb76ff1Sjsg 		struct dmub_cmd_fw_assisted_mclk_switch_pipe_data_v2 *pipe_data)
4501bb76ff1Sjsg {
4511bb76ff1Sjsg 	struct dc_crtc_timing *main_timing = &subvp_pipe->stream->timing;
4521bb76ff1Sjsg 	struct dc_crtc_timing *phantom_timing = &subvp_pipe->stream->mall_stream_config.paired_stream->timing;
4531bb76ff1Sjsg 	struct dc_crtc_timing *drr_timing = &vblank_pipe->stream->timing;
4541bb76ff1Sjsg 	uint16_t drr_frame_us = 0;
4551bb76ff1Sjsg 	uint16_t min_drr_supported_us = 0;
4561bb76ff1Sjsg 	uint16_t max_drr_supported_us = 0;
4571bb76ff1Sjsg 	uint16_t max_drr_vblank_us = 0;
4581bb76ff1Sjsg 	uint16_t max_drr_mallregion_us = 0;
4591bb76ff1Sjsg 	uint16_t mall_region_us = 0;
4601bb76ff1Sjsg 	uint16_t prefetch_us = 0;
4611bb76ff1Sjsg 	uint16_t subvp_active_us = 0;
4621bb76ff1Sjsg 	uint16_t drr_active_us = 0;
4631bb76ff1Sjsg 	uint16_t min_vtotal_supported = 0;
4641bb76ff1Sjsg 	uint16_t max_vtotal_supported = 0;
4651bb76ff1Sjsg 
4661bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.drr_info.drr_in_use = true;
4671bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.drr_info.use_ramping = false; // for now don't use ramping
4681bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.drr_info.drr_window_size_ms = 4; // hardcode 4ms DRR window for now
4691bb76ff1Sjsg 
4701bb76ff1Sjsg 	drr_frame_us = div64_u64(((uint64_t)drr_timing->v_total * drr_timing->h_total * 1000000),
4711bb76ff1Sjsg 			(((uint64_t)drr_timing->pix_clk_100hz * 100)));
4721bb76ff1Sjsg 	// P-State allow width and FW delays already included phantom_timing->v_addressable
4731bb76ff1Sjsg 	mall_region_us = div64_u64(((uint64_t)phantom_timing->v_addressable * phantom_timing->h_total * 1000000),
4741bb76ff1Sjsg 			(((uint64_t)phantom_timing->pix_clk_100hz * 100)));
4751bb76ff1Sjsg 	min_drr_supported_us = drr_frame_us + mall_region_us + SUBVP_DRR_MARGIN_US;
4761bb76ff1Sjsg 	min_vtotal_supported = div64_u64(((uint64_t)drr_timing->pix_clk_100hz * 100 * min_drr_supported_us),
4771bb76ff1Sjsg 			(((uint64_t)drr_timing->h_total * 1000000)));
4781bb76ff1Sjsg 
4791bb76ff1Sjsg 	prefetch_us = div64_u64(((uint64_t)(phantom_timing->v_total - phantom_timing->v_front_porch) * phantom_timing->h_total * 1000000),
4801bb76ff1Sjsg 			(((uint64_t)phantom_timing->pix_clk_100hz * 100) + dc->caps.subvp_prefetch_end_to_mall_start_us));
4811bb76ff1Sjsg 	subvp_active_us = div64_u64(((uint64_t)main_timing->v_addressable * main_timing->h_total * 1000000),
4821bb76ff1Sjsg 			(((uint64_t)main_timing->pix_clk_100hz * 100)));
4831bb76ff1Sjsg 	drr_active_us = div64_u64(((uint64_t)drr_timing->v_addressable * drr_timing->h_total * 1000000),
4841bb76ff1Sjsg 			(((uint64_t)drr_timing->pix_clk_100hz * 100)));
485*f005ef32Sjsg 	max_drr_vblank_us = div64_u64((subvp_active_us - prefetch_us -
486*f005ef32Sjsg 			dc->caps.subvp_fw_processing_delay_us - drr_active_us), 2) + drr_active_us;
487*f005ef32Sjsg 	max_drr_mallregion_us = subvp_active_us - prefetch_us - mall_region_us - dc->caps.subvp_fw_processing_delay_us;
4881bb76ff1Sjsg 	max_drr_supported_us = max_drr_vblank_us > max_drr_mallregion_us ? max_drr_vblank_us : max_drr_mallregion_us;
4891bb76ff1Sjsg 	max_vtotal_supported = div64_u64(((uint64_t)drr_timing->pix_clk_100hz * 100 * max_drr_supported_us),
4901bb76ff1Sjsg 			(((uint64_t)drr_timing->h_total * 1000000)));
4911bb76ff1Sjsg 
492*f005ef32Sjsg 	/* When calculating the max vtotal supported for SubVP + DRR cases, add
493*f005ef32Sjsg 	 * margin due to possible rounding errors (being off by 1 line in the
494*f005ef32Sjsg 	 * FW calculation can incorrectly push the P-State switch to wait 1 frame
495*f005ef32Sjsg 	 * longer).
496*f005ef32Sjsg 	 */
497*f005ef32Sjsg 	max_vtotal_supported = max_vtotal_supported - dc->caps.subvp_drr_max_vblank_margin_us;
498*f005ef32Sjsg 
4991bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.drr_info.min_vtotal_supported = min_vtotal_supported;
5001bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.drr_info.max_vtotal_supported = max_vtotal_supported;
501*f005ef32Sjsg 	pipe_data->pipe_config.vblank_data.drr_info.drr_vblank_start_margin = dc->caps.subvp_drr_vblank_start_margin_us;
5021bb76ff1Sjsg }
5031bb76ff1Sjsg 
5041bb76ff1Sjsg /**
505*f005ef32Sjsg  * populate_subvp_cmd_vblank_pipe_info - Helper to populate VBLANK pipe info for the DMUB subvp command
5061bb76ff1Sjsg  *
507*f005ef32Sjsg  * @dc: [in] current dc state
508*f005ef32Sjsg  * @context: [in] new dc state
509*f005ef32Sjsg  * @cmd: [in] DMUB cmd to be populated with SubVP info
510*f005ef32Sjsg  * @vblank_pipe: [in] pipe_ctx for the VBLANK pipe
511*f005ef32Sjsg  * @cmd_pipe_index: [in] index for the pipe array in DMCUB SubVP cmd
5121bb76ff1Sjsg  *
513*f005ef32Sjsg  * Populate the DMCUB SubVP command with VBLANK pipe info. All the information
514*f005ef32Sjsg  * required to calculate the microschedule for SubVP + VBLANK case is stored in
515*f005ef32Sjsg  * the pipe_data (subvp_data and vblank_data).  Also check if the VBLANK pipe
516*f005ef32Sjsg  * is a DRR display -- if it is make a call to populate drr_info.
5171bb76ff1Sjsg  */
populate_subvp_cmd_vblank_pipe_info(struct dc * dc,struct dc_state * context,union dmub_rb_cmd * cmd,struct pipe_ctx * vblank_pipe,uint8_t cmd_pipe_index)5181bb76ff1Sjsg static void populate_subvp_cmd_vblank_pipe_info(struct dc *dc,
5191bb76ff1Sjsg 		struct dc_state *context,
5201bb76ff1Sjsg 		union dmub_rb_cmd *cmd,
5211bb76ff1Sjsg 		struct pipe_ctx *vblank_pipe,
5221bb76ff1Sjsg 		uint8_t cmd_pipe_index)
5231bb76ff1Sjsg {
5241bb76ff1Sjsg 	uint32_t i;
5251bb76ff1Sjsg 	struct pipe_ctx *pipe = NULL;
5261bb76ff1Sjsg 	struct dmub_cmd_fw_assisted_mclk_switch_pipe_data_v2 *pipe_data =
5271bb76ff1Sjsg 			&cmd->fw_assisted_mclk_switch_v2.config_data.pipe_data[cmd_pipe_index];
5281bb76ff1Sjsg 
5291bb76ff1Sjsg 	// Find the SubVP pipe
5301bb76ff1Sjsg 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5311bb76ff1Sjsg 		pipe = &context->res_ctx.pipe_ctx[i];
5321bb76ff1Sjsg 
5331bb76ff1Sjsg 		// We check for master pipe, but it shouldn't matter since we only need
5341bb76ff1Sjsg 		// the pipe for timing info (stream should be same for any pipe splits)
535*f005ef32Sjsg 		if (!resource_is_pipe_type(pipe, OTG_MASTER) ||
536*f005ef32Sjsg 				!resource_is_pipe_type(pipe, DPP_PIPE))
5371bb76ff1Sjsg 			continue;
5381bb76ff1Sjsg 
5391bb76ff1Sjsg 		// Find the SubVP pipe
5401bb76ff1Sjsg 		if (pipe->stream->mall_stream_config.type == SUBVP_MAIN)
5411bb76ff1Sjsg 			break;
5421bb76ff1Sjsg 	}
5431bb76ff1Sjsg 
5441bb76ff1Sjsg 	pipe_data->mode = VBLANK;
5451bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.pix_clk_100hz = vblank_pipe->stream->timing.pix_clk_100hz;
5461bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.vblank_start = vblank_pipe->stream->timing.v_total -
5471bb76ff1Sjsg 							vblank_pipe->stream->timing.v_front_porch;
5481bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.vtotal = vblank_pipe->stream->timing.v_total;
5491bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.htotal = vblank_pipe->stream->timing.h_total;
5501bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.vblank_pipe_index = vblank_pipe->pipe_idx;
5511bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.vstartup_start = vblank_pipe->pipe_dlg_param.vstartup_start;
5521bb76ff1Sjsg 	pipe_data->pipe_config.vblank_data.vblank_end =
5531bb76ff1Sjsg 			vblank_pipe->stream->timing.v_total - vblank_pipe->stream->timing.v_front_porch - vblank_pipe->stream->timing.v_addressable;
5541bb76ff1Sjsg 
5551bb76ff1Sjsg 	if (vblank_pipe->stream->ignore_msa_timing_param)
5561bb76ff1Sjsg 		populate_subvp_cmd_drr_info(dc, pipe, vblank_pipe, pipe_data);
5571bb76ff1Sjsg }
5581bb76ff1Sjsg 
5591bb76ff1Sjsg /**
560*f005ef32Sjsg  * update_subvp_prefetch_end_to_mall_start - Helper for SubVP + SubVP case
5611bb76ff1Sjsg  *
562*f005ef32Sjsg  * @dc: [in] current dc state
563*f005ef32Sjsg  * @context: [in] new dc state
564*f005ef32Sjsg  * @cmd: [in] DMUB cmd to be populated with SubVP info
565*f005ef32Sjsg  * @subvp_pipes: [in] Array of SubVP pipes (should always be length 2)
5661bb76ff1Sjsg  *
567*f005ef32Sjsg  * For SubVP + SubVP, we use a single vertical interrupt to start the
568*f005ef32Sjsg  * microschedule for both SubVP pipes. In order for this to work correctly, the
569*f005ef32Sjsg  * MALL REGION of both SubVP pipes must start at the same time. This function
570*f005ef32Sjsg  * lengthens the prefetch end to mall start delay of the SubVP pipe that has
571*f005ef32Sjsg  * the shorter prefetch so that both MALL REGION's will start at the same time.
5721bb76ff1Sjsg  */
update_subvp_prefetch_end_to_mall_start(struct dc * dc,struct dc_state * context,union dmub_rb_cmd * cmd,struct pipe_ctx * subvp_pipes[])5731bb76ff1Sjsg static void update_subvp_prefetch_end_to_mall_start(struct dc *dc,
5741bb76ff1Sjsg 		struct dc_state *context,
5751bb76ff1Sjsg 		union dmub_rb_cmd *cmd,
5761bb76ff1Sjsg 		struct pipe_ctx *subvp_pipes[])
5771bb76ff1Sjsg {
5781bb76ff1Sjsg 	uint32_t subvp0_prefetch_us = 0;
5791bb76ff1Sjsg 	uint32_t subvp1_prefetch_us = 0;
5801bb76ff1Sjsg 	uint32_t prefetch_delta_us = 0;
5811bb76ff1Sjsg 	struct dc_crtc_timing *phantom_timing0 = &subvp_pipes[0]->stream->mall_stream_config.paired_stream->timing;
5821bb76ff1Sjsg 	struct dc_crtc_timing *phantom_timing1 = &subvp_pipes[1]->stream->mall_stream_config.paired_stream->timing;
5831bb76ff1Sjsg 	struct dmub_cmd_fw_assisted_mclk_switch_pipe_data_v2 *pipe_data = NULL;
5841bb76ff1Sjsg 
5851bb76ff1Sjsg 	subvp0_prefetch_us = div64_u64(((uint64_t)(phantom_timing0->v_total - phantom_timing0->v_front_porch) *
5861bb76ff1Sjsg 			(uint64_t)phantom_timing0->h_total * 1000000),
5871bb76ff1Sjsg 			(((uint64_t)phantom_timing0->pix_clk_100hz * 100) + dc->caps.subvp_prefetch_end_to_mall_start_us));
5881bb76ff1Sjsg 	subvp1_prefetch_us = div64_u64(((uint64_t)(phantom_timing1->v_total - phantom_timing1->v_front_porch) *
5891bb76ff1Sjsg 			(uint64_t)phantom_timing1->h_total * 1000000),
5901bb76ff1Sjsg 			(((uint64_t)phantom_timing1->pix_clk_100hz * 100) + dc->caps.subvp_prefetch_end_to_mall_start_us));
5911bb76ff1Sjsg 
5921bb76ff1Sjsg 	// Whichever SubVP PIPE has the smaller prefetch (including the prefetch end to mall start time)
5931bb76ff1Sjsg 	// should increase it's prefetch time to match the other
5941bb76ff1Sjsg 	if (subvp0_prefetch_us > subvp1_prefetch_us) {
5951bb76ff1Sjsg 		pipe_data = &cmd->fw_assisted_mclk_switch_v2.config_data.pipe_data[1];
5961bb76ff1Sjsg 		prefetch_delta_us = subvp0_prefetch_us - subvp1_prefetch_us;
5971bb76ff1Sjsg 		pipe_data->pipe_config.subvp_data.prefetch_to_mall_start_lines =
5981bb76ff1Sjsg 				div64_u64(((uint64_t)(dc->caps.subvp_prefetch_end_to_mall_start_us + prefetch_delta_us) *
5991bb76ff1Sjsg 					((uint64_t)phantom_timing1->pix_clk_100hz * 100) + ((uint64_t)phantom_timing1->h_total * 1000000 - 1)),
6001bb76ff1Sjsg 					((uint64_t)phantom_timing1->h_total * 1000000));
6011bb76ff1Sjsg 
6021bb76ff1Sjsg 	} else if (subvp1_prefetch_us >  subvp0_prefetch_us) {
6031bb76ff1Sjsg 		pipe_data = &cmd->fw_assisted_mclk_switch_v2.config_data.pipe_data[0];
6041bb76ff1Sjsg 		prefetch_delta_us = subvp1_prefetch_us - subvp0_prefetch_us;
6051bb76ff1Sjsg 		pipe_data->pipe_config.subvp_data.prefetch_to_mall_start_lines =
6061bb76ff1Sjsg 				div64_u64(((uint64_t)(dc->caps.subvp_prefetch_end_to_mall_start_us + prefetch_delta_us) *
6071bb76ff1Sjsg 					((uint64_t)phantom_timing0->pix_clk_100hz * 100) + ((uint64_t)phantom_timing0->h_total * 1000000 - 1)),
6081bb76ff1Sjsg 					((uint64_t)phantom_timing0->h_total * 1000000));
6091bb76ff1Sjsg 	}
6101bb76ff1Sjsg }
6111bb76ff1Sjsg 
6121bb76ff1Sjsg /**
613*f005ef32Sjsg  * populate_subvp_cmd_pipe_info - Helper to populate the SubVP pipe info for the DMUB subvp command
6141bb76ff1Sjsg  *
615*f005ef32Sjsg  * @dc: [in] current dc state
616*f005ef32Sjsg  * @context: [in] new dc state
617*f005ef32Sjsg  * @cmd: [in] DMUB cmd to be populated with SubVP info
618*f005ef32Sjsg  * @subvp_pipe: [in] pipe_ctx for the SubVP pipe
619*f005ef32Sjsg  * @cmd_pipe_index: [in] index for the pipe array in DMCUB SubVP cmd
6201bb76ff1Sjsg  *
621*f005ef32Sjsg  * Populate the DMCUB SubVP command with SubVP pipe info. All the information
622*f005ef32Sjsg  * required to calculate the microschedule for the SubVP pipe is stored in the
623*f005ef32Sjsg  * pipe_data of the DMCUB SubVP command.
6241bb76ff1Sjsg  */
populate_subvp_cmd_pipe_info(struct dc * dc,struct dc_state * context,union dmub_rb_cmd * cmd,struct pipe_ctx * subvp_pipe,uint8_t cmd_pipe_index)6251bb76ff1Sjsg static void populate_subvp_cmd_pipe_info(struct dc *dc,
6261bb76ff1Sjsg 		struct dc_state *context,
6271bb76ff1Sjsg 		union dmub_rb_cmd *cmd,
6281bb76ff1Sjsg 		struct pipe_ctx *subvp_pipe,
6291bb76ff1Sjsg 		uint8_t cmd_pipe_index)
6301bb76ff1Sjsg {
6311bb76ff1Sjsg 	uint32_t j;
6321bb76ff1Sjsg 	struct dmub_cmd_fw_assisted_mclk_switch_pipe_data_v2 *pipe_data =
6331bb76ff1Sjsg 			&cmd->fw_assisted_mclk_switch_v2.config_data.pipe_data[cmd_pipe_index];
6341bb76ff1Sjsg 	struct dc_crtc_timing *main_timing = &subvp_pipe->stream->timing;
6351bb76ff1Sjsg 	struct dc_crtc_timing *phantom_timing = &subvp_pipe->stream->mall_stream_config.paired_stream->timing;
6361bb76ff1Sjsg 	uint32_t out_num_stream, out_den_stream, out_num_plane, out_den_plane, out_num, out_den;
6371bb76ff1Sjsg 
6381bb76ff1Sjsg 	pipe_data->mode = SUBVP;
6391bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.pix_clk_100hz = subvp_pipe->stream->timing.pix_clk_100hz;
6401bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.htotal = subvp_pipe->stream->timing.h_total;
6411bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.vtotal = subvp_pipe->stream->timing.v_total;
6421bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.main_vblank_start =
6431bb76ff1Sjsg 			main_timing->v_total - main_timing->v_front_porch;
6441bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.main_vblank_end =
6451bb76ff1Sjsg 			main_timing->v_total - main_timing->v_front_porch - main_timing->v_addressable;
6461bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.mall_region_lines = phantom_timing->v_addressable;
647*f005ef32Sjsg 	pipe_data->pipe_config.subvp_data.main_pipe_index = subvp_pipe->stream_res.tg->inst;
6481bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.is_drr = subvp_pipe->stream->ignore_msa_timing_param;
6491bb76ff1Sjsg 
6501bb76ff1Sjsg 	/* Calculate the scaling factor from the src and dst height.
6511bb76ff1Sjsg 	 * e.g. If 3840x2160 being downscaled to 1920x1080, the scaling factor is 1/2.
6521bb76ff1Sjsg 	 * Reduce the fraction 1080/2160 = 1/2 for the "scaling factor"
6531bb76ff1Sjsg 	 *
6541bb76ff1Sjsg 	 * Make sure to combine stream and plane scaling together.
6551bb76ff1Sjsg 	 */
6561bb76ff1Sjsg 	reduce_fraction(subvp_pipe->stream->src.height, subvp_pipe->stream->dst.height,
6571bb76ff1Sjsg 			&out_num_stream, &out_den_stream);
6581bb76ff1Sjsg 	reduce_fraction(subvp_pipe->plane_state->src_rect.height, subvp_pipe->plane_state->dst_rect.height,
6591bb76ff1Sjsg 			&out_num_plane, &out_den_plane);
6601bb76ff1Sjsg 	reduce_fraction(out_num_stream * out_num_plane, out_den_stream * out_den_plane, &out_num, &out_den);
6611bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.scale_factor_numerator = out_num;
6621bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.scale_factor_denominator = out_den;
6631bb76ff1Sjsg 
6641bb76ff1Sjsg 	// Prefetch lines is equal to VACTIVE + BP + VSYNC
6651bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.prefetch_lines =
6661bb76ff1Sjsg 			phantom_timing->v_total - phantom_timing->v_front_porch;
6671bb76ff1Sjsg 
6681bb76ff1Sjsg 	// Round up
6691bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.prefetch_to_mall_start_lines =
6701bb76ff1Sjsg 			div64_u64(((uint64_t)dc->caps.subvp_prefetch_end_to_mall_start_us * ((uint64_t)phantom_timing->pix_clk_100hz * 100) +
6711bb76ff1Sjsg 					((uint64_t)phantom_timing->h_total * 1000000 - 1)), ((uint64_t)phantom_timing->h_total * 1000000));
6721bb76ff1Sjsg 	pipe_data->pipe_config.subvp_data.processing_delay_lines =
6731bb76ff1Sjsg 			div64_u64(((uint64_t)(dc->caps.subvp_fw_processing_delay_us) * ((uint64_t)phantom_timing->pix_clk_100hz * 100) +
6741bb76ff1Sjsg 					((uint64_t)phantom_timing->h_total * 1000000 - 1)), ((uint64_t)phantom_timing->h_total * 1000000));
6751bb76ff1Sjsg 
6761bb76ff1Sjsg 	if (subvp_pipe->bottom_pipe) {
6771bb76ff1Sjsg 		pipe_data->pipe_config.subvp_data.main_split_pipe_index = subvp_pipe->bottom_pipe->pipe_idx;
6781bb76ff1Sjsg 	} else if (subvp_pipe->next_odm_pipe) {
6791bb76ff1Sjsg 		pipe_data->pipe_config.subvp_data.main_split_pipe_index = subvp_pipe->next_odm_pipe->pipe_idx;
6801bb76ff1Sjsg 	} else {
6811bb76ff1Sjsg 		pipe_data->pipe_config.subvp_data.main_split_pipe_index = 0;
6821bb76ff1Sjsg 	}
6831bb76ff1Sjsg 
6841bb76ff1Sjsg 	// Find phantom pipe index based on phantom stream
6851bb76ff1Sjsg 	for (j = 0; j < dc->res_pool->pipe_count; j++) {
6861bb76ff1Sjsg 		struct pipe_ctx *phantom_pipe = &context->res_ctx.pipe_ctx[j];
6871bb76ff1Sjsg 
6881bb76ff1Sjsg 		if (phantom_pipe->stream == subvp_pipe->stream->mall_stream_config.paired_stream) {
689*f005ef32Sjsg 			pipe_data->pipe_config.subvp_data.phantom_pipe_index = phantom_pipe->stream_res.tg->inst;
6901bb76ff1Sjsg 			if (phantom_pipe->bottom_pipe) {
691*f005ef32Sjsg 				pipe_data->pipe_config.subvp_data.phantom_split_pipe_index = phantom_pipe->bottom_pipe->plane_res.hubp->inst;
6921bb76ff1Sjsg 			} else if (phantom_pipe->next_odm_pipe) {
693*f005ef32Sjsg 				pipe_data->pipe_config.subvp_data.phantom_split_pipe_index = phantom_pipe->next_odm_pipe->plane_res.hubp->inst;
6941bb76ff1Sjsg 			} else {
6951bb76ff1Sjsg 				pipe_data->pipe_config.subvp_data.phantom_split_pipe_index = 0;
6961bb76ff1Sjsg 			}
6971bb76ff1Sjsg 			break;
6981bb76ff1Sjsg 		}
6991bb76ff1Sjsg 	}
7001bb76ff1Sjsg }
7011bb76ff1Sjsg 
7021bb76ff1Sjsg /**
703*f005ef32Sjsg  * dc_dmub_setup_subvp_dmub_command - Populate the DMCUB SubVP command
7041bb76ff1Sjsg  *
705*f005ef32Sjsg  * @dc: [in] current dc state
706*f005ef32Sjsg  * @context: [in] new dc state
707*f005ef32Sjsg  * @enable: [in] if true enables the pipes population
7081bb76ff1Sjsg  *
709*f005ef32Sjsg  * This function loops through each pipe and populates the DMUB SubVP CMD info
710*f005ef32Sjsg  * based on the pipe (e.g. SubVP, VBLANK).
7111bb76ff1Sjsg  */
dc_dmub_setup_subvp_dmub_command(struct dc * dc,struct dc_state * context,bool enable)7121bb76ff1Sjsg void dc_dmub_setup_subvp_dmub_command(struct dc *dc,
7131bb76ff1Sjsg 		struct dc_state *context,
7141bb76ff1Sjsg 		bool enable)
7151bb76ff1Sjsg {
7161bb76ff1Sjsg 	uint8_t cmd_pipe_index = 0;
7171bb76ff1Sjsg 	uint32_t i, pipe_idx;
7181bb76ff1Sjsg 	uint8_t subvp_count = 0;
7191bb76ff1Sjsg 	union dmub_rb_cmd cmd;
7201bb76ff1Sjsg 	struct pipe_ctx *subvp_pipes[2];
7211bb76ff1Sjsg 	uint32_t wm_val_refclk = 0;
7221bb76ff1Sjsg 
7231bb76ff1Sjsg 	memset(&cmd, 0, sizeof(cmd));
7241bb76ff1Sjsg 	// FW command for SUBVP
7251bb76ff1Sjsg 	cmd.fw_assisted_mclk_switch_v2.header.type = DMUB_CMD__FW_ASSISTED_MCLK_SWITCH;
7261bb76ff1Sjsg 	cmd.fw_assisted_mclk_switch_v2.header.sub_type = DMUB_CMD__HANDLE_SUBVP_CMD;
7271bb76ff1Sjsg 	cmd.fw_assisted_mclk_switch_v2.header.payload_bytes =
7281bb76ff1Sjsg 			sizeof(cmd.fw_assisted_mclk_switch_v2) - sizeof(cmd.fw_assisted_mclk_switch_v2.header);
7291bb76ff1Sjsg 
7301bb76ff1Sjsg 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
7311bb76ff1Sjsg 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
7321bb76ff1Sjsg 
7331bb76ff1Sjsg 		/* For SubVP pipe count, only count the top most (ODM / MPC) pipe
7341bb76ff1Sjsg 		 */
735*f005ef32Sjsg 		if (resource_is_pipe_type(pipe, OTG_MASTER) &&
736*f005ef32Sjsg 				resource_is_pipe_type(pipe, DPP_PIPE) &&
7371bb76ff1Sjsg 				pipe->stream->mall_stream_config.type == SUBVP_MAIN)
7381bb76ff1Sjsg 			subvp_pipes[subvp_count++] = pipe;
7391bb76ff1Sjsg 	}
7401bb76ff1Sjsg 
7411bb76ff1Sjsg 	if (enable) {
7421bb76ff1Sjsg 		// For each pipe that is a "main" SUBVP pipe, fill in pipe data for DMUB SUBVP cmd
7431bb76ff1Sjsg 		for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
7441bb76ff1Sjsg 			struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
7451bb76ff1Sjsg 
7461bb76ff1Sjsg 			if (!pipe->stream)
7471bb76ff1Sjsg 				continue;
7481bb76ff1Sjsg 
7491bb76ff1Sjsg 			/* When populating subvp cmd info, only pass in the top most (ODM / MPC) pipe.
7501bb76ff1Sjsg 			 * Any ODM or MPC splits being used in SubVP will be handled internally in
7511bb76ff1Sjsg 			 * populate_subvp_cmd_pipe_info
7521bb76ff1Sjsg 			 */
753*f005ef32Sjsg 			if (resource_is_pipe_type(pipe, OTG_MASTER) &&
754*f005ef32Sjsg 					resource_is_pipe_type(pipe, DPP_PIPE) &&
755*f005ef32Sjsg 					pipe->stream->mall_stream_config.paired_stream &&
7561bb76ff1Sjsg 					pipe->stream->mall_stream_config.type == SUBVP_MAIN) {
7571bb76ff1Sjsg 				populate_subvp_cmd_pipe_info(dc, context, &cmd, pipe, cmd_pipe_index++);
758*f005ef32Sjsg 			} else if (resource_is_pipe_type(pipe, OTG_MASTER) &&
759*f005ef32Sjsg 					resource_is_pipe_type(pipe, DPP_PIPE) &&
760*f005ef32Sjsg 					pipe->stream->mall_stream_config.type == SUBVP_NONE) {
7611bb76ff1Sjsg 				// Don't need to check for ActiveDRAMClockChangeMargin < 0, not valid in cases where
7621bb76ff1Sjsg 				// we run through DML without calculating "natural" P-state support
7631bb76ff1Sjsg 				populate_subvp_cmd_vblank_pipe_info(dc, context, &cmd, pipe, cmd_pipe_index++);
7641bb76ff1Sjsg 
7651bb76ff1Sjsg 			}
7661bb76ff1Sjsg 			pipe_idx++;
7671bb76ff1Sjsg 		}
7681bb76ff1Sjsg 		if (subvp_count == 2) {
7691bb76ff1Sjsg 			update_subvp_prefetch_end_to_mall_start(dc, context, &cmd, subvp_pipes);
7701bb76ff1Sjsg 		}
7711bb76ff1Sjsg 		cmd.fw_assisted_mclk_switch_v2.config_data.pstate_allow_width_us = dc->caps.subvp_pstate_allow_width_us;
7721bb76ff1Sjsg 		cmd.fw_assisted_mclk_switch_v2.config_data.vertical_int_margin_us = dc->caps.subvp_vertical_int_margin_us;
7731bb76ff1Sjsg 
7741bb76ff1Sjsg 		// Store the original watermark value for this SubVP config so we can lower it when the
7751bb76ff1Sjsg 		// MCLK switch starts
7761bb76ff1Sjsg 		wm_val_refclk = context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns *
7771bb76ff1Sjsg 				(dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000) / 1000;
7781bb76ff1Sjsg 
7791bb76ff1Sjsg 		cmd.fw_assisted_mclk_switch_v2.config_data.watermark_a_cache = wm_val_refclk < 0xFFFF ? wm_val_refclk : 0xFFFF;
7801bb76ff1Sjsg 	}
781*f005ef32Sjsg 
782*f005ef32Sjsg 	dm_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
7831bb76ff1Sjsg }
7841bb76ff1Sjsg 
dc_dmub_srv_get_diagnostic_data(struct dc_dmub_srv * dc_dmub_srv,struct dmub_diagnostic_data * diag_data)7855ca02815Sjsg bool dc_dmub_srv_get_diagnostic_data(struct dc_dmub_srv *dc_dmub_srv, struct dmub_diagnostic_data *diag_data)
7865ca02815Sjsg {
7875ca02815Sjsg 	if (!dc_dmub_srv || !dc_dmub_srv->dmub || !diag_data)
7885ca02815Sjsg 		return false;
7895ca02815Sjsg 	return dmub_srv_get_diagnostic_data(dc_dmub_srv->dmub, diag_data);
7905ca02815Sjsg }
7915ca02815Sjsg 
dc_dmub_srv_log_diagnostic_data(struct dc_dmub_srv * dc_dmub_srv)7925ca02815Sjsg void dc_dmub_srv_log_diagnostic_data(struct dc_dmub_srv *dc_dmub_srv)
7935ca02815Sjsg {
7945ca02815Sjsg 	struct dmub_diagnostic_data diag_data = {0};
7955ca02815Sjsg 
7965ca02815Sjsg 	if (!dc_dmub_srv || !dc_dmub_srv->dmub) {
7975ca02815Sjsg 		DC_LOG_ERROR("%s: invalid parameters.", __func__);
7985ca02815Sjsg 		return;
7995ca02815Sjsg 	}
8005ca02815Sjsg 
8015ca02815Sjsg 	if (!dc_dmub_srv_get_diagnostic_data(dc_dmub_srv, &diag_data)) {
8025ca02815Sjsg 		DC_LOG_ERROR("%s: dc_dmub_srv_get_diagnostic_data failed.", __func__);
8035ca02815Sjsg 		return;
8045ca02815Sjsg 	}
8055ca02815Sjsg 
806*f005ef32Sjsg 	DC_LOG_DEBUG("DMCUB STATE:");
807*f005ef32Sjsg 	DC_LOG_DEBUG("    dmcub_version      : %08x", diag_data.dmcub_version);
808*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [0]       : %08x", diag_data.scratch[0]);
809*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [1]       : %08x", diag_data.scratch[1]);
810*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [2]       : %08x", diag_data.scratch[2]);
811*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [3]       : %08x", diag_data.scratch[3]);
812*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [4]       : %08x", diag_data.scratch[4]);
813*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [5]       : %08x", diag_data.scratch[5]);
814*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [6]       : %08x", diag_data.scratch[6]);
815*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [7]       : %08x", diag_data.scratch[7]);
816*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [8]       : %08x", diag_data.scratch[8]);
817*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch  [9]       : %08x", diag_data.scratch[9]);
818*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch [10]       : %08x", diag_data.scratch[10]);
819*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch [11]       : %08x", diag_data.scratch[11]);
820*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch [12]       : %08x", diag_data.scratch[12]);
821*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch [13]       : %08x", diag_data.scratch[13]);
822*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch [14]       : %08x", diag_data.scratch[14]);
823*f005ef32Sjsg 	DC_LOG_DEBUG("    scratch [15]       : %08x", diag_data.scratch[15]);
824*f005ef32Sjsg 	DC_LOG_DEBUG("    pc                 : %08x", diag_data.pc);
825*f005ef32Sjsg 	DC_LOG_DEBUG("    unk_fault_addr     : %08x", diag_data.undefined_address_fault_addr);
826*f005ef32Sjsg 	DC_LOG_DEBUG("    inst_fault_addr    : %08x", diag_data.inst_fetch_fault_addr);
827*f005ef32Sjsg 	DC_LOG_DEBUG("    data_fault_addr    : %08x", diag_data.data_write_fault_addr);
828*f005ef32Sjsg 	DC_LOG_DEBUG("    inbox1_rptr        : %08x", diag_data.inbox1_rptr);
829*f005ef32Sjsg 	DC_LOG_DEBUG("    inbox1_wptr        : %08x", diag_data.inbox1_wptr);
830*f005ef32Sjsg 	DC_LOG_DEBUG("    inbox1_size        : %08x", diag_data.inbox1_size);
831*f005ef32Sjsg 	DC_LOG_DEBUG("    inbox0_rptr        : %08x", diag_data.inbox0_rptr);
832*f005ef32Sjsg 	DC_LOG_DEBUG("    inbox0_wptr        : %08x", diag_data.inbox0_wptr);
833*f005ef32Sjsg 	DC_LOG_DEBUG("    inbox0_size        : %08x", diag_data.inbox0_size);
834*f005ef32Sjsg 	DC_LOG_DEBUG("    is_enabled         : %d", diag_data.is_dmcub_enabled);
835*f005ef32Sjsg 	DC_LOG_DEBUG("    is_soft_reset      : %d", diag_data.is_dmcub_soft_reset);
836*f005ef32Sjsg 	DC_LOG_DEBUG("    is_secure_reset    : %d", diag_data.is_dmcub_secure_reset);
837*f005ef32Sjsg 	DC_LOG_DEBUG("    is_traceport_en    : %d", diag_data.is_traceport_en);
838*f005ef32Sjsg 	DC_LOG_DEBUG("    is_cw0_en          : %d", diag_data.is_cw0_enabled);
839*f005ef32Sjsg 	DC_LOG_DEBUG("    is_cw6_en          : %d", diag_data.is_cw6_enabled);
840*f005ef32Sjsg }
841*f005ef32Sjsg 
dc_can_pipe_disable_cursor(struct pipe_ctx * pipe_ctx)842*f005ef32Sjsg static bool dc_can_pipe_disable_cursor(struct pipe_ctx *pipe_ctx)
843*f005ef32Sjsg {
844*f005ef32Sjsg 	struct pipe_ctx *test_pipe, *split_pipe;
845*f005ef32Sjsg 	const struct scaler_data *scl_data = &pipe_ctx->plane_res.scl_data;
846*f005ef32Sjsg 	struct rect r1 = scl_data->recout, r2, r2_half;
847*f005ef32Sjsg 	int r1_r = r1.x + r1.width, r1_b = r1.y + r1.height, r2_r, r2_b;
848*f005ef32Sjsg 	int cur_layer = pipe_ctx->plane_state->layer_index;
849*f005ef32Sjsg 
850*f005ef32Sjsg 	/**
851*f005ef32Sjsg 	 * Disable the cursor if there's another pipe above this with a
852*f005ef32Sjsg 	 * plane that contains this pipe's viewport to prevent double cursor
853*f005ef32Sjsg 	 * and incorrect scaling artifacts.
854*f005ef32Sjsg 	 */
855*f005ef32Sjsg 	for (test_pipe = pipe_ctx->top_pipe; test_pipe;
856*f005ef32Sjsg 	     test_pipe = test_pipe->top_pipe) {
857*f005ef32Sjsg 		// Skip invisible layer and pipe-split plane on same layer
858*f005ef32Sjsg 		if (!test_pipe->plane_state->visible || test_pipe->plane_state->layer_index == cur_layer)
859*f005ef32Sjsg 			continue;
860*f005ef32Sjsg 
861*f005ef32Sjsg 		r2 = test_pipe->plane_res.scl_data.recout;
862*f005ef32Sjsg 		r2_r = r2.x + r2.width;
863*f005ef32Sjsg 		r2_b = r2.y + r2.height;
864*f005ef32Sjsg 		split_pipe = test_pipe;
865*f005ef32Sjsg 
866*f005ef32Sjsg 		/**
867*f005ef32Sjsg 		 * There is another half plane on same layer because of
868*f005ef32Sjsg 		 * pipe-split, merge together per same height.
869*f005ef32Sjsg 		 */
870*f005ef32Sjsg 		for (split_pipe = pipe_ctx->top_pipe; split_pipe;
871*f005ef32Sjsg 		     split_pipe = split_pipe->top_pipe)
872*f005ef32Sjsg 			if (split_pipe->plane_state->layer_index == test_pipe->plane_state->layer_index) {
873*f005ef32Sjsg 				r2_half = split_pipe->plane_res.scl_data.recout;
874*f005ef32Sjsg 				r2.x = (r2_half.x < r2.x) ? r2_half.x : r2.x;
875*f005ef32Sjsg 				r2.width = r2.width + r2_half.width;
876*f005ef32Sjsg 				r2_r = r2.x + r2.width;
877*f005ef32Sjsg 				break;
878*f005ef32Sjsg 			}
879*f005ef32Sjsg 
880*f005ef32Sjsg 		if (r1.x >= r2.x && r1.y >= r2.y && r1_r <= r2_r && r1_b <= r2_b)
881*f005ef32Sjsg 			return true;
882*f005ef32Sjsg 	}
883*f005ef32Sjsg 
884*f005ef32Sjsg 	return false;
8855ca02815Sjsg }
8861bb76ff1Sjsg 
dc_dmub_should_update_cursor_data(struct pipe_ctx * pipe_ctx)8871bb76ff1Sjsg static bool dc_dmub_should_update_cursor_data(struct pipe_ctx *pipe_ctx)
8881bb76ff1Sjsg {
8891bb76ff1Sjsg 	if (pipe_ctx->plane_state != NULL) {
8901bb76ff1Sjsg 		if (pipe_ctx->plane_state->address.type == PLN_ADDR_TYPE_VIDEO_PROGRESSIVE)
8911bb76ff1Sjsg 			return false;
892*f005ef32Sjsg 
893*f005ef32Sjsg 		if (dc_can_pipe_disable_cursor(pipe_ctx))
894*f005ef32Sjsg 			return false;
8951bb76ff1Sjsg 	}
8961bb76ff1Sjsg 
8971bb76ff1Sjsg 	if ((pipe_ctx->stream->link->psr_settings.psr_version == DC_PSR_VERSION_SU_1 ||
8981bb76ff1Sjsg 		pipe_ctx->stream->link->psr_settings.psr_version == DC_PSR_VERSION_1) &&
8991bb76ff1Sjsg 		pipe_ctx->stream->ctx->dce_version >= DCN_VERSION_3_1)
9001bb76ff1Sjsg 		return true;
9011bb76ff1Sjsg 
902*f005ef32Sjsg 	if (pipe_ctx->stream->link->replay_settings.config.replay_supported)
903*f005ef32Sjsg 		return true;
904*f005ef32Sjsg 
9051bb76ff1Sjsg 	return false;
9061bb76ff1Sjsg }
9071bb76ff1Sjsg 
dc_build_cursor_update_payload0(struct pipe_ctx * pipe_ctx,uint8_t p_idx,struct dmub_cmd_update_cursor_payload0 * payload)9081bb76ff1Sjsg static void dc_build_cursor_update_payload0(
9091bb76ff1Sjsg 		struct pipe_ctx *pipe_ctx, uint8_t p_idx,
9101bb76ff1Sjsg 		struct dmub_cmd_update_cursor_payload0 *payload)
9111bb76ff1Sjsg {
9121bb76ff1Sjsg 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
9131bb76ff1Sjsg 	unsigned int panel_inst = 0;
9141bb76ff1Sjsg 
9151bb76ff1Sjsg 	if (!dc_get_edp_link_panel_inst(hubp->ctx->dc,
9161bb76ff1Sjsg 		pipe_ctx->stream->link, &panel_inst))
9171bb76ff1Sjsg 		return;
9181bb76ff1Sjsg 
9191bb76ff1Sjsg 	/* Payload: Cursor Rect is built from position & attribute
9201bb76ff1Sjsg 	 * x & y are obtained from postion
9211bb76ff1Sjsg 	 */
9221bb76ff1Sjsg 	payload->cursor_rect.x = hubp->cur_rect.x;
9231bb76ff1Sjsg 	payload->cursor_rect.y = hubp->cur_rect.y;
9241bb76ff1Sjsg 	/* w & h are obtained from attribute */
9251bb76ff1Sjsg 	payload->cursor_rect.width  = hubp->cur_rect.w;
9261bb76ff1Sjsg 	payload->cursor_rect.height = hubp->cur_rect.h;
9271bb76ff1Sjsg 
9281bb76ff1Sjsg 	payload->enable      = hubp->pos.cur_ctl.bits.cur_enable;
9291bb76ff1Sjsg 	payload->pipe_idx    = p_idx;
9301bb76ff1Sjsg 	payload->cmd_version = DMUB_CMD_PSR_CONTROL_VERSION_1;
9311bb76ff1Sjsg 	payload->panel_inst  = panel_inst;
9321bb76ff1Sjsg }
9331bb76ff1Sjsg 
dc_build_cursor_position_update_payload0(struct dmub_cmd_update_cursor_payload0 * pl,const uint8_t p_idx,const struct hubp * hubp,const struct dpp * dpp)9341bb76ff1Sjsg static void dc_build_cursor_position_update_payload0(
9351bb76ff1Sjsg 		struct dmub_cmd_update_cursor_payload0 *pl, const uint8_t p_idx,
9361bb76ff1Sjsg 		const struct hubp *hubp, const struct dpp *dpp)
9371bb76ff1Sjsg {
9381bb76ff1Sjsg 	/* Hubp */
9391bb76ff1Sjsg 	pl->position_cfg.pHubp.cur_ctl.raw  = hubp->pos.cur_ctl.raw;
9401bb76ff1Sjsg 	pl->position_cfg.pHubp.position.raw = hubp->pos.position.raw;
9411bb76ff1Sjsg 	pl->position_cfg.pHubp.hot_spot.raw = hubp->pos.hot_spot.raw;
9421bb76ff1Sjsg 	pl->position_cfg.pHubp.dst_offset.raw = hubp->pos.dst_offset.raw;
9431bb76ff1Sjsg 
9441bb76ff1Sjsg 	/* dpp */
9451bb76ff1Sjsg 	pl->position_cfg.pDpp.cur0_ctl.raw = dpp->pos.cur0_ctl.raw;
9461bb76ff1Sjsg 	pl->position_cfg.pipe_idx = p_idx;
9471bb76ff1Sjsg }
9481bb76ff1Sjsg 
dc_build_cursor_attribute_update_payload1(struct dmub_cursor_attributes_cfg * pl_A,const uint8_t p_idx,const struct hubp * hubp,const struct dpp * dpp)9491bb76ff1Sjsg static void dc_build_cursor_attribute_update_payload1(
9501bb76ff1Sjsg 		struct dmub_cursor_attributes_cfg *pl_A, const uint8_t p_idx,
9511bb76ff1Sjsg 		const struct hubp *hubp, const struct dpp *dpp)
9521bb76ff1Sjsg {
9531bb76ff1Sjsg 	/* Hubp */
9541bb76ff1Sjsg 	pl_A->aHubp.SURFACE_ADDR_HIGH = hubp->att.SURFACE_ADDR_HIGH;
9551bb76ff1Sjsg 	pl_A->aHubp.SURFACE_ADDR = hubp->att.SURFACE_ADDR;
9561bb76ff1Sjsg 	pl_A->aHubp.cur_ctl.raw  = hubp->att.cur_ctl.raw;
9571bb76ff1Sjsg 	pl_A->aHubp.size.raw     = hubp->att.size.raw;
9581bb76ff1Sjsg 	pl_A->aHubp.settings.raw = hubp->att.settings.raw;
9591bb76ff1Sjsg 
9601bb76ff1Sjsg 	/* dpp */
9611bb76ff1Sjsg 	pl_A->aDpp.cur0_ctl.raw = dpp->att.cur0_ctl.raw;
9621bb76ff1Sjsg }
9631bb76ff1Sjsg 
9641bb76ff1Sjsg /**
965*f005ef32Sjsg  * dc_send_update_cursor_info_to_dmu - Populate the DMCUB Cursor update info command
9661bb76ff1Sjsg  *
967*f005ef32Sjsg  * @pCtx: [in] pipe context
968*f005ef32Sjsg  * @pipe_idx: [in] pipe index
9691bb76ff1Sjsg  *
970*f005ef32Sjsg  * This function would store the cursor related information and pass it into
971*f005ef32Sjsg  * dmub
9721bb76ff1Sjsg  */
dc_send_update_cursor_info_to_dmu(struct pipe_ctx * pCtx,uint8_t pipe_idx)9731bb76ff1Sjsg void dc_send_update_cursor_info_to_dmu(
9741bb76ff1Sjsg 		struct pipe_ctx *pCtx, uint8_t pipe_idx)
9751bb76ff1Sjsg {
976*f005ef32Sjsg 	union dmub_rb_cmd cmd[2];
977*f005ef32Sjsg 	union dmub_cmd_update_cursor_info_data *update_cursor_info_0 =
978*f005ef32Sjsg 					&cmd[0].update_cursor_info.update_cursor_info_data;
979*f005ef32Sjsg 
980*f005ef32Sjsg 	memset(cmd, 0, sizeof(cmd));
9811bb76ff1Sjsg 
9821bb76ff1Sjsg 	if (!dc_dmub_should_update_cursor_data(pCtx))
9831bb76ff1Sjsg 		return;
9841bb76ff1Sjsg 	/*
9851bb76ff1Sjsg 	 * Since we use multi_cmd_pending for dmub command, the 2nd command is
9861bb76ff1Sjsg 	 * only assigned to store cursor attributes info.
9871bb76ff1Sjsg 	 * 1st command can view as 2 parts, 1st is for PSR/Replay data, the other
9881bb76ff1Sjsg 	 * is to store cursor position info.
9891bb76ff1Sjsg 	 *
9901bb76ff1Sjsg 	 * Command heaer type must be the same type if using  multi_cmd_pending.
9911bb76ff1Sjsg 	 * Besides, while process 2nd command in DMU, the sub type is useless.
9921bb76ff1Sjsg 	 * So it's meanless to pass the sub type header with different type.
9931bb76ff1Sjsg 	 */
9941bb76ff1Sjsg 
9951bb76ff1Sjsg 	{
9961bb76ff1Sjsg 		/* Build Payload#0 Header */
997*f005ef32Sjsg 		cmd[0].update_cursor_info.header.type = DMUB_CMD__UPDATE_CURSOR_INFO;
998*f005ef32Sjsg 		cmd[0].update_cursor_info.header.payload_bytes =
999*f005ef32Sjsg 				sizeof(cmd[0].update_cursor_info.update_cursor_info_data);
1000*f005ef32Sjsg 		cmd[0].update_cursor_info.header.multi_cmd_pending = 1; //To combine multi dmu cmd, 1st cmd
10011bb76ff1Sjsg 
10021bb76ff1Sjsg 		/* Prepare Payload */
1003*f005ef32Sjsg 		dc_build_cursor_update_payload0(pCtx, pipe_idx, &update_cursor_info_0->payload0);
10041bb76ff1Sjsg 
1005*f005ef32Sjsg 		dc_build_cursor_position_update_payload0(&update_cursor_info_0->payload0, pipe_idx,
10061bb76ff1Sjsg 				pCtx->plane_res.hubp, pCtx->plane_res.dpp);
10071bb76ff1Sjsg 		}
10081bb76ff1Sjsg 	{
10091bb76ff1Sjsg 		/* Build Payload#1 Header */
1010*f005ef32Sjsg 		cmd[1].update_cursor_info.header.type = DMUB_CMD__UPDATE_CURSOR_INFO;
1011*f005ef32Sjsg 		cmd[1].update_cursor_info.header.payload_bytes = sizeof(struct cursor_attributes_cfg);
1012*f005ef32Sjsg 		cmd[1].update_cursor_info.header.multi_cmd_pending = 0; //Indicate it's the last command.
10131bb76ff1Sjsg 
10141bb76ff1Sjsg 		dc_build_cursor_attribute_update_payload1(
1015*f005ef32Sjsg 				&cmd[1].update_cursor_info.update_cursor_info_data.payload1.attribute_cfg,
10161bb76ff1Sjsg 				pipe_idx, pCtx->plane_res.hubp, pCtx->plane_res.dpp);
10171bb76ff1Sjsg 
10181bb76ff1Sjsg 		/* Combine 2nd cmds update_curosr_info to DMU */
1019*f005ef32Sjsg 		dm_execute_dmub_cmd_list(pCtx->stream->ctx, 2, cmd, DM_DMUB_WAIT_TYPE_WAIT);
10201bb76ff1Sjsg 	}
10211bb76ff1Sjsg }
10225dce7169Sjsg 
dc_dmub_check_min_version(struct dmub_srv * srv)10235dce7169Sjsg bool dc_dmub_check_min_version(struct dmub_srv *srv)
10245dce7169Sjsg {
10255dce7169Sjsg 	if (!srv->hw_funcs.is_psrsu_supported)
10265dce7169Sjsg 		return true;
10275dce7169Sjsg 	return srv->hw_funcs.is_psrsu_supported(srv);
10285dce7169Sjsg }
1029*f005ef32Sjsg 
dc_dmub_srv_enable_dpia_trace(const struct dc * dc)1030*f005ef32Sjsg void dc_dmub_srv_enable_dpia_trace(const struct dc *dc)
1031*f005ef32Sjsg {
1032*f005ef32Sjsg 	struct dc_dmub_srv *dc_dmub_srv = dc->ctx->dmub_srv;
1033*f005ef32Sjsg 	struct dmub_srv *dmub;
1034*f005ef32Sjsg 	enum dmub_status status;
1035*f005ef32Sjsg 	static const uint32_t timeout_us = 30;
1036*f005ef32Sjsg 
1037*f005ef32Sjsg 	if (!dc_dmub_srv || !dc_dmub_srv->dmub) {
1038*f005ef32Sjsg 		DC_LOG_ERROR("%s: invalid parameters.", __func__);
1039*f005ef32Sjsg 		return;
1040*f005ef32Sjsg 	}
1041*f005ef32Sjsg 
1042*f005ef32Sjsg 	dmub = dc_dmub_srv->dmub;
1043*f005ef32Sjsg 
1044*f005ef32Sjsg 	status = dmub_srv_send_gpint_command(dmub, DMUB_GPINT__SET_TRACE_BUFFER_MASK_WORD1, 0x0010, timeout_us);
1045*f005ef32Sjsg 	if (status != DMUB_STATUS_OK) {
1046*f005ef32Sjsg 		DC_LOG_ERROR("timeout updating trace buffer mask word\n");
1047*f005ef32Sjsg 		return;
1048*f005ef32Sjsg 	}
1049*f005ef32Sjsg 
1050*f005ef32Sjsg 	status = dmub_srv_send_gpint_command(dmub, DMUB_GPINT__UPDATE_TRACE_BUFFER_MASK, 0x0000, timeout_us);
1051*f005ef32Sjsg 	if (status != DMUB_STATUS_OK) {
1052*f005ef32Sjsg 		DC_LOG_ERROR("timeout updating trace buffer mask word\n");
1053*f005ef32Sjsg 		return;
1054*f005ef32Sjsg 	}
1055*f005ef32Sjsg 
1056*f005ef32Sjsg 	DC_LOG_DEBUG("Enabled DPIA trace\n");
1057*f005ef32Sjsg }