1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: AMD
24  *
25  */
26 #include "dcn32_fpu.h"
27 #include "dcn32/dcn32_resource.h"
28 #include "dcn20/dcn20_resource.h"
29 #include "display_mode_vba_util_32.h"
30 #include "dml/dcn32/display_mode_vba_32.h"
31 // We need this includes for WATERMARKS_* defines
32 #include "clk_mgr/dcn32/dcn32_smu13_driver_if.h"
33 #include "dcn30/dcn30_resource.h"
34 #include "link.h"
35 
36 #define DC_LOGGER_INIT(logger)
37 
38 struct _vcs_dpi_ip_params_st dcn3_2_ip = {
39 	.gpuvm_enable = 0,
40 	.gpuvm_max_page_table_levels = 4,
41 	.hostvm_enable = 0,
42 	.rob_buffer_size_kbytes = 128,
43 	.det_buffer_size_kbytes = DCN3_2_DEFAULT_DET_SIZE,
44 	.config_return_buffer_size_in_kbytes = 1280,
45 	.compressed_buffer_segment_size_in_kbytes = 64,
46 	.meta_fifo_size_in_kentries = 22,
47 	.zero_size_buffer_entries = 512,
48 	.compbuf_reserved_space_64b = 256,
49 	.compbuf_reserved_space_zs = 64,
50 	.dpp_output_buffer_pixels = 2560,
51 	.opp_output_buffer_lines = 1,
52 	.pixel_chunk_size_kbytes = 8,
53 	.alpha_pixel_chunk_size_kbytes = 4,
54 	.min_pixel_chunk_size_bytes = 1024,
55 	.dcc_meta_buffer_size_bytes = 6272,
56 	.meta_chunk_size_kbytes = 2,
57 	.min_meta_chunk_size_bytes = 256,
58 	.writeback_chunk_size_kbytes = 8,
59 	.ptoi_supported = false,
60 	.num_dsc = 4,
61 	.maximum_dsc_bits_per_component = 12,
62 	.maximum_pixels_per_line_per_dsc_unit = 6016,
63 	.dsc422_native_support = true,
64 	.is_line_buffer_bpp_fixed = true,
65 	.line_buffer_fixed_bpp = 57,
66 	.line_buffer_size_bits = 1171920,
67 	.max_line_buffer_lines = 32,
68 	.writeback_interface_buffer_size_kbytes = 90,
69 	.max_num_dpp = 4,
70 	.max_num_otg = 4,
71 	.max_num_hdmi_frl_outputs = 1,
72 	.max_num_wb = 1,
73 	.max_dchub_pscl_bw_pix_per_clk = 4,
74 	.max_pscl_lb_bw_pix_per_clk = 2,
75 	.max_lb_vscl_bw_pix_per_clk = 4,
76 	.max_vscl_hscl_bw_pix_per_clk = 4,
77 	.max_hscl_ratio = 6,
78 	.max_vscl_ratio = 6,
79 	.max_hscl_taps = 8,
80 	.max_vscl_taps = 8,
81 	.dpte_buffer_size_in_pte_reqs_luma = 64,
82 	.dpte_buffer_size_in_pte_reqs_chroma = 34,
83 	.dispclk_ramp_margin_percent = 1,
84 	.max_inter_dcn_tile_repeaters = 8,
85 	.cursor_buffer_size = 16,
86 	.cursor_chunk_size = 2,
87 	.writeback_line_buffer_buffer_size = 0,
88 	.writeback_min_hscl_ratio = 1,
89 	.writeback_min_vscl_ratio = 1,
90 	.writeback_max_hscl_ratio = 1,
91 	.writeback_max_vscl_ratio = 1,
92 	.writeback_max_hscl_taps = 1,
93 	.writeback_max_vscl_taps = 1,
94 	.dppclk_delay_subtotal = 47,
95 	.dppclk_delay_scl = 50,
96 	.dppclk_delay_scl_lb_only = 16,
97 	.dppclk_delay_cnvc_formatter = 28,
98 	.dppclk_delay_cnvc_cursor = 6,
99 	.dispclk_delay_subtotal = 125,
100 	.dynamic_metadata_vm_enabled = false,
101 	.odm_combine_4to1_supported = false,
102 	.dcc_supported = true,
103 	.max_num_dp2p0_outputs = 2,
104 	.max_num_dp2p0_streams = 4,
105 };
106 
107 struct _vcs_dpi_soc_bounding_box_st dcn3_2_soc = {
108 	.clock_limits = {
109 		{
110 			.state = 0,
111 			.dcfclk_mhz = 1564.0,
112 			.fabricclk_mhz = 400.0,
113 			.dispclk_mhz = 2150.0,
114 			.dppclk_mhz = 2150.0,
115 			.phyclk_mhz = 810.0,
116 			.phyclk_d18_mhz = 667.0,
117 			.phyclk_d32_mhz = 625.0,
118 			.socclk_mhz = 1200.0,
119 			.dscclk_mhz = 716.667,
120 			.dram_speed_mts = 16000.0,
121 			.dtbclk_mhz = 1564.0,
122 		},
123 	},
124 	.num_states = 1,
125 	.sr_exit_time_us = 42.97,
126 	.sr_enter_plus_exit_time_us = 49.94,
127 	.sr_exit_z8_time_us = 285.0,
128 	.sr_enter_plus_exit_z8_time_us = 320,
129 	.writeback_latency_us = 12.0,
130 	.round_trip_ping_latency_dcfclk_cycles = 263,
131 	.urgent_latency_pixel_data_only_us = 4.0,
132 	.urgent_latency_pixel_mixed_with_vm_data_us = 4.0,
133 	.urgent_latency_vm_data_only_us = 4.0,
134 	.fclk_change_latency_us = 20,
135 	.usr_retraining_latency_us = 2,
136 	.smn_latency_us = 2,
137 	.mall_allocated_for_dcn_mbytes = 64,
138 	.urgent_out_of_order_return_per_channel_pixel_only_bytes = 4096,
139 	.urgent_out_of_order_return_per_channel_pixel_and_vm_bytes = 4096,
140 	.urgent_out_of_order_return_per_channel_vm_only_bytes = 4096,
141 	.pct_ideal_sdp_bw_after_urgent = 100.0,
142 	.pct_ideal_fabric_bw_after_urgent = 67.0,
143 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_only = 20.0,
144 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_and_vm = 60.0, // N/A, for now keep as is until DML implemented
145 	.pct_ideal_dram_sdp_bw_after_urgent_vm_only = 30.0, // N/A, for now keep as is until DML implemented
146 	.pct_ideal_dram_bw_after_urgent_strobe = 67.0,
147 	.max_avg_sdp_bw_use_normal_percent = 80.0,
148 	.max_avg_fabric_bw_use_normal_percent = 60.0,
149 	.max_avg_dram_bw_use_normal_strobe_percent = 50.0,
150 	.max_avg_dram_bw_use_normal_percent = 15.0,
151 	.num_chans = 8,
152 	.dram_channel_width_bytes = 2,
153 	.fabric_datapath_to_dcn_data_return_bytes = 64,
154 	.return_bus_width_bytes = 64,
155 	.downspread_percent = 0.38,
156 	.dcn_downspread_percent = 0.5,
157 	.dram_clock_change_latency_us = 400,
158 	.dispclk_dppclk_vco_speed_mhz = 4300.0,
159 	.do_urgent_latency_adjustment = true,
160 	.urgent_latency_adjustment_fabric_clock_component_us = 1.0,
161 	.urgent_latency_adjustment_fabric_clock_reference_mhz = 3000,
162 };
163 
164 void dcn32_build_wm_range_table_fpu(struct clk_mgr_internal *clk_mgr)
165 {
166 	/* defaults */
167 	double pstate_latency_us = clk_mgr->base.ctx->dc->dml.soc.dram_clock_change_latency_us;
168 	double fclk_change_latency_us = clk_mgr->base.ctx->dc->dml.soc.fclk_change_latency_us;
169 	double sr_exit_time_us = clk_mgr->base.ctx->dc->dml.soc.sr_exit_time_us;
170 	double sr_enter_plus_exit_time_us = clk_mgr->base.ctx->dc->dml.soc.sr_enter_plus_exit_time_us;
171 	/* For min clocks use as reported by PM FW and report those as min */
172 	uint16_t min_uclk_mhz			= clk_mgr->base.bw_params->clk_table.entries[0].memclk_mhz;
173 	uint16_t min_dcfclk_mhz			= clk_mgr->base.bw_params->clk_table.entries[0].dcfclk_mhz;
174 	uint16_t setb_min_uclk_mhz		= min_uclk_mhz;
175 	uint16_t dcfclk_mhz_for_the_second_state = clk_mgr->base.ctx->dc->dml.soc.clock_limits[2].dcfclk_mhz;
176 
177 	dc_assert_fp_enabled();
178 
179 	/* For Set B ranges use min clocks state 2 when available, and report those to PM FW */
180 	if (dcfclk_mhz_for_the_second_state)
181 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.min_dcfclk = dcfclk_mhz_for_the_second_state;
182 	else
183 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.min_dcfclk = clk_mgr->base.bw_params->clk_table.entries[0].dcfclk_mhz;
184 
185 	if (clk_mgr->base.bw_params->clk_table.entries[2].memclk_mhz)
186 		setb_min_uclk_mhz = clk_mgr->base.bw_params->clk_table.entries[2].memclk_mhz;
187 
188 	/* Set A - Normal - default values */
189 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].valid = true;
190 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us = pstate_latency_us;
191 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].dml_input.fclk_change_latency_us = fclk_change_latency_us;
192 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].dml_input.sr_exit_time_us = sr_exit_time_us;
193 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].dml_input.sr_enter_plus_exit_time_us = sr_enter_plus_exit_time_us;
194 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.wm_type = WATERMARKS_CLOCK_RANGE;
195 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.min_dcfclk = min_dcfclk_mhz;
196 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.max_dcfclk = 0xFFFF;
197 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.min_uclk = min_uclk_mhz;
198 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.max_uclk = 0xFFFF;
199 
200 	/* Set B - Performance - higher clocks, using DPM[2] DCFCLK and UCLK */
201 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].valid = true;
202 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].dml_input.pstate_latency_us = pstate_latency_us;
203 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].dml_input.fclk_change_latency_us = fclk_change_latency_us;
204 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].dml_input.sr_exit_time_us = sr_exit_time_us;
205 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].dml_input.sr_enter_plus_exit_time_us = sr_enter_plus_exit_time_us;
206 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.wm_type = WATERMARKS_CLOCK_RANGE;
207 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.max_dcfclk = 0xFFFF;
208 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.min_uclk = setb_min_uclk_mhz;
209 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.max_uclk = 0xFFFF;
210 
211 	/* Set C - Dummy P-State - P-State latency set to "dummy p-state" value */
212 	/* 'DalDummyClockChangeLatencyNs' registry key option set to 0x7FFFFFFF can be used to disable Set C for dummy p-state */
213 	if (clk_mgr->base.ctx->dc->bb_overrides.dummy_clock_change_latency_ns != 0x7FFFFFFF) {
214 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].valid = true;
215 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].dml_input.pstate_latency_us = 50;
216 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].dml_input.fclk_change_latency_us = fclk_change_latency_us;
217 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].dml_input.sr_exit_time_us = sr_exit_time_us;
218 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].dml_input.sr_enter_plus_exit_time_us = sr_enter_plus_exit_time_us;
219 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.wm_type = WATERMARKS_DUMMY_PSTATE;
220 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.min_dcfclk = min_dcfclk_mhz;
221 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.max_dcfclk = 0xFFFF;
222 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.min_uclk = min_uclk_mhz;
223 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.max_uclk = 0xFFFF;
224 		clk_mgr->base.bw_params->dummy_pstate_table[0].dram_speed_mts = clk_mgr->base.bw_params->clk_table.entries[0].memclk_mhz * 16;
225 		clk_mgr->base.bw_params->dummy_pstate_table[0].dummy_pstate_latency_us = 50;
226 		clk_mgr->base.bw_params->dummy_pstate_table[1].dram_speed_mts = clk_mgr->base.bw_params->clk_table.entries[1].memclk_mhz * 16;
227 		clk_mgr->base.bw_params->dummy_pstate_table[1].dummy_pstate_latency_us = 9;
228 		clk_mgr->base.bw_params->dummy_pstate_table[2].dram_speed_mts = clk_mgr->base.bw_params->clk_table.entries[2].memclk_mhz * 16;
229 		clk_mgr->base.bw_params->dummy_pstate_table[2].dummy_pstate_latency_us = 8;
230 		clk_mgr->base.bw_params->dummy_pstate_table[3].dram_speed_mts = clk_mgr->base.bw_params->clk_table.entries[3].memclk_mhz * 16;
231 		clk_mgr->base.bw_params->dummy_pstate_table[3].dummy_pstate_latency_us = 5;
232 	}
233 	/* Set D - MALL - SR enter and exit time specific to MALL, TBD after bringup or later phase for now use DRAM values / 2 */
234 	/* For MALL DRAM clock change latency is N/A, for watermak calculations use lowest value dummy P state latency */
235 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].valid = true;
236 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].dml_input.pstate_latency_us = clk_mgr->base.bw_params->dummy_pstate_table[3].dummy_pstate_latency_us;
237 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].dml_input.fclk_change_latency_us = fclk_change_latency_us;
238 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].dml_input.sr_exit_time_us = sr_exit_time_us / 2; // TBD
239 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].dml_input.sr_enter_plus_exit_time_us = sr_enter_plus_exit_time_us / 2; // TBD
240 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.wm_type = WATERMARKS_MALL;
241 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.min_dcfclk = min_dcfclk_mhz;
242 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.max_dcfclk = 0xFFFF;
243 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.min_uclk = min_uclk_mhz;
244 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.max_uclk = 0xFFFF;
245 }
246 
247 /*
248  * Finds dummy_latency_index when MCLK switching using firmware based
249  * vblank stretch is enabled. This function will iterate through the
250  * table of dummy pstate latencies until the lowest value that allows
251  * dm_allow_self_refresh_and_mclk_switch to happen is found
252  */
253 int dcn32_find_dummy_latency_index_for_fw_based_mclk_switch(struct dc *dc,
254 							    struct dc_state *context,
255 							    display_e2e_pipe_params_st *pipes,
256 							    int pipe_cnt,
257 							    int vlevel)
258 {
259 	const int max_latency_table_entries = 4;
260 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
261 	int dummy_latency_index = 0;
262 	enum clock_change_support temp_clock_change_support = vba->DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
263 
264 	dc_assert_fp_enabled();
265 
266 	while (dummy_latency_index < max_latency_table_entries) {
267 		if (temp_clock_change_support != dm_dram_clock_change_unsupported)
268 			vba->DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] = temp_clock_change_support;
269 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
270 				dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
271 		dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel, false);
272 
273 		/* for subvp + DRR case, if subvp pipes are still present we support pstate */
274 		if (vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported &&
275 				dcn32_subvp_in_use(dc, context))
276 			vba->DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] = temp_clock_change_support;
277 
278 		if (vlevel < context->bw_ctx.dml.vba.soc.num_states &&
279 				vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] != dm_dram_clock_change_unsupported)
280 			break;
281 
282 		dummy_latency_index++;
283 	}
284 
285 	if (dummy_latency_index == max_latency_table_entries) {
286 		ASSERT(dummy_latency_index != max_latency_table_entries);
287 		/* If the execution gets here, it means dummy p_states are
288 		 * not possible. This should never happen and would mean
289 		 * something is severely wrong.
290 		 * Here we reset dummy_latency_index to 3, because it is
291 		 * better to have underflows than system crashes.
292 		 */
293 		dummy_latency_index = max_latency_table_entries - 1;
294 	}
295 
296 	return dummy_latency_index;
297 }
298 
299 /**
300  * dcn32_helper_populate_phantom_dlg_params - Get DLG params for phantom pipes
301  * and populate pipe_ctx with those params.
302  * @dc: [in] current dc state
303  * @context: [in] new dc state
304  * @pipes: [in] DML pipe params array
305  * @pipe_cnt: [in] DML pipe count
306  *
307  * This function must be called AFTER the phantom pipes are added to context
308  * and run through DML (so that the DLG params for the phantom pipes can be
309  * populated), and BEFORE we program the timing for the phantom pipes.
310  */
311 void dcn32_helper_populate_phantom_dlg_params(struct dc *dc,
312 					      struct dc_state *context,
313 					      display_e2e_pipe_params_st *pipes,
314 					      int pipe_cnt)
315 {
316 	uint32_t i, pipe_idx;
317 
318 	dc_assert_fp_enabled();
319 
320 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
321 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
322 
323 		if (!pipe->stream)
324 			continue;
325 
326 		if (pipe->plane_state && pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
327 			pipes[pipe_idx].pipe.dest.vstartup_start =
328 				get_vstartup(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
329 			pipes[pipe_idx].pipe.dest.vupdate_offset =
330 				get_vupdate_offset(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
331 			pipes[pipe_idx].pipe.dest.vupdate_width =
332 				get_vupdate_width(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
333 			pipes[pipe_idx].pipe.dest.vready_offset =
334 				get_vready_offset(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
335 			pipe->pipe_dlg_param = pipes[pipe_idx].pipe.dest;
336 		}
337 		pipe_idx++;
338 	}
339 }
340 
341 /**
342  * dcn32_predict_pipe_split - Predict if pipe split will occur for a given DML pipe
343  * @context: [in] New DC state to be programmed
344  * @pipe_e2e: [in] DML pipe end to end context
345  *
346  * This function takes in a DML pipe (pipe_e2e) and predicts if pipe split is required (both
347  * ODM and MPC). For pipe split, ODM combine is determined by the ODM mode, and MPC combine is
348  * determined by DPPClk requirements
349  *
350  * This function follows the same policy as DML:
351  * - Check for ODM combine requirements / policy first
352  * - MPC combine is only chosen if there is no ODM combine requirements / policy in place, and
353  *   MPC is required
354  *
355  * Return: Number of splits expected (1 for 2:1 split, 3 for 4:1 split, 0 for no splits).
356  */
357 uint8_t dcn32_predict_pipe_split(struct dc_state *context,
358 				  display_e2e_pipe_params_st *pipe_e2e)
359 {
360 	double pscl_throughput;
361 	double pscl_throughput_chroma;
362 	double dpp_clk_single_dpp, clock;
363 	double clk_frequency = 0.0;
364 	double vco_speed = context->bw_ctx.dml.soc.dispclk_dppclk_vco_speed_mhz;
365 	bool total_available_pipes_support = false;
366 	uint32_t number_of_dpp = 0;
367 	enum odm_combine_mode odm_mode = dm_odm_combine_mode_disabled;
368 	double req_dispclk_per_surface = 0;
369 	uint8_t num_splits = 0;
370 
371 	dc_assert_fp_enabled();
372 
373 	dml32_CalculateODMMode(context->bw_ctx.dml.ip.maximum_pixels_per_line_per_dsc_unit,
374 			pipe_e2e->pipe.dest.hactive,
375 			pipe_e2e->dout.output_format,
376 			pipe_e2e->dout.output_type,
377 			pipe_e2e->pipe.dest.odm_combine_policy,
378 			context->bw_ctx.dml.soc.clock_limits[context->bw_ctx.dml.soc.num_states - 1].dispclk_mhz,
379 			context->bw_ctx.dml.soc.clock_limits[context->bw_ctx.dml.soc.num_states - 1].dispclk_mhz,
380 			pipe_e2e->dout.dsc_enable != 0,
381 			0, /* TotalNumberOfActiveDPP can be 0 since we're predicting pipe split requirement */
382 			context->bw_ctx.dml.ip.max_num_dpp,
383 			pipe_e2e->pipe.dest.pixel_rate_mhz,
384 			context->bw_ctx.dml.soc.dcn_downspread_percent,
385 			context->bw_ctx.dml.ip.dispclk_ramp_margin_percent,
386 			context->bw_ctx.dml.soc.dispclk_dppclk_vco_speed_mhz,
387 			pipe_e2e->dout.dsc_slices,
388 			/* Output */
389 			&total_available_pipes_support,
390 			&number_of_dpp,
391 			&odm_mode,
392 			&req_dispclk_per_surface);
393 
394 	dml32_CalculateSinglePipeDPPCLKAndSCLThroughput(pipe_e2e->pipe.scale_ratio_depth.hscl_ratio,
395 			pipe_e2e->pipe.scale_ratio_depth.hscl_ratio_c,
396 			pipe_e2e->pipe.scale_ratio_depth.vscl_ratio,
397 			pipe_e2e->pipe.scale_ratio_depth.vscl_ratio_c,
398 			context->bw_ctx.dml.ip.max_dchub_pscl_bw_pix_per_clk,
399 			context->bw_ctx.dml.ip.max_pscl_lb_bw_pix_per_clk,
400 			pipe_e2e->pipe.dest.pixel_rate_mhz,
401 			pipe_e2e->pipe.src.source_format,
402 			pipe_e2e->pipe.scale_taps.htaps,
403 			pipe_e2e->pipe.scale_taps.htaps_c,
404 			pipe_e2e->pipe.scale_taps.vtaps,
405 			pipe_e2e->pipe.scale_taps.vtaps_c,
406 			/* Output */
407 			&pscl_throughput, &pscl_throughput_chroma,
408 			&dpp_clk_single_dpp);
409 
410 	clock = dpp_clk_single_dpp * (1 + context->bw_ctx.dml.soc.dcn_downspread_percent / 100);
411 
412 	if (clock > 0)
413 		clk_frequency = vco_speed * 4.0 / ((int)(vco_speed * 4.0) / clock);
414 
415 	if (odm_mode == dm_odm_combine_mode_2to1)
416 		num_splits = 1;
417 	else if (odm_mode == dm_odm_combine_mode_4to1)
418 		num_splits = 3;
419 	else if (clk_frequency > context->bw_ctx.dml.soc.clock_limits[context->bw_ctx.dml.soc.num_states - 1].dppclk_mhz)
420 		num_splits = 1;
421 
422 	return num_splits;
423 }
424 
425 static float calculate_net_bw_in_kbytes_sec(struct _vcs_dpi_voltage_scaling_st *entry)
426 {
427 	float memory_bw_kbytes_sec;
428 	float fabric_bw_kbytes_sec;
429 	float sdp_bw_kbytes_sec;
430 	float limiting_bw_kbytes_sec;
431 
432 	memory_bw_kbytes_sec = entry->dram_speed_mts *
433 				dcn3_2_soc.num_chans *
434 				dcn3_2_soc.dram_channel_width_bytes *
435 				((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100);
436 
437 	fabric_bw_kbytes_sec = entry->fabricclk_mhz *
438 				dcn3_2_soc.return_bus_width_bytes *
439 				((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100);
440 
441 	sdp_bw_kbytes_sec = entry->dcfclk_mhz *
442 				dcn3_2_soc.return_bus_width_bytes *
443 				((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100);
444 
445 	limiting_bw_kbytes_sec = memory_bw_kbytes_sec;
446 
447 	if (fabric_bw_kbytes_sec < limiting_bw_kbytes_sec)
448 		limiting_bw_kbytes_sec = fabric_bw_kbytes_sec;
449 
450 	if (sdp_bw_kbytes_sec < limiting_bw_kbytes_sec)
451 		limiting_bw_kbytes_sec = sdp_bw_kbytes_sec;
452 
453 	return limiting_bw_kbytes_sec;
454 }
455 
456 static void get_optimal_ntuple(struct _vcs_dpi_voltage_scaling_st *entry)
457 {
458 	if (entry->dcfclk_mhz > 0) {
459 		float bw_on_sdp = entry->dcfclk_mhz * dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100);
460 
461 		entry->fabricclk_mhz = bw_on_sdp / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100));
462 		entry->dram_speed_mts = bw_on_sdp / (dcn3_2_soc.num_chans *
463 				dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100));
464 	} else if (entry->fabricclk_mhz > 0) {
465 		float bw_on_fabric = entry->fabricclk_mhz * dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100);
466 
467 		entry->dcfclk_mhz = bw_on_fabric / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100));
468 		entry->dram_speed_mts = bw_on_fabric / (dcn3_2_soc.num_chans *
469 				dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100));
470 	} else if (entry->dram_speed_mts > 0) {
471 		float bw_on_dram = entry->dram_speed_mts * dcn3_2_soc.num_chans *
472 				dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100);
473 
474 		entry->fabricclk_mhz = bw_on_dram / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100));
475 		entry->dcfclk_mhz = bw_on_dram / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100));
476 	}
477 }
478 
479 void insert_entry_into_table_sorted(struct _vcs_dpi_voltage_scaling_st *table,
480 				    unsigned int *num_entries,
481 				    struct _vcs_dpi_voltage_scaling_st *entry)
482 {
483 	int i = 0;
484 	int index = 0;
485 	float net_bw_of_new_state = 0;
486 
487 	dc_assert_fp_enabled();
488 
489 	get_optimal_ntuple(entry);
490 
491 	if (*num_entries == 0) {
492 		table[0] = *entry;
493 		(*num_entries)++;
494 	} else {
495 		net_bw_of_new_state = calculate_net_bw_in_kbytes_sec(entry);
496 		while (net_bw_of_new_state > calculate_net_bw_in_kbytes_sec(&table[index])) {
497 			index++;
498 			if (index >= *num_entries)
499 				break;
500 		}
501 
502 		for (i = *num_entries; i > index; i--)
503 			table[i] = table[i - 1];
504 
505 		table[index] = *entry;
506 		(*num_entries)++;
507 	}
508 }
509 
510 /**
511  * dcn32_set_phantom_stream_timing - Set timing params for the phantom stream
512  * @dc: current dc state
513  * @context: new dc state
514  * @ref_pipe: Main pipe for the phantom stream
515  * @phantom_stream: target phantom stream state
516  * @pipes: DML pipe params
517  * @pipe_cnt: number of DML pipes
518  * @dc_pipe_idx: DC pipe index for the main pipe (i.e. ref_pipe)
519  *
520  * Set timing params of the phantom stream based on calculated output from DML.
521  * This function first gets the DML pipe index using the DC pipe index, then
522  * calls into DML (get_subviewport_lines_needed_in_mall) to get the number of
523  * lines required for SubVP MCLK switching and assigns to the phantom stream
524  * accordingly.
525  *
526  * - The number of SubVP lines calculated in DML does not take into account
527  * FW processing delays and required pstate allow width, so we must include
528  * that separately.
529  *
530  * - Set phantom backporch = vstartup of main pipe
531  */
532 void dcn32_set_phantom_stream_timing(struct dc *dc,
533 				     struct dc_state *context,
534 				     struct pipe_ctx *ref_pipe,
535 				     struct dc_stream_state *phantom_stream,
536 				     display_e2e_pipe_params_st *pipes,
537 				     unsigned int pipe_cnt,
538 				     unsigned int dc_pipe_idx)
539 {
540 	unsigned int i, pipe_idx;
541 	struct pipe_ctx *pipe;
542 	uint32_t phantom_vactive, phantom_bp, pstate_width_fw_delay_lines;
543 	unsigned int num_dpp;
544 	unsigned int vlevel = context->bw_ctx.dml.vba.VoltageLevel;
545 	unsigned int dcfclk = context->bw_ctx.dml.vba.DCFCLKState[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
546 	unsigned int socclk = context->bw_ctx.dml.vba.SOCCLKPerState[vlevel];
547 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
548 	struct dc_stream_state *main_stream = ref_pipe->stream;
549 
550 	dc_assert_fp_enabled();
551 
552 	// Find DML pipe index (pipe_idx) using dc_pipe_idx
553 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
554 		pipe = &context->res_ctx.pipe_ctx[i];
555 
556 		if (!pipe->stream)
557 			continue;
558 
559 		if (i == dc_pipe_idx)
560 			break;
561 
562 		pipe_idx++;
563 	}
564 
565 	// Calculate lines required for pstate allow width and FW processing delays
566 	pstate_width_fw_delay_lines = ((double)(dc->caps.subvp_fw_processing_delay_us +
567 			dc->caps.subvp_pstate_allow_width_us) / 1000000) *
568 			(ref_pipe->stream->timing.pix_clk_100hz * 100) /
569 			(double)ref_pipe->stream->timing.h_total;
570 
571 	// Update clks_cfg for calling into recalculate
572 	pipes[0].clks_cfg.voltage = vlevel;
573 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
574 	pipes[0].clks_cfg.socclk_mhz = socclk;
575 
576 	// DML calculation for MALL region doesn't take into account FW delay
577 	// and required pstate allow width for multi-display cases
578 	/* Add 16 lines margin to the MALL REGION because SUB_VP_START_LINE must be aligned
579 	 * to 2 swaths (i.e. 16 lines)
580 	 */
581 	phantom_vactive = get_subviewport_lines_needed_in_mall(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx) +
582 				pstate_width_fw_delay_lines + dc->caps.subvp_swath_height_margin_lines;
583 
584 	// W/A for DCC corruption with certain high resolution timings.
585 	// Determing if pipesplit is used. If so, add meta_row_height to the phantom vactive.
586 	num_dpp = vba->NoOfDPP[vba->VoltageLevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]];
587 	phantom_vactive += num_dpp > 1 ? vba->meta_row_height[vba->pipe_plane[pipe_idx]] : 0;
588 
589 	/* dc->debug.subvp_extra_lines 0 by default*/
590 	phantom_vactive += dc->debug.subvp_extra_lines;
591 
592 	// For backporch of phantom pipe, use vstartup of the main pipe
593 	phantom_bp = get_vstartup(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
594 
595 	phantom_stream->dst.y = 0;
596 	phantom_stream->dst.height = phantom_vactive;
597 	/* When scaling, DML provides the end to end required number of lines for MALL.
598 	 * dst.height is always correct for this case, but src.height is not which causes a
599 	 * delta between main and phantom pipe scaling outputs. Need to adjust src.height on
600 	 * phantom for this case.
601 	 */
602 	phantom_stream->src.y = 0;
603 	phantom_stream->src.height = (double)phantom_vactive * (double)main_stream->src.height / (double)main_stream->dst.height;
604 
605 	phantom_stream->timing.v_addressable = phantom_vactive;
606 	phantom_stream->timing.v_front_porch = 1;
607 	phantom_stream->timing.v_total = phantom_stream->timing.v_addressable +
608 						phantom_stream->timing.v_front_porch +
609 						phantom_stream->timing.v_sync_width +
610 						phantom_bp;
611 	phantom_stream->timing.flags.DSC = 0; // Don't need DSC for phantom timing
612 }
613 
614 /**
615  * dcn32_get_num_free_pipes - Calculate number of free pipes
616  * @dc: current dc state
617  * @context: new dc state
618  *
619  * This function assumes that a "used" pipe is a pipe that has
620  * both a stream and a plane assigned to it.
621  *
622  * Return: Number of free pipes available in the context
623  */
624 static unsigned int dcn32_get_num_free_pipes(struct dc *dc, struct dc_state *context)
625 {
626 	unsigned int i;
627 	unsigned int free_pipes = 0;
628 	unsigned int num_pipes = 0;
629 
630 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
631 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
632 
633 		if (pipe->stream && !pipe->top_pipe) {
634 			while (pipe) {
635 				num_pipes++;
636 				pipe = pipe->bottom_pipe;
637 			}
638 		}
639 	}
640 
641 	free_pipes = dc->res_pool->pipe_count - num_pipes;
642 	return free_pipes;
643 }
644 
645 /**
646  * dcn32_assign_subvp_pipe - Function to decide which pipe will use Sub-VP.
647  * @dc: current dc state
648  * @context: new dc state
649  * @index: [out] dc pipe index for the pipe chosen to have phantom pipes assigned
650  *
651  * We enter this function if we are Sub-VP capable (i.e. enough pipes available)
652  * and regular P-State switching (i.e. VACTIVE/VBLANK) is not supported, or if
653  * we are forcing SubVP P-State switching on the current config.
654  *
655  * The number of pipes used for the chosen surface must be less than or equal to the
656  * number of free pipes available.
657  *
658  * In general we choose surfaces with the longest frame time first (better for SubVP + VBLANK).
659  * For multi-display cases the ActiveDRAMClockChangeMargin doesn't provide enough info on its own
660  * for determining which should be the SubVP pipe (need a way to determine if a pipe / plane doesn't
661  * support MCLK switching naturally [i.e. ACTIVE or VBLANK]).
662  *
663  * Return: True if a valid pipe assignment was found for Sub-VP. Otherwise false.
664  */
665 static bool dcn32_assign_subvp_pipe(struct dc *dc,
666 				    struct dc_state *context,
667 				    unsigned int *index)
668 {
669 	unsigned int i, pipe_idx;
670 	unsigned int max_frame_time = 0;
671 	bool valid_assignment_found = false;
672 	unsigned int free_pipes = dcn32_get_num_free_pipes(dc, context);
673 	bool current_assignment_freesync = false;
674 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
675 
676 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
677 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
678 		unsigned int num_pipes = 0;
679 		unsigned int refresh_rate = 0;
680 
681 		if (!pipe->stream)
682 			continue;
683 
684 		// Round up
685 		refresh_rate = (pipe->stream->timing.pix_clk_100hz * 100 +
686 				pipe->stream->timing.v_total * pipe->stream->timing.h_total - 1)
687 				/ (double)(pipe->stream->timing.v_total * pipe->stream->timing.h_total);
688 		/* SubVP pipe candidate requirements:
689 		 * - Refresh rate < 120hz
690 		 * - Not able to switch in vactive naturally (switching in active means the
691 		 *   DET provides enough buffer to hide the P-State switch latency -- trying
692 		 *   to combine this with SubVP can cause issues with the scheduling).
693 		 * - Not TMZ surface
694 		 */
695 		if (pipe->plane_state && !pipe->top_pipe && !dcn32_is_center_timing(pipe) && !dcn32_is_psr_capable(pipe) &&
696 				pipe->stream->mall_stream_config.type == SUBVP_NONE && refresh_rate < 120 && !pipe->plane_state->address.tmz_surface &&
697 				(vba->ActiveDRAMClockChangeLatencyMarginPerState[vba->VoltageLevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]] <= 0 ||
698 				(vba->ActiveDRAMClockChangeLatencyMarginPerState[vba->VoltageLevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]] > 0 &&
699 						dcn32_allow_subvp_with_active_margin(pipe)))) {
700 			while (pipe) {
701 				num_pipes++;
702 				pipe = pipe->bottom_pipe;
703 			}
704 
705 			pipe = &context->res_ctx.pipe_ctx[i];
706 			if (num_pipes <= free_pipes) {
707 				struct dc_stream_state *stream = pipe->stream;
708 				unsigned int frame_us = (stream->timing.v_total * stream->timing.h_total /
709 						(double)(stream->timing.pix_clk_100hz * 100)) * 1000000;
710 				if (frame_us > max_frame_time && !stream->ignore_msa_timing_param) {
711 					*index = i;
712 					max_frame_time = frame_us;
713 					valid_assignment_found = true;
714 					current_assignment_freesync = false;
715 				/* For the 2-Freesync display case, still choose the one with the
716 			     * longest frame time
717 			     */
718 				} else if (stream->ignore_msa_timing_param && (!valid_assignment_found ||
719 						(current_assignment_freesync && frame_us > max_frame_time))) {
720 					*index = i;
721 					valid_assignment_found = true;
722 					current_assignment_freesync = true;
723 				}
724 			}
725 		}
726 		pipe_idx++;
727 	}
728 	return valid_assignment_found;
729 }
730 
731 /**
732  * dcn32_enough_pipes_for_subvp - Function to check if there are "enough" pipes for SubVP.
733  * @dc: current dc state
734  * @context: new dc state
735  *
736  * This function returns true if there are enough free pipes
737  * to create the required phantom pipes for any given stream
738  * (that does not already have phantom pipe assigned).
739  *
740  * e.g. For a 2 stream config where the first stream uses one
741  * pipe and the second stream uses 2 pipes (i.e. pipe split),
742  * this function will return true because there is 1 remaining
743  * pipe which can be used as the phantom pipe for the non pipe
744  * split pipe.
745  *
746  * Return:
747  * True if there are enough free pipes to assign phantom pipes to at least one
748  * stream that does not already have phantom pipes assigned. Otherwise false.
749  */
750 static bool dcn32_enough_pipes_for_subvp(struct dc *dc, struct dc_state *context)
751 {
752 	unsigned int i, split_cnt, free_pipes;
753 	unsigned int min_pipe_split = dc->res_pool->pipe_count + 1; // init as max number of pipes + 1
754 	bool subvp_possible = false;
755 
756 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
757 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
758 
759 		// Find the minimum pipe split count for non SubVP pipes
760 		if (pipe->stream && !pipe->top_pipe &&
761 		    pipe->stream->mall_stream_config.type == SUBVP_NONE) {
762 			split_cnt = 0;
763 			while (pipe) {
764 				split_cnt++;
765 				pipe = pipe->bottom_pipe;
766 			}
767 
768 			if (split_cnt < min_pipe_split)
769 				min_pipe_split = split_cnt;
770 		}
771 	}
772 
773 	free_pipes = dcn32_get_num_free_pipes(dc, context);
774 
775 	// SubVP only possible if at least one pipe is being used (i.e. free_pipes
776 	// should not equal to the pipe_count)
777 	if (free_pipes >= min_pipe_split && free_pipes < dc->res_pool->pipe_count)
778 		subvp_possible = true;
779 
780 	return subvp_possible;
781 }
782 
783 /**
784  * subvp_subvp_schedulable - Determine if SubVP + SubVP config is schedulable
785  * @dc: current dc state
786  * @context: new dc state
787  *
788  * High level algorithm:
789  * 1. Find longest microschedule length (in us) between the two SubVP pipes
790  * 2. Check if the worst case overlap (VBLANK in middle of ACTIVE) for both
791  * pipes still allows for the maximum microschedule to fit in the active
792  * region for both pipes.
793  *
794  * Return: True if the SubVP + SubVP config is schedulable, false otherwise
795  */
796 static bool subvp_subvp_schedulable(struct dc *dc, struct dc_state *context)
797 {
798 	struct pipe_ctx *subvp_pipes[2];
799 	struct dc_stream_state *phantom = NULL;
800 	uint32_t microschedule_lines = 0;
801 	uint32_t index = 0;
802 	uint32_t i;
803 	uint32_t max_microschedule_us = 0;
804 	int32_t vactive1_us, vactive2_us, vblank1_us, vblank2_us;
805 
806 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
807 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
808 		uint32_t time_us = 0;
809 
810 		/* Loop to calculate the maximum microschedule time between the two SubVP pipes,
811 		 * and also to store the two main SubVP pipe pointers in subvp_pipes[2].
812 		 */
813 		if (pipe->stream && pipe->plane_state && !pipe->top_pipe &&
814 		    pipe->stream->mall_stream_config.type == SUBVP_MAIN) {
815 			phantom = pipe->stream->mall_stream_config.paired_stream;
816 			microschedule_lines = (phantom->timing.v_total - phantom->timing.v_front_porch) +
817 					phantom->timing.v_addressable;
818 
819 			// Round up when calculating microschedule time (+ 1 at the end)
820 			time_us = (microschedule_lines * phantom->timing.h_total) /
821 					(double)(phantom->timing.pix_clk_100hz * 100) * 1000000 +
822 						dc->caps.subvp_prefetch_end_to_mall_start_us +
823 						dc->caps.subvp_fw_processing_delay_us + 1;
824 			if (time_us > max_microschedule_us)
825 				max_microschedule_us = time_us;
826 
827 			subvp_pipes[index] = pipe;
828 			index++;
829 
830 			// Maximum 2 SubVP pipes
831 			if (index == 2)
832 				break;
833 		}
834 	}
835 	vactive1_us = ((subvp_pipes[0]->stream->timing.v_addressable * subvp_pipes[0]->stream->timing.h_total) /
836 			(double)(subvp_pipes[0]->stream->timing.pix_clk_100hz * 100)) * 1000000;
837 	vactive2_us = ((subvp_pipes[1]->stream->timing.v_addressable * subvp_pipes[1]->stream->timing.h_total) /
838 				(double)(subvp_pipes[1]->stream->timing.pix_clk_100hz * 100)) * 1000000;
839 	vblank1_us = (((subvp_pipes[0]->stream->timing.v_total - subvp_pipes[0]->stream->timing.v_addressable) *
840 			subvp_pipes[0]->stream->timing.h_total) /
841 			(double)(subvp_pipes[0]->stream->timing.pix_clk_100hz * 100)) * 1000000;
842 	vblank2_us = (((subvp_pipes[1]->stream->timing.v_total - subvp_pipes[1]->stream->timing.v_addressable) *
843 			subvp_pipes[1]->stream->timing.h_total) /
844 			(double)(subvp_pipes[1]->stream->timing.pix_clk_100hz * 100)) * 1000000;
845 
846 	if ((vactive1_us - vblank2_us) / 2 > max_microschedule_us &&
847 	    (vactive2_us - vblank1_us) / 2 > max_microschedule_us)
848 		return true;
849 
850 	return false;
851 }
852 
853 /**
854  * subvp_drr_schedulable - Determine if SubVP + DRR config is schedulable
855  * @dc: current dc state
856  * @context: new dc state
857  * @drr_pipe: DRR pipe_ctx for the SubVP + DRR config
858  *
859  * High level algorithm:
860  * 1. Get timing for SubVP pipe, phantom pipe, and DRR pipe
861  * 2. Determine the frame time for the DRR display when adding required margin for MCLK switching
862  * (the margin is equal to the MALL region + DRR margin (500us))
863  * 3.If (SubVP Active - Prefetch > Stretched DRR frame + max(MALL region, Stretched DRR frame))
864  * then report the configuration as supported
865  *
866  * Return: True if the SubVP + DRR config is schedulable, false otherwise
867  */
868 static bool subvp_drr_schedulable(struct dc *dc, struct dc_state *context, struct pipe_ctx *drr_pipe)
869 {
870 	bool schedulable = false;
871 	uint32_t i;
872 	struct pipe_ctx *pipe = NULL;
873 	struct dc_crtc_timing *main_timing = NULL;
874 	struct dc_crtc_timing *phantom_timing = NULL;
875 	struct dc_crtc_timing *drr_timing = NULL;
876 	int16_t prefetch_us = 0;
877 	int16_t mall_region_us = 0;
878 	int16_t drr_frame_us = 0;	// nominal frame time
879 	int16_t subvp_active_us = 0;
880 	int16_t stretched_drr_us = 0;
881 	int16_t drr_stretched_vblank_us = 0;
882 	int16_t max_vblank_mallregion = 0;
883 	const struct dc_config *config = &dc->config;
884 
885 	if (config->disable_subvp_drr)
886 		return false;
887 
888 	// Find SubVP pipe
889 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
890 		pipe = &context->res_ctx.pipe_ctx[i];
891 
892 		// We check for master pipe, but it shouldn't matter since we only need
893 		// the pipe for timing info (stream should be same for any pipe splits)
894 		if (!pipe->stream || !pipe->plane_state || pipe->top_pipe || pipe->prev_odm_pipe)
895 			continue;
896 
897 		// Find the SubVP pipe
898 		if (pipe->stream->mall_stream_config.type == SUBVP_MAIN)
899 			break;
900 	}
901 
902 	main_timing = &pipe->stream->timing;
903 	phantom_timing = &pipe->stream->mall_stream_config.paired_stream->timing;
904 	drr_timing = &drr_pipe->stream->timing;
905 	prefetch_us = (phantom_timing->v_total - phantom_timing->v_front_porch) * phantom_timing->h_total /
906 			(double)(phantom_timing->pix_clk_100hz * 100) * 1000000 +
907 			dc->caps.subvp_prefetch_end_to_mall_start_us;
908 	subvp_active_us = main_timing->v_addressable * main_timing->h_total /
909 			(double)(main_timing->pix_clk_100hz * 100) * 1000000;
910 	drr_frame_us = drr_timing->v_total * drr_timing->h_total /
911 			(double)(drr_timing->pix_clk_100hz * 100) * 1000000;
912 	// P-State allow width and FW delays already included phantom_timing->v_addressable
913 	mall_region_us = phantom_timing->v_addressable * phantom_timing->h_total /
914 			(double)(phantom_timing->pix_clk_100hz * 100) * 1000000;
915 	stretched_drr_us = drr_frame_us + mall_region_us + SUBVP_DRR_MARGIN_US;
916 	drr_stretched_vblank_us = (drr_timing->v_total - drr_timing->v_addressable) * drr_timing->h_total /
917 			(double)(drr_timing->pix_clk_100hz * 100) * 1000000 + (stretched_drr_us - drr_frame_us);
918 	max_vblank_mallregion = drr_stretched_vblank_us > mall_region_us ? drr_stretched_vblank_us : mall_region_us;
919 
920 	/* We consider SubVP + DRR schedulable if the stretched frame duration of the DRR display (i.e. the
921 	 * highest refresh rate + margin that can support UCLK P-State switch) passes the static analysis
922 	 * for VBLANK: (VACTIVE region of the SubVP pipe can fit the MALL prefetch, VBLANK frame time,
923 	 * and the max of (VBLANK blanking time, MALL region)).
924 	 */
925 	if (stretched_drr_us < (1 / (double)drr_timing->min_refresh_in_uhz) * 1000000 * 1000000 &&
926 			subvp_active_us - prefetch_us - stretched_drr_us - max_vblank_mallregion > 0)
927 		schedulable = true;
928 
929 	return schedulable;
930 }
931 
932 
933 /**
934  * subvp_vblank_schedulable - Determine if SubVP + VBLANK config is schedulable
935  * @dc: current dc state
936  * @context: new dc state
937  *
938  * High level algorithm:
939  * 1. Get timing for SubVP pipe, phantom pipe, and VBLANK pipe
940  * 2. If (SubVP Active - Prefetch > Vblank Frame Time + max(MALL region, Vblank blanking time))
941  * then report the configuration as supported
942  * 3. If the VBLANK display is DRR, then take the DRR static schedulability path
943  *
944  * Return: True if the SubVP + VBLANK/DRR config is schedulable, false otherwise
945  */
946 static bool subvp_vblank_schedulable(struct dc *dc, struct dc_state *context)
947 {
948 	struct pipe_ctx *pipe = NULL;
949 	struct pipe_ctx *subvp_pipe = NULL;
950 	bool found = false;
951 	bool schedulable = false;
952 	uint32_t i = 0;
953 	uint8_t vblank_index = 0;
954 	uint16_t prefetch_us = 0;
955 	uint16_t mall_region_us = 0;
956 	uint16_t vblank_frame_us = 0;
957 	uint16_t subvp_active_us = 0;
958 	uint16_t vblank_blank_us = 0;
959 	uint16_t max_vblank_mallregion = 0;
960 	struct dc_crtc_timing *main_timing = NULL;
961 	struct dc_crtc_timing *phantom_timing = NULL;
962 	struct dc_crtc_timing *vblank_timing = NULL;
963 
964 	/* For SubVP + VBLANK/DRR cases, we assume there can only be
965 	 * a single VBLANK/DRR display. If DML outputs SubVP + VBLANK
966 	 * is supported, it is either a single VBLANK case or two VBLANK
967 	 * displays which are synchronized (in which case they have identical
968 	 * timings).
969 	 */
970 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
971 		pipe = &context->res_ctx.pipe_ctx[i];
972 
973 		// We check for master pipe, but it shouldn't matter since we only need
974 		// the pipe for timing info (stream should be same for any pipe splits)
975 		if (!pipe->stream || !pipe->plane_state || pipe->top_pipe || pipe->prev_odm_pipe)
976 			continue;
977 
978 		if (!found && pipe->stream->mall_stream_config.type == SUBVP_NONE) {
979 			// Found pipe which is not SubVP or Phantom (i.e. the VBLANK pipe).
980 			vblank_index = i;
981 			found = true;
982 		}
983 
984 		if (!subvp_pipe && pipe->stream->mall_stream_config.type == SUBVP_MAIN)
985 			subvp_pipe = pipe;
986 	}
987 	// Use ignore_msa_timing_param and VRR active, or Freesync flag to identify as DRR On
988 	if (found && context->res_ctx.pipe_ctx[vblank_index].stream->ignore_msa_timing_param &&
989 			(context->res_ctx.pipe_ctx[vblank_index].stream->allow_freesync ||
990 			context->res_ctx.pipe_ctx[vblank_index].stream->vrr_active_variable)) {
991 		// SUBVP + DRR case -- only allowed if run through DRR validation path
992 		schedulable = false;
993 	} else if (found) {
994 		main_timing = &subvp_pipe->stream->timing;
995 		phantom_timing = &subvp_pipe->stream->mall_stream_config.paired_stream->timing;
996 		vblank_timing = &context->res_ctx.pipe_ctx[vblank_index].stream->timing;
997 		// Prefetch time is equal to VACTIVE + BP + VSYNC of the phantom pipe
998 		// Also include the prefetch end to mallstart delay time
999 		prefetch_us = (phantom_timing->v_total - phantom_timing->v_front_porch) * phantom_timing->h_total /
1000 				(double)(phantom_timing->pix_clk_100hz * 100) * 1000000 +
1001 				dc->caps.subvp_prefetch_end_to_mall_start_us;
1002 		// P-State allow width and FW delays already included phantom_timing->v_addressable
1003 		mall_region_us = phantom_timing->v_addressable * phantom_timing->h_total /
1004 				(double)(phantom_timing->pix_clk_100hz * 100) * 1000000;
1005 		vblank_frame_us = vblank_timing->v_total * vblank_timing->h_total /
1006 				(double)(vblank_timing->pix_clk_100hz * 100) * 1000000;
1007 		vblank_blank_us =  (vblank_timing->v_total - vblank_timing->v_addressable) * vblank_timing->h_total /
1008 				(double)(vblank_timing->pix_clk_100hz * 100) * 1000000;
1009 		subvp_active_us = main_timing->v_addressable * main_timing->h_total /
1010 				(double)(main_timing->pix_clk_100hz * 100) * 1000000;
1011 		max_vblank_mallregion = vblank_blank_us > mall_region_us ? vblank_blank_us : mall_region_us;
1012 
1013 		// Schedulable if VACTIVE region of the SubVP pipe can fit the MALL prefetch, VBLANK frame time,
1014 		// and the max of (VBLANK blanking time, MALL region)
1015 		// TODO: Possibly add some margin (i.e. the below conditions should be [...] > X instead of [...] > 0)
1016 		if (subvp_active_us - prefetch_us - vblank_frame_us - max_vblank_mallregion > 0)
1017 			schedulable = true;
1018 	}
1019 	return schedulable;
1020 }
1021 
1022 /**
1023  * subvp_validate_static_schedulability - Check which SubVP case is calculated
1024  * and handle static analysis based on the case.
1025  * @dc: current dc state
1026  * @context: new dc state
1027  * @vlevel: Voltage level calculated by DML
1028  *
1029  * Three cases:
1030  * 1. SubVP + SubVP
1031  * 2. SubVP + VBLANK (DRR checked internally)
1032  * 3. SubVP + VACTIVE (currently unsupported)
1033  *
1034  * Return: True if statically schedulable, false otherwise
1035  */
1036 static bool subvp_validate_static_schedulability(struct dc *dc,
1037 				struct dc_state *context,
1038 				int vlevel)
1039 {
1040 	bool schedulable = true;	// true by default for single display case
1041 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
1042 	uint32_t i, pipe_idx;
1043 	uint8_t subvp_count = 0;
1044 	uint8_t vactive_count = 0;
1045 
1046 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1047 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1048 
1049 		if (!pipe->stream)
1050 			continue;
1051 
1052 		if (pipe->plane_state && !pipe->top_pipe &&
1053 				pipe->stream->mall_stream_config.type == SUBVP_MAIN)
1054 			subvp_count++;
1055 
1056 		// Count how many planes that aren't SubVP/phantom are capable of VACTIVE
1057 		// switching (SubVP + VACTIVE unsupported). In situations where we force
1058 		// SubVP for a VACTIVE plane, we don't want to increment the vactive_count.
1059 		if (vba->ActiveDRAMClockChangeLatencyMargin[vba->pipe_plane[pipe_idx]] > 0 &&
1060 		    pipe->stream->mall_stream_config.type == SUBVP_NONE) {
1061 			vactive_count++;
1062 		}
1063 		pipe_idx++;
1064 	}
1065 
1066 	if (subvp_count == 2) {
1067 		// Static schedulability check for SubVP + SubVP case
1068 		schedulable = subvp_subvp_schedulable(dc, context);
1069 	} else if (vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_vblank_w_mall_sub_vp) {
1070 		// Static schedulability check for SubVP + VBLANK case. Also handle the case where
1071 		// DML outputs SubVP + VBLANK + VACTIVE (DML will report as SubVP + VBLANK)
1072 		if (vactive_count > 0)
1073 			schedulable = false;
1074 		else
1075 			schedulable = subvp_vblank_schedulable(dc, context);
1076 	} else if (vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_vactive_w_mall_sub_vp &&
1077 			vactive_count > 0) {
1078 		// For single display SubVP cases, DML will output dm_dram_clock_change_vactive_w_mall_sub_vp by default.
1079 		// We tell the difference between SubVP vs. SubVP + VACTIVE by checking the vactive_count.
1080 		// SubVP + VACTIVE currently unsupported
1081 		schedulable = false;
1082 	}
1083 	return schedulable;
1084 }
1085 
1086 static void dcn32_full_validate_bw_helper(struct dc *dc,
1087 				   struct dc_state *context,
1088 				   display_e2e_pipe_params_st *pipes,
1089 				   int *vlevel,
1090 				   int *split,
1091 				   bool *merge,
1092 				   int *pipe_cnt)
1093 {
1094 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
1095 	unsigned int dc_pipe_idx = 0;
1096 	int i = 0;
1097 	bool found_supported_config = false;
1098 	struct pipe_ctx *pipe = NULL;
1099 	uint32_t non_subvp_pipes = 0;
1100 	bool drr_pipe_found = false;
1101 	uint32_t drr_pipe_index = 0;
1102 
1103 	dc_assert_fp_enabled();
1104 
1105 	/*
1106 	 * DML favors voltage over p-state, but we're more interested in
1107 	 * supporting p-state over voltage. We can't support p-state in
1108 	 * prefetch mode > 0 so try capping the prefetch mode to start.
1109 	 * Override present for testing.
1110 	 */
1111 	if (dc->debug.dml_disallow_alternate_prefetch_modes)
1112 		context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
1113 			dm_prefetch_support_uclk_fclk_and_stutter;
1114 	else
1115 		context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
1116 			dm_prefetch_support_uclk_fclk_and_stutter_if_possible;
1117 
1118 	*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
1119 	/* This may adjust vlevel and maxMpcComb */
1120 	if (*vlevel < context->bw_ctx.dml.soc.num_states) {
1121 		*vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, *vlevel, split, merge);
1122 		vba->VoltageLevel = *vlevel;
1123 	}
1124 
1125 	/* Conditions for setting up phantom pipes for SubVP:
1126 	 * 1. Not force disable SubVP
1127 	 * 2. Full update (i.e. !fast_validate)
1128 	 * 3. Enough pipes are available to support SubVP (TODO: Which pipes will use VACTIVE / VBLANK / SUBVP?)
1129 	 * 4. Display configuration passes validation
1130 	 * 5. (Config doesn't support MCLK in VACTIVE/VBLANK || dc->debug.force_subvp_mclk_switch)
1131 	 */
1132 	if (!dc->debug.force_disable_subvp && dcn32_all_pipes_have_stream_and_plane(dc, context) &&
1133 	    !dcn32_mpo_in_use(context) && !dcn32_any_surfaces_rotated(dc, context) &&
1134 		(*vlevel == context->bw_ctx.dml.soc.num_states ||
1135 	    vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported ||
1136 	    dc->debug.force_subvp_mclk_switch)) {
1137 
1138 		dcn32_merge_pipes_for_subvp(dc, context);
1139 		memset(merge, 0, MAX_PIPES * sizeof(bool));
1140 
1141 		/* to re-initialize viewport after the pipe merge */
1142 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
1143 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1144 
1145 			if (!pipe_ctx->plane_state || !pipe_ctx->stream)
1146 				continue;
1147 
1148 			resource_build_scaling_params(pipe_ctx);
1149 		}
1150 
1151 		while (!found_supported_config && dcn32_enough_pipes_for_subvp(dc, context) &&
1152 			dcn32_assign_subvp_pipe(dc, context, &dc_pipe_idx)) {
1153 			/* For the case where *vlevel = num_states, bandwidth validation has failed for this config.
1154 			 * Adding phantom pipes won't change the validation result, so change the DML input param
1155 			 * for P-State support before adding phantom pipes and recalculating the DML result.
1156 			 * However, this case is only applicable for SubVP + DRR cases because the prefetch mode
1157 			 * will not allow for switch in VBLANK. The DRR display must have it's VBLANK stretched
1158 			 * enough to support MCLK switching.
1159 			 */
1160 			if (*vlevel == context->bw_ctx.dml.soc.num_states &&
1161 				context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final ==
1162 					dm_prefetch_support_uclk_fclk_and_stutter) {
1163 				context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
1164 								dm_prefetch_support_fclk_and_stutter;
1165 				/* There are params (such as FabricClock) that need to be recalculated
1166 				 * after validation fails (otherwise it will be 0). Calculation for
1167 				 * phantom vactive requires call into DML, so we must ensure all the
1168 				 * vba params are valid otherwise we'll get incorrect phantom vactive.
1169 				 */
1170 				*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
1171 			}
1172 
1173 			dc->res_pool->funcs->add_phantom_pipes(dc, context, pipes, *pipe_cnt, dc_pipe_idx);
1174 
1175 			*pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, false);
1176 			// Populate dppclk to trigger a recalculate in dml_get_voltage_level
1177 			// so the phantom pipe DLG params can be assigned correctly.
1178 			pipes[0].clks_cfg.dppclk_mhz = get_dppclk_calculated(&context->bw_ctx.dml, pipes, *pipe_cnt, 0);
1179 			*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
1180 
1181 			/* Check that vlevel requested supports pstate or not
1182 			 * if not, select the lowest vlevel that supports it
1183 			 */
1184 			for (i = *vlevel; i < context->bw_ctx.dml.soc.num_states; i++) {
1185 				if (vba->DRAMClockChangeSupport[i][vba->maxMpcComb] != dm_dram_clock_change_unsupported) {
1186 					*vlevel = i;
1187 					break;
1188 				}
1189 			}
1190 
1191 			if (*vlevel < context->bw_ctx.dml.soc.num_states &&
1192 			    vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] != dm_dram_clock_change_unsupported
1193 			    && subvp_validate_static_schedulability(dc, context, *vlevel)) {
1194 				found_supported_config = true;
1195 			} else if (*vlevel < context->bw_ctx.dml.soc.num_states) {
1196 				/* Case where 1 SubVP is added, and DML reports MCLK unsupported or DRR is allowed.
1197 				 * This handles the case for SubVP + DRR, where the DRR display does not support MCLK
1198 				 * switch at it's native refresh rate / timing, or DRR is allowed for the non-subvp
1199 				 * display.
1200 				 */
1201 				for (i = 0; i < dc->res_pool->pipe_count; i++) {
1202 					pipe = &context->res_ctx.pipe_ctx[i];
1203 					if (pipe->stream && pipe->plane_state && !pipe->top_pipe &&
1204 					    pipe->stream->mall_stream_config.type == SUBVP_NONE) {
1205 						non_subvp_pipes++;
1206 						// Use ignore_msa_timing_param flag to identify as DRR
1207 						if (pipe->stream->ignore_msa_timing_param && pipe->stream->allow_freesync) {
1208 							drr_pipe_found = true;
1209 							drr_pipe_index = i;
1210 						}
1211 					}
1212 				}
1213 				// If there is only 1 remaining non SubVP pipe that is DRR, check static
1214 				// schedulability for SubVP + DRR.
1215 				if (non_subvp_pipes == 1 && drr_pipe_found) {
1216 					/* find lowest vlevel that supports the config */
1217 					for (i = *vlevel; i >= 0; i--) {
1218 						if (vba->ModeSupport[i][vba->maxMpcComb]) {
1219 							*vlevel = i;
1220 						} else {
1221 							break;
1222 						}
1223 					}
1224 
1225 					found_supported_config = subvp_drr_schedulable(dc, context,
1226 										       &context->res_ctx.pipe_ctx[drr_pipe_index]);
1227 				}
1228 			}
1229 		}
1230 
1231 		// If SubVP pipe config is unsupported (or cannot be used for UCLK switching)
1232 		// remove phantom pipes and repopulate dml pipes
1233 		if (!found_supported_config) {
1234 			dc->res_pool->funcs->remove_phantom_pipes(dc, context, false);
1235 			vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] = dm_dram_clock_change_unsupported;
1236 			*pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, false);
1237 
1238 			*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
1239 			/* This may adjust vlevel and maxMpcComb */
1240 			if (*vlevel < context->bw_ctx.dml.soc.num_states) {
1241 				*vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, *vlevel, split, merge);
1242 				vba->VoltageLevel = *vlevel;
1243 			}
1244 		} else {
1245 			// Most populate phantom DLG params before programming hardware / timing for phantom pipe
1246 			dcn32_helper_populate_phantom_dlg_params(dc, context, pipes, *pipe_cnt);
1247 
1248 			/* Call validate_apply_pipe_split flags after calling DML getters for
1249 			 * phantom dlg params, or some of the VBA params indicating pipe split
1250 			 * can be overwritten by the getters.
1251 			 *
1252 			 * When setting up SubVP config, all pipes are merged before attempting to
1253 			 * add phantom pipes. If pipe split (ODM / MPC) is required, both the main
1254 			 * and phantom pipes will be split in the regular pipe splitting sequence.
1255 			 */
1256 			memset(split, 0, MAX_PIPES * sizeof(int));
1257 			memset(merge, 0, MAX_PIPES * sizeof(bool));
1258 			*vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, *vlevel, split, merge);
1259 			vba->VoltageLevel = *vlevel;
1260 			// Note: We can't apply the phantom pipes to hardware at this time. We have to wait
1261 			// until driver has acquired the DMCUB lock to do it safely.
1262 		}
1263 	}
1264 }
1265 
1266 static bool is_dtbclk_required(struct dc *dc, struct dc_state *context)
1267 {
1268 	int i;
1269 
1270 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1271 		if (!context->res_ctx.pipe_ctx[i].stream)
1272 			continue;
1273 		if (dc->link_srv->dp_is_128b_132b_signal(&context->res_ctx.pipe_ctx[i]))
1274 			return true;
1275 	}
1276 	return false;
1277 }
1278 
1279 static void dcn20_adjust_freesync_v_startup(const struct dc_crtc_timing *dc_crtc_timing, int *vstartup_start)
1280 {
1281 	struct dc_crtc_timing patched_crtc_timing;
1282 	uint32_t asic_blank_end   = 0;
1283 	uint32_t asic_blank_start = 0;
1284 	uint32_t newVstartup	  = 0;
1285 
1286 	patched_crtc_timing = *dc_crtc_timing;
1287 
1288 	if (patched_crtc_timing.flags.INTERLACE == 1) {
1289 		if (patched_crtc_timing.v_front_porch < 2)
1290 			patched_crtc_timing.v_front_porch = 2;
1291 	} else {
1292 		if (patched_crtc_timing.v_front_porch < 1)
1293 			patched_crtc_timing.v_front_porch = 1;
1294 	}
1295 
1296 	/* blank_start = frame end - front porch */
1297 	asic_blank_start = patched_crtc_timing.v_total -
1298 					patched_crtc_timing.v_front_porch;
1299 
1300 	/* blank_end = blank_start - active */
1301 	asic_blank_end = asic_blank_start -
1302 					patched_crtc_timing.v_border_bottom -
1303 					patched_crtc_timing.v_addressable -
1304 					patched_crtc_timing.v_border_top;
1305 
1306 	newVstartup = asic_blank_end + (patched_crtc_timing.v_total - asic_blank_start);
1307 
1308 	*vstartup_start = ((newVstartup > *vstartup_start) ? newVstartup : *vstartup_start);
1309 }
1310 
1311 static void dcn32_calculate_dlg_params(struct dc *dc, struct dc_state *context,
1312 				       display_e2e_pipe_params_st *pipes,
1313 				       int pipe_cnt, int vlevel)
1314 {
1315 	int i, pipe_idx, active_hubp_count = 0;
1316 	bool usr_retraining_support = false;
1317 	bool unbounded_req_enabled = false;
1318 
1319 	dc_assert_fp_enabled();
1320 
1321 	/* Writeback MCIF_WB arbitration parameters */
1322 	dc->res_pool->funcs->set_mcif_arb_params(dc, context, pipes, pipe_cnt);
1323 
1324 	context->bw_ctx.bw.dcn.clk.dispclk_khz = context->bw_ctx.dml.vba.DISPCLK * 1000;
1325 	context->bw_ctx.bw.dcn.clk.dcfclk_khz = context->bw_ctx.dml.vba.DCFCLK * 1000;
1326 	context->bw_ctx.bw.dcn.clk.socclk_khz = context->bw_ctx.dml.vba.SOCCLK * 1000;
1327 	context->bw_ctx.bw.dcn.clk.dramclk_khz = context->bw_ctx.dml.vba.DRAMSpeed * 1000 / 16;
1328 	context->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz = context->bw_ctx.dml.vba.DCFCLKDeepSleep * 1000;
1329 	context->bw_ctx.bw.dcn.clk.fclk_khz = context->bw_ctx.dml.vba.FabricClock * 1000;
1330 	context->bw_ctx.bw.dcn.clk.p_state_change_support =
1331 			context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb]
1332 					!= dm_dram_clock_change_unsupported;
1333 
1334 	context->bw_ctx.bw.dcn.clk.dppclk_khz = 0;
1335 	context->bw_ctx.bw.dcn.clk.dtbclk_en = is_dtbclk_required(dc, context);
1336 	context->bw_ctx.bw.dcn.clk.ref_dtbclk_khz = context->bw_ctx.dml.vba.DTBCLKPerState[vlevel] * 1000;
1337 	if (context->bw_ctx.dml.vba.FCLKChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] == dm_fclock_change_unsupported)
1338 		context->bw_ctx.bw.dcn.clk.fclk_p_state_change_support = false;
1339 	else
1340 		context->bw_ctx.bw.dcn.clk.fclk_p_state_change_support = true;
1341 
1342 	usr_retraining_support = context->bw_ctx.dml.vba.USRRetrainingSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
1343 	ASSERT(usr_retraining_support);
1344 
1345 	if (context->bw_ctx.bw.dcn.clk.dispclk_khz < dc->debug.min_disp_clk_khz)
1346 		context->bw_ctx.bw.dcn.clk.dispclk_khz = dc->debug.min_disp_clk_khz;
1347 
1348 	unbounded_req_enabled = get_unbounded_request_enabled(&context->bw_ctx.dml, pipes, pipe_cnt);
1349 
1350 	if (unbounded_req_enabled && pipe_cnt > 1) {
1351 		// Unbounded requesting should not ever be used when more than 1 pipe is enabled.
1352 		ASSERT(false);
1353 		unbounded_req_enabled = false;
1354 	}
1355 
1356 	context->bw_ctx.bw.dcn.mall_ss_size_bytes = 0;
1357 	context->bw_ctx.bw.dcn.mall_ss_psr_active_size_bytes = 0;
1358 	context->bw_ctx.bw.dcn.mall_subvp_size_bytes = 0;
1359 
1360 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1361 		if (!context->res_ctx.pipe_ctx[i].stream)
1362 			continue;
1363 		if (context->res_ctx.pipe_ctx[i].plane_state)
1364 			active_hubp_count++;
1365 		pipes[pipe_idx].pipe.dest.vstartup_start = get_vstartup(&context->bw_ctx.dml, pipes, pipe_cnt,
1366 				pipe_idx);
1367 		pipes[pipe_idx].pipe.dest.vupdate_offset = get_vupdate_offset(&context->bw_ctx.dml, pipes, pipe_cnt,
1368 				pipe_idx);
1369 		pipes[pipe_idx].pipe.dest.vupdate_width = get_vupdate_width(&context->bw_ctx.dml, pipes, pipe_cnt,
1370 				pipe_idx);
1371 		pipes[pipe_idx].pipe.dest.vready_offset = get_vready_offset(&context->bw_ctx.dml, pipes, pipe_cnt,
1372 				pipe_idx);
1373 
1374 		if (context->res_ctx.pipe_ctx[i].stream->mall_stream_config.type == SUBVP_PHANTOM) {
1375 			// Phantom pipe requires that DET_SIZE = 0 and no unbounded requests
1376 			context->res_ctx.pipe_ctx[i].det_buffer_size_kb = 0;
1377 			context->res_ctx.pipe_ctx[i].unbounded_req = false;
1378 		} else {
1379 			context->res_ctx.pipe_ctx[i].det_buffer_size_kb = get_det_buffer_size_kbytes(&context->bw_ctx.dml, pipes, pipe_cnt,
1380 							pipe_idx);
1381 			context->res_ctx.pipe_ctx[i].unbounded_req = unbounded_req_enabled;
1382 		}
1383 
1384 		if (context->bw_ctx.bw.dcn.clk.dppclk_khz < pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000)
1385 			context->bw_ctx.bw.dcn.clk.dppclk_khz = pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000;
1386 		if (context->res_ctx.pipe_ctx[i].plane_state)
1387 			context->res_ctx.pipe_ctx[i].plane_res.bw.dppclk_khz = pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000;
1388 		else
1389 			context->res_ctx.pipe_ctx[i].plane_res.bw.dppclk_khz = 0;
1390 		context->res_ctx.pipe_ctx[i].pipe_dlg_param = pipes[pipe_idx].pipe.dest;
1391 
1392 		context->res_ctx.pipe_ctx[i].surface_size_in_mall_bytes = get_surface_size_in_mall(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
1393 
1394 		/* MALL Allocation Sizes */
1395 		/* count from active, top pipes per plane only */
1396 		if (context->res_ctx.pipe_ctx[i].stream && context->res_ctx.pipe_ctx[i].plane_state &&
1397 				(context->res_ctx.pipe_ctx[i].top_pipe == NULL ||
1398 				context->res_ctx.pipe_ctx[i].plane_state != context->res_ctx.pipe_ctx[i].top_pipe->plane_state) &&
1399 				context->res_ctx.pipe_ctx[i].prev_odm_pipe == NULL) {
1400 			/* SS: all active surfaces stored in MALL */
1401 			if (context->res_ctx.pipe_ctx[i].stream->mall_stream_config.type != SUBVP_PHANTOM) {
1402 				context->bw_ctx.bw.dcn.mall_ss_size_bytes += context->res_ctx.pipe_ctx[i].surface_size_in_mall_bytes;
1403 
1404 				if (context->res_ctx.pipe_ctx[i].stream->link->psr_settings.psr_version == DC_PSR_VERSION_UNSUPPORTED) {
1405 					/* SS PSR On: all active surfaces part of streams not supporting PSR stored in MALL */
1406 					context->bw_ctx.bw.dcn.mall_ss_psr_active_size_bytes += context->res_ctx.pipe_ctx[i].surface_size_in_mall_bytes;
1407 				}
1408 			} else {
1409 				/* SUBVP: phantom surfaces only stored in MALL */
1410 				context->bw_ctx.bw.dcn.mall_subvp_size_bytes += context->res_ctx.pipe_ctx[i].surface_size_in_mall_bytes;
1411 			}
1412 		}
1413 
1414 		if (context->res_ctx.pipe_ctx[i].stream->adaptive_sync_infopacket.valid)
1415 			dcn20_adjust_freesync_v_startup(
1416 				&context->res_ctx.pipe_ctx[i].stream->timing,
1417 				&context->res_ctx.pipe_ctx[i].pipe_dlg_param.vstartup_start);
1418 
1419 		pipe_idx++;
1420 	}
1421 	/* If DCN isn't making memory requests we can allow pstate change and lower clocks */
1422 	if (!active_hubp_count) {
1423 		context->bw_ctx.bw.dcn.clk.socclk_khz = 0;
1424 		context->bw_ctx.bw.dcn.clk.dppclk_khz = 0;
1425 		context->bw_ctx.bw.dcn.clk.dcfclk_khz = 0;
1426 		context->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz = 0;
1427 		context->bw_ctx.bw.dcn.clk.dramclk_khz = 0;
1428 		context->bw_ctx.bw.dcn.clk.fclk_khz = 0;
1429 		context->bw_ctx.bw.dcn.clk.p_state_change_support = true;
1430 	}
1431 	/*save a original dppclock copy*/
1432 	context->bw_ctx.bw.dcn.clk.bw_dppclk_khz = context->bw_ctx.bw.dcn.clk.dppclk_khz;
1433 	context->bw_ctx.bw.dcn.clk.bw_dispclk_khz = context->bw_ctx.bw.dcn.clk.dispclk_khz;
1434 	context->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz = context->bw_ctx.dml.soc.clock_limits[vlevel].dppclk_mhz
1435 			* 1000;
1436 	context->bw_ctx.bw.dcn.clk.max_supported_dispclk_khz = context->bw_ctx.dml.soc.clock_limits[vlevel].dispclk_mhz
1437 			* 1000;
1438 
1439 	context->bw_ctx.bw.dcn.clk.num_ways = dcn32_helper_calculate_num_ways_for_subvp(dc, context);
1440 
1441 	context->bw_ctx.bw.dcn.compbuf_size_kb = context->bw_ctx.dml.ip.config_return_buffer_size_in_kbytes;
1442 
1443 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1444 		if (context->res_ctx.pipe_ctx[i].stream)
1445 			context->bw_ctx.bw.dcn.compbuf_size_kb -= context->res_ctx.pipe_ctx[i].det_buffer_size_kb;
1446 	}
1447 
1448 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1449 
1450 		if (!context->res_ctx.pipe_ctx[i].stream)
1451 			continue;
1452 
1453 		context->bw_ctx.dml.funcs.rq_dlg_get_dlg_reg_v2(&context->bw_ctx.dml,
1454 				&context->res_ctx.pipe_ctx[i].dlg_regs, &context->res_ctx.pipe_ctx[i].ttu_regs, pipes,
1455 				pipe_cnt, pipe_idx);
1456 
1457 		context->bw_ctx.dml.funcs.rq_dlg_get_rq_reg_v2(&context->res_ctx.pipe_ctx[i].rq_regs,
1458 				&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
1459 		pipe_idx++;
1460 	}
1461 }
1462 
1463 static struct pipe_ctx *dcn32_find_split_pipe(
1464 		struct dc *dc,
1465 		struct dc_state *context,
1466 		int old_index)
1467 {
1468 	struct pipe_ctx *pipe = NULL;
1469 	int i;
1470 
1471 	if (old_index >= 0 && context->res_ctx.pipe_ctx[old_index].stream == NULL) {
1472 		pipe = &context->res_ctx.pipe_ctx[old_index];
1473 		pipe->pipe_idx = old_index;
1474 	}
1475 
1476 	if (!pipe)
1477 		for (i = dc->res_pool->pipe_count - 1; i >= 0; i--) {
1478 			if (dc->current_state->res_ctx.pipe_ctx[i].top_pipe == NULL
1479 					&& dc->current_state->res_ctx.pipe_ctx[i].prev_odm_pipe == NULL) {
1480 				if (context->res_ctx.pipe_ctx[i].stream == NULL) {
1481 					pipe = &context->res_ctx.pipe_ctx[i];
1482 					pipe->pipe_idx = i;
1483 					break;
1484 				}
1485 			}
1486 		}
1487 
1488 	/*
1489 	 * May need to fix pipes getting tossed from 1 opp to another on flip
1490 	 * Add for debugging transient underflow during topology updates:
1491 	 * ASSERT(pipe);
1492 	 */
1493 	if (!pipe)
1494 		for (i = dc->res_pool->pipe_count - 1; i >= 0; i--) {
1495 			if (context->res_ctx.pipe_ctx[i].stream == NULL) {
1496 				pipe = &context->res_ctx.pipe_ctx[i];
1497 				pipe->pipe_idx = i;
1498 				break;
1499 			}
1500 		}
1501 
1502 	return pipe;
1503 }
1504 
1505 static bool dcn32_split_stream_for_mpc_or_odm(
1506 		const struct dc *dc,
1507 		struct resource_context *res_ctx,
1508 		struct pipe_ctx *pri_pipe,
1509 		struct pipe_ctx *sec_pipe,
1510 		bool odm)
1511 {
1512 	int pipe_idx = sec_pipe->pipe_idx;
1513 	const struct resource_pool *pool = dc->res_pool;
1514 
1515 	DC_LOGGER_INIT(dc->ctx->logger);
1516 
1517 	if (odm && pri_pipe->plane_state) {
1518 		/* ODM + window MPO, where MPO window is on left half only */
1519 		if (pri_pipe->plane_state->clip_rect.x + pri_pipe->plane_state->clip_rect.width <=
1520 				pri_pipe->stream->src.x + pri_pipe->stream->src.width/2) {
1521 
1522 			DC_LOG_SCALER("%s - ODM + window MPO(left). pri_pipe:%d\n",
1523 					__func__,
1524 					pri_pipe->pipe_idx);
1525 			return true;
1526 		}
1527 
1528 		/* ODM + window MPO, where MPO window is on right half only */
1529 		if (pri_pipe->plane_state->clip_rect.x >= pri_pipe->stream->src.x +  pri_pipe->stream->src.width/2) {
1530 
1531 			DC_LOG_SCALER("%s - ODM + window MPO(right). pri_pipe:%d\n",
1532 					__func__,
1533 					pri_pipe->pipe_idx);
1534 			return true;
1535 		}
1536 	}
1537 
1538 	*sec_pipe = *pri_pipe;
1539 
1540 	sec_pipe->pipe_idx = pipe_idx;
1541 	sec_pipe->plane_res.mi = pool->mis[pipe_idx];
1542 	sec_pipe->plane_res.hubp = pool->hubps[pipe_idx];
1543 	sec_pipe->plane_res.ipp = pool->ipps[pipe_idx];
1544 	sec_pipe->plane_res.xfm = pool->transforms[pipe_idx];
1545 	sec_pipe->plane_res.dpp = pool->dpps[pipe_idx];
1546 	sec_pipe->plane_res.mpcc_inst = pool->dpps[pipe_idx]->inst;
1547 	sec_pipe->stream_res.dsc = NULL;
1548 	if (odm) {
1549 		if (pri_pipe->next_odm_pipe) {
1550 			ASSERT(pri_pipe->next_odm_pipe != sec_pipe);
1551 			sec_pipe->next_odm_pipe = pri_pipe->next_odm_pipe;
1552 			sec_pipe->next_odm_pipe->prev_odm_pipe = sec_pipe;
1553 		}
1554 		if (pri_pipe->top_pipe && pri_pipe->top_pipe->next_odm_pipe) {
1555 			pri_pipe->top_pipe->next_odm_pipe->bottom_pipe = sec_pipe;
1556 			sec_pipe->top_pipe = pri_pipe->top_pipe->next_odm_pipe;
1557 		}
1558 		if (pri_pipe->bottom_pipe && pri_pipe->bottom_pipe->next_odm_pipe) {
1559 			pri_pipe->bottom_pipe->next_odm_pipe->top_pipe = sec_pipe;
1560 			sec_pipe->bottom_pipe = pri_pipe->bottom_pipe->next_odm_pipe;
1561 		}
1562 		pri_pipe->next_odm_pipe = sec_pipe;
1563 		sec_pipe->prev_odm_pipe = pri_pipe;
1564 		ASSERT(sec_pipe->top_pipe == NULL);
1565 
1566 		if (!sec_pipe->top_pipe)
1567 			sec_pipe->stream_res.opp = pool->opps[pipe_idx];
1568 		else
1569 			sec_pipe->stream_res.opp = sec_pipe->top_pipe->stream_res.opp;
1570 		if (sec_pipe->stream->timing.flags.DSC == 1) {
1571 			dcn20_acquire_dsc(dc, res_ctx, &sec_pipe->stream_res.dsc, pipe_idx);
1572 			ASSERT(sec_pipe->stream_res.dsc);
1573 			if (sec_pipe->stream_res.dsc == NULL)
1574 				return false;
1575 		}
1576 	} else {
1577 		if (pri_pipe->bottom_pipe) {
1578 			ASSERT(pri_pipe->bottom_pipe != sec_pipe);
1579 			sec_pipe->bottom_pipe = pri_pipe->bottom_pipe;
1580 			sec_pipe->bottom_pipe->top_pipe = sec_pipe;
1581 		}
1582 		pri_pipe->bottom_pipe = sec_pipe;
1583 		sec_pipe->top_pipe = pri_pipe;
1584 
1585 		ASSERT(pri_pipe->plane_state);
1586 	}
1587 
1588 	return true;
1589 }
1590 
1591 bool dcn32_internal_validate_bw(struct dc *dc,
1592 				struct dc_state *context,
1593 				display_e2e_pipe_params_st *pipes,
1594 				int *pipe_cnt_out,
1595 				int *vlevel_out,
1596 				bool fast_validate)
1597 {
1598 	bool out = false;
1599 	bool repopulate_pipes = false;
1600 	int split[MAX_PIPES] = { 0 };
1601 	bool merge[MAX_PIPES] = { false };
1602 	bool newly_split[MAX_PIPES] = { false };
1603 	int pipe_cnt, i, pipe_idx;
1604 	int vlevel = context->bw_ctx.dml.soc.num_states;
1605 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
1606 
1607 	dc_assert_fp_enabled();
1608 
1609 	ASSERT(pipes);
1610 	if (!pipes)
1611 		return false;
1612 
1613 	// For each full update, remove all existing phantom pipes first
1614 	dc->res_pool->funcs->remove_phantom_pipes(dc, context, fast_validate);
1615 
1616 	dc->res_pool->funcs->update_soc_for_wm_a(dc, context);
1617 
1618 	pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, fast_validate);
1619 
1620 	if (!pipe_cnt) {
1621 		out = true;
1622 		goto validate_out;
1623 	}
1624 
1625 	dml_log_pipe_params(&context->bw_ctx.dml, pipes, pipe_cnt);
1626 	context->bw_ctx.dml.soc.max_vratio_pre = dcn32_determine_max_vratio_prefetch(dc, context);
1627 
1628 	if (!fast_validate)
1629 		dcn32_full_validate_bw_helper(dc, context, pipes, &vlevel, split, merge, &pipe_cnt);
1630 
1631 	if (fast_validate ||
1632 			(dc->debug.dml_disallow_alternate_prefetch_modes &&
1633 			(vlevel == context->bw_ctx.dml.soc.num_states ||
1634 				vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported))) {
1635 		/*
1636 		 * If dml_disallow_alternate_prefetch_modes is false, then we have already
1637 		 * tried alternate prefetch modes during full validation.
1638 		 *
1639 		 * If mode is unsupported or there is no p-state support, then
1640 		 * fall back to favouring voltage.
1641 		 *
1642 		 * If Prefetch mode 0 failed for this config, or passed with Max UCLK, then try
1643 		 * to support with Prefetch mode 1 (dm_prefetch_support_fclk_and_stutter == 2)
1644 		 */
1645 		context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
1646 			dm_prefetch_support_none;
1647 
1648 		context->bw_ctx.dml.validate_max_state = fast_validate;
1649 		vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
1650 
1651 		context->bw_ctx.dml.validate_max_state = false;
1652 
1653 		if (vlevel < context->bw_ctx.dml.soc.num_states) {
1654 			memset(split, 0, sizeof(split));
1655 			memset(merge, 0, sizeof(merge));
1656 			vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, vlevel, split, merge);
1657 			// dcn20_validate_apply_pipe_split_flags can modify voltage level outside of DML
1658 			vba->VoltageLevel = vlevel;
1659 		}
1660 	}
1661 
1662 	dml_log_mode_support_params(&context->bw_ctx.dml);
1663 
1664 	if (vlevel == context->bw_ctx.dml.soc.num_states)
1665 		goto validate_fail;
1666 
1667 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1668 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1669 		struct pipe_ctx *mpo_pipe = pipe->bottom_pipe;
1670 
1671 		if (!pipe->stream)
1672 			continue;
1673 
1674 		if (vba->ODMCombineEnabled[vba->pipe_plane[pipe_idx]] != dm_odm_combine_mode_disabled
1675 				&& !dc->config.enable_windowed_mpo_odm
1676 				&& pipe->plane_state && mpo_pipe
1677 				&& memcmp(&mpo_pipe->plane_res.scl_data.recout,
1678 						&pipe->plane_res.scl_data.recout,
1679 						sizeof(struct rect)) != 0) {
1680 			ASSERT(mpo_pipe->plane_state != pipe->plane_state);
1681 			goto validate_fail;
1682 		}
1683 		pipe_idx++;
1684 	}
1685 
1686 	/* merge pipes if necessary */
1687 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1688 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1689 
1690 		/*skip pipes that don't need merging*/
1691 		if (!merge[i])
1692 			continue;
1693 
1694 		/* if ODM merge we ignore mpc tree, mpo pipes will have their own flags */
1695 		if (pipe->prev_odm_pipe) {
1696 			/*split off odm pipe*/
1697 			pipe->prev_odm_pipe->next_odm_pipe = pipe->next_odm_pipe;
1698 			if (pipe->next_odm_pipe)
1699 				pipe->next_odm_pipe->prev_odm_pipe = pipe->prev_odm_pipe;
1700 
1701 			/*2:1ODM+MPC Split MPO to Single Pipe + MPC Split MPO*/
1702 			if (pipe->bottom_pipe) {
1703 				if (pipe->bottom_pipe->prev_odm_pipe || pipe->bottom_pipe->next_odm_pipe) {
1704 					/*MPC split rules will handle this case*/
1705 					pipe->bottom_pipe->top_pipe = NULL;
1706 				} else {
1707 					/* when merging an ODM pipes, the bottom MPC pipe must now point to
1708 					 * the previous ODM pipe and its associated stream assets
1709 					 */
1710 					if (pipe->prev_odm_pipe->bottom_pipe) {
1711 						/* 3 plane MPO*/
1712 						pipe->bottom_pipe->top_pipe = pipe->prev_odm_pipe->bottom_pipe;
1713 						pipe->prev_odm_pipe->bottom_pipe->bottom_pipe = pipe->bottom_pipe;
1714 					} else {
1715 						/* 2 plane MPO*/
1716 						pipe->bottom_pipe->top_pipe = pipe->prev_odm_pipe;
1717 						pipe->prev_odm_pipe->bottom_pipe = pipe->bottom_pipe;
1718 					}
1719 
1720 					memcpy(&pipe->bottom_pipe->stream_res, &pipe->bottom_pipe->top_pipe->stream_res, sizeof(struct stream_resource));
1721 				}
1722 			}
1723 
1724 			if (pipe->top_pipe) {
1725 				pipe->top_pipe->bottom_pipe = NULL;
1726 			}
1727 
1728 			pipe->bottom_pipe = NULL;
1729 			pipe->next_odm_pipe = NULL;
1730 			pipe->plane_state = NULL;
1731 			pipe->stream = NULL;
1732 			pipe->top_pipe = NULL;
1733 			pipe->prev_odm_pipe = NULL;
1734 			if (pipe->stream_res.dsc)
1735 				dcn20_release_dsc(&context->res_ctx, dc->res_pool, &pipe->stream_res.dsc);
1736 			memset(&pipe->plane_res, 0, sizeof(pipe->plane_res));
1737 			memset(&pipe->stream_res, 0, sizeof(pipe->stream_res));
1738 			memset(&pipe->link_res, 0, sizeof(pipe->link_res));
1739 			repopulate_pipes = true;
1740 		} else if (pipe->top_pipe && pipe->top_pipe->plane_state == pipe->plane_state) {
1741 			struct pipe_ctx *top_pipe = pipe->top_pipe;
1742 			struct pipe_ctx *bottom_pipe = pipe->bottom_pipe;
1743 
1744 			top_pipe->bottom_pipe = bottom_pipe;
1745 			if (bottom_pipe)
1746 				bottom_pipe->top_pipe = top_pipe;
1747 
1748 			pipe->top_pipe = NULL;
1749 			pipe->bottom_pipe = NULL;
1750 			pipe->plane_state = NULL;
1751 			pipe->stream = NULL;
1752 			memset(&pipe->plane_res, 0, sizeof(pipe->plane_res));
1753 			memset(&pipe->stream_res, 0, sizeof(pipe->stream_res));
1754 			memset(&pipe->link_res, 0, sizeof(pipe->link_res));
1755 			repopulate_pipes = true;
1756 		} else
1757 			ASSERT(0); /* Should never try to merge master pipe */
1758 
1759 	}
1760 
1761 	for (i = 0, pipe_idx = -1; i < dc->res_pool->pipe_count; i++) {
1762 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1763 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1764 		struct pipe_ctx *hsplit_pipe = NULL;
1765 		bool odm;
1766 		int old_index = -1;
1767 
1768 		if (!pipe->stream || newly_split[i])
1769 			continue;
1770 
1771 		pipe_idx++;
1772 		odm = vba->ODMCombineEnabled[vba->pipe_plane[pipe_idx]] != dm_odm_combine_mode_disabled;
1773 
1774 		if (!pipe->plane_state && !odm)
1775 			continue;
1776 
1777 		if (split[i]) {
1778 			if (odm) {
1779 				if (split[i] == 4 && old_pipe->next_odm_pipe && old_pipe->next_odm_pipe->next_odm_pipe)
1780 					old_index = old_pipe->next_odm_pipe->next_odm_pipe->pipe_idx;
1781 				else if (old_pipe->next_odm_pipe)
1782 					old_index = old_pipe->next_odm_pipe->pipe_idx;
1783 			} else {
1784 				if (split[i] == 4 && old_pipe->bottom_pipe && old_pipe->bottom_pipe->bottom_pipe &&
1785 						old_pipe->bottom_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
1786 					old_index = old_pipe->bottom_pipe->bottom_pipe->pipe_idx;
1787 				else if (old_pipe->bottom_pipe &&
1788 						old_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
1789 					old_index = old_pipe->bottom_pipe->pipe_idx;
1790 			}
1791 			hsplit_pipe = dcn32_find_split_pipe(dc, context, old_index);
1792 			ASSERT(hsplit_pipe);
1793 			if (!hsplit_pipe)
1794 				goto validate_fail;
1795 
1796 			if (!dcn32_split_stream_for_mpc_or_odm(
1797 					dc, &context->res_ctx,
1798 					pipe, hsplit_pipe, odm))
1799 				goto validate_fail;
1800 
1801 			newly_split[hsplit_pipe->pipe_idx] = true;
1802 			repopulate_pipes = true;
1803 		}
1804 		if (split[i] == 4) {
1805 			struct pipe_ctx *pipe_4to1;
1806 
1807 			if (odm && old_pipe->next_odm_pipe)
1808 				old_index = old_pipe->next_odm_pipe->pipe_idx;
1809 			else if (!odm && old_pipe->bottom_pipe &&
1810 						old_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
1811 				old_index = old_pipe->bottom_pipe->pipe_idx;
1812 			else
1813 				old_index = -1;
1814 			pipe_4to1 = dcn32_find_split_pipe(dc, context, old_index);
1815 			ASSERT(pipe_4to1);
1816 			if (!pipe_4to1)
1817 				goto validate_fail;
1818 			if (!dcn32_split_stream_for_mpc_or_odm(
1819 					dc, &context->res_ctx,
1820 					pipe, pipe_4to1, odm))
1821 				goto validate_fail;
1822 			newly_split[pipe_4to1->pipe_idx] = true;
1823 
1824 			if (odm && old_pipe->next_odm_pipe && old_pipe->next_odm_pipe->next_odm_pipe
1825 					&& old_pipe->next_odm_pipe->next_odm_pipe->next_odm_pipe)
1826 				old_index = old_pipe->next_odm_pipe->next_odm_pipe->next_odm_pipe->pipe_idx;
1827 			else if (!odm && old_pipe->bottom_pipe && old_pipe->bottom_pipe->bottom_pipe &&
1828 					old_pipe->bottom_pipe->bottom_pipe->bottom_pipe &&
1829 					old_pipe->bottom_pipe->bottom_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
1830 				old_index = old_pipe->bottom_pipe->bottom_pipe->bottom_pipe->pipe_idx;
1831 			else
1832 				old_index = -1;
1833 			pipe_4to1 = dcn32_find_split_pipe(dc, context, old_index);
1834 			ASSERT(pipe_4to1);
1835 			if (!pipe_4to1)
1836 				goto validate_fail;
1837 			if (!dcn32_split_stream_for_mpc_or_odm(
1838 					dc, &context->res_ctx,
1839 					hsplit_pipe, pipe_4to1, odm))
1840 				goto validate_fail;
1841 			newly_split[pipe_4to1->pipe_idx] = true;
1842 		}
1843 		if (odm)
1844 			dcn20_build_mapped_resource(dc, context, pipe->stream);
1845 	}
1846 
1847 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1848 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1849 
1850 		if (pipe->plane_state) {
1851 			if (!resource_build_scaling_params(pipe))
1852 				goto validate_fail;
1853 		}
1854 	}
1855 
1856 	/* Actual dsc count per stream dsc validation*/
1857 	if (!dcn20_validate_dsc(dc, context)) {
1858 		vba->ValidationStatus[vba->soc.num_states] = DML_FAIL_DSC_VALIDATION_FAILURE;
1859 		goto validate_fail;
1860 	}
1861 
1862 	if (repopulate_pipes) {
1863 		int flag_max_mpc_comb = vba->maxMpcComb;
1864 		int flag_vlevel = vlevel;
1865 		int i;
1866 
1867 		pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, fast_validate);
1868 
1869 		/* repopulate_pipes = 1 means the pipes were either split or merged. In this case
1870 		 * we have to re-calculate the DET allocation and run through DML once more to
1871 		 * ensure all the params are calculated correctly. We do not need to run the
1872 		 * pipe split check again after this call (pipes are already split / merged).
1873 		 * */
1874 		context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
1875 					dm_prefetch_support_uclk_fclk_and_stutter_if_possible;
1876 		vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
1877 		if (vlevel == context->bw_ctx.dml.soc.num_states) {
1878 			/* failed after DET size changes */
1879 			goto validate_fail;
1880 		} else if (flag_max_mpc_comb == 0 &&
1881 				flag_max_mpc_comb != context->bw_ctx.dml.vba.maxMpcComb) {
1882 			/* check the context constructed with pipe split flags is still valid*/
1883 			bool flags_valid = false;
1884 			for (i = flag_vlevel; i < context->bw_ctx.dml.soc.num_states; i++) {
1885 				if (vba->ModeSupport[i][flag_max_mpc_comb]) {
1886 					vba->maxMpcComb = flag_max_mpc_comb;
1887 					vba->VoltageLevel = i;
1888 					vlevel = i;
1889 					flags_valid = true;
1890 				}
1891 			}
1892 
1893 			/* this should never happen */
1894 			if (!flags_valid)
1895 				goto validate_fail;
1896 		}
1897 	}
1898 	*vlevel_out = vlevel;
1899 	*pipe_cnt_out = pipe_cnt;
1900 
1901 	out = true;
1902 	goto validate_out;
1903 
1904 validate_fail:
1905 	out = false;
1906 
1907 validate_out:
1908 	return out;
1909 }
1910 
1911 
1912 void dcn32_calculate_wm_and_dlg_fpu(struct dc *dc, struct dc_state *context,
1913 				display_e2e_pipe_params_st *pipes,
1914 				int pipe_cnt,
1915 				int vlevel)
1916 {
1917 	int i, pipe_idx, vlevel_temp = 0;
1918 	double dcfclk = dcn3_2_soc.clock_limits[0].dcfclk_mhz;
1919 	double dcfclk_from_validation = context->bw_ctx.dml.vba.DCFCLKState[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
1920 	double dcfclk_from_fw_based_mclk_switching = dcfclk_from_validation;
1921 	bool pstate_en = context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] !=
1922 			dm_dram_clock_change_unsupported;
1923 	unsigned int dummy_latency_index = 0;
1924 	int maxMpcComb = context->bw_ctx.dml.vba.maxMpcComb;
1925 	unsigned int min_dram_speed_mts = context->bw_ctx.dml.vba.DRAMSpeed;
1926 	bool subvp_in_use = dcn32_subvp_in_use(dc, context);
1927 	unsigned int min_dram_speed_mts_margin;
1928 	bool need_fclk_lat_as_dummy = false;
1929 	bool is_subvp_p_drr = false;
1930 
1931 	dc_assert_fp_enabled();
1932 
1933 	/* need to find dummy latency index for subvp */
1934 	if (subvp_in_use) {
1935 		/* Override DRAMClockChangeSupport for SubVP + DRR case where the DRR cannot switch without stretching it's VBLANK */
1936 		if (!pstate_en) {
1937 			context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][maxMpcComb] = dm_dram_clock_change_vblank_w_mall_sub_vp;
1938 			context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final = dm_prefetch_support_fclk_and_stutter;
1939 			pstate_en = true;
1940 			is_subvp_p_drr = true;
1941 		}
1942 		dummy_latency_index = dcn32_find_dummy_latency_index_for_fw_based_mclk_switch(dc,
1943 						context, pipes, pipe_cnt, vlevel);
1944 
1945 		/* For DCN32/321 need to validate with fclk pstate change latency equal to dummy so prefetch is
1946 		 * scheduled correctly to account for dummy pstate.
1947 		 */
1948 		if (context->bw_ctx.dml.soc.fclk_change_latency_us < dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us) {
1949 			need_fclk_lat_as_dummy = true;
1950 			context->bw_ctx.dml.soc.fclk_change_latency_us =
1951 					dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
1952 		}
1953 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
1954 							dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
1955 		dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel, false);
1956 		maxMpcComb = context->bw_ctx.dml.vba.maxMpcComb;
1957 		if (is_subvp_p_drr) {
1958 			context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][maxMpcComb] = dm_dram_clock_change_vblank_w_mall_sub_vp;
1959 		}
1960 	}
1961 
1962 	context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching = false;
1963 
1964 	if (!pstate_en || (!dc->debug.disable_fpo_optimizations &&
1965 			pstate_en && vlevel != 0)) {
1966 		/* only when the mclk switch can not be natural, is the fw based vblank stretch attempted */
1967 		context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching =
1968 			dcn30_can_support_mclk_switch_using_fw_based_vblank_stretch(dc, context);
1969 
1970 		if (context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching) {
1971 			dummy_latency_index = dcn32_find_dummy_latency_index_for_fw_based_mclk_switch(dc,
1972 				context, pipes, pipe_cnt, vlevel);
1973 
1974 			/* After calling dcn30_find_dummy_latency_index_for_fw_based_mclk_switch
1975 			 * we reinstate the original dram_clock_change_latency_us on the context
1976 			 * and all variables that may have changed up to this point, except the
1977 			 * newly found dummy_latency_index
1978 			 */
1979 			context->bw_ctx.dml.soc.dram_clock_change_latency_us =
1980 					dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
1981 			/* For DCN32/321 need to validate with fclk pstate change latency equal to dummy so
1982 			 * prefetch is scheduled correctly to account for dummy pstate.
1983 			 */
1984 			if (context->bw_ctx.dml.soc.fclk_change_latency_us < dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us) {
1985 				need_fclk_lat_as_dummy = true;
1986 				context->bw_ctx.dml.soc.fclk_change_latency_us =
1987 						dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
1988 			}
1989 			dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel_temp, false);
1990 			if (vlevel_temp < vlevel) {
1991 				vlevel = vlevel_temp;
1992 				maxMpcComb = context->bw_ctx.dml.vba.maxMpcComb;
1993 				dcfclk_from_fw_based_mclk_switching = context->bw_ctx.dml.vba.DCFCLKState[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
1994 				pstate_en = true;
1995 			} else {
1996 				/* Restore FCLK latency and re-run validation to go back to original validation
1997 				 * output if we find that enabling FPO does not give us any benefit (i.e. lower
1998 				 * voltage level)
1999 				 */
2000 				context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching = false;
2001 				context->bw_ctx.dml.soc.fclk_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.fclk_change_latency_us;
2002 				dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel, false);
2003 			}
2004 		}
2005 	}
2006 
2007 	/* Set B:
2008 	 * For Set B calculations use clocks from clock_limits[2] when available i.e. when SMU is present,
2009 	 * otherwise use arbitrary low value from spreadsheet for DCFCLK as lower is safer for watermark
2010 	 * calculations to cover bootup clocks.
2011 	 * DCFCLK: soc.clock_limits[2] when available
2012 	 * UCLK: soc.clock_limits[2] when available
2013 	 */
2014 	if (dcn3_2_soc.num_states > 2) {
2015 		vlevel_temp = 2;
2016 		dcfclk = dcn3_2_soc.clock_limits[2].dcfclk_mhz;
2017 	} else
2018 		dcfclk = 615; //DCFCLK Vmin_lv
2019 
2020 	pipes[0].clks_cfg.voltage = vlevel_temp;
2021 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
2022 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel_temp].socclk_mhz;
2023 
2024 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].valid) {
2025 		context->bw_ctx.dml.soc.dram_clock_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].dml_input.pstate_latency_us;
2026 		context->bw_ctx.dml.soc.fclk_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].dml_input.fclk_change_latency_us;
2027 		context->bw_ctx.dml.soc.sr_enter_plus_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].dml_input.sr_enter_plus_exit_time_us;
2028 		context->bw_ctx.dml.soc.sr_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].dml_input.sr_exit_time_us;
2029 	}
2030 	context->bw_ctx.bw.dcn.watermarks.b.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2031 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.cstate_enter_plus_exit_ns = get_wm_stutter_enter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2032 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2033 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.pstate_change_ns = get_wm_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2034 	context->bw_ctx.bw.dcn.watermarks.b.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2035 	context->bw_ctx.bw.dcn.watermarks.b.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2036 	context->bw_ctx.bw.dcn.watermarks.b.frac_urg_bw_flip = get_fraction_of_urgent_bandwidth_imm_flip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2037 	context->bw_ctx.bw.dcn.watermarks.b.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2038 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2039 	context->bw_ctx.bw.dcn.watermarks.b.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2040 
2041 	/* Set D:
2042 	 * All clocks min.
2043 	 * DCFCLK: Min, as reported by PM FW when available
2044 	 * UCLK  : Min, as reported by PM FW when available
2045 	 * sr_enter_exit/sr_exit should be lower than used for DRAM (TBD after bringup or later, use as decided in Clk Mgr)
2046 	 */
2047 
2048 	if (dcn3_2_soc.num_states > 2) {
2049 		vlevel_temp = 0;
2050 		dcfclk = dc->clk_mgr->bw_params->clk_table.entries[0].dcfclk_mhz;
2051 	} else
2052 		dcfclk = 615; //DCFCLK Vmin_lv
2053 
2054 	pipes[0].clks_cfg.voltage = vlevel_temp;
2055 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
2056 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel_temp].socclk_mhz;
2057 
2058 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].valid) {
2059 		context->bw_ctx.dml.soc.dram_clock_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].dml_input.pstate_latency_us;
2060 		context->bw_ctx.dml.soc.fclk_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].dml_input.fclk_change_latency_us;
2061 		context->bw_ctx.dml.soc.sr_enter_plus_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].dml_input.sr_enter_plus_exit_time_us;
2062 		context->bw_ctx.dml.soc.sr_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].dml_input.sr_exit_time_us;
2063 	}
2064 	context->bw_ctx.bw.dcn.watermarks.d.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2065 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.cstate_enter_plus_exit_ns = get_wm_stutter_enter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2066 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2067 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.pstate_change_ns = get_wm_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2068 	context->bw_ctx.bw.dcn.watermarks.d.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2069 	context->bw_ctx.bw.dcn.watermarks.d.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2070 	context->bw_ctx.bw.dcn.watermarks.d.frac_urg_bw_flip = get_fraction_of_urgent_bandwidth_imm_flip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2071 	context->bw_ctx.bw.dcn.watermarks.d.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2072 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2073 	context->bw_ctx.bw.dcn.watermarks.d.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2074 
2075 	/* Set C, for Dummy P-State:
2076 	 * All clocks min.
2077 	 * DCFCLK: Min, as reported by PM FW, when available
2078 	 * UCLK  : Min,  as reported by PM FW, when available
2079 	 * pstate latency as per UCLK state dummy pstate latency
2080 	 */
2081 
2082 	// For Set A and Set C use values from validation
2083 	pipes[0].clks_cfg.voltage = vlevel;
2084 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk_from_validation;
2085 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel].socclk_mhz;
2086 
2087 	if (context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching) {
2088 		pipes[0].clks_cfg.dcfclk_mhz = dcfclk_from_fw_based_mclk_switching;
2089 	}
2090 
2091 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].valid) {
2092 		min_dram_speed_mts = context->bw_ctx.dml.vba.DRAMSpeed;
2093 		min_dram_speed_mts_margin = 160;
2094 
2095 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
2096 			dc->clk_mgr->bw_params->dummy_pstate_table[0].dummy_pstate_latency_us;
2097 
2098 		if (context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][maxMpcComb] ==
2099 			dm_dram_clock_change_unsupported) {
2100 			int min_dram_speed_mts_offset = dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_memclk_levels - 1;
2101 
2102 			min_dram_speed_mts =
2103 				dc->clk_mgr->bw_params->clk_table.entries[min_dram_speed_mts_offset].memclk_mhz * 16;
2104 		}
2105 
2106 		if (!context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching && !subvp_in_use) {
2107 			/* find largest table entry that is lower than dram speed,
2108 			 * but lower than DPM0 still uses DPM0
2109 			 */
2110 			for (dummy_latency_index = 3; dummy_latency_index > 0; dummy_latency_index--)
2111 				if (min_dram_speed_mts + min_dram_speed_mts_margin >
2112 					dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dram_speed_mts)
2113 					break;
2114 		}
2115 
2116 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
2117 			dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
2118 
2119 		context->bw_ctx.dml.soc.fclk_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].dml_input.fclk_change_latency_us;
2120 		context->bw_ctx.dml.soc.sr_enter_plus_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].dml_input.sr_enter_plus_exit_time_us;
2121 		context->bw_ctx.dml.soc.sr_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].dml_input.sr_exit_time_us;
2122 	}
2123 
2124 	context->bw_ctx.bw.dcn.watermarks.c.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2125 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.cstate_enter_plus_exit_ns = get_wm_stutter_enter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2126 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2127 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.pstate_change_ns = get_wm_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2128 	context->bw_ctx.bw.dcn.watermarks.c.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2129 	context->bw_ctx.bw.dcn.watermarks.c.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2130 	context->bw_ctx.bw.dcn.watermarks.c.frac_urg_bw_flip = get_fraction_of_urgent_bandwidth_imm_flip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2131 	context->bw_ctx.bw.dcn.watermarks.c.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2132 	/* On DCN32/321, PMFW will set PSTATE_CHANGE_TYPE = 1 (FCLK) for UCLK dummy p-state.
2133 	 * In this case we must program FCLK WM Set C to use the UCLK dummy p-state WM
2134 	 * value.
2135 	 */
2136 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.fclk_pstate_change_ns = get_wm_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2137 	context->bw_ctx.bw.dcn.watermarks.c.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2138 
2139 	if ((!pstate_en) && (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].valid)) {
2140 		/* The only difference between A and C is p-state latency, if p-state is not supported
2141 		 * with full p-state latency we want to calculate DLG based on dummy p-state latency,
2142 		 * Set A p-state watermark set to 0 on DCN30, when p-state unsupported, for now keep as DCN30.
2143 		 */
2144 		context->bw_ctx.bw.dcn.watermarks.a = context->bw_ctx.bw.dcn.watermarks.c;
2145 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns = 0;
2146 		/* Calculate FCLK p-state change watermark based on FCLK pstate change latency in case
2147 		 * UCLK p-state is not supported, to avoid underflow in case FCLK pstate is supported
2148 		 */
2149 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2150 	} else {
2151 		/* Set A:
2152 		 * All clocks min.
2153 		 * DCFCLK: Min, as reported by PM FW, when available
2154 		 * UCLK: Min, as reported by PM FW, when available
2155 		 */
2156 		dc->res_pool->funcs->update_soc_for_wm_a(dc, context);
2157 		context->bw_ctx.bw.dcn.watermarks.a.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2158 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.cstate_enter_plus_exit_ns = get_wm_stutter_enter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2159 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2160 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns = get_wm_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2161 		context->bw_ctx.bw.dcn.watermarks.a.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2162 		context->bw_ctx.bw.dcn.watermarks.a.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2163 		context->bw_ctx.bw.dcn.watermarks.a.frac_urg_bw_flip = get_fraction_of_urgent_bandwidth_imm_flip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2164 		context->bw_ctx.bw.dcn.watermarks.a.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2165 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2166 		context->bw_ctx.bw.dcn.watermarks.a.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2167 	}
2168 
2169 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2170 		if (!context->res_ctx.pipe_ctx[i].stream)
2171 			continue;
2172 
2173 		pipes[pipe_idx].clks_cfg.dispclk_mhz = get_dispclk_calculated(&context->bw_ctx.dml, pipes, pipe_cnt);
2174 		pipes[pipe_idx].clks_cfg.dppclk_mhz = get_dppclk_calculated(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
2175 
2176 		if (dc->config.forced_clocks) {
2177 			pipes[pipe_idx].clks_cfg.dispclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dispclk_mhz;
2178 			pipes[pipe_idx].clks_cfg.dppclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dppclk_mhz;
2179 		}
2180 		if (dc->debug.min_disp_clk_khz > pipes[pipe_idx].clks_cfg.dispclk_mhz * 1000)
2181 			pipes[pipe_idx].clks_cfg.dispclk_mhz = dc->debug.min_disp_clk_khz / 1000.0;
2182 		if (dc->debug.min_dpp_clk_khz > pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000)
2183 			pipes[pipe_idx].clks_cfg.dppclk_mhz = dc->debug.min_dpp_clk_khz / 1000.0;
2184 
2185 		pipe_idx++;
2186 	}
2187 
2188 	context->perf_params.stutter_period_us = context->bw_ctx.dml.vba.StutterPeriod;
2189 
2190 	/* for proper prefetch calculations, if dummy lat > fclk lat, use fclk lat = dummy lat */
2191 	if (need_fclk_lat_as_dummy)
2192 		context->bw_ctx.dml.soc.fclk_change_latency_us =
2193 				dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
2194 
2195 	dcn32_calculate_dlg_params(dc, context, pipes, pipe_cnt, vlevel);
2196 
2197 	if (!pstate_en)
2198 		/* Restore full p-state latency */
2199 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
2200 				dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
2201 
2202 	if (context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching) {
2203 		dcn30_setup_mclk_switch_using_fw_based_vblank_stretch(dc, context);
2204 	}
2205 
2206 	/* revert fclk lat changes if required */
2207 	if (need_fclk_lat_as_dummy)
2208 		context->bw_ctx.dml.soc.fclk_change_latency_us =
2209 				dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.fclk_change_latency_us;
2210 }
2211 
2212 static void dcn32_get_optimal_dcfclk_fclk_for_uclk(unsigned int uclk_mts,
2213 		unsigned int *optimal_dcfclk,
2214 		unsigned int *optimal_fclk)
2215 {
2216 	double bw_from_dram, bw_from_dram1, bw_from_dram2;
2217 
2218 	bw_from_dram1 = uclk_mts * dcn3_2_soc.num_chans *
2219 		dcn3_2_soc.dram_channel_width_bytes * (dcn3_2_soc.max_avg_dram_bw_use_normal_percent / 100);
2220 	bw_from_dram2 = uclk_mts * dcn3_2_soc.num_chans *
2221 		dcn3_2_soc.dram_channel_width_bytes * (dcn3_2_soc.max_avg_sdp_bw_use_normal_percent / 100);
2222 
2223 	bw_from_dram = (bw_from_dram1 < bw_from_dram2) ? bw_from_dram1 : bw_from_dram2;
2224 
2225 	if (optimal_fclk)
2226 		*optimal_fclk = bw_from_dram /
2227 		(dcn3_2_soc.fabric_datapath_to_dcn_data_return_bytes * (dcn3_2_soc.max_avg_sdp_bw_use_normal_percent / 100));
2228 
2229 	if (optimal_dcfclk)
2230 		*optimal_dcfclk =  bw_from_dram /
2231 		(dcn3_2_soc.return_bus_width_bytes * (dcn3_2_soc.max_avg_sdp_bw_use_normal_percent / 100));
2232 }
2233 
2234 static void remove_entry_from_table_at_index(struct _vcs_dpi_voltage_scaling_st *table, unsigned int *num_entries,
2235 		unsigned int index)
2236 {
2237 	int i;
2238 
2239 	if (*num_entries == 0)
2240 		return;
2241 
2242 	for (i = index; i < *num_entries - 1; i++) {
2243 		table[i] = table[i + 1];
2244 	}
2245 	memset(&table[--(*num_entries)], 0, sizeof(struct _vcs_dpi_voltage_scaling_st));
2246 }
2247 
2248 void dcn32_patch_dpm_table(struct clk_bw_params *bw_params)
2249 {
2250 	int i;
2251 	unsigned int max_dcfclk_mhz = 0, max_dispclk_mhz = 0, max_dppclk_mhz = 0,
2252 			max_phyclk_mhz = 0, max_dtbclk_mhz = 0, max_fclk_mhz = 0, max_uclk_mhz = 0;
2253 
2254 	for (i = 0; i < MAX_NUM_DPM_LVL; i++) {
2255 		if (bw_params->clk_table.entries[i].dcfclk_mhz > max_dcfclk_mhz)
2256 			max_dcfclk_mhz = bw_params->clk_table.entries[i].dcfclk_mhz;
2257 		if (bw_params->clk_table.entries[i].fclk_mhz > max_fclk_mhz)
2258 			max_fclk_mhz = bw_params->clk_table.entries[i].fclk_mhz;
2259 		if (bw_params->clk_table.entries[i].memclk_mhz > max_uclk_mhz)
2260 			max_uclk_mhz = bw_params->clk_table.entries[i].memclk_mhz;
2261 		if (bw_params->clk_table.entries[i].dispclk_mhz > max_dispclk_mhz)
2262 			max_dispclk_mhz = bw_params->clk_table.entries[i].dispclk_mhz;
2263 		if (bw_params->clk_table.entries[i].dppclk_mhz > max_dppclk_mhz)
2264 			max_dppclk_mhz = bw_params->clk_table.entries[i].dppclk_mhz;
2265 		if (bw_params->clk_table.entries[i].phyclk_mhz > max_phyclk_mhz)
2266 			max_phyclk_mhz = bw_params->clk_table.entries[i].phyclk_mhz;
2267 		if (bw_params->clk_table.entries[i].dtbclk_mhz > max_dtbclk_mhz)
2268 			max_dtbclk_mhz = bw_params->clk_table.entries[i].dtbclk_mhz;
2269 	}
2270 
2271 	/* Scan through clock values we currently have and if they are 0,
2272 	 *  then populate it with dcn3_2_soc.clock_limits[] value.
2273 	 *
2274 	 * Do it for DCFCLK, DISPCLK, DTBCLK and UCLK as any of those being
2275 	 *  0, will cause it to skip building the clock table.
2276 	 */
2277 	if (max_dcfclk_mhz == 0)
2278 		bw_params->clk_table.entries[0].dcfclk_mhz = dcn3_2_soc.clock_limits[0].dcfclk_mhz;
2279 	if (max_dispclk_mhz == 0)
2280 		bw_params->clk_table.entries[0].dispclk_mhz = dcn3_2_soc.clock_limits[0].dispclk_mhz;
2281 	if (max_dtbclk_mhz == 0)
2282 		bw_params->clk_table.entries[0].dtbclk_mhz = dcn3_2_soc.clock_limits[0].dtbclk_mhz;
2283 	if (max_uclk_mhz == 0)
2284 		bw_params->clk_table.entries[0].memclk_mhz = dcn3_2_soc.clock_limits[0].dram_speed_mts / 16;
2285 }
2286 
2287 static int build_synthetic_soc_states(struct clk_bw_params *bw_params,
2288 		struct _vcs_dpi_voltage_scaling_st *table, unsigned int *num_entries)
2289 {
2290 	int i, j;
2291 	struct _vcs_dpi_voltage_scaling_st entry = {0};
2292 
2293 	unsigned int max_dcfclk_mhz = 0, max_dispclk_mhz = 0, max_dppclk_mhz = 0,
2294 			max_phyclk_mhz = 0, max_dtbclk_mhz = 0, max_fclk_mhz = 0, max_uclk_mhz = 0;
2295 
2296 	unsigned int min_dcfclk_mhz = 199, min_fclk_mhz = 299;
2297 
2298 	static const unsigned int num_dcfclk_stas = 5;
2299 	unsigned int dcfclk_sta_targets[DC__VOLTAGE_STATES] = {199, 615, 906, 1324, 1564};
2300 
2301 	unsigned int num_uclk_dpms = 0;
2302 	unsigned int num_fclk_dpms = 0;
2303 	unsigned int num_dcfclk_dpms = 0;
2304 
2305 	for (i = 0; i < MAX_NUM_DPM_LVL; i++) {
2306 		if (bw_params->clk_table.entries[i].dcfclk_mhz > max_dcfclk_mhz)
2307 			max_dcfclk_mhz = bw_params->clk_table.entries[i].dcfclk_mhz;
2308 		if (bw_params->clk_table.entries[i].fclk_mhz > max_fclk_mhz)
2309 			max_fclk_mhz = bw_params->clk_table.entries[i].fclk_mhz;
2310 		if (bw_params->clk_table.entries[i].memclk_mhz > max_uclk_mhz)
2311 			max_uclk_mhz = bw_params->clk_table.entries[i].memclk_mhz;
2312 		if (bw_params->clk_table.entries[i].dispclk_mhz > max_dispclk_mhz)
2313 			max_dispclk_mhz = bw_params->clk_table.entries[i].dispclk_mhz;
2314 		if (bw_params->clk_table.entries[i].dppclk_mhz > max_dppclk_mhz)
2315 			max_dppclk_mhz = bw_params->clk_table.entries[i].dppclk_mhz;
2316 		if (bw_params->clk_table.entries[i].phyclk_mhz > max_phyclk_mhz)
2317 			max_phyclk_mhz = bw_params->clk_table.entries[i].phyclk_mhz;
2318 		if (bw_params->clk_table.entries[i].dtbclk_mhz > max_dtbclk_mhz)
2319 			max_dtbclk_mhz = bw_params->clk_table.entries[i].dtbclk_mhz;
2320 
2321 		if (bw_params->clk_table.entries[i].memclk_mhz > 0)
2322 			num_uclk_dpms++;
2323 		if (bw_params->clk_table.entries[i].fclk_mhz > 0)
2324 			num_fclk_dpms++;
2325 		if (bw_params->clk_table.entries[i].dcfclk_mhz > 0)
2326 			num_dcfclk_dpms++;
2327 	}
2328 
2329 	if (num_dcfclk_dpms > 0 && bw_params->clk_table.entries[0].fclk_mhz > min_fclk_mhz)
2330 		min_fclk_mhz = bw_params->clk_table.entries[0].fclk_mhz;
2331 
2332 	if (!max_dcfclk_mhz || !max_dispclk_mhz || !max_dtbclk_mhz)
2333 		return -1;
2334 
2335 	if (max_dppclk_mhz == 0)
2336 		max_dppclk_mhz = max_dispclk_mhz;
2337 
2338 	if (max_fclk_mhz == 0)
2339 		max_fclk_mhz = max_dcfclk_mhz * dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / dcn3_2_soc.pct_ideal_fabric_bw_after_urgent;
2340 
2341 	if (max_phyclk_mhz == 0)
2342 		max_phyclk_mhz = dcn3_2_soc.clock_limits[0].phyclk_mhz;
2343 
2344 	*num_entries = 0;
2345 	entry.dispclk_mhz = max_dispclk_mhz;
2346 	entry.dscclk_mhz = max_dispclk_mhz / 3;
2347 	entry.dppclk_mhz = max_dppclk_mhz;
2348 	entry.dtbclk_mhz = max_dtbclk_mhz;
2349 	entry.phyclk_mhz = max_phyclk_mhz;
2350 	entry.phyclk_d18_mhz = dcn3_2_soc.clock_limits[0].phyclk_d18_mhz;
2351 	entry.phyclk_d32_mhz = dcn3_2_soc.clock_limits[0].phyclk_d32_mhz;
2352 
2353 	// Insert all the DCFCLK STAs
2354 	for (i = 0; i < num_dcfclk_stas; i++) {
2355 		entry.dcfclk_mhz = dcfclk_sta_targets[i];
2356 		entry.fabricclk_mhz = 0;
2357 		entry.dram_speed_mts = 0;
2358 
2359 		insert_entry_into_table_sorted(table, num_entries, &entry);
2360 	}
2361 
2362 	// Insert the max DCFCLK
2363 	entry.dcfclk_mhz = max_dcfclk_mhz;
2364 	entry.fabricclk_mhz = 0;
2365 	entry.dram_speed_mts = 0;
2366 
2367 	insert_entry_into_table_sorted(table, num_entries, &entry);
2368 
2369 	// Insert the UCLK DPMS
2370 	for (i = 0; i < num_uclk_dpms; i++) {
2371 		entry.dcfclk_mhz = 0;
2372 		entry.fabricclk_mhz = 0;
2373 		entry.dram_speed_mts = bw_params->clk_table.entries[i].memclk_mhz * 16;
2374 
2375 		insert_entry_into_table_sorted(table, num_entries, &entry);
2376 	}
2377 
2378 	// If FCLK is coarse grained, insert individual DPMs.
2379 	if (num_fclk_dpms > 2) {
2380 		for (i = 0; i < num_fclk_dpms; i++) {
2381 			entry.dcfclk_mhz = 0;
2382 			entry.fabricclk_mhz = bw_params->clk_table.entries[i].fclk_mhz;
2383 			entry.dram_speed_mts = 0;
2384 
2385 			insert_entry_into_table_sorted(table, num_entries, &entry);
2386 		}
2387 	}
2388 	// If FCLK fine grained, only insert max
2389 	else {
2390 		entry.dcfclk_mhz = 0;
2391 		entry.fabricclk_mhz = max_fclk_mhz;
2392 		entry.dram_speed_mts = 0;
2393 
2394 		insert_entry_into_table_sorted(table, num_entries, &entry);
2395 	}
2396 
2397 	// At this point, the table contains all "points of interest" based on
2398 	// DPMs from PMFW, and STAs.  Table is sorted by BW, and all clock
2399 	// ratios (by derate, are exact).
2400 
2401 	// Remove states that require higher clocks than are supported
2402 	for (i = *num_entries - 1; i >= 0 ; i--) {
2403 		if (table[i].dcfclk_mhz > max_dcfclk_mhz ||
2404 				table[i].fabricclk_mhz > max_fclk_mhz ||
2405 				table[i].dram_speed_mts > max_uclk_mhz * 16)
2406 			remove_entry_from_table_at_index(table, num_entries, i);
2407 	}
2408 
2409 	// At this point, the table only contains supported points of interest
2410 	// it could be used as is, but some states may be redundant due to
2411 	// coarse grained nature of some clocks, so we want to round up to
2412 	// coarse grained DPMs and remove duplicates.
2413 
2414 	// Round up UCLKs
2415 	for (i = *num_entries - 1; i >= 0 ; i--) {
2416 		for (j = 0; j < num_uclk_dpms; j++) {
2417 			if (bw_params->clk_table.entries[j].memclk_mhz * 16 >= table[i].dram_speed_mts) {
2418 				table[i].dram_speed_mts = bw_params->clk_table.entries[j].memclk_mhz * 16;
2419 				break;
2420 			}
2421 		}
2422 	}
2423 
2424 	// If FCLK is coarse grained, round up to next DPMs
2425 	if (num_fclk_dpms > 2) {
2426 		for (i = *num_entries - 1; i >= 0 ; i--) {
2427 			for (j = 0; j < num_fclk_dpms; j++) {
2428 				if (bw_params->clk_table.entries[j].fclk_mhz >= table[i].fabricclk_mhz) {
2429 					table[i].fabricclk_mhz = bw_params->clk_table.entries[j].fclk_mhz;
2430 					break;
2431 				}
2432 			}
2433 		}
2434 	}
2435 	// Otherwise, round up to minimum.
2436 	else {
2437 		for (i = *num_entries - 1; i >= 0 ; i--) {
2438 			if (table[i].fabricclk_mhz < min_fclk_mhz) {
2439 				table[i].fabricclk_mhz = min_fclk_mhz;
2440 			}
2441 		}
2442 	}
2443 
2444 	// Round DCFCLKs up to minimum
2445 	for (i = *num_entries - 1; i >= 0 ; i--) {
2446 		if (table[i].dcfclk_mhz < min_dcfclk_mhz) {
2447 			table[i].dcfclk_mhz = min_dcfclk_mhz;
2448 		}
2449 	}
2450 
2451 	// Remove duplicate states, note duplicate states are always neighbouring since table is sorted.
2452 	i = 0;
2453 	while (i < *num_entries - 1) {
2454 		if (table[i].dcfclk_mhz == table[i + 1].dcfclk_mhz &&
2455 				table[i].fabricclk_mhz == table[i + 1].fabricclk_mhz &&
2456 				table[i].dram_speed_mts == table[i + 1].dram_speed_mts)
2457 			remove_entry_from_table_at_index(table, num_entries, i + 1);
2458 		else
2459 			i++;
2460 	}
2461 
2462 	// Fix up the state indicies
2463 	for (i = *num_entries - 1; i >= 0 ; i--) {
2464 		table[i].state = i;
2465 	}
2466 
2467 	return 0;
2468 }
2469 
2470 /*
2471  * dcn32_update_bw_bounding_box
2472  *
2473  * This would override some dcn3_2 ip_or_soc initial parameters hardcoded from
2474  * spreadsheet with actual values as per dGPU SKU:
2475  * - with passed few options from dc->config
2476  * - with dentist_vco_frequency from Clk Mgr (currently hardcoded, but might
2477  *   need to get it from PM FW)
2478  * - with passed latency values (passed in ns units) in dc-> bb override for
2479  *   debugging purposes
2480  * - with passed latencies from VBIOS (in 100_ns units) if available for
2481  *   certain dGPU SKU
2482  * - with number of DRAM channels from VBIOS (which differ for certain dGPU SKU
2483  *   of the same ASIC)
2484  * - clocks levels with passed clk_table entries from Clk Mgr as reported by PM
2485  *   FW for different clocks (which might differ for certain dGPU SKU of the
2486  *   same ASIC)
2487  */
2488 void dcn32_update_bw_bounding_box_fpu(struct dc *dc, struct clk_bw_params *bw_params)
2489 {
2490 	dc_assert_fp_enabled();
2491 
2492 	if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
2493 		/* Overrides from dc->config options */
2494 		dcn3_2_ip.clamp_min_dcfclk = dc->config.clamp_min_dcfclk;
2495 
2496 		/* Override from passed dc->bb_overrides if available*/
2497 		if ((int)(dcn3_2_soc.sr_exit_time_us * 1000) != dc->bb_overrides.sr_exit_time_ns
2498 				&& dc->bb_overrides.sr_exit_time_ns) {
2499 			dcn3_2_soc.sr_exit_time_us = dc->bb_overrides.sr_exit_time_ns / 1000.0;
2500 		}
2501 
2502 		if ((int)(dcn3_2_soc.sr_enter_plus_exit_time_us * 1000)
2503 				!= dc->bb_overrides.sr_enter_plus_exit_time_ns
2504 				&& dc->bb_overrides.sr_enter_plus_exit_time_ns) {
2505 			dcn3_2_soc.sr_enter_plus_exit_time_us =
2506 				dc->bb_overrides.sr_enter_plus_exit_time_ns / 1000.0;
2507 		}
2508 
2509 		if ((int)(dcn3_2_soc.urgent_latency_us * 1000) != dc->bb_overrides.urgent_latency_ns
2510 			&& dc->bb_overrides.urgent_latency_ns) {
2511 			dcn3_2_soc.urgent_latency_us = dc->bb_overrides.urgent_latency_ns / 1000.0;
2512 			dcn3_2_soc.urgent_latency_pixel_data_only_us = dc->bb_overrides.urgent_latency_ns / 1000.0;
2513 		}
2514 
2515 		if ((int)(dcn3_2_soc.dram_clock_change_latency_us * 1000)
2516 				!= dc->bb_overrides.dram_clock_change_latency_ns
2517 				&& dc->bb_overrides.dram_clock_change_latency_ns) {
2518 			dcn3_2_soc.dram_clock_change_latency_us =
2519 				dc->bb_overrides.dram_clock_change_latency_ns / 1000.0;
2520 		}
2521 
2522 		if ((int)(dcn3_2_soc.fclk_change_latency_us * 1000)
2523 				!= dc->bb_overrides.fclk_clock_change_latency_ns
2524 				&& dc->bb_overrides.fclk_clock_change_latency_ns) {
2525 			dcn3_2_soc.fclk_change_latency_us =
2526 				dc->bb_overrides.fclk_clock_change_latency_ns / 1000;
2527 		}
2528 
2529 		if ((int)(dcn3_2_soc.dummy_pstate_latency_us * 1000)
2530 				!= dc->bb_overrides.dummy_clock_change_latency_ns
2531 				&& dc->bb_overrides.dummy_clock_change_latency_ns) {
2532 			dcn3_2_soc.dummy_pstate_latency_us =
2533 				dc->bb_overrides.dummy_clock_change_latency_ns / 1000.0;
2534 		}
2535 
2536 		/* Override from VBIOS if VBIOS bb_info available */
2537 		if (dc->ctx->dc_bios->funcs->get_soc_bb_info) {
2538 			struct bp_soc_bb_info bb_info = {0};
2539 
2540 			if (dc->ctx->dc_bios->funcs->get_soc_bb_info(dc->ctx->dc_bios, &bb_info) == BP_RESULT_OK) {
2541 				if (bb_info.dram_clock_change_latency_100ns > 0)
2542 					dcn3_2_soc.dram_clock_change_latency_us =
2543 						bb_info.dram_clock_change_latency_100ns * 10;
2544 
2545 				if (bb_info.dram_sr_enter_exit_latency_100ns > 0)
2546 					dcn3_2_soc.sr_enter_plus_exit_time_us =
2547 						bb_info.dram_sr_enter_exit_latency_100ns * 10;
2548 
2549 				if (bb_info.dram_sr_exit_latency_100ns > 0)
2550 					dcn3_2_soc.sr_exit_time_us =
2551 						bb_info.dram_sr_exit_latency_100ns * 10;
2552 			}
2553 		}
2554 
2555 		/* Override from VBIOS for num_chan */
2556 		if (dc->ctx->dc_bios->vram_info.num_chans) {
2557 			dcn3_2_soc.num_chans = dc->ctx->dc_bios->vram_info.num_chans;
2558 			dcn3_2_soc.mall_allocated_for_dcn_mbytes = (double)(dcn32_calc_num_avail_chans_for_mall(dc,
2559 				dc->ctx->dc_bios->vram_info.num_chans) * dc->caps.mall_size_per_mem_channel);
2560 		}
2561 
2562 		if (dc->ctx->dc_bios->vram_info.dram_channel_width_bytes)
2563 			dcn3_2_soc.dram_channel_width_bytes = dc->ctx->dc_bios->vram_info.dram_channel_width_bytes;
2564 	}
2565 
2566 	/* DML DSC delay factor workaround */
2567 	dcn3_2_ip.dsc_delay_factor_wa = dc->debug.dsc_delay_factor_wa_x1000 / 1000.0;
2568 
2569 	dcn3_2_ip.min_prefetch_in_strobe_us = dc->debug.min_prefetch_in_strobe_ns / 1000.0;
2570 
2571 	/* Override dispclk_dppclk_vco_speed_mhz from Clk Mgr */
2572 	dcn3_2_soc.dispclk_dppclk_vco_speed_mhz = dc->clk_mgr->dentist_vco_freq_khz / 1000.0;
2573 	dc->dml.soc.dispclk_dppclk_vco_speed_mhz = dc->clk_mgr->dentist_vco_freq_khz / 1000.0;
2574 
2575 	/* Overrides Clock levelsfrom CLK Mgr table entries as reported by PM FW */
2576 	if ((!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) && (bw_params->clk_table.entries[0].memclk_mhz)) {
2577 		if (dc->debug.use_legacy_soc_bb_mechanism) {
2578 			unsigned int i = 0, j = 0, num_states = 0;
2579 
2580 			unsigned int dcfclk_mhz[DC__VOLTAGE_STATES] = {0};
2581 			unsigned int dram_speed_mts[DC__VOLTAGE_STATES] = {0};
2582 			unsigned int optimal_uclk_for_dcfclk_sta_targets[DC__VOLTAGE_STATES] = {0};
2583 			unsigned int optimal_dcfclk_for_uclk[DC__VOLTAGE_STATES] = {0};
2584 			unsigned int min_dcfclk = UINT_MAX;
2585 			/* Set 199 as first value in STA target array to have a minimum DCFCLK value.
2586 			 * For DCN32 we set min to 199 so minimum FCLK DPM0 (300Mhz can be achieved) */
2587 			unsigned int dcfclk_sta_targets[DC__VOLTAGE_STATES] = {199, 615, 906, 1324, 1564};
2588 			unsigned int num_dcfclk_sta_targets = 4, num_uclk_states = 0;
2589 			unsigned int max_dcfclk_mhz = 0, max_dispclk_mhz = 0, max_dppclk_mhz = 0, max_phyclk_mhz = 0;
2590 
2591 			for (i = 0; i < MAX_NUM_DPM_LVL; i++) {
2592 				if (bw_params->clk_table.entries[i].dcfclk_mhz > max_dcfclk_mhz)
2593 					max_dcfclk_mhz = bw_params->clk_table.entries[i].dcfclk_mhz;
2594 				if (bw_params->clk_table.entries[i].dcfclk_mhz != 0 &&
2595 						bw_params->clk_table.entries[i].dcfclk_mhz < min_dcfclk)
2596 					min_dcfclk = bw_params->clk_table.entries[i].dcfclk_mhz;
2597 				if (bw_params->clk_table.entries[i].dispclk_mhz > max_dispclk_mhz)
2598 					max_dispclk_mhz = bw_params->clk_table.entries[i].dispclk_mhz;
2599 				if (bw_params->clk_table.entries[i].dppclk_mhz > max_dppclk_mhz)
2600 					max_dppclk_mhz = bw_params->clk_table.entries[i].dppclk_mhz;
2601 				if (bw_params->clk_table.entries[i].phyclk_mhz > max_phyclk_mhz)
2602 					max_phyclk_mhz = bw_params->clk_table.entries[i].phyclk_mhz;
2603 			}
2604 			if (min_dcfclk > dcfclk_sta_targets[0])
2605 				dcfclk_sta_targets[0] = min_dcfclk;
2606 			if (!max_dcfclk_mhz)
2607 				max_dcfclk_mhz = dcn3_2_soc.clock_limits[0].dcfclk_mhz;
2608 			if (!max_dispclk_mhz)
2609 				max_dispclk_mhz = dcn3_2_soc.clock_limits[0].dispclk_mhz;
2610 			if (!max_dppclk_mhz)
2611 				max_dppclk_mhz = dcn3_2_soc.clock_limits[0].dppclk_mhz;
2612 			if (!max_phyclk_mhz)
2613 				max_phyclk_mhz = dcn3_2_soc.clock_limits[0].phyclk_mhz;
2614 
2615 			if (max_dcfclk_mhz > dcfclk_sta_targets[num_dcfclk_sta_targets-1]) {
2616 				// If max DCFCLK is greater than the max DCFCLK STA target, insert into the DCFCLK STA target array
2617 				dcfclk_sta_targets[num_dcfclk_sta_targets] = max_dcfclk_mhz;
2618 				num_dcfclk_sta_targets++;
2619 			} else if (max_dcfclk_mhz < dcfclk_sta_targets[num_dcfclk_sta_targets-1]) {
2620 				// If max DCFCLK is less than the max DCFCLK STA target, cap values and remove duplicates
2621 				for (i = 0; i < num_dcfclk_sta_targets; i++) {
2622 					if (dcfclk_sta_targets[i] > max_dcfclk_mhz) {
2623 						dcfclk_sta_targets[i] = max_dcfclk_mhz;
2624 						break;
2625 					}
2626 				}
2627 				// Update size of array since we "removed" duplicates
2628 				num_dcfclk_sta_targets = i + 1;
2629 			}
2630 
2631 			num_uclk_states = bw_params->clk_table.num_entries;
2632 
2633 			// Calculate optimal dcfclk for each uclk
2634 			for (i = 0; i < num_uclk_states; i++) {
2635 				dcn32_get_optimal_dcfclk_fclk_for_uclk(bw_params->clk_table.entries[i].memclk_mhz * 16,
2636 						&optimal_dcfclk_for_uclk[i], NULL);
2637 				if (optimal_dcfclk_for_uclk[i] < bw_params->clk_table.entries[0].dcfclk_mhz) {
2638 					optimal_dcfclk_for_uclk[i] = bw_params->clk_table.entries[0].dcfclk_mhz;
2639 				}
2640 			}
2641 
2642 			// Calculate optimal uclk for each dcfclk sta target
2643 			for (i = 0; i < num_dcfclk_sta_targets; i++) {
2644 				for (j = 0; j < num_uclk_states; j++) {
2645 					if (dcfclk_sta_targets[i] < optimal_dcfclk_for_uclk[j]) {
2646 						optimal_uclk_for_dcfclk_sta_targets[i] =
2647 								bw_params->clk_table.entries[j].memclk_mhz * 16;
2648 						break;
2649 					}
2650 				}
2651 			}
2652 
2653 			i = 0;
2654 			j = 0;
2655 			// create the final dcfclk and uclk table
2656 			while (i < num_dcfclk_sta_targets && j < num_uclk_states && num_states < DC__VOLTAGE_STATES) {
2657 				if (dcfclk_sta_targets[i] < optimal_dcfclk_for_uclk[j] && i < num_dcfclk_sta_targets) {
2658 					dcfclk_mhz[num_states] = dcfclk_sta_targets[i];
2659 					dram_speed_mts[num_states++] = optimal_uclk_for_dcfclk_sta_targets[i++];
2660 				} else {
2661 					if (j < num_uclk_states && optimal_dcfclk_for_uclk[j] <= max_dcfclk_mhz) {
2662 						dcfclk_mhz[num_states] = optimal_dcfclk_for_uclk[j];
2663 						dram_speed_mts[num_states++] = bw_params->clk_table.entries[j++].memclk_mhz * 16;
2664 					} else {
2665 						j = num_uclk_states;
2666 					}
2667 				}
2668 			}
2669 
2670 			while (i < num_dcfclk_sta_targets && num_states < DC__VOLTAGE_STATES) {
2671 				dcfclk_mhz[num_states] = dcfclk_sta_targets[i];
2672 				dram_speed_mts[num_states++] = optimal_uclk_for_dcfclk_sta_targets[i++];
2673 			}
2674 
2675 			while (j < num_uclk_states && num_states < DC__VOLTAGE_STATES &&
2676 					optimal_dcfclk_for_uclk[j] <= max_dcfclk_mhz) {
2677 				dcfclk_mhz[num_states] = optimal_dcfclk_for_uclk[j];
2678 				dram_speed_mts[num_states++] = bw_params->clk_table.entries[j++].memclk_mhz * 16;
2679 			}
2680 
2681 			dcn3_2_soc.num_states = num_states;
2682 			for (i = 0; i < dcn3_2_soc.num_states; i++) {
2683 				dcn3_2_soc.clock_limits[i].state = i;
2684 				dcn3_2_soc.clock_limits[i].dcfclk_mhz = dcfclk_mhz[i];
2685 				dcn3_2_soc.clock_limits[i].fabricclk_mhz = dcfclk_mhz[i];
2686 
2687 				/* Fill all states with max values of all these clocks */
2688 				dcn3_2_soc.clock_limits[i].dispclk_mhz = max_dispclk_mhz;
2689 				dcn3_2_soc.clock_limits[i].dppclk_mhz  = max_dppclk_mhz;
2690 				dcn3_2_soc.clock_limits[i].phyclk_mhz  = max_phyclk_mhz;
2691 				dcn3_2_soc.clock_limits[i].dscclk_mhz  = max_dispclk_mhz / 3;
2692 
2693 				/* Populate from bw_params for DTBCLK, SOCCLK */
2694 				if (i > 0) {
2695 					if (!bw_params->clk_table.entries[i].dtbclk_mhz) {
2696 						dcn3_2_soc.clock_limits[i].dtbclk_mhz  = dcn3_2_soc.clock_limits[i-1].dtbclk_mhz;
2697 					} else {
2698 						dcn3_2_soc.clock_limits[i].dtbclk_mhz  = bw_params->clk_table.entries[i].dtbclk_mhz;
2699 					}
2700 				} else if (bw_params->clk_table.entries[i].dtbclk_mhz) {
2701 					dcn3_2_soc.clock_limits[i].dtbclk_mhz  = bw_params->clk_table.entries[i].dtbclk_mhz;
2702 				}
2703 
2704 				if (!bw_params->clk_table.entries[i].socclk_mhz && i > 0)
2705 					dcn3_2_soc.clock_limits[i].socclk_mhz = dcn3_2_soc.clock_limits[i-1].socclk_mhz;
2706 				else
2707 					dcn3_2_soc.clock_limits[i].socclk_mhz = bw_params->clk_table.entries[i].socclk_mhz;
2708 
2709 				if (!dram_speed_mts[i] && i > 0)
2710 					dcn3_2_soc.clock_limits[i].dram_speed_mts = dcn3_2_soc.clock_limits[i-1].dram_speed_mts;
2711 				else
2712 					dcn3_2_soc.clock_limits[i].dram_speed_mts = dram_speed_mts[i];
2713 
2714 				/* These clocks cannot come from bw_params, always fill from dcn3_2_soc[0] */
2715 				/* PHYCLK_D18, PHYCLK_D32 */
2716 				dcn3_2_soc.clock_limits[i].phyclk_d18_mhz = dcn3_2_soc.clock_limits[0].phyclk_d18_mhz;
2717 				dcn3_2_soc.clock_limits[i].phyclk_d32_mhz = dcn3_2_soc.clock_limits[0].phyclk_d32_mhz;
2718 			}
2719 		} else {
2720 			build_synthetic_soc_states(bw_params, dcn3_2_soc.clock_limits, &dcn3_2_soc.num_states);
2721 		}
2722 
2723 		/* Re-init DML with updated bb */
2724 		dml_init_instance(&dc->dml, &dcn3_2_soc, &dcn3_2_ip, DML_PROJECT_DCN32);
2725 		if (dc->current_state)
2726 			dml_init_instance(&dc->current_state->bw_ctx.dml, &dcn3_2_soc, &dcn3_2_ip, DML_PROJECT_DCN32);
2727 	}
2728 }
2729 
2730 void dcn32_zero_pipe_dcc_fraction(display_e2e_pipe_params_st *pipes,
2731 				  int pipe_cnt)
2732 {
2733 	dc_assert_fp_enabled();
2734 
2735 	pipes[pipe_cnt].pipe.src.dcc_fraction_of_zs_req_luma = 0;
2736 	pipes[pipe_cnt].pipe.src.dcc_fraction_of_zs_req_chroma = 0;
2737 }
2738 
2739 bool dcn32_allow_subvp_with_active_margin(struct pipe_ctx *pipe)
2740 {
2741 	bool allow = false;
2742 	uint32_t refresh_rate = 0;
2743 
2744 	/* Allow subvp on displays that have active margin for 2560x1440@60hz displays
2745 	 * only for now. There must be no scaling as well.
2746 	 *
2747 	 * For now we only enable on 2560x1440@60hz displays to enable 4K60 + 1440p60 configs
2748 	 * for p-state switching.
2749 	 */
2750 	if (pipe->stream && pipe->plane_state) {
2751 		refresh_rate = (pipe->stream->timing.pix_clk_100hz * 100 +
2752 						pipe->stream->timing.v_total * pipe->stream->timing.h_total - 1)
2753 						/ (double)(pipe->stream->timing.v_total * pipe->stream->timing.h_total);
2754 		if (pipe->stream->timing.v_addressable == 1440 &&
2755 				pipe->stream->timing.h_addressable == 2560 &&
2756 				refresh_rate >= 55 && refresh_rate <= 65 &&
2757 				pipe->plane_state->src_rect.height == 1440 &&
2758 				pipe->plane_state->src_rect.width == 2560 &&
2759 				pipe->plane_state->dst_rect.height == 1440 &&
2760 				pipe->plane_state->dst_rect.width == 2560)
2761 			allow = true;
2762 	}
2763 	return allow;
2764 }
2765 
2766 /**
2767  * *******************************************************************************************
2768  * dcn32_determine_max_vratio_prefetch: Determine max Vratio for prefetch by driver policy
2769  *
2770  * @param [in]: dc: Current DC state
2771  * @param [in]: context: New DC state to be programmed
2772  *
2773  * @return: Max vratio for prefetch
2774  *
2775  * *******************************************************************************************
2776  */
2777 double dcn32_determine_max_vratio_prefetch(struct dc *dc, struct dc_state *context)
2778 {
2779 	double max_vratio_pre = __DML_MAX_BW_RATIO_PRE__; // Default value is 4
2780 	int i;
2781 
2782 	/* For single display MPO configs, allow the max vratio to be 8
2783 	 * if any plane is YUV420 format
2784 	 */
2785 	if (context->stream_count == 1 && context->stream_status[0].plane_count > 1) {
2786 		for (i = 0; i < context->stream_status[0].plane_count; i++) {
2787 			if (context->stream_status[0].plane_states[i]->format == SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr ||
2788 					context->stream_status[0].plane_states[i]->format == SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb) {
2789 				max_vratio_pre = __DML_MAX_VRATIO_PRE__;
2790 			}
2791 		}
2792 	}
2793 	return max_vratio_pre;
2794 }
2795