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 #include "dc_state_priv.h"
36 
37 #define DC_LOGGER_INIT(logger)
38 
39 static const struct subvp_high_refresh_list subvp_high_refresh_list = {
40 			.min_refresh = 120,
41 			.max_refresh = 175,
42 			.res = {
43 				{.width = 3840, .height = 2160, },
44 				{.width = 3440, .height = 1440, },
45 				{.width = 2560, .height = 1440, },
46 				{.width = 1920, .height = 1080, }},
47 };
48 
49 static const struct subvp_active_margin_list subvp_active_margin_list = {
50 			.min_refresh = 55,
51 			.max_refresh = 65,
52 			.res = {
53 				{.width = 2560, .height = 1440, },
54 				{.width = 1920, .height = 1080, }},
55 };
56 
57 struct _vcs_dpi_ip_params_st dcn3_2_ip = {
58 	.gpuvm_enable = 0,
59 	.gpuvm_max_page_table_levels = 4,
60 	.hostvm_enable = 0,
61 	.rob_buffer_size_kbytes = 128,
62 	.det_buffer_size_kbytes = DCN3_2_DEFAULT_DET_SIZE,
63 	.config_return_buffer_size_in_kbytes = 1280,
64 	.compressed_buffer_segment_size_in_kbytes = 64,
65 	.meta_fifo_size_in_kentries = 22,
66 	.zero_size_buffer_entries = 512,
67 	.compbuf_reserved_space_64b = 256,
68 	.compbuf_reserved_space_zs = 64,
69 	.dpp_output_buffer_pixels = 2560,
70 	.opp_output_buffer_lines = 1,
71 	.pixel_chunk_size_kbytes = 8,
72 	.alpha_pixel_chunk_size_kbytes = 4,
73 	.min_pixel_chunk_size_bytes = 1024,
74 	.dcc_meta_buffer_size_bytes = 6272,
75 	.meta_chunk_size_kbytes = 2,
76 	.min_meta_chunk_size_bytes = 256,
77 	.writeback_chunk_size_kbytes = 8,
78 	.ptoi_supported = false,
79 	.num_dsc = 4,
80 	.maximum_dsc_bits_per_component = 12,
81 	.maximum_pixels_per_line_per_dsc_unit = 6016,
82 	.dsc422_native_support = true,
83 	.is_line_buffer_bpp_fixed = true,
84 	.line_buffer_fixed_bpp = 57,
85 	.line_buffer_size_bits = 1171920,
86 	.max_line_buffer_lines = 32,
87 	.writeback_interface_buffer_size_kbytes = 90,
88 	.max_num_dpp = 4,
89 	.max_num_otg = 4,
90 	.max_num_hdmi_frl_outputs = 1,
91 	.max_num_wb = 1,
92 	.max_dchub_pscl_bw_pix_per_clk = 4,
93 	.max_pscl_lb_bw_pix_per_clk = 2,
94 	.max_lb_vscl_bw_pix_per_clk = 4,
95 	.max_vscl_hscl_bw_pix_per_clk = 4,
96 	.max_hscl_ratio = 6,
97 	.max_vscl_ratio = 6,
98 	.max_hscl_taps = 8,
99 	.max_vscl_taps = 8,
100 	.dpte_buffer_size_in_pte_reqs_luma = 64,
101 	.dpte_buffer_size_in_pte_reqs_chroma = 34,
102 	.dispclk_ramp_margin_percent = 1,
103 	.max_inter_dcn_tile_repeaters = 8,
104 	.cursor_buffer_size = 16,
105 	.cursor_chunk_size = 2,
106 	.writeback_line_buffer_buffer_size = 0,
107 	.writeback_min_hscl_ratio = 1,
108 	.writeback_min_vscl_ratio = 1,
109 	.writeback_max_hscl_ratio = 1,
110 	.writeback_max_vscl_ratio = 1,
111 	.writeback_max_hscl_taps = 1,
112 	.writeback_max_vscl_taps = 1,
113 	.dppclk_delay_subtotal = 47,
114 	.dppclk_delay_scl = 50,
115 	.dppclk_delay_scl_lb_only = 16,
116 	.dppclk_delay_cnvc_formatter = 28,
117 	.dppclk_delay_cnvc_cursor = 6,
118 	.dispclk_delay_subtotal = 125,
119 	.dynamic_metadata_vm_enabled = false,
120 	.odm_combine_4to1_supported = false,
121 	.dcc_supported = true,
122 	.max_num_dp2p0_outputs = 2,
123 	.max_num_dp2p0_streams = 4,
124 };
125 
126 struct _vcs_dpi_soc_bounding_box_st dcn3_2_soc = {
127 	.clock_limits = {
128 		{
129 			.state = 0,
130 			.dcfclk_mhz = 1564.0,
131 			.fabricclk_mhz = 2500.0,
132 			.dispclk_mhz = 2150.0,
133 			.dppclk_mhz = 2150.0,
134 			.phyclk_mhz = 810.0,
135 			.phyclk_d18_mhz = 667.0,
136 			.phyclk_d32_mhz = 625.0,
137 			.socclk_mhz = 1200.0,
138 			.dscclk_mhz = 716.667,
139 			.dram_speed_mts = 18000.0,
140 			.dtbclk_mhz = 1564.0,
141 		},
142 	},
143 	.num_states = 1,
144 	.sr_exit_time_us = 42.97,
145 	.sr_enter_plus_exit_time_us = 49.94,
146 	.sr_exit_z8_time_us = 285.0,
147 	.sr_enter_plus_exit_z8_time_us = 320,
148 	.writeback_latency_us = 12.0,
149 	.round_trip_ping_latency_dcfclk_cycles = 263,
150 	.urgent_latency_pixel_data_only_us = 4.0,
151 	.urgent_latency_pixel_mixed_with_vm_data_us = 4.0,
152 	.urgent_latency_vm_data_only_us = 4.0,
153 	.fclk_change_latency_us = 25,
154 	.usr_retraining_latency_us = 2,
155 	.smn_latency_us = 2,
156 	.mall_allocated_for_dcn_mbytes = 64,
157 	.urgent_out_of_order_return_per_channel_pixel_only_bytes = 4096,
158 	.urgent_out_of_order_return_per_channel_pixel_and_vm_bytes = 4096,
159 	.urgent_out_of_order_return_per_channel_vm_only_bytes = 4096,
160 	.pct_ideal_sdp_bw_after_urgent = 90.0,
161 	.pct_ideal_fabric_bw_after_urgent = 67.0,
162 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_only = 20.0,
163 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_and_vm = 60.0,
164 	.pct_ideal_dram_sdp_bw_after_urgent_vm_only = 30.0,
165 	.pct_ideal_dram_bw_after_urgent_strobe = 67.0,
166 	.max_avg_sdp_bw_use_normal_percent = 80.0,
167 	.max_avg_fabric_bw_use_normal_percent = 60.0,
168 	.max_avg_dram_bw_use_normal_strobe_percent = 50.0,
169 	.max_avg_dram_bw_use_normal_percent = 15.0,
170 	.num_chans = 24,
171 	.dram_channel_width_bytes = 2,
172 	.fabric_datapath_to_dcn_data_return_bytes = 64,
173 	.return_bus_width_bytes = 64,
174 	.downspread_percent = 0.38,
175 	.dcn_downspread_percent = 0.5,
176 	.dram_clock_change_latency_us = 400,
177 	.dispclk_dppclk_vco_speed_mhz = 4300.0,
178 	.do_urgent_latency_adjustment = true,
179 	.urgent_latency_adjustment_fabric_clock_component_us = 1.0,
180 	.urgent_latency_adjustment_fabric_clock_reference_mhz = 3000,
181 };
182 
183 static bool dcn32_apply_merge_split_flags_helper(struct dc *dc, struct dc_state *context,
184 	bool *repopulate_pipes, int *split, bool *merge);
185 
dcn32_build_wm_range_table_fpu(struct clk_mgr_internal * clk_mgr)186 void dcn32_build_wm_range_table_fpu(struct clk_mgr_internal *clk_mgr)
187 {
188 	/* defaults */
189 	double pstate_latency_us = clk_mgr->base.ctx->dc->dml.soc.dram_clock_change_latency_us;
190 	double fclk_change_latency_us = clk_mgr->base.ctx->dc->dml.soc.fclk_change_latency_us;
191 	double sr_exit_time_us = clk_mgr->base.ctx->dc->dml.soc.sr_exit_time_us;
192 	double sr_enter_plus_exit_time_us = clk_mgr->base.ctx->dc->dml.soc.sr_enter_plus_exit_time_us;
193 	/* For min clocks use as reported by PM FW and report those as min */
194 	uint16_t min_uclk_mhz			= clk_mgr->base.bw_params->clk_table.entries[0].memclk_mhz;
195 	uint16_t min_dcfclk_mhz			= clk_mgr->base.bw_params->clk_table.entries[0].dcfclk_mhz;
196 	uint16_t setb_min_uclk_mhz		= min_uclk_mhz;
197 	uint16_t dcfclk_mhz_for_the_second_state = clk_mgr->base.ctx->dc->dml.soc.clock_limits[2].dcfclk_mhz;
198 
199 	dc_assert_fp_enabled();
200 
201 	/* For Set B ranges use min clocks state 2 when available, and report those to PM FW */
202 	if (dcfclk_mhz_for_the_second_state)
203 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.min_dcfclk = dcfclk_mhz_for_the_second_state;
204 	else
205 		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;
206 
207 	if (clk_mgr->base.bw_params->clk_table.entries[2].memclk_mhz)
208 		setb_min_uclk_mhz = clk_mgr->base.bw_params->clk_table.entries[2].memclk_mhz;
209 
210 	/* Set A - Normal - default values */
211 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].valid = true;
212 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us = pstate_latency_us;
213 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].dml_input.fclk_change_latency_us = fclk_change_latency_us;
214 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].dml_input.sr_exit_time_us = sr_exit_time_us;
215 	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;
216 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.wm_type = WATERMARKS_CLOCK_RANGE;
217 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.min_dcfclk = min_dcfclk_mhz;
218 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.max_dcfclk = 0xFFFF;
219 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.min_uclk = min_uclk_mhz;
220 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_A].pmfw_breakdown.max_uclk = 0xFFFF;
221 
222 	/* Set B - Performance - higher clocks, using DPM[2] DCFCLK and UCLK */
223 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].valid = true;
224 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].dml_input.pstate_latency_us = pstate_latency_us;
225 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].dml_input.fclk_change_latency_us = fclk_change_latency_us;
226 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].dml_input.sr_exit_time_us = sr_exit_time_us;
227 	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;
228 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.wm_type = WATERMARKS_CLOCK_RANGE;
229 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.max_dcfclk = 0xFFFF;
230 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.min_uclk = setb_min_uclk_mhz;
231 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_B].pmfw_breakdown.max_uclk = 0xFFFF;
232 
233 	/* Set C - Dummy P-State - P-State latency set to "dummy p-state" value */
234 	/* 'DalDummyClockChangeLatencyNs' registry key option set to 0x7FFFFFFF can be used to disable Set C for dummy p-state */
235 	if (clk_mgr->base.ctx->dc->bb_overrides.dummy_clock_change_latency_ns != 0x7FFFFFFF) {
236 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].valid = true;
237 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].dml_input.pstate_latency_us = 50;
238 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].dml_input.fclk_change_latency_us = fclk_change_latency_us;
239 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].dml_input.sr_exit_time_us = sr_exit_time_us;
240 		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;
241 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.wm_type = WATERMARKS_DUMMY_PSTATE;
242 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.min_dcfclk = min_dcfclk_mhz;
243 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.max_dcfclk = 0xFFFF;
244 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.min_uclk = min_uclk_mhz;
245 		clk_mgr->base.bw_params->wm_table.nv_entries[WM_C].pmfw_breakdown.max_uclk = 0xFFFF;
246 		clk_mgr->base.bw_params->dummy_pstate_table[0].dram_speed_mts = clk_mgr->base.bw_params->clk_table.entries[0].memclk_mhz * 16;
247 		clk_mgr->base.bw_params->dummy_pstate_table[0].dummy_pstate_latency_us = 50;
248 		clk_mgr->base.bw_params->dummy_pstate_table[1].dram_speed_mts = clk_mgr->base.bw_params->clk_table.entries[1].memclk_mhz * 16;
249 		clk_mgr->base.bw_params->dummy_pstate_table[1].dummy_pstate_latency_us = 9;
250 		clk_mgr->base.bw_params->dummy_pstate_table[2].dram_speed_mts = clk_mgr->base.bw_params->clk_table.entries[2].memclk_mhz * 16;
251 		clk_mgr->base.bw_params->dummy_pstate_table[2].dummy_pstate_latency_us = 8;
252 		clk_mgr->base.bw_params->dummy_pstate_table[3].dram_speed_mts = clk_mgr->base.bw_params->clk_table.entries[3].memclk_mhz * 16;
253 		clk_mgr->base.bw_params->dummy_pstate_table[3].dummy_pstate_latency_us = 5;
254 	}
255 	/* Set D - MALL - SR enter and exit time specific to MALL, TBD after bringup or later phase for now use DRAM values / 2 */
256 	/* For MALL DRAM clock change latency is N/A, for watermak calculations use lowest value dummy P state latency */
257 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].valid = true;
258 	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;
259 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].dml_input.fclk_change_latency_us = fclk_change_latency_us;
260 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].dml_input.sr_exit_time_us = sr_exit_time_us / 2; // TBD
261 	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
262 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.wm_type = WATERMARKS_MALL;
263 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.min_dcfclk = min_dcfclk_mhz;
264 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.max_dcfclk = 0xFFFF;
265 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.min_uclk = min_uclk_mhz;
266 	clk_mgr->base.bw_params->wm_table.nv_entries[WM_D].pmfw_breakdown.max_uclk = 0xFFFF;
267 }
268 
269 /*
270  * Finds dummy_latency_index when MCLK switching using firmware based
271  * vblank stretch is enabled. This function will iterate through the
272  * table of dummy pstate latencies until the lowest value that allows
273  * dm_allow_self_refresh_and_mclk_switch to happen is found
274  */
dcn32_find_dummy_latency_index_for_fw_based_mclk_switch(struct dc * dc,struct dc_state * context,display_e2e_pipe_params_st * pipes,int pipe_cnt,int vlevel)275 int dcn32_find_dummy_latency_index_for_fw_based_mclk_switch(struct dc *dc,
276 							    struct dc_state *context,
277 							    display_e2e_pipe_params_st *pipes,
278 							    int pipe_cnt,
279 							    int vlevel)
280 {
281 	const int max_latency_table_entries = 4;
282 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
283 	int dummy_latency_index = 0;
284 	enum clock_change_support temp_clock_change_support = vba->DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
285 
286 	dc_assert_fp_enabled();
287 
288 	while (dummy_latency_index < max_latency_table_entries) {
289 		if (temp_clock_change_support != dm_dram_clock_change_unsupported)
290 			vba->DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] = temp_clock_change_support;
291 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
292 				dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
293 		dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel, false);
294 
295 		/* for subvp + DRR case, if subvp pipes are still present we support pstate */
296 		if (vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported &&
297 				dcn32_subvp_in_use(dc, context))
298 			vba->DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] = temp_clock_change_support;
299 
300 		if (vlevel < context->bw_ctx.dml.vba.soc.num_states &&
301 				vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] != dm_dram_clock_change_unsupported)
302 			break;
303 
304 		dummy_latency_index++;
305 	}
306 
307 	if (dummy_latency_index == max_latency_table_entries) {
308 		ASSERT(dummy_latency_index != max_latency_table_entries);
309 		/* If the execution gets here, it means dummy p_states are
310 		 * not possible. This should never happen and would mean
311 		 * something is severely wrong.
312 		 * Here we reset dummy_latency_index to 3, because it is
313 		 * better to have underflows than system crashes.
314 		 */
315 		dummy_latency_index = max_latency_table_entries - 1;
316 	}
317 
318 	return dummy_latency_index;
319 }
320 
321 /**
322  * dcn32_helper_populate_phantom_dlg_params - Get DLG params for phantom pipes
323  * and populate pipe_ctx with those params.
324  * @dc: [in] current dc state
325  * @context: [in] new dc state
326  * @pipes: [in] DML pipe params array
327  * @pipe_cnt: [in] DML pipe count
328  *
329  * This function must be called AFTER the phantom pipes are added to context
330  * and run through DML (so that the DLG params for the phantom pipes can be
331  * populated), and BEFORE we program the timing for the phantom pipes.
332  */
dcn32_helper_populate_phantom_dlg_params(struct dc * dc,struct dc_state * context,display_e2e_pipe_params_st * pipes,int pipe_cnt)333 void dcn32_helper_populate_phantom_dlg_params(struct dc *dc,
334 					      struct dc_state *context,
335 					      display_e2e_pipe_params_st *pipes,
336 					      int pipe_cnt)
337 {
338 	uint32_t i, pipe_idx;
339 
340 	dc_assert_fp_enabled();
341 
342 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
343 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
344 
345 		if (!pipe->stream)
346 			continue;
347 
348 		if (pipe->plane_state && dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
349 			pipes[pipe_idx].pipe.dest.vstartup_start =
350 				get_vstartup(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
351 			pipes[pipe_idx].pipe.dest.vupdate_offset =
352 				get_vupdate_offset(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
353 			pipes[pipe_idx].pipe.dest.vupdate_width =
354 				get_vupdate_width(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
355 			pipes[pipe_idx].pipe.dest.vready_offset =
356 				get_vready_offset(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
357 			pipe->pipe_dlg_param = pipes[pipe_idx].pipe.dest;
358 		}
359 		pipe_idx++;
360 	}
361 }
362 
calculate_net_bw_in_kbytes_sec(struct _vcs_dpi_voltage_scaling_st * entry)363 static float calculate_net_bw_in_kbytes_sec(struct _vcs_dpi_voltage_scaling_st *entry)
364 {
365 	float memory_bw_kbytes_sec;
366 	float fabric_bw_kbytes_sec;
367 	float sdp_bw_kbytes_sec;
368 	float limiting_bw_kbytes_sec;
369 
370 	memory_bw_kbytes_sec = entry->dram_speed_mts *
371 				dcn3_2_soc.num_chans *
372 				dcn3_2_soc.dram_channel_width_bytes *
373 				((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100);
374 
375 	fabric_bw_kbytes_sec = entry->fabricclk_mhz *
376 				dcn3_2_soc.return_bus_width_bytes *
377 				((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100);
378 
379 	sdp_bw_kbytes_sec = entry->dcfclk_mhz *
380 				dcn3_2_soc.return_bus_width_bytes *
381 				((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100);
382 
383 	limiting_bw_kbytes_sec = memory_bw_kbytes_sec;
384 
385 	if (fabric_bw_kbytes_sec < limiting_bw_kbytes_sec)
386 		limiting_bw_kbytes_sec = fabric_bw_kbytes_sec;
387 
388 	if (sdp_bw_kbytes_sec < limiting_bw_kbytes_sec)
389 		limiting_bw_kbytes_sec = sdp_bw_kbytes_sec;
390 
391 	return limiting_bw_kbytes_sec;
392 }
393 
get_optimal_ntuple(struct _vcs_dpi_voltage_scaling_st * entry)394 static void get_optimal_ntuple(struct _vcs_dpi_voltage_scaling_st *entry)
395 {
396 	if (entry->dcfclk_mhz > 0) {
397 		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);
398 
399 		entry->fabricclk_mhz = bw_on_sdp / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100));
400 		entry->dram_speed_mts = bw_on_sdp / (dcn3_2_soc.num_chans *
401 				dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100));
402 	} else if (entry->fabricclk_mhz > 0) {
403 		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);
404 
405 		entry->dcfclk_mhz = bw_on_fabric / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100));
406 		entry->dram_speed_mts = bw_on_fabric / (dcn3_2_soc.num_chans *
407 				dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100));
408 	} else if (entry->dram_speed_mts > 0) {
409 		float bw_on_dram = entry->dram_speed_mts * dcn3_2_soc.num_chans *
410 				dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100);
411 
412 		entry->fabricclk_mhz = bw_on_dram / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100));
413 		entry->dcfclk_mhz = bw_on_dram / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100));
414 	}
415 }
416 
insert_entry_into_table_sorted(struct _vcs_dpi_voltage_scaling_st * table,unsigned int * num_entries,struct _vcs_dpi_voltage_scaling_st * entry)417 static void insert_entry_into_table_sorted(struct _vcs_dpi_voltage_scaling_st *table,
418 				    unsigned int *num_entries,
419 				    struct _vcs_dpi_voltage_scaling_st *entry)
420 {
421 	int i = 0;
422 	int index = 0;
423 
424 	dc_assert_fp_enabled();
425 
426 	if (*num_entries == 0) {
427 		table[0] = *entry;
428 		(*num_entries)++;
429 	} else {
430 		while (entry->net_bw_in_kbytes_sec > table[index].net_bw_in_kbytes_sec) {
431 			index++;
432 			if (index >= *num_entries)
433 				break;
434 		}
435 
436 		for (i = *num_entries; i > index; i--)
437 			table[i] = table[i - 1];
438 
439 		table[index] = *entry;
440 		(*num_entries)++;
441 	}
442 }
443 
444 /**
445  * dcn32_set_phantom_stream_timing - Set timing params for the phantom stream
446  * @dc: current dc state
447  * @context: new dc state
448  * @ref_pipe: Main pipe for the phantom stream
449  * @phantom_stream: target phantom stream state
450  * @pipes: DML pipe params
451  * @pipe_cnt: number of DML pipes
452  * @dc_pipe_idx: DC pipe index for the main pipe (i.e. ref_pipe)
453  *
454  * Set timing params of the phantom stream based on calculated output from DML.
455  * This function first gets the DML pipe index using the DC pipe index, then
456  * calls into DML (get_subviewport_lines_needed_in_mall) to get the number of
457  * lines required for SubVP MCLK switching and assigns to the phantom stream
458  * accordingly.
459  *
460  * - The number of SubVP lines calculated in DML does not take into account
461  * FW processing delays and required pstate allow width, so we must include
462  * that separately.
463  *
464  * - Set phantom backporch = vstartup of main pipe
465  */
dcn32_set_phantom_stream_timing(struct dc * dc,struct dc_state * context,struct pipe_ctx * ref_pipe,struct dc_stream_state * phantom_stream,display_e2e_pipe_params_st * pipes,unsigned int pipe_cnt,unsigned int dc_pipe_idx)466 void dcn32_set_phantom_stream_timing(struct dc *dc,
467 				     struct dc_state *context,
468 				     struct pipe_ctx *ref_pipe,
469 				     struct dc_stream_state *phantom_stream,
470 				     display_e2e_pipe_params_st *pipes,
471 				     unsigned int pipe_cnt,
472 				     unsigned int dc_pipe_idx)
473 {
474 	unsigned int i, pipe_idx;
475 	struct pipe_ctx *pipe;
476 	uint32_t phantom_vactive, phantom_bp, pstate_width_fw_delay_lines;
477 	unsigned int num_dpp;
478 	unsigned int vlevel = context->bw_ctx.dml.vba.VoltageLevel;
479 	unsigned int dcfclk = context->bw_ctx.dml.vba.DCFCLKState[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
480 	unsigned int socclk = context->bw_ctx.dml.vba.SOCCLKPerState[vlevel];
481 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
482 	struct dc_stream_state *main_stream = ref_pipe->stream;
483 
484 	dc_assert_fp_enabled();
485 
486 	// Find DML pipe index (pipe_idx) using dc_pipe_idx
487 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
488 		pipe = &context->res_ctx.pipe_ctx[i];
489 
490 		if (!pipe->stream)
491 			continue;
492 
493 		if (i == dc_pipe_idx)
494 			break;
495 
496 		pipe_idx++;
497 	}
498 
499 	// Calculate lines required for pstate allow width and FW processing delays
500 	pstate_width_fw_delay_lines = ((double)(dc->caps.subvp_fw_processing_delay_us +
501 			dc->caps.subvp_pstate_allow_width_us) / 1000000) *
502 			(ref_pipe->stream->timing.pix_clk_100hz * 100) /
503 			(double)ref_pipe->stream->timing.h_total;
504 
505 	// Update clks_cfg for calling into recalculate
506 	pipes[0].clks_cfg.voltage = vlevel;
507 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
508 	pipes[0].clks_cfg.socclk_mhz = socclk;
509 
510 	// DML calculation for MALL region doesn't take into account FW delay
511 	// and required pstate allow width for multi-display cases
512 	/* Add 16 lines margin to the MALL REGION because SUB_VP_START_LINE must be aligned
513 	 * to 2 swaths (i.e. 16 lines)
514 	 */
515 	phantom_vactive = get_subviewport_lines_needed_in_mall(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx) +
516 				pstate_width_fw_delay_lines + dc->caps.subvp_swath_height_margin_lines;
517 
518 	// W/A for DCC corruption with certain high resolution timings.
519 	// Determing if pipesplit is used. If so, add meta_row_height to the phantom vactive.
520 	num_dpp = vba->NoOfDPP[vba->VoltageLevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]];
521 	phantom_vactive += num_dpp > 1 ? vba->meta_row_height[vba->pipe_plane[pipe_idx]] : 0;
522 
523 	/* dc->debug.subvp_extra_lines 0 by default*/
524 	phantom_vactive += dc->debug.subvp_extra_lines;
525 
526 	// For backporch of phantom pipe, use vstartup of the main pipe
527 	phantom_bp = get_vstartup(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
528 
529 	phantom_stream->dst.y = 0;
530 	phantom_stream->dst.height = phantom_vactive;
531 	/* When scaling, DML provides the end to end required number of lines for MALL.
532 	 * dst.height is always correct for this case, but src.height is not which causes a
533 	 * delta between main and phantom pipe scaling outputs. Need to adjust src.height on
534 	 * phantom for this case.
535 	 */
536 	phantom_stream->src.y = 0;
537 	phantom_stream->src.height = (double)phantom_vactive * (double)main_stream->src.height / (double)main_stream->dst.height;
538 
539 	phantom_stream->timing.v_addressable = phantom_vactive;
540 	phantom_stream->timing.v_front_porch = 1;
541 	phantom_stream->timing.v_total = phantom_stream->timing.v_addressable +
542 						phantom_stream->timing.v_front_porch +
543 						phantom_stream->timing.v_sync_width +
544 						phantom_bp;
545 	phantom_stream->timing.flags.DSC = 0; // Don't need DSC for phantom timing
546 }
547 
548 /**
549  * dcn32_get_num_free_pipes - Calculate number of free pipes
550  * @dc: current dc state
551  * @context: new dc state
552  *
553  * This function assumes that a "used" pipe is a pipe that has
554  * both a stream and a plane assigned to it.
555  *
556  * Return: Number of free pipes available in the context
557  */
dcn32_get_num_free_pipes(struct dc * dc,struct dc_state * context)558 static unsigned int dcn32_get_num_free_pipes(struct dc *dc, struct dc_state *context)
559 {
560 	unsigned int i;
561 	unsigned int free_pipes = 0;
562 	unsigned int num_pipes = 0;
563 
564 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
565 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
566 
567 		if (pipe->stream && !pipe->top_pipe) {
568 			while (pipe) {
569 				num_pipes++;
570 				pipe = pipe->bottom_pipe;
571 			}
572 		}
573 	}
574 
575 	free_pipes = dc->res_pool->pipe_count - num_pipes;
576 	return free_pipes;
577 }
578 
579 /**
580  * dcn32_assign_subvp_pipe - Function to decide which pipe will use Sub-VP.
581  * @dc: current dc state
582  * @context: new dc state
583  * @index: [out] dc pipe index for the pipe chosen to have phantom pipes assigned
584  *
585  * We enter this function if we are Sub-VP capable (i.e. enough pipes available)
586  * and regular P-State switching (i.e. VACTIVE/VBLANK) is not supported, or if
587  * we are forcing SubVP P-State switching on the current config.
588  *
589  * The number of pipes used for the chosen surface must be less than or equal to the
590  * number of free pipes available.
591  *
592  * In general we choose surfaces with the longest frame time first (better for SubVP + VBLANK).
593  * For multi-display cases the ActiveDRAMClockChangeMargin doesn't provide enough info on its own
594  * for determining which should be the SubVP pipe (need a way to determine if a pipe / plane doesn't
595  * support MCLK switching naturally [i.e. ACTIVE or VBLANK]).
596  *
597  * Return: True if a valid pipe assignment was found for Sub-VP. Otherwise false.
598  */
dcn32_assign_subvp_pipe(struct dc * dc,struct dc_state * context,unsigned int * index)599 static bool dcn32_assign_subvp_pipe(struct dc *dc,
600 				    struct dc_state *context,
601 				    unsigned int *index)
602 {
603 	unsigned int i, pipe_idx;
604 	unsigned int max_frame_time = 0;
605 	bool valid_assignment_found = false;
606 	unsigned int free_pipes = dcn32_get_num_free_pipes(dc, context);
607 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
608 
609 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
610 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
611 		unsigned int num_pipes = 0;
612 		unsigned int refresh_rate = 0;
613 
614 		if (!pipe->stream)
615 			continue;
616 
617 		// Round up
618 		refresh_rate = (pipe->stream->timing.pix_clk_100hz * 100 +
619 				pipe->stream->timing.v_total * pipe->stream->timing.h_total - 1)
620 				/ (double)(pipe->stream->timing.v_total * pipe->stream->timing.h_total);
621 		/* SubVP pipe candidate requirements:
622 		 * - Refresh rate < 120hz
623 		 * - Not able to switch in vactive naturally (switching in active means the
624 		 *   DET provides enough buffer to hide the P-State switch latency -- trying
625 		 *   to combine this with SubVP can cause issues with the scheduling).
626 		 * - Not TMZ surface
627 		 */
628 		if (pipe->plane_state && !pipe->top_pipe && !pipe->prev_odm_pipe && !dcn32_is_center_timing(pipe) &&
629 				!pipe->stream->hw_cursor_req &&
630 				!(pipe->stream->timing.pix_clk_100hz / 10000 > DCN3_2_MAX_SUBVP_PIXEL_RATE_MHZ) &&
631 				(!dcn32_is_psr_capable(pipe) || (context->stream_count == 1 && dc->caps.dmub_caps.subvp_psr)) &&
632 				dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_NONE &&
633 				(refresh_rate < 120 || dcn32_allow_subvp_high_refresh_rate(dc, context, pipe)) &&
634 				!pipe->plane_state->address.tmz_surface &&
635 				(vba->ActiveDRAMClockChangeLatencyMarginPerState[vba->VoltageLevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]] <= 0 ||
636 				(vba->ActiveDRAMClockChangeLatencyMarginPerState[vba->VoltageLevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]] > 0 &&
637 						dcn32_allow_subvp_with_active_margin(pipe)))) {
638 			while (pipe) {
639 				num_pipes++;
640 				pipe = pipe->bottom_pipe;
641 			}
642 
643 			pipe = &context->res_ctx.pipe_ctx[i];
644 			if (num_pipes <= free_pipes) {
645 				struct dc_stream_state *stream = pipe->stream;
646 				unsigned int frame_us = (stream->timing.v_total * stream->timing.h_total /
647 						(double)(stream->timing.pix_clk_100hz * 100)) * 1000000;
648 				if (frame_us > max_frame_time) {
649 					*index = i;
650 					max_frame_time = frame_us;
651 					valid_assignment_found = true;
652 				}
653 			}
654 		}
655 		pipe_idx++;
656 	}
657 	return valid_assignment_found;
658 }
659 
660 /**
661  * dcn32_enough_pipes_for_subvp - Function to check if there are "enough" pipes for SubVP.
662  * @dc: current dc state
663  * @context: new dc state
664  *
665  * This function returns true if there are enough free pipes
666  * to create the required phantom pipes for any given stream
667  * (that does not already have phantom pipe assigned).
668  *
669  * e.g. For a 2 stream config where the first stream uses one
670  * pipe and the second stream uses 2 pipes (i.e. pipe split),
671  * this function will return true because there is 1 remaining
672  * pipe which can be used as the phantom pipe for the non pipe
673  * split pipe.
674  *
675  * Return:
676  * True if there are enough free pipes to assign phantom pipes to at least one
677  * stream that does not already have phantom pipes assigned. Otherwise false.
678  */
dcn32_enough_pipes_for_subvp(struct dc * dc,struct dc_state * context)679 static bool dcn32_enough_pipes_for_subvp(struct dc *dc, struct dc_state *context)
680 {
681 	unsigned int i, split_cnt, free_pipes;
682 	unsigned int min_pipe_split = dc->res_pool->pipe_count + 1; // init as max number of pipes + 1
683 	bool subvp_possible = false;
684 
685 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
686 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
687 
688 		// Find the minimum pipe split count for non SubVP pipes
689 		if (resource_is_pipe_type(pipe, OPP_HEAD) &&
690 			dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_NONE) {
691 			split_cnt = 0;
692 			while (pipe) {
693 				split_cnt++;
694 				pipe = pipe->bottom_pipe;
695 			}
696 
697 			if (split_cnt < min_pipe_split)
698 				min_pipe_split = split_cnt;
699 		}
700 	}
701 
702 	free_pipes = dcn32_get_num_free_pipes(dc, context);
703 
704 	// SubVP only possible if at least one pipe is being used (i.e. free_pipes
705 	// should not equal to the pipe_count)
706 	if (free_pipes >= min_pipe_split && free_pipes < dc->res_pool->pipe_count)
707 		subvp_possible = true;
708 
709 	return subvp_possible;
710 }
711 
712 /**
713  * subvp_subvp_schedulable - Determine if SubVP + SubVP config is schedulable
714  * @dc: current dc state
715  * @context: new dc state
716  *
717  * High level algorithm:
718  * 1. Find longest microschedule length (in us) between the two SubVP pipes
719  * 2. Check if the worst case overlap (VBLANK in middle of ACTIVE) for both
720  * pipes still allows for the maximum microschedule to fit in the active
721  * region for both pipes.
722  *
723  * Return: True if the SubVP + SubVP config is schedulable, false otherwise
724  */
subvp_subvp_schedulable(struct dc * dc,struct dc_state * context)725 static bool subvp_subvp_schedulable(struct dc *dc, struct dc_state *context)
726 {
727 	struct pipe_ctx *subvp_pipes[2] = {0};
728 	struct dc_stream_state *phantom = NULL;
729 	uint32_t microschedule_lines = 0;
730 	uint32_t index = 0;
731 	uint32_t i;
732 	uint32_t max_microschedule_us = 0;
733 	int32_t vactive1_us, vactive2_us, vblank1_us, vblank2_us;
734 
735 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
736 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
737 		uint32_t time_us = 0;
738 
739 		/* Loop to calculate the maximum microschedule time between the two SubVP pipes,
740 		 * and also to store the two main SubVP pipe pointers in subvp_pipes[2].
741 		 */
742 		phantom = dc_state_get_paired_subvp_stream(context, pipe->stream);
743 		if (phantom && pipe->stream && pipe->plane_state && !pipe->top_pipe &&
744 			dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_MAIN) {
745 			microschedule_lines = (phantom->timing.v_total - phantom->timing.v_front_porch) +
746 					phantom->timing.v_addressable;
747 
748 			// Round up when calculating microschedule time (+ 1 at the end)
749 			time_us = (microschedule_lines * phantom->timing.h_total) /
750 					(double)(phantom->timing.pix_clk_100hz * 100) * 1000000 +
751 						dc->caps.subvp_prefetch_end_to_mall_start_us +
752 						dc->caps.subvp_fw_processing_delay_us + 1;
753 			if (time_us > max_microschedule_us)
754 				max_microschedule_us = time_us;
755 
756 			subvp_pipes[index] = pipe;
757 			index++;
758 
759 			// Maximum 2 SubVP pipes
760 			if (index == 2)
761 				break;
762 		}
763 	}
764 	vactive1_us = ((subvp_pipes[0]->stream->timing.v_addressable * subvp_pipes[0]->stream->timing.h_total) /
765 			(double)(subvp_pipes[0]->stream->timing.pix_clk_100hz * 100)) * 1000000;
766 	vactive2_us = ((subvp_pipes[1]->stream->timing.v_addressable * subvp_pipes[1]->stream->timing.h_total) /
767 				(double)(subvp_pipes[1]->stream->timing.pix_clk_100hz * 100)) * 1000000;
768 	vblank1_us = (((subvp_pipes[0]->stream->timing.v_total - subvp_pipes[0]->stream->timing.v_addressable) *
769 			subvp_pipes[0]->stream->timing.h_total) /
770 			(double)(subvp_pipes[0]->stream->timing.pix_clk_100hz * 100)) * 1000000;
771 	vblank2_us = (((subvp_pipes[1]->stream->timing.v_total - subvp_pipes[1]->stream->timing.v_addressable) *
772 			subvp_pipes[1]->stream->timing.h_total) /
773 			(double)(subvp_pipes[1]->stream->timing.pix_clk_100hz * 100)) * 1000000;
774 
775 	if ((vactive1_us - vblank2_us) / 2 > max_microschedule_us &&
776 	    (vactive2_us - vblank1_us) / 2 > max_microschedule_us)
777 		return true;
778 
779 	return false;
780 }
781 
782 /**
783  * subvp_drr_schedulable() - Determine if SubVP + DRR config is schedulable
784  * @dc: current dc state
785  * @context: new dc state
786  *
787  * High level algorithm:
788  * 1. Get timing for SubVP pipe, phantom pipe, and DRR pipe
789  * 2. Determine the frame time for the DRR display when adding required margin for MCLK switching
790  * (the margin is equal to the MALL region + DRR margin (500us))
791  * 3.If (SubVP Active - Prefetch > Stretched DRR frame + max(MALL region, Stretched DRR frame))
792  * then report the configuration as supported
793  *
794  * Return: True if the SubVP + DRR config is schedulable, false otherwise
795  */
subvp_drr_schedulable(struct dc * dc,struct dc_state * context)796 static bool subvp_drr_schedulable(struct dc *dc, struct dc_state *context)
797 {
798 	bool schedulable = false;
799 	uint32_t i;
800 	struct pipe_ctx *pipe = NULL;
801 	struct pipe_ctx *drr_pipe = NULL;
802 	struct dc_crtc_timing *main_timing = NULL;
803 	struct dc_crtc_timing *phantom_timing = NULL;
804 	struct dc_crtc_timing *drr_timing = NULL;
805 	int16_t prefetch_us = 0;
806 	int16_t mall_region_us = 0;
807 	int16_t drr_frame_us = 0;	// nominal frame time
808 	int16_t subvp_active_us = 0;
809 	int16_t stretched_drr_us = 0;
810 	int16_t drr_stretched_vblank_us = 0;
811 	int16_t max_vblank_mallregion = 0;
812 	struct dc_stream_state *phantom_stream;
813 	bool subvp_found = false;
814 	bool drr_found = false;
815 
816 	// Find SubVP pipe
817 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
818 		pipe = &context->res_ctx.pipe_ctx[i];
819 
820 		// We check for master pipe, but it shouldn't matter since we only need
821 		// the pipe for timing info (stream should be same for any pipe splits)
822 		if (!resource_is_pipe_type(pipe, OTG_MASTER) ||
823 				!resource_is_pipe_type(pipe, DPP_PIPE))
824 			continue;
825 
826 		// Find the SubVP pipe
827 		if (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_MAIN) {
828 			subvp_found = true;
829 			break;
830 		}
831 	}
832 
833 	// Find the DRR pipe
834 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
835 		drr_pipe = &context->res_ctx.pipe_ctx[i];
836 
837 		// We check for master pipe only
838 		if (!resource_is_pipe_type(drr_pipe, OTG_MASTER) ||
839 				!resource_is_pipe_type(drr_pipe, DPP_PIPE))
840 			continue;
841 
842 		if (dc_state_get_pipe_subvp_type(context, drr_pipe) == SUBVP_NONE && drr_pipe->stream->ignore_msa_timing_param &&
843 				(drr_pipe->stream->allow_freesync || drr_pipe->stream->vrr_active_variable || drr_pipe->stream->vrr_active_fixed)) {
844 			drr_found = true;
845 			break;
846 		}
847 	}
848 
849 	phantom_stream = dc_state_get_paired_subvp_stream(context, pipe->stream);
850 	if (phantom_stream && subvp_found && drr_found) {
851 		main_timing = &pipe->stream->timing;
852 		phantom_timing = &phantom_stream->timing;
853 		drr_timing = &drr_pipe->stream->timing;
854 		prefetch_us = (phantom_timing->v_total - phantom_timing->v_front_porch) * phantom_timing->h_total /
855 				(double)(phantom_timing->pix_clk_100hz * 100) * 1000000 +
856 				dc->caps.subvp_prefetch_end_to_mall_start_us;
857 		subvp_active_us = main_timing->v_addressable * main_timing->h_total /
858 				(double)(main_timing->pix_clk_100hz * 100) * 1000000;
859 		drr_frame_us = drr_timing->v_total * drr_timing->h_total /
860 				(double)(drr_timing->pix_clk_100hz * 100) * 1000000;
861 		// P-State allow width and FW delays already included phantom_timing->v_addressable
862 		mall_region_us = phantom_timing->v_addressable * phantom_timing->h_total /
863 				(double)(phantom_timing->pix_clk_100hz * 100) * 1000000;
864 		stretched_drr_us = drr_frame_us + mall_region_us + SUBVP_DRR_MARGIN_US;
865 		drr_stretched_vblank_us = (drr_timing->v_total - drr_timing->v_addressable) * drr_timing->h_total /
866 				(double)(drr_timing->pix_clk_100hz * 100) * 1000000 + (stretched_drr_us - drr_frame_us);
867 		max_vblank_mallregion = drr_stretched_vblank_us > mall_region_us ? drr_stretched_vblank_us : mall_region_us;
868 	}
869 
870 	/* We consider SubVP + DRR schedulable if the stretched frame duration of the DRR display (i.e. the
871 	 * highest refresh rate + margin that can support UCLK P-State switch) passes the static analysis
872 	 * for VBLANK: (VACTIVE region of the SubVP pipe can fit the MALL prefetch, VBLANK frame time,
873 	 * and the max of (VBLANK blanking time, MALL region)).
874 	 */
875 	if (drr_timing &&
876 	    stretched_drr_us < (1 / (double)drr_timing->min_refresh_in_uhz) * 1000000 * 1000000 &&
877 	    subvp_active_us - prefetch_us - stretched_drr_us - max_vblank_mallregion > 0)
878 		schedulable = true;
879 
880 	return schedulable;
881 }
882 
883 
884 /**
885  * subvp_vblank_schedulable - Determine if SubVP + VBLANK config is schedulable
886  * @dc: current dc state
887  * @context: new dc state
888  *
889  * High level algorithm:
890  * 1. Get timing for SubVP pipe, phantom pipe, and VBLANK pipe
891  * 2. If (SubVP Active - Prefetch > Vblank Frame Time + max(MALL region, Vblank blanking time))
892  * then report the configuration as supported
893  * 3. If the VBLANK display is DRR, then take the DRR static schedulability path
894  *
895  * Return: True if the SubVP + VBLANK/DRR config is schedulable, false otherwise
896  */
subvp_vblank_schedulable(struct dc * dc,struct dc_state * context)897 static bool subvp_vblank_schedulable(struct dc *dc, struct dc_state *context)
898 {
899 	struct pipe_ctx *pipe = NULL;
900 	struct pipe_ctx *subvp_pipe = NULL;
901 	bool found = false;
902 	bool schedulable = false;
903 	uint32_t i = 0;
904 	uint8_t vblank_index = 0;
905 	uint16_t prefetch_us = 0;
906 	uint16_t mall_region_us = 0;
907 	uint16_t vblank_frame_us = 0;
908 	uint16_t subvp_active_us = 0;
909 	uint16_t vblank_blank_us = 0;
910 	uint16_t max_vblank_mallregion = 0;
911 	struct dc_crtc_timing *main_timing = NULL;
912 	struct dc_crtc_timing *phantom_timing = NULL;
913 	struct dc_crtc_timing *vblank_timing = NULL;
914 	struct dc_stream_state *phantom_stream;
915 	enum mall_stream_type pipe_mall_type;
916 
917 	/* For SubVP + VBLANK/DRR cases, we assume there can only be
918 	 * a single VBLANK/DRR display. If DML outputs SubVP + VBLANK
919 	 * is supported, it is either a single VBLANK case or two VBLANK
920 	 * displays which are synchronized (in which case they have identical
921 	 * timings).
922 	 */
923 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
924 		pipe = &context->res_ctx.pipe_ctx[i];
925 		pipe_mall_type = dc_state_get_pipe_subvp_type(context, pipe);
926 
927 		// We check for master pipe, but it shouldn't matter since we only need
928 		// the pipe for timing info (stream should be same for any pipe splits)
929 		if (!resource_is_pipe_type(pipe, OTG_MASTER) ||
930 				!resource_is_pipe_type(pipe, DPP_PIPE))
931 			continue;
932 
933 		if (!found && pipe_mall_type == SUBVP_NONE) {
934 			// Found pipe which is not SubVP or Phantom (i.e. the VBLANK pipe).
935 			vblank_index = i;
936 			found = true;
937 		}
938 
939 		if (!subvp_pipe && pipe_mall_type == SUBVP_MAIN)
940 			subvp_pipe = pipe;
941 	}
942 	if (found && subvp_pipe) {
943 		phantom_stream = dc_state_get_paired_subvp_stream(context, subvp_pipe->stream);
944 		main_timing = &subvp_pipe->stream->timing;
945 		phantom_timing = &phantom_stream->timing;
946 		vblank_timing = &context->res_ctx.pipe_ctx[vblank_index].stream->timing;
947 		// Prefetch time is equal to VACTIVE + BP + VSYNC of the phantom pipe
948 		// Also include the prefetch end to mallstart delay time
949 		prefetch_us = (phantom_timing->v_total - phantom_timing->v_front_porch) * phantom_timing->h_total /
950 				(double)(phantom_timing->pix_clk_100hz * 100) * 1000000 +
951 				dc->caps.subvp_prefetch_end_to_mall_start_us;
952 		// P-State allow width and FW delays already included phantom_timing->v_addressable
953 		mall_region_us = phantom_timing->v_addressable * phantom_timing->h_total /
954 				(double)(phantom_timing->pix_clk_100hz * 100) * 1000000;
955 		vblank_frame_us = vblank_timing->v_total * vblank_timing->h_total /
956 				(double)(vblank_timing->pix_clk_100hz * 100) * 1000000;
957 		vblank_blank_us =  (vblank_timing->v_total - vblank_timing->v_addressable) * vblank_timing->h_total /
958 				(double)(vblank_timing->pix_clk_100hz * 100) * 1000000;
959 		subvp_active_us = main_timing->v_addressable * main_timing->h_total /
960 				(double)(main_timing->pix_clk_100hz * 100) * 1000000;
961 		max_vblank_mallregion = vblank_blank_us > mall_region_us ? vblank_blank_us : mall_region_us;
962 
963 		// Schedulable if VACTIVE region of the SubVP pipe can fit the MALL prefetch, VBLANK frame time,
964 		// and the max of (VBLANK blanking time, MALL region)
965 		// TODO: Possibly add some margin (i.e. the below conditions should be [...] > X instead of [...] > 0)
966 		if (subvp_active_us - prefetch_us - vblank_frame_us - max_vblank_mallregion > 0)
967 			schedulable = true;
968 	}
969 	return schedulable;
970 }
971 
972 /**
973  * subvp_subvp_admissable() - Determine if subvp + subvp config is admissible
974  *
975  * @dc: Current DC state
976  * @context: New DC state to be programmed
977  *
978  * SubVP + SubVP is admissible under the following conditions:
979  * - All SubVP pipes are < 120Hz OR
980  * - All SubVP pipes are >= 120hz
981  *
982  * Return: True if admissible, false otherwise
983  */
subvp_subvp_admissable(struct dc * dc,struct dc_state * context)984 static bool subvp_subvp_admissable(struct dc *dc,
985 				struct dc_state *context)
986 {
987 	bool result = false;
988 	uint32_t i;
989 	uint8_t subvp_count = 0;
990 	uint32_t min_refresh = subvp_high_refresh_list.min_refresh, max_refresh = 0;
991 	uint64_t refresh_rate = 0;
992 
993 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
994 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
995 
996 		if (!pipe->stream)
997 			continue;
998 
999 		if (pipe->plane_state && !pipe->top_pipe &&
1000 				dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_MAIN) {
1001 			refresh_rate = (pipe->stream->timing.pix_clk_100hz * (uint64_t)100 +
1002 				pipe->stream->timing.v_total * (uint64_t)pipe->stream->timing.h_total - (uint64_t)1);
1003 			refresh_rate = div_u64(refresh_rate, pipe->stream->timing.v_total);
1004 			refresh_rate = div_u64(refresh_rate, pipe->stream->timing.h_total);
1005 
1006 			if ((uint32_t)refresh_rate < min_refresh)
1007 				min_refresh = (uint32_t)refresh_rate;
1008 			if ((uint32_t)refresh_rate > max_refresh)
1009 				max_refresh = (uint32_t)refresh_rate;
1010 			subvp_count++;
1011 		}
1012 	}
1013 
1014 	if (subvp_count == 2 && ((min_refresh < 120 && max_refresh < 120) ||
1015 		(min_refresh >= subvp_high_refresh_list.min_refresh &&
1016 				max_refresh <= subvp_high_refresh_list.max_refresh)))
1017 		result = true;
1018 
1019 	return result;
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  */
subvp_validate_static_schedulability(struct dc * dc,struct dc_state * context,int vlevel)1036 static bool subvp_validate_static_schedulability(struct dc *dc,
1037 				struct dc_state *context,
1038 				int vlevel)
1039 {
1040 	bool schedulable = false;
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 	uint8_t non_subvp_pipes = 0;
1046 
1047 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1048 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1049 		enum mall_stream_type pipe_mall_type = dc_state_get_pipe_subvp_type(context, pipe);
1050 
1051 		if (!pipe->stream)
1052 			continue;
1053 
1054 		if (pipe->plane_state && !pipe->top_pipe) {
1055 			if (pipe_mall_type == SUBVP_MAIN)
1056 				subvp_count++;
1057 			if (pipe_mall_type == SUBVP_NONE)
1058 				non_subvp_pipes++;
1059 		}
1060 
1061 		// Count how many planes that aren't SubVP/phantom are capable of VACTIVE
1062 		// switching (SubVP + VACTIVE unsupported). In situations where we force
1063 		// SubVP for a VACTIVE plane, we don't want to increment the vactive_count.
1064 		if (vba->ActiveDRAMClockChangeLatencyMarginPerState[vlevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]] > 0 &&
1065 				pipe_mall_type == SUBVP_NONE) {
1066 			vactive_count++;
1067 		}
1068 		pipe_idx++;
1069 	}
1070 
1071 	if (subvp_count == 2) {
1072 		// Static schedulability check for SubVP + SubVP case
1073 		schedulable = subvp_subvp_admissable(dc, context) && subvp_subvp_schedulable(dc, context);
1074 	} else if (subvp_count == 1 && non_subvp_pipes == 0) {
1075 		// Single SubVP configs will be supported by default as long as it's suppported by DML
1076 		schedulable = true;
1077 	} else if (subvp_count == 1 && non_subvp_pipes == 1) {
1078 		if (dcn32_subvp_drr_admissable(dc, context))
1079 			schedulable = subvp_drr_schedulable(dc, context);
1080 		else if (dcn32_subvp_vblank_admissable(dc, context, vlevel))
1081 			schedulable = subvp_vblank_schedulable(dc, context);
1082 	} else if (vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_vactive_w_mall_sub_vp &&
1083 			vactive_count > 0) {
1084 		// For single display SubVP cases, DML will output dm_dram_clock_change_vactive_w_mall_sub_vp by default.
1085 		// We tell the difference between SubVP vs. SubVP + VACTIVE by checking the vactive_count.
1086 		// SubVP + VACTIVE currently unsupported
1087 		schedulable = false;
1088 	}
1089 	return schedulable;
1090 }
1091 
assign_subvp_index(struct dc * dc,struct dc_state * context)1092 static void assign_subvp_index(struct dc *dc, struct dc_state *context)
1093 {
1094 	int i;
1095 	int index = 0;
1096 
1097 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1098 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1099 
1100 		if (resource_is_pipe_type(pipe_ctx, OTG_MASTER) &&
1101 				dc_state_get_pipe_subvp_type(context, pipe_ctx) == SUBVP_MAIN) {
1102 			pipe_ctx->subvp_index = index++;
1103 		} else {
1104 			pipe_ctx->subvp_index = 0;
1105 		}
1106 	}
1107 }
1108 
1109 struct pipe_slice_table {
1110 	struct {
1111 		struct dc_stream_state *stream;
1112 		int slice_count;
1113 	} odm_combines[MAX_STREAMS];
1114 	int odm_combine_count;
1115 
1116 	struct {
1117 		struct pipe_ctx *pri_pipe;
1118 		struct dc_plane_state *plane;
1119 		int slice_count;
1120 	} mpc_combines[MAX_PLANES];
1121 	int mpc_combine_count;
1122 };
1123 
1124 
update_slice_table_for_stream(struct pipe_slice_table * table,struct dc_stream_state * stream,int diff)1125 static void update_slice_table_for_stream(struct pipe_slice_table *table,
1126 		struct dc_stream_state *stream, int diff)
1127 {
1128 	int i;
1129 
1130 	for (i = 0; i < table->odm_combine_count; i++) {
1131 		if (table->odm_combines[i].stream == stream) {
1132 			table->odm_combines[i].slice_count += diff;
1133 			break;
1134 		}
1135 	}
1136 
1137 	if (i == table->odm_combine_count) {
1138 		table->odm_combine_count++;
1139 		table->odm_combines[i].stream = stream;
1140 		table->odm_combines[i].slice_count = diff;
1141 	}
1142 }
1143 
update_slice_table_for_plane(struct pipe_slice_table * table,struct pipe_ctx * dpp_pipe,struct dc_plane_state * plane,int diff)1144 static void update_slice_table_for_plane(struct pipe_slice_table *table,
1145 		struct pipe_ctx *dpp_pipe, struct dc_plane_state *plane, int diff)
1146 {
1147 	int i;
1148 	struct pipe_ctx *pri_dpp_pipe = resource_get_primary_dpp_pipe(dpp_pipe);
1149 
1150 	for (i = 0; i < table->mpc_combine_count; i++) {
1151 		if (table->mpc_combines[i].plane == plane &&
1152 				table->mpc_combines[i].pri_pipe == pri_dpp_pipe) {
1153 			table->mpc_combines[i].slice_count += diff;
1154 			break;
1155 		}
1156 	}
1157 
1158 	if (i == table->mpc_combine_count) {
1159 		table->mpc_combine_count++;
1160 		table->mpc_combines[i].plane = plane;
1161 		table->mpc_combines[i].pri_pipe = pri_dpp_pipe;
1162 		table->mpc_combines[i].slice_count = diff;
1163 	}
1164 }
1165 
init_pipe_slice_table_from_context(struct pipe_slice_table * table,struct dc_state * context)1166 static void init_pipe_slice_table_from_context(
1167 		struct pipe_slice_table *table,
1168 		struct dc_state *context)
1169 {
1170 	int i, j;
1171 	struct pipe_ctx *otg_master;
1172 	struct pipe_ctx *dpp_pipes[MAX_PIPES];
1173 	struct dc_stream_state *stream;
1174 	int count;
1175 
1176 	memset(table, 0, sizeof(*table));
1177 
1178 	for (i = 0; i < context->stream_count; i++) {
1179 		stream = context->streams[i];
1180 		otg_master = resource_get_otg_master_for_stream(
1181 				&context->res_ctx, stream);
1182 		if (!otg_master)
1183 			continue;
1184 
1185 		count = resource_get_odm_slice_count(otg_master);
1186 		update_slice_table_for_stream(table, stream, count);
1187 
1188 		count = resource_get_dpp_pipes_for_opp_head(otg_master,
1189 				&context->res_ctx, dpp_pipes);
1190 		for (j = 0; j < count; j++)
1191 			if (dpp_pipes[j]->plane_state)
1192 				update_slice_table_for_plane(table, dpp_pipes[j],
1193 						dpp_pipes[j]->plane_state, 1);
1194 	}
1195 }
1196 
update_pipe_slice_table_with_split_flags(struct pipe_slice_table * table,struct dc * dc,struct dc_state * context,struct vba_vars_st * vba,int split[MAX_PIPES],bool merge[MAX_PIPES])1197 static bool update_pipe_slice_table_with_split_flags(
1198 		struct pipe_slice_table *table,
1199 		struct dc *dc,
1200 		struct dc_state *context,
1201 		struct vba_vars_st *vba,
1202 		int split[MAX_PIPES],
1203 		bool merge[MAX_PIPES])
1204 {
1205 	/* NOTE: we are deprecating the support for the concept of pipe splitting
1206 	 * or pipe merging. Instead we append slices to the end and remove
1207 	 * slices from the end. The following code converts a pipe split or
1208 	 * merge to an append or remove operation.
1209 	 *
1210 	 * For example:
1211 	 * When split flags describe the following pipe connection transition
1212 	 *
1213 	 * from:
1214 	 *  pipe 0 (split=2) -> pipe 1 (split=2)
1215 	 * to: (old behavior)
1216 	 *  pipe 0 -> pipe 2 -> pipe 1 -> pipe 3
1217 	 *
1218 	 * the code below actually does:
1219 	 *  pipe 0 -> pipe 1 -> pipe 2 -> pipe 3
1220 	 *
1221 	 * This is the new intended behavior and for future DCNs we will retire
1222 	 * the old concept completely.
1223 	 */
1224 	struct pipe_ctx *pipe;
1225 	bool odm;
1226 	int dc_pipe_idx, dml_pipe_idx = 0;
1227 	bool updated = false;
1228 
1229 	for (dc_pipe_idx = 0;
1230 			dc_pipe_idx < dc->res_pool->pipe_count; dc_pipe_idx++) {
1231 		pipe = &context->res_ctx.pipe_ctx[dc_pipe_idx];
1232 		if (resource_is_pipe_type(pipe, FREE_PIPE))
1233 			continue;
1234 
1235 		if (merge[dc_pipe_idx]) {
1236 			if (resource_is_pipe_type(pipe, OPP_HEAD))
1237 				/* merging OPP head means reducing ODM slice
1238 				 * count by 1
1239 				 */
1240 				update_slice_table_for_stream(table, pipe->stream, -1);
1241 			else if (resource_is_pipe_type(pipe, DPP_PIPE) &&
1242 					resource_get_odm_slice_index(resource_get_opp_head(pipe)) == 0)
1243 				/* merging DPP pipe of the first ODM slice means
1244 				 * reducing MPC slice count by 1
1245 				 */
1246 				update_slice_table_for_plane(table, pipe, pipe->plane_state, -1);
1247 			updated = true;
1248 		}
1249 
1250 		if (split[dc_pipe_idx]) {
1251 			odm = vba->ODMCombineEnabled[vba->pipe_plane[dml_pipe_idx]] !=
1252 					dm_odm_combine_mode_disabled;
1253 			if (odm && resource_is_pipe_type(pipe, OPP_HEAD))
1254 				update_slice_table_for_stream(
1255 						table, pipe->stream, split[dc_pipe_idx] - 1);
1256 			else if (!odm && resource_is_pipe_type(pipe, DPP_PIPE))
1257 				update_slice_table_for_plane(table, pipe,
1258 						pipe->plane_state, split[dc_pipe_idx] - 1);
1259 			updated = true;
1260 		}
1261 		dml_pipe_idx++;
1262 	}
1263 	return updated;
1264 }
1265 
update_pipes_with_slice_table(struct dc * dc,struct dc_state * context,struct pipe_slice_table * table)1266 static void update_pipes_with_slice_table(struct dc *dc, struct dc_state *context,
1267 		struct pipe_slice_table *table)
1268 {
1269 	int i;
1270 
1271 	for (i = 0; i < table->odm_combine_count; i++)
1272 		resource_update_pipes_for_stream_with_slice_count(context,
1273 				dc->current_state, dc->res_pool,
1274 				table->odm_combines[i].stream,
1275 				table->odm_combines[i].slice_count);
1276 
1277 	for (i = 0; i < table->mpc_combine_count; i++)
1278 		resource_update_pipes_for_plane_with_slice_count(context,
1279 				dc->current_state, dc->res_pool,
1280 				table->mpc_combines[i].plane,
1281 				table->mpc_combines[i].slice_count);
1282 }
1283 
update_pipes_with_split_flags(struct dc * dc,struct dc_state * context,struct vba_vars_st * vba,int split[MAX_PIPES],bool merge[MAX_PIPES])1284 static bool update_pipes_with_split_flags(struct dc *dc, struct dc_state *context,
1285 		struct vba_vars_st *vba, int split[MAX_PIPES],
1286 		bool merge[MAX_PIPES])
1287 {
1288 	struct pipe_slice_table slice_table;
1289 	bool updated;
1290 
1291 	init_pipe_slice_table_from_context(&slice_table, context);
1292 	updated = update_pipe_slice_table_with_split_flags(
1293 			&slice_table, dc, context, vba,
1294 			split, merge);
1295 	update_pipes_with_slice_table(dc, context, &slice_table);
1296 	return updated;
1297 }
1298 
should_apply_odm_power_optimization(struct dc * dc,struct dc_state * context,struct vba_vars_st * v,int * split,bool * merge)1299 static bool should_apply_odm_power_optimization(struct dc *dc,
1300 		struct dc_state *context, struct vba_vars_st *v, int *split,
1301 		bool *merge)
1302 {
1303 	struct dc_stream_state *stream = context->streams[0];
1304 	struct pipe_slice_table slice_table;
1305 	int i;
1306 
1307 	/*
1308 	 * this debug flag allows us to disable ODM power optimization feature
1309 	 * unconditionally. we force the feature off if this is set to false.
1310 	 */
1311 	if (!dc->debug.enable_single_display_2to1_odm_policy)
1312 		return false;
1313 
1314 	/* current design and test coverage is only limited to allow ODM power
1315 	 * optimization for single stream. Supporting it for multiple streams
1316 	 * use case would require additional algorithm to decide how to
1317 	 * optimize power consumption when there are not enough free pipes to
1318 	 * allocate for all the streams. This level of optimization would
1319 	 * require multiple attempts of revalidation to make an optimized
1320 	 * decision. Unfortunately We do not support revalidation flow in
1321 	 * current version of DML.
1322 	 */
1323 	if (context->stream_count != 1)
1324 		return false;
1325 
1326 	/*
1327 	 * Our hardware doesn't support ODM for HDMI TMDS
1328 	 */
1329 	if (dc_is_hdmi_signal(stream->signal))
1330 		return false;
1331 
1332 	/*
1333 	 * ODM Combine 2:1 requires horizontal timing divisible by 2 so each
1334 	 * ODM segment has the same size.
1335 	 */
1336 	if (!is_h_timing_divisible_by_2(stream))
1337 		return false;
1338 
1339 	/*
1340 	 * No power benefits if the timing's pixel clock is not high enough to
1341 	 * raise display clock from minimum power state.
1342 	 */
1343 	if (stream->timing.pix_clk_100hz * 100 <= DCN3_2_VMIN_DISPCLK_HZ)
1344 		return false;
1345 
1346 	if (dc->config.enable_windowed_mpo_odm) {
1347 		/*
1348 		 * ODM power optimization should only be allowed if the feature
1349 		 * can be seamlessly toggled off within an update. This would
1350 		 * require that the feature is applied on top of a minimal
1351 		 * state. A minimal state is defined as a state validated
1352 		 * without the need of pipe split. Therefore, when transition to
1353 		 * toggle the feature off, the same stream and plane
1354 		 * configuration can be supported by the pipe resource in the
1355 		 * first ODM slice alone without the need to acquire extra
1356 		 * resources.
1357 		 */
1358 		init_pipe_slice_table_from_context(&slice_table, context);
1359 		update_pipe_slice_table_with_split_flags(
1360 				&slice_table, dc, context, v,
1361 				split, merge);
1362 		for (i = 0; i < slice_table.mpc_combine_count; i++)
1363 			if (slice_table.mpc_combines[i].slice_count > 1)
1364 				return false;
1365 
1366 		for (i = 0; i < slice_table.odm_combine_count; i++)
1367 			if (slice_table.odm_combines[i].slice_count > 1)
1368 				return false;
1369 	} else {
1370 		/*
1371 		 * the new ODM power optimization feature reduces software
1372 		 * design limitation and allows ODM power optimization to be
1373 		 * supported even with presence of overlay planes. The new
1374 		 * feature is enabled based on enable_windowed_mpo_odm flag. If
1375 		 * the flag is not set, we limit our feature scope due to
1376 		 * previous software design limitation
1377 		 */
1378 		if (context->stream_status[0].plane_count != 1)
1379 			return false;
1380 
1381 		if (memcmp(&context->stream_status[0].plane_states[0]->clip_rect,
1382 				&stream->src, sizeof(struct rect)) != 0)
1383 			return false;
1384 
1385 		if (stream->src.width >= 5120 &&
1386 				stream->src.width > stream->dst.width)
1387 			return false;
1388 	}
1389 	return true;
1390 }
1391 
try_odm_power_optimization_and_revalidate(struct dc * dc,struct dc_state * context,display_e2e_pipe_params_st * pipes,int * split,bool * merge,unsigned int * vlevel,int pipe_cnt)1392 static void try_odm_power_optimization_and_revalidate(
1393 		struct dc *dc,
1394 		struct dc_state *context,
1395 		display_e2e_pipe_params_st *pipes,
1396 		int *split,
1397 		bool *merge,
1398 		unsigned int *vlevel,
1399 		int pipe_cnt)
1400 {
1401 	int i;
1402 	unsigned int new_vlevel;
1403 	unsigned int cur_policy[MAX_PIPES];
1404 
1405 	for (i = 0; i < pipe_cnt; i++) {
1406 		cur_policy[i] = pipes[i].pipe.dest.odm_combine_policy;
1407 		pipes[i].pipe.dest.odm_combine_policy = dm_odm_combine_policy_2to1;
1408 	}
1409 
1410 	new_vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
1411 
1412 	if (new_vlevel < context->bw_ctx.dml.soc.num_states) {
1413 		memset(split, 0, MAX_PIPES * sizeof(int));
1414 		memset(merge, 0, MAX_PIPES * sizeof(bool));
1415 		*vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, new_vlevel, split, merge);
1416 		context->bw_ctx.dml.vba.VoltageLevel = *vlevel;
1417 	} else {
1418 		for (i = 0; i < pipe_cnt; i++)
1419 			pipes[i].pipe.dest.odm_combine_policy = cur_policy[i];
1420 	}
1421 }
1422 
is_test_pattern_enabled(struct dc_state * context)1423 static bool is_test_pattern_enabled(
1424 		struct dc_state *context)
1425 {
1426 	int i;
1427 
1428 	for (i = 0; i < context->stream_count; i++) {
1429 		if (context->streams[i]->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE)
1430 			return true;
1431 	}
1432 
1433 	return false;
1434 }
1435 
dcn32_full_validate_bw_helper(struct dc * dc,struct dc_state * context,display_e2e_pipe_params_st * pipes,int * vlevel,int * split,bool * merge,int * pipe_cnt,bool * repopulate_pipes)1436 static bool dcn32_full_validate_bw_helper(struct dc *dc,
1437 				   struct dc_state *context,
1438 				   display_e2e_pipe_params_st *pipes,
1439 				   int *vlevel,
1440 				   int *split,
1441 				   bool *merge,
1442 				   int *pipe_cnt,
1443 				   bool *repopulate_pipes)
1444 {
1445 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
1446 	unsigned int dc_pipe_idx = 0;
1447 	int i = 0;
1448 	bool found_supported_config = false;
1449 	int vlevel_temp = 0;
1450 
1451 	dc_assert_fp_enabled();
1452 
1453 	/*
1454 	 * DML favors voltage over p-state, but we're more interested in
1455 	 * supporting p-state over voltage. We can't support p-state in
1456 	 * prefetch mode > 0 so try capping the prefetch mode to start.
1457 	 * Override present for testing.
1458 	 */
1459 	if (dc->debug.dml_disallow_alternate_prefetch_modes)
1460 		context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
1461 			dm_prefetch_support_uclk_fclk_and_stutter;
1462 	else
1463 		context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
1464 			dm_prefetch_support_uclk_fclk_and_stutter_if_possible;
1465 
1466 	*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
1467 	/* This may adjust vlevel and maxMpcComb */
1468 	if (*vlevel < context->bw_ctx.dml.soc.num_states) {
1469 		*vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, *vlevel, split, merge);
1470 		vba->VoltageLevel = *vlevel;
1471 	}
1472 
1473 	/* Apply split and merge flags before checking for subvp */
1474 	if (!dcn32_apply_merge_split_flags_helper(dc, context, repopulate_pipes, split, merge))
1475 		return false;
1476 	memset(split, 0, MAX_PIPES * sizeof(int));
1477 	memset(merge, 0, MAX_PIPES * sizeof(bool));
1478 
1479 	/* Conditions for setting up phantom pipes for SubVP:
1480 	 * 1. Not force disable SubVP
1481 	 * 2. Full update (i.e. !fast_validate)
1482 	 * 3. Enough pipes are available to support SubVP (TODO: Which pipes will use VACTIVE / VBLANK / SUBVP?)
1483 	 * 4. Display configuration passes validation
1484 	 * 5. (Config doesn't support MCLK in VACTIVE/VBLANK || dc->debug.force_subvp_mclk_switch)
1485 	 */
1486 	if (!dc->debug.force_disable_subvp && !dc->caps.dmub_caps.gecc_enable && dcn32_all_pipes_have_stream_and_plane(dc, context) &&
1487 	    !dcn32_mpo_in_use(context) && !dcn32_any_surfaces_rotated(dc, context) && !is_test_pattern_enabled(context) &&
1488 		(*vlevel == context->bw_ctx.dml.soc.num_states || (vba->DRAMSpeedPerState[*vlevel] != vba->DRAMSpeedPerState[0] &&
1489 				vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] != dm_dram_clock_change_unsupported) ||
1490 	    vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported ||
1491 	    dc->debug.force_subvp_mclk_switch)) {
1492 
1493 		vlevel_temp = *vlevel;
1494 
1495 		while (!found_supported_config && dcn32_enough_pipes_for_subvp(dc, context) &&
1496 			dcn32_assign_subvp_pipe(dc, context, &dc_pipe_idx)) {
1497 			/* For the case where *vlevel = num_states, bandwidth validation has failed for this config.
1498 			 * Adding phantom pipes won't change the validation result, so change the DML input param
1499 			 * for P-State support before adding phantom pipes and recalculating the DML result.
1500 			 * However, this case is only applicable for SubVP + DRR cases because the prefetch mode
1501 			 * will not allow for switch in VBLANK. The DRR display must have it's VBLANK stretched
1502 			 * enough to support MCLK switching.
1503 			 */
1504 			if (*vlevel == context->bw_ctx.dml.soc.num_states &&
1505 				context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final ==
1506 					dm_prefetch_support_uclk_fclk_and_stutter) {
1507 				context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
1508 								dm_prefetch_support_fclk_and_stutter;
1509 				/* There are params (such as FabricClock) that need to be recalculated
1510 				 * after validation fails (otherwise it will be 0). Calculation for
1511 				 * phantom vactive requires call into DML, so we must ensure all the
1512 				 * vba params are valid otherwise we'll get incorrect phantom vactive.
1513 				 */
1514 				*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
1515 			}
1516 
1517 			dc->res_pool->funcs->add_phantom_pipes(dc, context, pipes, *pipe_cnt, dc_pipe_idx);
1518 
1519 			*pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, false);
1520 			// Populate dppclk to trigger a recalculate in dml_get_voltage_level
1521 			// so the phantom pipe DLG params can be assigned correctly.
1522 			pipes[0].clks_cfg.dppclk_mhz = get_dppclk_calculated(&context->bw_ctx.dml, pipes, *pipe_cnt, 0);
1523 			*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
1524 
1525 			/* Check that vlevel requested supports pstate or not
1526 			 * if not, select the lowest vlevel that supports it
1527 			 */
1528 			for (i = *vlevel; i < context->bw_ctx.dml.soc.num_states; i++) {
1529 				if (vba->DRAMClockChangeSupport[i][vba->maxMpcComb] != dm_dram_clock_change_unsupported) {
1530 					*vlevel = i;
1531 					break;
1532 				}
1533 			}
1534 
1535 			if (*vlevel < context->bw_ctx.dml.soc.num_states
1536 			    && subvp_validate_static_schedulability(dc, context, *vlevel))
1537 				found_supported_config = true;
1538 			if (found_supported_config) {
1539 				// For SubVP + DRR cases, we can force the lowest vlevel that supports the mode
1540 				if (dcn32_subvp_drr_admissable(dc, context) && subvp_drr_schedulable(dc, context)) {
1541 					/* find lowest vlevel that supports the config */
1542 					for (i = *vlevel; i >= 0; i--) {
1543 						if (vba->ModeSupport[i][vba->maxMpcComb]) {
1544 							*vlevel = i;
1545 						} else {
1546 							break;
1547 						}
1548 					}
1549 				}
1550 			}
1551 		}
1552 
1553 		if (vba->DRAMSpeedPerState[*vlevel] >= vba->DRAMSpeedPerState[vlevel_temp])
1554 			found_supported_config = false;
1555 
1556 		// If SubVP pipe config is unsupported (or cannot be used for UCLK switching)
1557 		// remove phantom pipes and repopulate dml pipes
1558 		if (!found_supported_config) {
1559 			dc_state_remove_phantom_streams_and_planes(dc, context);
1560 			dc_state_release_phantom_streams_and_planes(dc, context);
1561 			vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] = dm_dram_clock_change_unsupported;
1562 			*pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, false);
1563 
1564 			*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
1565 			/* This may adjust vlevel and maxMpcComb */
1566 			if (*vlevel < context->bw_ctx.dml.soc.num_states) {
1567 				*vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, *vlevel, split, merge);
1568 				vba->VoltageLevel = *vlevel;
1569 			}
1570 		} else {
1571 			// Most populate phantom DLG params before programming hardware / timing for phantom pipe
1572 			dcn32_helper_populate_phantom_dlg_params(dc, context, pipes, *pipe_cnt);
1573 
1574 			/* Call validate_apply_pipe_split flags after calling DML getters for
1575 			 * phantom dlg params, or some of the VBA params indicating pipe split
1576 			 * can be overwritten by the getters.
1577 			 *
1578 			 * When setting up SubVP config, all pipes are merged before attempting to
1579 			 * add phantom pipes. If pipe split (ODM / MPC) is required, both the main
1580 			 * and phantom pipes will be split in the regular pipe splitting sequence.
1581 			 */
1582 			*vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, *vlevel, split, merge);
1583 			vba->VoltageLevel = *vlevel;
1584 			// Note: We can't apply the phantom pipes to hardware at this time. We have to wait
1585 			// until driver has acquired the DMCUB lock to do it safely.
1586 			assign_subvp_index(dc, context);
1587 		}
1588 	}
1589 
1590 	if (should_apply_odm_power_optimization(dc, context, vba, split, merge))
1591 		try_odm_power_optimization_and_revalidate(
1592 				dc, context, pipes, split, merge, vlevel, *pipe_cnt);
1593 
1594 	return true;
1595 }
1596 
is_dtbclk_required(struct dc * dc,struct dc_state * context)1597 static bool is_dtbclk_required(struct dc *dc, struct dc_state *context)
1598 {
1599 	int i;
1600 
1601 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1602 		if (!context->res_ctx.pipe_ctx[i].stream)
1603 			continue;
1604 		if (dc->link_srv->dp_is_128b_132b_signal(&context->res_ctx.pipe_ctx[i]))
1605 			return true;
1606 	}
1607 	return false;
1608 }
1609 
dcn20_adjust_freesync_v_startup(const struct dc_crtc_timing * dc_crtc_timing,int * vstartup_start)1610 static void dcn20_adjust_freesync_v_startup(const struct dc_crtc_timing *dc_crtc_timing, int *vstartup_start)
1611 {
1612 	struct dc_crtc_timing patched_crtc_timing;
1613 	uint32_t asic_blank_end   = 0;
1614 	uint32_t asic_blank_start = 0;
1615 	uint32_t newVstartup	  = 0;
1616 
1617 	patched_crtc_timing = *dc_crtc_timing;
1618 
1619 	if (patched_crtc_timing.flags.INTERLACE == 1) {
1620 		if (patched_crtc_timing.v_front_porch < 2)
1621 			patched_crtc_timing.v_front_porch = 2;
1622 	} else {
1623 		if (patched_crtc_timing.v_front_porch < 1)
1624 			patched_crtc_timing.v_front_porch = 1;
1625 	}
1626 
1627 	/* blank_start = frame end - front porch */
1628 	asic_blank_start = patched_crtc_timing.v_total -
1629 					patched_crtc_timing.v_front_porch;
1630 
1631 	/* blank_end = blank_start - active */
1632 	asic_blank_end = asic_blank_start -
1633 					patched_crtc_timing.v_border_bottom -
1634 					patched_crtc_timing.v_addressable -
1635 					patched_crtc_timing.v_border_top;
1636 
1637 	newVstartup = asic_blank_end + (patched_crtc_timing.v_total - asic_blank_start);
1638 
1639 	*vstartup_start = ((newVstartup > *vstartup_start) ? newVstartup : *vstartup_start);
1640 }
1641 
dcn32_calculate_dlg_params(struct dc * dc,struct dc_state * context,display_e2e_pipe_params_st * pipes,int pipe_cnt,int vlevel)1642 static void dcn32_calculate_dlg_params(struct dc *dc, struct dc_state *context,
1643 				       display_e2e_pipe_params_st *pipes,
1644 				       int pipe_cnt, int vlevel)
1645 {
1646 	int i, pipe_idx, active_hubp_count = 0;
1647 	bool usr_retraining_support = false;
1648 	bool unbounded_req_enabled = false;
1649 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
1650 
1651 	dc_assert_fp_enabled();
1652 
1653 	/* Writeback MCIF_WB arbitration parameters */
1654 	dc->res_pool->funcs->set_mcif_arb_params(dc, context, pipes, pipe_cnt);
1655 
1656 	context->bw_ctx.bw.dcn.clk.dispclk_khz = context->bw_ctx.dml.vba.DISPCLK * 1000;
1657 	context->bw_ctx.bw.dcn.clk.dcfclk_khz = context->bw_ctx.dml.vba.DCFCLK * 1000;
1658 	context->bw_ctx.bw.dcn.clk.socclk_khz = context->bw_ctx.dml.vba.SOCCLK * 1000;
1659 	context->bw_ctx.bw.dcn.clk.dramclk_khz = context->bw_ctx.dml.vba.DRAMSpeed * 1000 / 16;
1660 	context->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz = context->bw_ctx.dml.vba.DCFCLKDeepSleep * 1000;
1661 	context->bw_ctx.bw.dcn.clk.fclk_khz = context->bw_ctx.dml.vba.FabricClock * 1000;
1662 	context->bw_ctx.bw.dcn.clk.p_state_change_support =
1663 			context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb]
1664 					!= dm_dram_clock_change_unsupported;
1665 
1666 	/* Pstate change might not be supported by hardware, but it might be
1667 	 * possible with firmware driven vertical blank stretching.
1668 	 */
1669 	context->bw_ctx.bw.dcn.clk.p_state_change_support |= context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching;
1670 
1671 	context->bw_ctx.bw.dcn.clk.dppclk_khz = 0;
1672 	context->bw_ctx.bw.dcn.clk.dtbclk_en = is_dtbclk_required(dc, context);
1673 	context->bw_ctx.bw.dcn.clk.ref_dtbclk_khz = context->bw_ctx.dml.vba.DTBCLKPerState[vlevel] * 1000;
1674 	if (context->bw_ctx.dml.vba.FCLKChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] == dm_fclock_change_unsupported)
1675 		context->bw_ctx.bw.dcn.clk.fclk_p_state_change_support = false;
1676 	else
1677 		context->bw_ctx.bw.dcn.clk.fclk_p_state_change_support = true;
1678 
1679 	usr_retraining_support = context->bw_ctx.dml.vba.USRRetrainingSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
1680 	ASSERT(usr_retraining_support);
1681 
1682 	if (context->bw_ctx.bw.dcn.clk.dispclk_khz < dc->debug.min_disp_clk_khz)
1683 		context->bw_ctx.bw.dcn.clk.dispclk_khz = dc->debug.min_disp_clk_khz;
1684 
1685 	unbounded_req_enabled = get_unbounded_request_enabled(&context->bw_ctx.dml, pipes, pipe_cnt);
1686 
1687 	if (unbounded_req_enabled && pipe_cnt > 1) {
1688 		// Unbounded requesting should not ever be used when more than 1 pipe is enabled.
1689 		ASSERT(false);
1690 		unbounded_req_enabled = false;
1691 	}
1692 
1693 	context->bw_ctx.bw.dcn.mall_ss_size_bytes = 0;
1694 	context->bw_ctx.bw.dcn.mall_ss_psr_active_size_bytes = 0;
1695 	context->bw_ctx.bw.dcn.mall_subvp_size_bytes = 0;
1696 
1697 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1698 		if (!context->res_ctx.pipe_ctx[i].stream)
1699 			continue;
1700 		if (context->res_ctx.pipe_ctx[i].plane_state)
1701 			active_hubp_count++;
1702 		pipes[pipe_idx].pipe.dest.vstartup_start = get_vstartup(&context->bw_ctx.dml, pipes, pipe_cnt,
1703 				pipe_idx);
1704 		pipes[pipe_idx].pipe.dest.vupdate_offset = get_vupdate_offset(&context->bw_ctx.dml, pipes, pipe_cnt,
1705 				pipe_idx);
1706 		pipes[pipe_idx].pipe.dest.vupdate_width = get_vupdate_width(&context->bw_ctx.dml, pipes, pipe_cnt,
1707 				pipe_idx);
1708 		pipes[pipe_idx].pipe.dest.vready_offset = get_vready_offset(&context->bw_ctx.dml, pipes, pipe_cnt,
1709 				pipe_idx);
1710 
1711 		if (dc_state_get_pipe_subvp_type(context, &context->res_ctx.pipe_ctx[i]) == SUBVP_PHANTOM) {
1712 			// Phantom pipe requires that DET_SIZE = 0 and no unbounded requests
1713 			context->res_ctx.pipe_ctx[i].det_buffer_size_kb = 0;
1714 			context->res_ctx.pipe_ctx[i].unbounded_req = false;
1715 		} else {
1716 			context->res_ctx.pipe_ctx[i].det_buffer_size_kb = get_det_buffer_size_kbytes(&context->bw_ctx.dml, pipes, pipe_cnt,
1717 							pipe_idx);
1718 			context->res_ctx.pipe_ctx[i].unbounded_req = unbounded_req_enabled;
1719 		}
1720 
1721 		if (context->bw_ctx.bw.dcn.clk.dppclk_khz < pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000)
1722 			context->bw_ctx.bw.dcn.clk.dppclk_khz = pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000;
1723 		if (context->res_ctx.pipe_ctx[i].plane_state)
1724 			context->res_ctx.pipe_ctx[i].plane_res.bw.dppclk_khz = pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000;
1725 		else
1726 			context->res_ctx.pipe_ctx[i].plane_res.bw.dppclk_khz = 0;
1727 		context->res_ctx.pipe_ctx[i].pipe_dlg_param = pipes[pipe_idx].pipe.dest;
1728 
1729 		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);
1730 
1731 		if (vba->ActiveDRAMClockChangeLatencyMarginPerState[vba->VoltageLevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]] > 0)
1732 			context->res_ctx.pipe_ctx[i].has_vactive_margin = true;
1733 		else
1734 			context->res_ctx.pipe_ctx[i].has_vactive_margin = false;
1735 
1736 		/* MALL Allocation Sizes */
1737 		/* count from active, top pipes per plane only */
1738 		if (context->res_ctx.pipe_ctx[i].stream && context->res_ctx.pipe_ctx[i].plane_state &&
1739 				(context->res_ctx.pipe_ctx[i].top_pipe == NULL ||
1740 				context->res_ctx.pipe_ctx[i].plane_state != context->res_ctx.pipe_ctx[i].top_pipe->plane_state) &&
1741 				context->res_ctx.pipe_ctx[i].prev_odm_pipe == NULL) {
1742 			/* SS: all active surfaces stored in MALL */
1743 			if (dc_state_get_pipe_subvp_type(context, &context->res_ctx.pipe_ctx[i]) != SUBVP_PHANTOM) {
1744 				context->bw_ctx.bw.dcn.mall_ss_size_bytes += context->res_ctx.pipe_ctx[i].surface_size_in_mall_bytes;
1745 
1746 				if (context->res_ctx.pipe_ctx[i].stream->link->psr_settings.psr_version == DC_PSR_VERSION_UNSUPPORTED) {
1747 					/* SS PSR On: all active surfaces part of streams not supporting PSR stored in MALL */
1748 					context->bw_ctx.bw.dcn.mall_ss_psr_active_size_bytes += context->res_ctx.pipe_ctx[i].surface_size_in_mall_bytes;
1749 				}
1750 			} else {
1751 				/* SUBVP: phantom surfaces only stored in MALL */
1752 				context->bw_ctx.bw.dcn.mall_subvp_size_bytes += context->res_ctx.pipe_ctx[i].surface_size_in_mall_bytes;
1753 			}
1754 		}
1755 
1756 		if (context->res_ctx.pipe_ctx[i].stream->adaptive_sync_infopacket.valid)
1757 			dcn20_adjust_freesync_v_startup(
1758 				&context->res_ctx.pipe_ctx[i].stream->timing,
1759 				&context->res_ctx.pipe_ctx[i].pipe_dlg_param.vstartup_start);
1760 
1761 		pipe_idx++;
1762 	}
1763 	/* If DCN isn't making memory requests we can allow pstate change and lower clocks */
1764 	if (!active_hubp_count) {
1765 		context->bw_ctx.bw.dcn.clk.socclk_khz = 0;
1766 		context->bw_ctx.bw.dcn.clk.dppclk_khz = 0;
1767 		context->bw_ctx.bw.dcn.clk.dcfclk_khz = 0;
1768 		context->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz = 0;
1769 		context->bw_ctx.bw.dcn.clk.dramclk_khz = 0;
1770 		context->bw_ctx.bw.dcn.clk.fclk_khz = 0;
1771 		context->bw_ctx.bw.dcn.clk.p_state_change_support = true;
1772 		context->bw_ctx.bw.dcn.clk.fclk_p_state_change_support = true;
1773 	}
1774 	/*save a original dppclock copy*/
1775 	context->bw_ctx.bw.dcn.clk.bw_dppclk_khz = context->bw_ctx.bw.dcn.clk.dppclk_khz;
1776 	context->bw_ctx.bw.dcn.clk.bw_dispclk_khz = context->bw_ctx.bw.dcn.clk.dispclk_khz;
1777 	context->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz = context->bw_ctx.dml.soc.clock_limits[vlevel].dppclk_mhz
1778 			* 1000;
1779 	context->bw_ctx.bw.dcn.clk.max_supported_dispclk_khz = context->bw_ctx.dml.soc.clock_limits[vlevel].dispclk_mhz
1780 			* 1000;
1781 
1782 	context->bw_ctx.bw.dcn.clk.num_ways = dcn32_helper_calculate_num_ways_for_subvp(dc, context);
1783 
1784 	context->bw_ctx.bw.dcn.compbuf_size_kb = context->bw_ctx.dml.ip.config_return_buffer_size_in_kbytes;
1785 
1786 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1787 		if (context->res_ctx.pipe_ctx[i].stream)
1788 			context->bw_ctx.bw.dcn.compbuf_size_kb -= context->res_ctx.pipe_ctx[i].det_buffer_size_kb;
1789 	}
1790 
1791 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1792 
1793 		if (!context->res_ctx.pipe_ctx[i].stream)
1794 			continue;
1795 
1796 		context->bw_ctx.dml.funcs.rq_dlg_get_dlg_reg_v2(&context->bw_ctx.dml,
1797 				&context->res_ctx.pipe_ctx[i].dlg_regs, &context->res_ctx.pipe_ctx[i].ttu_regs, pipes,
1798 				pipe_cnt, pipe_idx);
1799 
1800 		context->bw_ctx.dml.funcs.rq_dlg_get_rq_reg_v2(&context->res_ctx.pipe_ctx[i].rq_regs,
1801 				&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
1802 		pipe_idx++;
1803 	}
1804 }
1805 
dcn32_find_split_pipe(struct dc * dc,struct dc_state * context,int old_index)1806 static struct pipe_ctx *dcn32_find_split_pipe(
1807 		struct dc *dc,
1808 		struct dc_state *context,
1809 		int old_index)
1810 {
1811 	struct pipe_ctx *pipe = NULL;
1812 	int i;
1813 
1814 	if (old_index >= 0 && context->res_ctx.pipe_ctx[old_index].stream == NULL) {
1815 		pipe = &context->res_ctx.pipe_ctx[old_index];
1816 		pipe->pipe_idx = old_index;
1817 	}
1818 
1819 	if (!pipe)
1820 		for (i = dc->res_pool->pipe_count - 1; i >= 0; i--) {
1821 			if (dc->current_state->res_ctx.pipe_ctx[i].top_pipe == NULL
1822 					&& dc->current_state->res_ctx.pipe_ctx[i].prev_odm_pipe == NULL) {
1823 				if (context->res_ctx.pipe_ctx[i].stream == NULL) {
1824 					pipe = &context->res_ctx.pipe_ctx[i];
1825 					pipe->pipe_idx = i;
1826 					break;
1827 				}
1828 			}
1829 		}
1830 
1831 	/*
1832 	 * May need to fix pipes getting tossed from 1 opp to another on flip
1833 	 * Add for debugging transient underflow during topology updates:
1834 	 * ASSERT(pipe);
1835 	 */
1836 	if (!pipe)
1837 		for (i = dc->res_pool->pipe_count - 1; i >= 0; i--) {
1838 			if (context->res_ctx.pipe_ctx[i].stream == NULL) {
1839 				pipe = &context->res_ctx.pipe_ctx[i];
1840 				pipe->pipe_idx = i;
1841 				break;
1842 			}
1843 		}
1844 
1845 	return pipe;
1846 }
1847 
dcn32_split_stream_for_mpc_or_odm(const struct dc * dc,struct resource_context * res_ctx,struct pipe_ctx * pri_pipe,struct pipe_ctx * sec_pipe,bool odm)1848 static bool dcn32_split_stream_for_mpc_or_odm(
1849 		const struct dc *dc,
1850 		struct resource_context *res_ctx,
1851 		struct pipe_ctx *pri_pipe,
1852 		struct pipe_ctx *sec_pipe,
1853 		bool odm)
1854 {
1855 	int pipe_idx = sec_pipe->pipe_idx;
1856 	const struct resource_pool *pool = dc->res_pool;
1857 
1858 	DC_LOGGER_INIT(dc->ctx->logger);
1859 
1860 	if (odm && pri_pipe->plane_state) {
1861 		/* ODM + window MPO, where MPO window is on left half only */
1862 		if (pri_pipe->plane_state->clip_rect.x + pri_pipe->plane_state->clip_rect.width <=
1863 				pri_pipe->stream->src.x + pri_pipe->stream->src.width/2) {
1864 
1865 			DC_LOG_SCALER("%s - ODM + window MPO(left). pri_pipe:%d\n",
1866 					__func__,
1867 					pri_pipe->pipe_idx);
1868 			return true;
1869 		}
1870 
1871 		/* ODM + window MPO, where MPO window is on right half only */
1872 		if (pri_pipe->plane_state->clip_rect.x >= pri_pipe->stream->src.x +  pri_pipe->stream->src.width/2) {
1873 
1874 			DC_LOG_SCALER("%s - ODM + window MPO(right). pri_pipe:%d\n",
1875 					__func__,
1876 					pri_pipe->pipe_idx);
1877 			return true;
1878 		}
1879 	}
1880 
1881 	*sec_pipe = *pri_pipe;
1882 
1883 	sec_pipe->pipe_idx = pipe_idx;
1884 	sec_pipe->plane_res.mi = pool->mis[pipe_idx];
1885 	sec_pipe->plane_res.hubp = pool->hubps[pipe_idx];
1886 	sec_pipe->plane_res.ipp = pool->ipps[pipe_idx];
1887 	sec_pipe->plane_res.xfm = pool->transforms[pipe_idx];
1888 	sec_pipe->plane_res.dpp = pool->dpps[pipe_idx];
1889 	sec_pipe->plane_res.mpcc_inst = pool->dpps[pipe_idx]->inst;
1890 	sec_pipe->stream_res.dsc = NULL;
1891 	if (odm) {
1892 		if (pri_pipe->next_odm_pipe) {
1893 			ASSERT(pri_pipe->next_odm_pipe != sec_pipe);
1894 			sec_pipe->next_odm_pipe = pri_pipe->next_odm_pipe;
1895 			sec_pipe->next_odm_pipe->prev_odm_pipe = sec_pipe;
1896 		}
1897 		if (pri_pipe->top_pipe && pri_pipe->top_pipe->next_odm_pipe) {
1898 			pri_pipe->top_pipe->next_odm_pipe->bottom_pipe = sec_pipe;
1899 			sec_pipe->top_pipe = pri_pipe->top_pipe->next_odm_pipe;
1900 		}
1901 		if (pri_pipe->bottom_pipe && pri_pipe->bottom_pipe->next_odm_pipe) {
1902 			pri_pipe->bottom_pipe->next_odm_pipe->top_pipe = sec_pipe;
1903 			sec_pipe->bottom_pipe = pri_pipe->bottom_pipe->next_odm_pipe;
1904 		}
1905 		pri_pipe->next_odm_pipe = sec_pipe;
1906 		sec_pipe->prev_odm_pipe = pri_pipe;
1907 		ASSERT(sec_pipe->top_pipe == NULL);
1908 
1909 		if (!sec_pipe->top_pipe)
1910 			sec_pipe->stream_res.opp = pool->opps[pipe_idx];
1911 		else
1912 			sec_pipe->stream_res.opp = sec_pipe->top_pipe->stream_res.opp;
1913 		if (sec_pipe->stream->timing.flags.DSC == 1) {
1914 			dcn20_acquire_dsc(dc, res_ctx, &sec_pipe->stream_res.dsc, pipe_idx);
1915 			ASSERT(sec_pipe->stream_res.dsc);
1916 			if (sec_pipe->stream_res.dsc == NULL)
1917 				return false;
1918 		}
1919 	} else {
1920 		if (pri_pipe->bottom_pipe) {
1921 			ASSERT(pri_pipe->bottom_pipe != sec_pipe);
1922 			sec_pipe->bottom_pipe = pri_pipe->bottom_pipe;
1923 			sec_pipe->bottom_pipe->top_pipe = sec_pipe;
1924 		}
1925 		pri_pipe->bottom_pipe = sec_pipe;
1926 		sec_pipe->top_pipe = pri_pipe;
1927 
1928 		ASSERT(pri_pipe->plane_state);
1929 	}
1930 
1931 	return true;
1932 }
1933 
dcn32_apply_merge_split_flags_helper(struct dc * dc,struct dc_state * context,bool * repopulate_pipes,int * split,bool * merge)1934 static bool dcn32_apply_merge_split_flags_helper(
1935 		struct dc *dc,
1936 		struct dc_state *context,
1937 		bool *repopulate_pipes,
1938 		int *split,
1939 		bool *merge)
1940 {
1941 	int i, pipe_idx;
1942 	bool newly_split[MAX_PIPES] = { false };
1943 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
1944 
1945 	if (dc->config.enable_windowed_mpo_odm) {
1946 		if (update_pipes_with_split_flags(
1947 			dc, context, vba, split, merge))
1948 			*repopulate_pipes = true;
1949 	} else {
1950 
1951 		/* the code below will be removed once windowed mpo odm is fully
1952 		 * enabled.
1953 		 */
1954 		/* merge pipes if necessary */
1955 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
1956 			struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1957 
1958 			/*skip pipes that don't need merging*/
1959 			if (!merge[i])
1960 				continue;
1961 
1962 			/* if ODM merge we ignore mpc tree, mpo pipes will have their own flags */
1963 			if (pipe->prev_odm_pipe) {
1964 				/*split off odm pipe*/
1965 				pipe->prev_odm_pipe->next_odm_pipe = pipe->next_odm_pipe;
1966 				if (pipe->next_odm_pipe)
1967 					pipe->next_odm_pipe->prev_odm_pipe = pipe->prev_odm_pipe;
1968 
1969 				/*2:1ODM+MPC Split MPO to Single Pipe + MPC Split MPO*/
1970 				if (pipe->bottom_pipe) {
1971 					if (pipe->bottom_pipe->prev_odm_pipe || pipe->bottom_pipe->next_odm_pipe) {
1972 						/*MPC split rules will handle this case*/
1973 						pipe->bottom_pipe->top_pipe = NULL;
1974 					} else {
1975 						/* when merging an ODM pipes, the bottom MPC pipe must now point to
1976 						 * the previous ODM pipe and its associated stream assets
1977 						 */
1978 						if (pipe->prev_odm_pipe->bottom_pipe) {
1979 							/* 3 plane MPO*/
1980 							pipe->bottom_pipe->top_pipe = pipe->prev_odm_pipe->bottom_pipe;
1981 							pipe->prev_odm_pipe->bottom_pipe->bottom_pipe = pipe->bottom_pipe;
1982 						} else {
1983 							/* 2 plane MPO*/
1984 							pipe->bottom_pipe->top_pipe = pipe->prev_odm_pipe;
1985 							pipe->prev_odm_pipe->bottom_pipe = pipe->bottom_pipe;
1986 						}
1987 
1988 						memcpy(&pipe->bottom_pipe->stream_res, &pipe->bottom_pipe->top_pipe->stream_res, sizeof(struct stream_resource));
1989 					}
1990 				}
1991 
1992 				if (pipe->top_pipe) {
1993 					pipe->top_pipe->bottom_pipe = NULL;
1994 				}
1995 
1996 				pipe->bottom_pipe = NULL;
1997 				pipe->next_odm_pipe = NULL;
1998 				pipe->plane_state = NULL;
1999 				pipe->stream = NULL;
2000 				pipe->top_pipe = NULL;
2001 				pipe->prev_odm_pipe = NULL;
2002 				if (pipe->stream_res.dsc)
2003 					dcn20_release_dsc(&context->res_ctx, dc->res_pool, &pipe->stream_res.dsc);
2004 				memset(&pipe->plane_res, 0, sizeof(pipe->plane_res));
2005 				memset(&pipe->stream_res, 0, sizeof(pipe->stream_res));
2006 				memset(&pipe->link_res, 0, sizeof(pipe->link_res));
2007 				*repopulate_pipes = true;
2008 			} else if (pipe->top_pipe && pipe->top_pipe->plane_state == pipe->plane_state) {
2009 				struct pipe_ctx *top_pipe = pipe->top_pipe;
2010 				struct pipe_ctx *bottom_pipe = pipe->bottom_pipe;
2011 
2012 				top_pipe->bottom_pipe = bottom_pipe;
2013 				if (bottom_pipe)
2014 					bottom_pipe->top_pipe = top_pipe;
2015 
2016 				pipe->top_pipe = NULL;
2017 				pipe->bottom_pipe = NULL;
2018 				pipe->plane_state = NULL;
2019 				pipe->stream = NULL;
2020 				memset(&pipe->plane_res, 0, sizeof(pipe->plane_res));
2021 				memset(&pipe->stream_res, 0, sizeof(pipe->stream_res));
2022 				memset(&pipe->link_res, 0, sizeof(pipe->link_res));
2023 				*repopulate_pipes = true;
2024 			} else
2025 				ASSERT(0); /* Should never try to merge master pipe */
2026 
2027 		}
2028 
2029 		for (i = 0, pipe_idx = -1; i < dc->res_pool->pipe_count; i++) {
2030 			struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2031 			struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2032 			struct pipe_ctx *hsplit_pipe = NULL;
2033 			bool odm;
2034 			int old_index = -1;
2035 
2036 			if (!pipe->stream || newly_split[i])
2037 				continue;
2038 
2039 			pipe_idx++;
2040 			odm = vba->ODMCombineEnabled[vba->pipe_plane[pipe_idx]] != dm_odm_combine_mode_disabled;
2041 
2042 			if (!pipe->plane_state && !odm)
2043 				continue;
2044 
2045 			if (split[i]) {
2046 				if (odm) {
2047 					if (split[i] == 4 && old_pipe->next_odm_pipe && old_pipe->next_odm_pipe->next_odm_pipe)
2048 						old_index = old_pipe->next_odm_pipe->next_odm_pipe->pipe_idx;
2049 					else if (old_pipe->next_odm_pipe)
2050 						old_index = old_pipe->next_odm_pipe->pipe_idx;
2051 				} else {
2052 					if (split[i] == 4 && old_pipe->bottom_pipe && old_pipe->bottom_pipe->bottom_pipe &&
2053 							old_pipe->bottom_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2054 						old_index = old_pipe->bottom_pipe->bottom_pipe->pipe_idx;
2055 					else if (old_pipe->bottom_pipe &&
2056 							old_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2057 						old_index = old_pipe->bottom_pipe->pipe_idx;
2058 				}
2059 				hsplit_pipe = dcn32_find_split_pipe(dc, context, old_index);
2060 				ASSERT(hsplit_pipe);
2061 				if (!hsplit_pipe)
2062 					return false;
2063 
2064 				if (!dcn32_split_stream_for_mpc_or_odm(
2065 						dc, &context->res_ctx,
2066 						pipe, hsplit_pipe, odm))
2067 					return false;
2068 
2069 				newly_split[hsplit_pipe->pipe_idx] = true;
2070 				*repopulate_pipes = true;
2071 			}
2072 			if (split[i] == 4) {
2073 				struct pipe_ctx *pipe_4to1;
2074 
2075 				if (odm && old_pipe->next_odm_pipe)
2076 					old_index = old_pipe->next_odm_pipe->pipe_idx;
2077 				else if (!odm && old_pipe->bottom_pipe &&
2078 							old_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2079 					old_index = old_pipe->bottom_pipe->pipe_idx;
2080 				else
2081 					old_index = -1;
2082 				pipe_4to1 = dcn32_find_split_pipe(dc, context, old_index);
2083 				ASSERT(pipe_4to1);
2084 				if (!pipe_4to1)
2085 					return false;
2086 				if (!dcn32_split_stream_for_mpc_or_odm(
2087 						dc, &context->res_ctx,
2088 						pipe, pipe_4to1, odm))
2089 					return false;
2090 				newly_split[pipe_4to1->pipe_idx] = true;
2091 
2092 				if (odm && old_pipe->next_odm_pipe && old_pipe->next_odm_pipe->next_odm_pipe
2093 						&& old_pipe->next_odm_pipe->next_odm_pipe->next_odm_pipe)
2094 					old_index = old_pipe->next_odm_pipe->next_odm_pipe->next_odm_pipe->pipe_idx;
2095 				else if (!odm && old_pipe->bottom_pipe && old_pipe->bottom_pipe->bottom_pipe &&
2096 						old_pipe->bottom_pipe->bottom_pipe->bottom_pipe &&
2097 						old_pipe->bottom_pipe->bottom_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2098 					old_index = old_pipe->bottom_pipe->bottom_pipe->bottom_pipe->pipe_idx;
2099 				else
2100 					old_index = -1;
2101 				pipe_4to1 = dcn32_find_split_pipe(dc, context, old_index);
2102 				ASSERT(pipe_4to1);
2103 				if (!pipe_4to1)
2104 					return false;
2105 				if (!dcn32_split_stream_for_mpc_or_odm(
2106 						dc, &context->res_ctx,
2107 						hsplit_pipe, pipe_4to1, odm))
2108 					return false;
2109 				newly_split[pipe_4to1->pipe_idx] = true;
2110 			}
2111 			if (odm)
2112 				dcn20_build_mapped_resource(dc, context, pipe->stream);
2113 		}
2114 
2115 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2116 			struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2117 
2118 			if (pipe->plane_state) {
2119 				if (!resource_build_scaling_params(pipe))
2120 					return false;
2121 			}
2122 		}
2123 
2124 		for (i = 0; i < context->stream_count; i++) {
2125 			struct pipe_ctx *otg_master = resource_get_otg_master_for_stream(&context->res_ctx,
2126 					context->streams[i]);
2127 
2128 			if (otg_master)
2129 				resource_build_test_pattern_params(&context->res_ctx, otg_master);
2130 		}
2131 	}
2132 	return true;
2133 }
2134 
dcn32_internal_validate_bw(struct dc * dc,struct dc_state * context,display_e2e_pipe_params_st * pipes,int * pipe_cnt_out,int * vlevel_out,bool fast_validate)2135 bool dcn32_internal_validate_bw(struct dc *dc,
2136 				struct dc_state *context,
2137 				display_e2e_pipe_params_st *pipes,
2138 				int *pipe_cnt_out,
2139 				int *vlevel_out,
2140 				bool fast_validate)
2141 {
2142 	bool out = false;
2143 	bool repopulate_pipes = false;
2144 	int split[MAX_PIPES] = { 0 };
2145 	bool merge[MAX_PIPES] = { false };
2146 	int pipe_cnt, i, pipe_idx;
2147 	int vlevel = context->bw_ctx.dml.soc.num_states;
2148 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
2149 
2150 	dc_assert_fp_enabled();
2151 
2152 	ASSERT(pipes);
2153 	if (!pipes)
2154 		return false;
2155 
2156 	/* For each full update, remove all existing phantom pipes first */
2157 	dc_state_remove_phantom_streams_and_planes(dc, context);
2158 	dc_state_release_phantom_streams_and_planes(dc, context);
2159 
2160 	dc->res_pool->funcs->update_soc_for_wm_a(dc, context);
2161 
2162 	for (i = 0; i < context->stream_count; i++)
2163 		resource_update_pipes_for_stream_with_slice_count(context, dc->current_state, dc->res_pool, context->streams[i], 1);
2164 	pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, fast_validate);
2165 
2166 	if (!pipe_cnt) {
2167 		out = true;
2168 		goto validate_out;
2169 	}
2170 
2171 	dml_log_pipe_params(&context->bw_ctx.dml, pipes, pipe_cnt);
2172 	context->bw_ctx.dml.soc.max_vratio_pre = dcn32_determine_max_vratio_prefetch(dc, context);
2173 
2174 	if (!fast_validate) {
2175 		if (!dcn32_full_validate_bw_helper(dc, context, pipes, &vlevel, split, merge,
2176 			&pipe_cnt, &repopulate_pipes))
2177 			goto validate_fail;
2178 	}
2179 
2180 	if (fast_validate ||
2181 			(dc->debug.dml_disallow_alternate_prefetch_modes &&
2182 			(vlevel == context->bw_ctx.dml.soc.num_states ||
2183 				vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported))) {
2184 		/*
2185 		 * If dml_disallow_alternate_prefetch_modes is false, then we have already
2186 		 * tried alternate prefetch modes during full validation.
2187 		 *
2188 		 * If mode is unsupported or there is no p-state support, then
2189 		 * fall back to favouring voltage.
2190 		 *
2191 		 * If Prefetch mode 0 failed for this config, or passed with Max UCLK, then try
2192 		 * to support with Prefetch mode 1 (dm_prefetch_support_fclk_and_stutter == 2)
2193 		 */
2194 		context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
2195 			dm_prefetch_support_none;
2196 
2197 		context->bw_ctx.dml.validate_max_state = fast_validate;
2198 		vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
2199 
2200 		context->bw_ctx.dml.validate_max_state = false;
2201 
2202 		if (vlevel < context->bw_ctx.dml.soc.num_states) {
2203 			memset(split, 0, sizeof(split));
2204 			memset(merge, 0, sizeof(merge));
2205 			vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, vlevel, split, merge);
2206 			/* dcn20_validate_apply_pipe_split_flags can modify voltage level outside of DML */
2207 			vba->VoltageLevel = vlevel;
2208 		}
2209 	}
2210 
2211 	dml_log_mode_support_params(&context->bw_ctx.dml);
2212 
2213 	if (vlevel == context->bw_ctx.dml.soc.num_states)
2214 		goto validate_fail;
2215 
2216 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2217 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2218 		struct pipe_ctx *mpo_pipe = pipe->bottom_pipe;
2219 
2220 		if (!pipe->stream)
2221 			continue;
2222 
2223 		if (vba->ODMCombineEnabled[vba->pipe_plane[pipe_idx]] != dm_odm_combine_mode_disabled
2224 				&& !dc->config.enable_windowed_mpo_odm
2225 				&& pipe->plane_state && mpo_pipe
2226 				&& memcmp(&mpo_pipe->plane_state->clip_rect,
2227 						&pipe->stream->src,
2228 						sizeof(struct rect)) != 0) {
2229 			ASSERT(mpo_pipe->plane_state != pipe->plane_state);
2230 			goto validate_fail;
2231 		}
2232 		pipe_idx++;
2233 	}
2234 
2235 	if (!dcn32_apply_merge_split_flags_helper(dc, context, &repopulate_pipes, split, merge))
2236 		goto validate_fail;
2237 
2238 	/* Actual dsc count per stream dsc validation*/
2239 	if (!dcn20_validate_dsc(dc, context)) {
2240 		vba->ValidationStatus[vba->soc.num_states] = DML_FAIL_DSC_VALIDATION_FAILURE;
2241 		goto validate_fail;
2242 	}
2243 
2244 	if (repopulate_pipes) {
2245 		int flag_max_mpc_comb = vba->maxMpcComb;
2246 		int flag_vlevel = vlevel;
2247 		int i;
2248 
2249 		pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, fast_validate);
2250 		if (!dc->config.enable_windowed_mpo_odm)
2251 			dcn32_update_dml_pipes_odm_policy_based_on_context(dc, context, pipes);
2252 
2253 		/* repopulate_pipes = 1 means the pipes were either split or merged. In this case
2254 		 * we have to re-calculate the DET allocation and run through DML once more to
2255 		 * ensure all the params are calculated correctly. We do not need to run the
2256 		 * pipe split check again after this call (pipes are already split / merged).
2257 		 * */
2258 		context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
2259 					dm_prefetch_support_uclk_fclk_and_stutter_if_possible;
2260 
2261 		vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
2262 
2263 		if (vlevel == context->bw_ctx.dml.soc.num_states) {
2264 			/* failed after DET size changes */
2265 			goto validate_fail;
2266 		} else if (flag_max_mpc_comb == 0 &&
2267 				flag_max_mpc_comb != context->bw_ctx.dml.vba.maxMpcComb) {
2268 			/* check the context constructed with pipe split flags is still valid*/
2269 			bool flags_valid = false;
2270 			for (i = flag_vlevel; i < context->bw_ctx.dml.soc.num_states; i++) {
2271 				if (vba->ModeSupport[i][flag_max_mpc_comb]) {
2272 					vba->maxMpcComb = flag_max_mpc_comb;
2273 					vba->VoltageLevel = i;
2274 					vlevel = i;
2275 					flags_valid = true;
2276 					break;
2277 				}
2278 			}
2279 
2280 			/* this should never happen */
2281 			if (!flags_valid)
2282 				goto validate_fail;
2283 		}
2284 	}
2285 	*vlevel_out = vlevel;
2286 	*pipe_cnt_out = pipe_cnt;
2287 
2288 	out = true;
2289 	goto validate_out;
2290 
2291 validate_fail:
2292 	out = false;
2293 
2294 validate_out:
2295 	return out;
2296 }
2297 
2298 
dcn32_calculate_wm_and_dlg_fpu(struct dc * dc,struct dc_state * context,display_e2e_pipe_params_st * pipes,int pipe_cnt,int vlevel)2299 void dcn32_calculate_wm_and_dlg_fpu(struct dc *dc, struct dc_state *context,
2300 				display_e2e_pipe_params_st *pipes,
2301 				int pipe_cnt,
2302 				int vlevel)
2303 {
2304 	int i, pipe_idx, vlevel_temp = 0;
2305 	double dcfclk = dcn3_2_soc.clock_limits[0].dcfclk_mhz;
2306 	double dcfclk_from_validation = context->bw_ctx.dml.vba.DCFCLKState[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
2307 	double dram_speed_from_validation = context->bw_ctx.dml.vba.DRAMSpeed;
2308 	double dcfclk_from_fw_based_mclk_switching = dcfclk_from_validation;
2309 	bool pstate_en = context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] !=
2310 			dm_dram_clock_change_unsupported;
2311 	unsigned int dummy_latency_index = 0;
2312 	int maxMpcComb = context->bw_ctx.dml.vba.maxMpcComb;
2313 	unsigned int min_dram_speed_mts = context->bw_ctx.dml.vba.DRAMSpeed;
2314 	bool subvp_in_use = dcn32_subvp_in_use(dc, context);
2315 	unsigned int min_dram_speed_mts_margin;
2316 	bool need_fclk_lat_as_dummy = false;
2317 	bool is_subvp_p_drr = false;
2318 	struct dc_stream_state *fpo_candidate_stream = NULL;
2319 	struct dc_stream_status *stream_status = NULL;
2320 
2321 	dc_assert_fp_enabled();
2322 
2323 	/* need to find dummy latency index for subvp */
2324 	if (subvp_in_use) {
2325 		/* Override DRAMClockChangeSupport for SubVP + DRR case where the DRR cannot switch without stretching it's VBLANK */
2326 		if (!pstate_en) {
2327 			context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][maxMpcComb] = dm_dram_clock_change_vblank_w_mall_sub_vp;
2328 			context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final = dm_prefetch_support_fclk_and_stutter;
2329 			pstate_en = true;
2330 			is_subvp_p_drr = true;
2331 		}
2332 		dummy_latency_index = dcn32_find_dummy_latency_index_for_fw_based_mclk_switch(dc,
2333 						context, pipes, pipe_cnt, vlevel);
2334 
2335 		/* For DCN32/321 need to validate with fclk pstate change latency equal to dummy so prefetch is
2336 		 * scheduled correctly to account for dummy pstate.
2337 		 */
2338 		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) {
2339 			need_fclk_lat_as_dummy = true;
2340 			context->bw_ctx.dml.soc.fclk_change_latency_us =
2341 					dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
2342 		}
2343 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
2344 							dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
2345 		dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel, false);
2346 		maxMpcComb = context->bw_ctx.dml.vba.maxMpcComb;
2347 		if (is_subvp_p_drr) {
2348 			context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][maxMpcComb] = dm_dram_clock_change_vblank_w_mall_sub_vp;
2349 		}
2350 	}
2351 
2352 	context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching = false;
2353 	for (i = 0; i < context->stream_count; i++) {
2354 		stream_status = NULL;
2355 		if (context->streams[i])
2356 			stream_status = dc_state_get_stream_status(context, context->streams[i]);
2357 		if (stream_status)
2358 			stream_status->fpo_in_use = false;
2359 	}
2360 
2361 	if (!pstate_en || (!dc->debug.disable_fpo_optimizations &&
2362 			pstate_en && vlevel != 0)) {
2363 		/* only when the mclk switch can not be natural, is the fw based vblank stretch attempted */
2364 		fpo_candidate_stream = dcn32_can_support_mclk_switch_using_fw_based_vblank_stretch(dc, context);
2365 		if (fpo_candidate_stream) {
2366 			stream_status = dc_state_get_stream_status(context, fpo_candidate_stream);
2367 			if (stream_status)
2368 				stream_status->fpo_in_use = true;
2369 			context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching = true;
2370 		}
2371 
2372 		if (context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching) {
2373 			dummy_latency_index = dcn32_find_dummy_latency_index_for_fw_based_mclk_switch(dc,
2374 				context, pipes, pipe_cnt, vlevel);
2375 
2376 			/* After calling dcn30_find_dummy_latency_index_for_fw_based_mclk_switch
2377 			 * we reinstate the original dram_clock_change_latency_us on the context
2378 			 * and all variables that may have changed up to this point, except the
2379 			 * newly found dummy_latency_index
2380 			 */
2381 			context->bw_ctx.dml.soc.dram_clock_change_latency_us =
2382 					dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
2383 			/* For DCN32/321 need to validate with fclk pstate change latency equal to dummy so
2384 			 * prefetch is scheduled correctly to account for dummy pstate.
2385 			 */
2386 			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) {
2387 				need_fclk_lat_as_dummy = true;
2388 				context->bw_ctx.dml.soc.fclk_change_latency_us =
2389 						dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
2390 			}
2391 			dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel_temp, false);
2392 			if (vlevel_temp < vlevel) {
2393 				vlevel = vlevel_temp;
2394 				maxMpcComb = context->bw_ctx.dml.vba.maxMpcComb;
2395 				dcfclk_from_fw_based_mclk_switching = context->bw_ctx.dml.vba.DCFCLKState[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
2396 				pstate_en = true;
2397 				context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][maxMpcComb] = dm_dram_clock_change_vblank;
2398 			} else {
2399 				/* Restore FCLK latency and re-run validation to go back to original validation
2400 				 * output if we find that enabling FPO does not give us any benefit (i.e. lower
2401 				 * voltage level)
2402 				 */
2403 				context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching = false;
2404 				for (i = 0; i < context->stream_count; i++) {
2405 					stream_status = NULL;
2406 					if (context->streams[i])
2407 						stream_status = dc_state_get_stream_status(context, context->streams[i]);
2408 					if (stream_status)
2409 						stream_status->fpo_in_use = false;
2410 				}
2411 				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;
2412 				dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel, false);
2413 			}
2414 		}
2415 	}
2416 
2417 	/* Set B:
2418 	 * For Set B calculations use clocks from clock_limits[2] when available i.e. when SMU is present,
2419 	 * otherwise use arbitrary low value from spreadsheet for DCFCLK as lower is safer for watermark
2420 	 * calculations to cover bootup clocks.
2421 	 * DCFCLK: soc.clock_limits[2] when available
2422 	 * UCLK: soc.clock_limits[2] when available
2423 	 */
2424 	if (dcn3_2_soc.num_states > 2) {
2425 		vlevel_temp = 2;
2426 		dcfclk = dcn3_2_soc.clock_limits[2].dcfclk_mhz;
2427 	} else
2428 		dcfclk = 615; //DCFCLK Vmin_lv
2429 
2430 	pipes[0].clks_cfg.voltage = vlevel_temp;
2431 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
2432 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel_temp].socclk_mhz;
2433 
2434 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].valid) {
2435 		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;
2436 		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;
2437 		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;
2438 		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;
2439 	}
2440 	context->bw_ctx.bw.dcn.watermarks.b.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2441 	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;
2442 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2443 	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;
2444 	context->bw_ctx.bw.dcn.watermarks.b.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2445 	context->bw_ctx.bw.dcn.watermarks.b.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2446 	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;
2447 	context->bw_ctx.bw.dcn.watermarks.b.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2448 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2449 	context->bw_ctx.bw.dcn.watermarks.b.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2450 
2451 	/* Set D:
2452 	 * All clocks min.
2453 	 * DCFCLK: Min, as reported by PM FW when available
2454 	 * UCLK  : Min, as reported by PM FW when available
2455 	 * sr_enter_exit/sr_exit should be lower than used for DRAM (TBD after bringup or later, use as decided in Clk Mgr)
2456 	 */
2457 
2458 	/*
2459 	if (dcn3_2_soc.num_states > 2) {
2460 		vlevel_temp = 0;
2461 		dcfclk = dc->clk_mgr->bw_params->clk_table.entries[0].dcfclk_mhz;
2462 	} else
2463 		dcfclk = 615; //DCFCLK Vmin_lv
2464 
2465 	pipes[0].clks_cfg.voltage = vlevel_temp;
2466 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
2467 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel_temp].socclk_mhz;
2468 
2469 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].valid) {
2470 		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;
2471 		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;
2472 		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;
2473 		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;
2474 	}
2475 	context->bw_ctx.bw.dcn.watermarks.d.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2476 	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;
2477 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2478 	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;
2479 	context->bw_ctx.bw.dcn.watermarks.d.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2480 	context->bw_ctx.bw.dcn.watermarks.d.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2481 	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;
2482 	context->bw_ctx.bw.dcn.watermarks.d.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2483 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2484 	context->bw_ctx.bw.dcn.watermarks.d.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2485 	*/
2486 
2487 	/* Set C, for Dummy P-State:
2488 	 * All clocks min.
2489 	 * DCFCLK: Min, as reported by PM FW, when available
2490 	 * UCLK  : Min,  as reported by PM FW, when available
2491 	 * pstate latency as per UCLK state dummy pstate latency
2492 	 */
2493 
2494 	// For Set A and Set C use values from validation
2495 	pipes[0].clks_cfg.voltage = vlevel;
2496 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk_from_validation;
2497 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel].socclk_mhz;
2498 
2499 	if (context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching) {
2500 		pipes[0].clks_cfg.dcfclk_mhz = dcfclk_from_fw_based_mclk_switching;
2501 	}
2502 
2503 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].valid) {
2504 		min_dram_speed_mts = dram_speed_from_validation;
2505 		min_dram_speed_mts_margin = 160;
2506 
2507 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
2508 			dc->clk_mgr->bw_params->dummy_pstate_table[0].dummy_pstate_latency_us;
2509 
2510 		if (context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][maxMpcComb] ==
2511 			dm_dram_clock_change_unsupported) {
2512 			int min_dram_speed_mts_offset = dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_memclk_levels - 1;
2513 
2514 			min_dram_speed_mts =
2515 				dc->clk_mgr->bw_params->clk_table.entries[min_dram_speed_mts_offset].memclk_mhz * 16;
2516 		}
2517 
2518 		if (!context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching && !subvp_in_use) {
2519 			/* find largest table entry that is lower than dram speed,
2520 			 * but lower than DPM0 still uses DPM0
2521 			 */
2522 			for (dummy_latency_index = 3; dummy_latency_index > 0; dummy_latency_index--)
2523 				if (min_dram_speed_mts + min_dram_speed_mts_margin >
2524 					dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dram_speed_mts)
2525 					break;
2526 		}
2527 
2528 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
2529 			dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
2530 
2531 		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;
2532 		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;
2533 		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;
2534 	}
2535 
2536 	context->bw_ctx.bw.dcn.watermarks.c.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2537 	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;
2538 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2539 	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;
2540 	context->bw_ctx.bw.dcn.watermarks.c.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2541 	context->bw_ctx.bw.dcn.watermarks.c.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2542 	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;
2543 	context->bw_ctx.bw.dcn.watermarks.c.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2544 	/* On DCN32/321, PMFW will set PSTATE_CHANGE_TYPE = 1 (FCLK) for UCLK dummy p-state.
2545 	 * In this case we must program FCLK WM Set C to use the UCLK dummy p-state WM
2546 	 * value.
2547 	 */
2548 	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;
2549 	context->bw_ctx.bw.dcn.watermarks.c.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2550 
2551 	if ((!pstate_en) && (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].valid)) {
2552 		/* The only difference between A and C is p-state latency, if p-state is not supported
2553 		 * with full p-state latency we want to calculate DLG based on dummy p-state latency,
2554 		 * Set A p-state watermark set to 0 on DCN30, when p-state unsupported, for now keep as DCN30.
2555 		 */
2556 		context->bw_ctx.bw.dcn.watermarks.a = context->bw_ctx.bw.dcn.watermarks.c;
2557 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns = 0;
2558 		/* Calculate FCLK p-state change watermark based on FCLK pstate change latency in case
2559 		 * UCLK p-state is not supported, to avoid underflow in case FCLK pstate is supported
2560 		 */
2561 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2562 	} else {
2563 		/* Set A:
2564 		 * All clocks min.
2565 		 * DCFCLK: Min, as reported by PM FW, when available
2566 		 * UCLK: Min, as reported by PM FW, when available
2567 		 */
2568 
2569 		/* For set A set the correct latency values (i.e. non-dummy values) unconditionally
2570 		 */
2571 		context->bw_ctx.dml.soc.dram_clock_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
2572 		context->bw_ctx.dml.soc.sr_enter_plus_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.sr_enter_plus_exit_time_us;
2573 		context->bw_ctx.dml.soc.sr_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.sr_exit_time_us;
2574 
2575 		context->bw_ctx.bw.dcn.watermarks.a.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2576 		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;
2577 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2578 		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;
2579 		context->bw_ctx.bw.dcn.watermarks.a.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2580 		context->bw_ctx.bw.dcn.watermarks.a.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2581 		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;
2582 		context->bw_ctx.bw.dcn.watermarks.a.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2583 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2584 		context->bw_ctx.bw.dcn.watermarks.a.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2585 	}
2586 
2587 	/* Make set D = set A since we do not optimized watermarks for MALL */
2588 	context->bw_ctx.bw.dcn.watermarks.d = context->bw_ctx.bw.dcn.watermarks.a;
2589 
2590 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2591 		if (!context->res_ctx.pipe_ctx[i].stream)
2592 			continue;
2593 
2594 		pipes[pipe_idx].clks_cfg.dispclk_mhz = get_dispclk_calculated(&context->bw_ctx.dml, pipes, pipe_cnt);
2595 		pipes[pipe_idx].clks_cfg.dppclk_mhz = get_dppclk_calculated(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
2596 
2597 		if (dc->config.forced_clocks) {
2598 			pipes[pipe_idx].clks_cfg.dispclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dispclk_mhz;
2599 			pipes[pipe_idx].clks_cfg.dppclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dppclk_mhz;
2600 		}
2601 		if (dc->debug.min_disp_clk_khz > pipes[pipe_idx].clks_cfg.dispclk_mhz * 1000)
2602 			pipes[pipe_idx].clks_cfg.dispclk_mhz = dc->debug.min_disp_clk_khz / 1000.0;
2603 		if (dc->debug.min_dpp_clk_khz > pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000)
2604 			pipes[pipe_idx].clks_cfg.dppclk_mhz = dc->debug.min_dpp_clk_khz / 1000.0;
2605 
2606 		pipe_idx++;
2607 	}
2608 
2609 	context->perf_params.stutter_period_us = context->bw_ctx.dml.vba.StutterPeriod;
2610 
2611 	/* for proper prefetch calculations, if dummy lat > fclk lat, use fclk lat = dummy lat */
2612 	if (need_fclk_lat_as_dummy)
2613 		context->bw_ctx.dml.soc.fclk_change_latency_us =
2614 				dc->clk_mgr->bw_params->dummy_pstate_table[dummy_latency_index].dummy_pstate_latency_us;
2615 
2616 	dcn32_calculate_dlg_params(dc, context, pipes, pipe_cnt, vlevel);
2617 
2618 	if (!pstate_en)
2619 		/* Restore full p-state latency */
2620 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
2621 				dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
2622 
2623 	/* revert fclk lat changes if required */
2624 	if (need_fclk_lat_as_dummy)
2625 		context->bw_ctx.dml.soc.fclk_change_latency_us =
2626 				dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.fclk_change_latency_us;
2627 }
2628 
dcn32_get_optimal_dcfclk_fclk_for_uclk(unsigned int uclk_mts,unsigned int * optimal_dcfclk,unsigned int * optimal_fclk)2629 static void dcn32_get_optimal_dcfclk_fclk_for_uclk(unsigned int uclk_mts,
2630 		unsigned int *optimal_dcfclk,
2631 		unsigned int *optimal_fclk)
2632 {
2633 	double bw_from_dram, bw_from_dram1, bw_from_dram2;
2634 
2635 	bw_from_dram1 = uclk_mts * dcn3_2_soc.num_chans *
2636 		dcn3_2_soc.dram_channel_width_bytes * (dcn3_2_soc.max_avg_dram_bw_use_normal_percent / 100);
2637 	bw_from_dram2 = uclk_mts * dcn3_2_soc.num_chans *
2638 		dcn3_2_soc.dram_channel_width_bytes * (dcn3_2_soc.max_avg_sdp_bw_use_normal_percent / 100);
2639 
2640 	bw_from_dram = (bw_from_dram1 < bw_from_dram2) ? bw_from_dram1 : bw_from_dram2;
2641 
2642 	if (optimal_fclk)
2643 		*optimal_fclk = bw_from_dram /
2644 		(dcn3_2_soc.fabric_datapath_to_dcn_data_return_bytes * (dcn3_2_soc.max_avg_sdp_bw_use_normal_percent / 100));
2645 
2646 	if (optimal_dcfclk)
2647 		*optimal_dcfclk =  bw_from_dram /
2648 		(dcn3_2_soc.return_bus_width_bytes * (dcn3_2_soc.max_avg_sdp_bw_use_normal_percent / 100));
2649 }
2650 
remove_entry_from_table_at_index(struct _vcs_dpi_voltage_scaling_st * table,unsigned int * num_entries,unsigned int index)2651 static void remove_entry_from_table_at_index(struct _vcs_dpi_voltage_scaling_st *table, unsigned int *num_entries,
2652 		unsigned int index)
2653 {
2654 	int i;
2655 
2656 	if (*num_entries == 0)
2657 		return;
2658 
2659 	for (i = index; i < *num_entries - 1; i++) {
2660 		table[i] = table[i + 1];
2661 	}
2662 	memset(&table[--(*num_entries)], 0, sizeof(struct _vcs_dpi_voltage_scaling_st));
2663 }
2664 
dcn32_patch_dpm_table(struct clk_bw_params * bw_params)2665 void dcn32_patch_dpm_table(struct clk_bw_params *bw_params)
2666 {
2667 	int i;
2668 	unsigned int max_dcfclk_mhz = 0, max_dispclk_mhz = 0, max_dppclk_mhz = 0,
2669 			max_phyclk_mhz = 0, max_dtbclk_mhz = 0, max_fclk_mhz = 0, max_uclk_mhz = 0;
2670 
2671 	for (i = 0; i < MAX_NUM_DPM_LVL; i++) {
2672 		if (bw_params->clk_table.entries[i].dcfclk_mhz > max_dcfclk_mhz)
2673 			max_dcfclk_mhz = bw_params->clk_table.entries[i].dcfclk_mhz;
2674 		if (bw_params->clk_table.entries[i].fclk_mhz > max_fclk_mhz)
2675 			max_fclk_mhz = bw_params->clk_table.entries[i].fclk_mhz;
2676 		if (bw_params->clk_table.entries[i].memclk_mhz > max_uclk_mhz)
2677 			max_uclk_mhz = bw_params->clk_table.entries[i].memclk_mhz;
2678 		if (bw_params->clk_table.entries[i].dispclk_mhz > max_dispclk_mhz)
2679 			max_dispclk_mhz = bw_params->clk_table.entries[i].dispclk_mhz;
2680 		if (bw_params->clk_table.entries[i].dppclk_mhz > max_dppclk_mhz)
2681 			max_dppclk_mhz = bw_params->clk_table.entries[i].dppclk_mhz;
2682 		if (bw_params->clk_table.entries[i].phyclk_mhz > max_phyclk_mhz)
2683 			max_phyclk_mhz = bw_params->clk_table.entries[i].phyclk_mhz;
2684 		if (bw_params->clk_table.entries[i].dtbclk_mhz > max_dtbclk_mhz)
2685 			max_dtbclk_mhz = bw_params->clk_table.entries[i].dtbclk_mhz;
2686 	}
2687 
2688 	/* Scan through clock values we currently have and if they are 0,
2689 	 *  then populate it with dcn3_2_soc.clock_limits[] value.
2690 	 *
2691 	 * Do it for DCFCLK, DISPCLK, DTBCLK and UCLK as any of those being
2692 	 *  0, will cause it to skip building the clock table.
2693 	 */
2694 	if (max_dcfclk_mhz == 0)
2695 		bw_params->clk_table.entries[0].dcfclk_mhz = dcn3_2_soc.clock_limits[0].dcfclk_mhz;
2696 	if (max_dispclk_mhz == 0)
2697 		bw_params->clk_table.entries[0].dispclk_mhz = dcn3_2_soc.clock_limits[0].dispclk_mhz;
2698 	if (max_dtbclk_mhz == 0)
2699 		bw_params->clk_table.entries[0].dtbclk_mhz = dcn3_2_soc.clock_limits[0].dtbclk_mhz;
2700 	if (max_uclk_mhz == 0)
2701 		bw_params->clk_table.entries[0].memclk_mhz = dcn3_2_soc.clock_limits[0].dram_speed_mts / 16;
2702 }
2703 
swap_table_entries(struct _vcs_dpi_voltage_scaling_st * first_entry,struct _vcs_dpi_voltage_scaling_st * second_entry)2704 static void swap_table_entries(struct _vcs_dpi_voltage_scaling_st *first_entry,
2705 		struct _vcs_dpi_voltage_scaling_st *second_entry)
2706 {
2707 	struct _vcs_dpi_voltage_scaling_st temp_entry = *first_entry;
2708 	*first_entry = *second_entry;
2709 	*second_entry = temp_entry;
2710 }
2711 
2712 /*
2713  * sort_entries_with_same_bw - Sort entries sharing the same bandwidth by DCFCLK
2714  */
sort_entries_with_same_bw(struct _vcs_dpi_voltage_scaling_st * table,unsigned int * num_entries)2715 static void sort_entries_with_same_bw(struct _vcs_dpi_voltage_scaling_st *table, unsigned int *num_entries)
2716 {
2717 	unsigned int start_index = 0;
2718 	unsigned int end_index = 0;
2719 	unsigned int current_bw = 0;
2720 
2721 	for (int i = 0; i < (*num_entries - 1); i++) {
2722 		if (table[i].net_bw_in_kbytes_sec == table[i+1].net_bw_in_kbytes_sec) {
2723 			current_bw = table[i].net_bw_in_kbytes_sec;
2724 			start_index = i;
2725 			end_index = ++i;
2726 
2727 			while ((i < (*num_entries - 1)) && (table[i+1].net_bw_in_kbytes_sec == current_bw))
2728 				end_index = ++i;
2729 		}
2730 
2731 		if (start_index != end_index) {
2732 			for (int j = start_index; j < end_index; j++) {
2733 				for (int k = start_index; k < end_index; k++) {
2734 					if (table[k].dcfclk_mhz > table[k+1].dcfclk_mhz)
2735 						swap_table_entries(&table[k], &table[k+1]);
2736 				}
2737 			}
2738 		}
2739 
2740 		start_index = 0;
2741 		end_index = 0;
2742 
2743 	}
2744 }
2745 
2746 /*
2747  * remove_inconsistent_entries - Ensure entries with the same bandwidth have MEMCLK and FCLK monotonically increasing
2748  *                               and remove entries that do not
2749  */
remove_inconsistent_entries(struct _vcs_dpi_voltage_scaling_st * table,unsigned int * num_entries)2750 static void remove_inconsistent_entries(struct _vcs_dpi_voltage_scaling_st *table, unsigned int *num_entries)
2751 {
2752 	for (int i = 0; i < (*num_entries - 1); i++) {
2753 		if (table[i].net_bw_in_kbytes_sec == table[i+1].net_bw_in_kbytes_sec) {
2754 			if ((table[i].dram_speed_mts > table[i+1].dram_speed_mts) ||
2755 				(table[i].fabricclk_mhz > table[i+1].fabricclk_mhz))
2756 				remove_entry_from_table_at_index(table, num_entries, i);
2757 		}
2758 	}
2759 }
2760 
2761 /*
2762  * override_max_clk_values - Overwrite the max clock frequencies with the max DC mode timings
2763  * Input:
2764  *	max_clk_limit - struct containing the desired clock timings
2765  * Output:
2766  *	curr_clk_limit  - struct containing the timings that need to be overwritten
2767  * Return: 0 upon success, non-zero for failure
2768  */
override_max_clk_values(struct clk_limit_table_entry * max_clk_limit,struct clk_limit_table_entry * curr_clk_limit)2769 static int override_max_clk_values(struct clk_limit_table_entry *max_clk_limit,
2770 		struct clk_limit_table_entry *curr_clk_limit)
2771 {
2772 	if (NULL == max_clk_limit || NULL == curr_clk_limit)
2773 		return -1; //invalid parameters
2774 
2775 	//only overwrite if desired max clock frequency is initialized
2776 	if (max_clk_limit->dcfclk_mhz != 0)
2777 		curr_clk_limit->dcfclk_mhz = max_clk_limit->dcfclk_mhz;
2778 
2779 	if (max_clk_limit->fclk_mhz != 0)
2780 		curr_clk_limit->fclk_mhz = max_clk_limit->fclk_mhz;
2781 
2782 	if (max_clk_limit->memclk_mhz != 0)
2783 		curr_clk_limit->memclk_mhz = max_clk_limit->memclk_mhz;
2784 
2785 	if (max_clk_limit->socclk_mhz != 0)
2786 		curr_clk_limit->socclk_mhz = max_clk_limit->socclk_mhz;
2787 
2788 	if (max_clk_limit->dtbclk_mhz != 0)
2789 		curr_clk_limit->dtbclk_mhz = max_clk_limit->dtbclk_mhz;
2790 
2791 	if (max_clk_limit->dispclk_mhz != 0)
2792 		curr_clk_limit->dispclk_mhz = max_clk_limit->dispclk_mhz;
2793 
2794 	return 0;
2795 }
2796 
build_synthetic_soc_states(bool disable_dc_mode_overwrite,struct clk_bw_params * bw_params,struct _vcs_dpi_voltage_scaling_st * table,unsigned int * num_entries)2797 static int build_synthetic_soc_states(bool disable_dc_mode_overwrite, struct clk_bw_params *bw_params,
2798 		struct _vcs_dpi_voltage_scaling_st *table, unsigned int *num_entries)
2799 {
2800 	int i, j;
2801 	struct _vcs_dpi_voltage_scaling_st entry = {0};
2802 	struct clk_limit_table_entry max_clk_data = {0};
2803 
2804 	unsigned int min_dcfclk_mhz = 199, min_fclk_mhz = 299;
2805 
2806 	static const unsigned int num_dcfclk_stas = 5;
2807 	unsigned int dcfclk_sta_targets[DC__VOLTAGE_STATES] = {199, 615, 906, 1324, 1564};
2808 
2809 	unsigned int num_uclk_dpms = 0;
2810 	unsigned int num_fclk_dpms = 0;
2811 	unsigned int num_dcfclk_dpms = 0;
2812 
2813 	unsigned int num_dc_uclk_dpms = 0;
2814 	unsigned int num_dc_fclk_dpms = 0;
2815 	unsigned int num_dc_dcfclk_dpms = 0;
2816 
2817 	for (i = 0; i < MAX_NUM_DPM_LVL; i++) {
2818 		if (bw_params->clk_table.entries[i].dcfclk_mhz > max_clk_data.dcfclk_mhz)
2819 			max_clk_data.dcfclk_mhz = bw_params->clk_table.entries[i].dcfclk_mhz;
2820 		if (bw_params->clk_table.entries[i].fclk_mhz > max_clk_data.fclk_mhz)
2821 			max_clk_data.fclk_mhz = bw_params->clk_table.entries[i].fclk_mhz;
2822 		if (bw_params->clk_table.entries[i].memclk_mhz > max_clk_data.memclk_mhz)
2823 			max_clk_data.memclk_mhz = bw_params->clk_table.entries[i].memclk_mhz;
2824 		if (bw_params->clk_table.entries[i].dispclk_mhz > max_clk_data.dispclk_mhz)
2825 			max_clk_data.dispclk_mhz = bw_params->clk_table.entries[i].dispclk_mhz;
2826 		if (bw_params->clk_table.entries[i].dppclk_mhz > max_clk_data.dppclk_mhz)
2827 			max_clk_data.dppclk_mhz = bw_params->clk_table.entries[i].dppclk_mhz;
2828 		if (bw_params->clk_table.entries[i].phyclk_mhz > max_clk_data.phyclk_mhz)
2829 			max_clk_data.phyclk_mhz = bw_params->clk_table.entries[i].phyclk_mhz;
2830 		if (bw_params->clk_table.entries[i].dtbclk_mhz > max_clk_data.dtbclk_mhz)
2831 			max_clk_data.dtbclk_mhz = bw_params->clk_table.entries[i].dtbclk_mhz;
2832 
2833 		if (bw_params->clk_table.entries[i].memclk_mhz > 0) {
2834 			num_uclk_dpms++;
2835 			if (bw_params->clk_table.entries[i].memclk_mhz <= bw_params->dc_mode_limit.memclk_mhz)
2836 				num_dc_uclk_dpms++;
2837 		}
2838 		if (bw_params->clk_table.entries[i].fclk_mhz > 0) {
2839 			num_fclk_dpms++;
2840 			if (bw_params->clk_table.entries[i].fclk_mhz <= bw_params->dc_mode_limit.fclk_mhz)
2841 				num_dc_fclk_dpms++;
2842 		}
2843 		if (bw_params->clk_table.entries[i].dcfclk_mhz > 0) {
2844 			num_dcfclk_dpms++;
2845 			if (bw_params->clk_table.entries[i].dcfclk_mhz <= bw_params->dc_mode_limit.dcfclk_mhz)
2846 				num_dc_dcfclk_dpms++;
2847 		}
2848 	}
2849 
2850 	if (!disable_dc_mode_overwrite) {
2851 		//Overwrite max frequencies with max DC mode frequencies for DC mode systems
2852 		override_max_clk_values(&bw_params->dc_mode_limit, &max_clk_data);
2853 		num_uclk_dpms = num_dc_uclk_dpms;
2854 		num_fclk_dpms = num_dc_fclk_dpms;
2855 		num_dcfclk_dpms = num_dc_dcfclk_dpms;
2856 		bw_params->clk_table.num_entries_per_clk.num_memclk_levels = num_uclk_dpms;
2857 		bw_params->clk_table.num_entries_per_clk.num_fclk_levels = num_fclk_dpms;
2858 	}
2859 
2860 	if (num_dcfclk_dpms > 0 && bw_params->clk_table.entries[0].fclk_mhz > min_fclk_mhz)
2861 		min_fclk_mhz = bw_params->clk_table.entries[0].fclk_mhz;
2862 
2863 	if (!max_clk_data.dcfclk_mhz || !max_clk_data.dispclk_mhz || !max_clk_data.dtbclk_mhz)
2864 		return -1;
2865 
2866 	if (max_clk_data.dppclk_mhz == 0)
2867 		max_clk_data.dppclk_mhz = max_clk_data.dispclk_mhz;
2868 
2869 	if (max_clk_data.fclk_mhz == 0)
2870 		max_clk_data.fclk_mhz = max_clk_data.dcfclk_mhz *
2871 				dcn3_2_soc.pct_ideal_sdp_bw_after_urgent /
2872 				dcn3_2_soc.pct_ideal_fabric_bw_after_urgent;
2873 
2874 	if (max_clk_data.phyclk_mhz == 0)
2875 		max_clk_data.phyclk_mhz = dcn3_2_soc.clock_limits[0].phyclk_mhz;
2876 
2877 	*num_entries = 0;
2878 	entry.dispclk_mhz = max_clk_data.dispclk_mhz;
2879 	entry.dscclk_mhz = max_clk_data.dispclk_mhz / 3;
2880 	entry.dppclk_mhz = max_clk_data.dppclk_mhz;
2881 	entry.dtbclk_mhz = max_clk_data.dtbclk_mhz;
2882 	entry.phyclk_mhz = max_clk_data.phyclk_mhz;
2883 	entry.phyclk_d18_mhz = dcn3_2_soc.clock_limits[0].phyclk_d18_mhz;
2884 	entry.phyclk_d32_mhz = dcn3_2_soc.clock_limits[0].phyclk_d32_mhz;
2885 
2886 	// Insert all the DCFCLK STAs
2887 	for (i = 0; i < num_dcfclk_stas; i++) {
2888 		entry.dcfclk_mhz = dcfclk_sta_targets[i];
2889 		entry.fabricclk_mhz = 0;
2890 		entry.dram_speed_mts = 0;
2891 
2892 		get_optimal_ntuple(&entry);
2893 		entry.net_bw_in_kbytes_sec = calculate_net_bw_in_kbytes_sec(&entry);
2894 		insert_entry_into_table_sorted(table, num_entries, &entry);
2895 	}
2896 
2897 	// Insert the max DCFCLK
2898 	entry.dcfclk_mhz = max_clk_data.dcfclk_mhz;
2899 	entry.fabricclk_mhz = 0;
2900 	entry.dram_speed_mts = 0;
2901 
2902 	get_optimal_ntuple(&entry);
2903 	entry.net_bw_in_kbytes_sec = calculate_net_bw_in_kbytes_sec(&entry);
2904 	insert_entry_into_table_sorted(table, num_entries, &entry);
2905 
2906 	// Insert the UCLK DPMS
2907 	for (i = 0; i < num_uclk_dpms; i++) {
2908 		entry.dcfclk_mhz = 0;
2909 		entry.fabricclk_mhz = 0;
2910 		entry.dram_speed_mts = bw_params->clk_table.entries[i].memclk_mhz * 16;
2911 
2912 		get_optimal_ntuple(&entry);
2913 		entry.net_bw_in_kbytes_sec = calculate_net_bw_in_kbytes_sec(&entry);
2914 		insert_entry_into_table_sorted(table, num_entries, &entry);
2915 	}
2916 
2917 	// If FCLK is coarse grained, insert individual DPMs.
2918 	if (num_fclk_dpms > 2) {
2919 		for (i = 0; i < num_fclk_dpms; i++) {
2920 			entry.dcfclk_mhz = 0;
2921 			entry.fabricclk_mhz = bw_params->clk_table.entries[i].fclk_mhz;
2922 			entry.dram_speed_mts = 0;
2923 
2924 			get_optimal_ntuple(&entry);
2925 			entry.net_bw_in_kbytes_sec = calculate_net_bw_in_kbytes_sec(&entry);
2926 			insert_entry_into_table_sorted(table, num_entries, &entry);
2927 		}
2928 	}
2929 	// If FCLK fine grained, only insert max
2930 	else {
2931 		entry.dcfclk_mhz = 0;
2932 		entry.fabricclk_mhz = max_clk_data.fclk_mhz;
2933 		entry.dram_speed_mts = 0;
2934 
2935 		get_optimal_ntuple(&entry);
2936 		entry.net_bw_in_kbytes_sec = calculate_net_bw_in_kbytes_sec(&entry);
2937 		insert_entry_into_table_sorted(table, num_entries, &entry);
2938 	}
2939 
2940 	// At this point, the table contains all "points of interest" based on
2941 	// DPMs from PMFW, and STAs.  Table is sorted by BW, and all clock
2942 	// ratios (by derate, are exact).
2943 
2944 	// Remove states that require higher clocks than are supported
2945 	for (i = *num_entries - 1; i >= 0 ; i--) {
2946 		if (table[i].dcfclk_mhz > max_clk_data.dcfclk_mhz ||
2947 				table[i].fabricclk_mhz > max_clk_data.fclk_mhz ||
2948 				table[i].dram_speed_mts > max_clk_data.memclk_mhz * 16)
2949 			remove_entry_from_table_at_index(table, num_entries, i);
2950 	}
2951 
2952 	// Insert entry with all max dc limits without bandwidth matching
2953 	if (!disable_dc_mode_overwrite) {
2954 		struct _vcs_dpi_voltage_scaling_st max_dc_limits_entry = entry;
2955 
2956 		max_dc_limits_entry.dcfclk_mhz = max_clk_data.dcfclk_mhz;
2957 		max_dc_limits_entry.fabricclk_mhz = max_clk_data.fclk_mhz;
2958 		max_dc_limits_entry.dram_speed_mts = max_clk_data.memclk_mhz * 16;
2959 
2960 		max_dc_limits_entry.net_bw_in_kbytes_sec = calculate_net_bw_in_kbytes_sec(&max_dc_limits_entry);
2961 		insert_entry_into_table_sorted(table, num_entries, &max_dc_limits_entry);
2962 
2963 		sort_entries_with_same_bw(table, num_entries);
2964 		remove_inconsistent_entries(table, num_entries);
2965 	}
2966 
2967 	// At this point, the table only contains supported points of interest
2968 	// it could be used as is, but some states may be redundant due to
2969 	// coarse grained nature of some clocks, so we want to round up to
2970 	// coarse grained DPMs and remove duplicates.
2971 
2972 	// Round up UCLKs
2973 	for (i = *num_entries - 1; i >= 0 ; i--) {
2974 		for (j = 0; j < num_uclk_dpms; j++) {
2975 			if (bw_params->clk_table.entries[j].memclk_mhz * 16 >= table[i].dram_speed_mts) {
2976 				table[i].dram_speed_mts = bw_params->clk_table.entries[j].memclk_mhz * 16;
2977 				break;
2978 			}
2979 		}
2980 	}
2981 
2982 	// If FCLK is coarse grained, round up to next DPMs
2983 	if (num_fclk_dpms > 2) {
2984 		for (i = *num_entries - 1; i >= 0 ; i--) {
2985 			for (j = 0; j < num_fclk_dpms; j++) {
2986 				if (bw_params->clk_table.entries[j].fclk_mhz >= table[i].fabricclk_mhz) {
2987 					table[i].fabricclk_mhz = bw_params->clk_table.entries[j].fclk_mhz;
2988 					break;
2989 				}
2990 			}
2991 		}
2992 	}
2993 	// Otherwise, round up to minimum.
2994 	else {
2995 		for (i = *num_entries - 1; i >= 0 ; i--) {
2996 			if (table[i].fabricclk_mhz < min_fclk_mhz) {
2997 				table[i].fabricclk_mhz = min_fclk_mhz;
2998 			}
2999 		}
3000 	}
3001 
3002 	// Round DCFCLKs up to minimum
3003 	for (i = *num_entries - 1; i >= 0 ; i--) {
3004 		if (table[i].dcfclk_mhz < min_dcfclk_mhz) {
3005 			table[i].dcfclk_mhz = min_dcfclk_mhz;
3006 		}
3007 	}
3008 
3009 	// Remove duplicate states, note duplicate states are always neighbouring since table is sorted.
3010 	i = 0;
3011 	while (i < *num_entries - 1) {
3012 		if (table[i].dcfclk_mhz == table[i + 1].dcfclk_mhz &&
3013 				table[i].fabricclk_mhz == table[i + 1].fabricclk_mhz &&
3014 				table[i].dram_speed_mts == table[i + 1].dram_speed_mts)
3015 			remove_entry_from_table_at_index(table, num_entries, i + 1);
3016 		else
3017 			i++;
3018 	}
3019 
3020 	// Fix up the state indicies
3021 	for (i = *num_entries - 1; i >= 0 ; i--) {
3022 		table[i].state = i;
3023 	}
3024 
3025 	return 0;
3026 }
3027 
3028 /*
3029  * dcn32_update_bw_bounding_box
3030  *
3031  * This would override some dcn3_2 ip_or_soc initial parameters hardcoded from
3032  * spreadsheet with actual values as per dGPU SKU:
3033  * - with passed few options from dc->config
3034  * - with dentist_vco_frequency from Clk Mgr (currently hardcoded, but might
3035  *   need to get it from PM FW)
3036  * - with passed latency values (passed in ns units) in dc-> bb override for
3037  *   debugging purposes
3038  * - with passed latencies from VBIOS (in 100_ns units) if available for
3039  *   certain dGPU SKU
3040  * - with number of DRAM channels from VBIOS (which differ for certain dGPU SKU
3041  *   of the same ASIC)
3042  * - clocks levels with passed clk_table entries from Clk Mgr as reported by PM
3043  *   FW for different clocks (which might differ for certain dGPU SKU of the
3044  *   same ASIC)
3045  */
dcn32_update_bw_bounding_box_fpu(struct dc * dc,struct clk_bw_params * bw_params)3046 void dcn32_update_bw_bounding_box_fpu(struct dc *dc, struct clk_bw_params *bw_params)
3047 {
3048 	dc_assert_fp_enabled();
3049 
3050 	/* Overrides from dc->config options */
3051 	dcn3_2_ip.clamp_min_dcfclk = dc->config.clamp_min_dcfclk;
3052 
3053 	/* Override from passed dc->bb_overrides if available*/
3054 	if ((int)(dcn3_2_soc.sr_exit_time_us * 1000) != dc->bb_overrides.sr_exit_time_ns
3055 			&& dc->bb_overrides.sr_exit_time_ns) {
3056 		dc->dml2_options.bbox_overrides.sr_exit_latency_us =
3057 		dcn3_2_soc.sr_exit_time_us = dc->bb_overrides.sr_exit_time_ns / 1000.0;
3058 	}
3059 
3060 	if ((int)(dcn3_2_soc.sr_enter_plus_exit_time_us * 1000)
3061 			!= dc->bb_overrides.sr_enter_plus_exit_time_ns
3062 			&& dc->bb_overrides.sr_enter_plus_exit_time_ns) {
3063 		dc->dml2_options.bbox_overrides.sr_enter_plus_exit_latency_us =
3064 		dcn3_2_soc.sr_enter_plus_exit_time_us =
3065 			dc->bb_overrides.sr_enter_plus_exit_time_ns / 1000.0;
3066 	}
3067 
3068 	if ((int)(dcn3_2_soc.urgent_latency_us * 1000) != dc->bb_overrides.urgent_latency_ns
3069 		&& dc->bb_overrides.urgent_latency_ns) {
3070 		dcn3_2_soc.urgent_latency_us = dc->bb_overrides.urgent_latency_ns / 1000.0;
3071 		dc->dml2_options.bbox_overrides.urgent_latency_us =
3072 		dcn3_2_soc.urgent_latency_pixel_data_only_us = dc->bb_overrides.urgent_latency_ns / 1000.0;
3073 	}
3074 
3075 	if ((int)(dcn3_2_soc.dram_clock_change_latency_us * 1000)
3076 			!= dc->bb_overrides.dram_clock_change_latency_ns
3077 			&& dc->bb_overrides.dram_clock_change_latency_ns) {
3078 		dc->dml2_options.bbox_overrides.dram_clock_change_latency_us =
3079 		dcn3_2_soc.dram_clock_change_latency_us =
3080 			dc->bb_overrides.dram_clock_change_latency_ns / 1000.0;
3081 	}
3082 
3083 	if ((int)(dcn3_2_soc.fclk_change_latency_us * 1000)
3084 			!= dc->bb_overrides.fclk_clock_change_latency_ns
3085 			&& dc->bb_overrides.fclk_clock_change_latency_ns) {
3086 		dc->dml2_options.bbox_overrides.fclk_change_latency_us =
3087 		dcn3_2_soc.fclk_change_latency_us =
3088 			dc->bb_overrides.fclk_clock_change_latency_ns / 1000;
3089 	}
3090 
3091 	if ((int)(dcn3_2_soc.dummy_pstate_latency_us * 1000)
3092 			!= dc->bb_overrides.dummy_clock_change_latency_ns
3093 			&& dc->bb_overrides.dummy_clock_change_latency_ns) {
3094 		dcn3_2_soc.dummy_pstate_latency_us =
3095 			dc->bb_overrides.dummy_clock_change_latency_ns / 1000.0;
3096 	}
3097 
3098 	/* Override from VBIOS if VBIOS bb_info available */
3099 	if (dc->ctx->dc_bios->funcs->get_soc_bb_info) {
3100 		struct bp_soc_bb_info bb_info = {0};
3101 
3102 		if (dc->ctx->dc_bios->funcs->get_soc_bb_info(dc->ctx->dc_bios, &bb_info) == BP_RESULT_OK) {
3103 			if (bb_info.dram_clock_change_latency_100ns > 0)
3104 				dc->dml2_options.bbox_overrides.dram_clock_change_latency_us =
3105 				dcn3_2_soc.dram_clock_change_latency_us =
3106 					bb_info.dram_clock_change_latency_100ns * 10;
3107 
3108 			if (bb_info.dram_sr_enter_exit_latency_100ns > 0)
3109 				dc->dml2_options.bbox_overrides.sr_enter_plus_exit_latency_us =
3110 				dcn3_2_soc.sr_enter_plus_exit_time_us =
3111 					bb_info.dram_sr_enter_exit_latency_100ns * 10;
3112 
3113 			if (bb_info.dram_sr_exit_latency_100ns > 0)
3114 				dc->dml2_options.bbox_overrides.sr_exit_latency_us =
3115 				dcn3_2_soc.sr_exit_time_us =
3116 					bb_info.dram_sr_exit_latency_100ns * 10;
3117 		}
3118 	}
3119 
3120 	/* Override from VBIOS for num_chan */
3121 	if (dc->ctx->dc_bios->vram_info.num_chans) {
3122 		dc->dml2_options.bbox_overrides.dram_num_chan =
3123 		dcn3_2_soc.num_chans = dc->ctx->dc_bios->vram_info.num_chans;
3124 		dcn3_2_soc.mall_allocated_for_dcn_mbytes = (double)(dcn32_calc_num_avail_chans_for_mall(dc,
3125 			dc->ctx->dc_bios->vram_info.num_chans) * dc->caps.mall_size_per_mem_channel);
3126 	}
3127 
3128 	if (dc->ctx->dc_bios->vram_info.dram_channel_width_bytes)
3129 		dc->dml2_options.bbox_overrides.dram_chanel_width_bytes =
3130 		dcn3_2_soc.dram_channel_width_bytes = dc->ctx->dc_bios->vram_info.dram_channel_width_bytes;
3131 
3132 	/* DML DSC delay factor workaround */
3133 	dcn3_2_ip.dsc_delay_factor_wa = dc->debug.dsc_delay_factor_wa_x1000 / 1000.0;
3134 
3135 	dcn3_2_ip.min_prefetch_in_strobe_us = dc->debug.min_prefetch_in_strobe_ns / 1000.0;
3136 
3137 	/* Override dispclk_dppclk_vco_speed_mhz from Clk Mgr */
3138 	dcn3_2_soc.dispclk_dppclk_vco_speed_mhz = dc->clk_mgr->dentist_vco_freq_khz / 1000.0;
3139 	dc->dml.soc.dispclk_dppclk_vco_speed_mhz = dc->clk_mgr->dentist_vco_freq_khz / 1000.0;
3140 	dc->dml2_options.bbox_overrides.disp_pll_vco_speed_mhz = dc->clk_mgr->dentist_vco_freq_khz / 1000.0;
3141 	dc->dml2_options.bbox_overrides.xtalclk_mhz = dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency / 1000.0;
3142 	dc->dml2_options.bbox_overrides.dchub_refclk_mhz = dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000.0;
3143 	dc->dml2_options.bbox_overrides.dprefclk_mhz = dc->clk_mgr->dprefclk_khz / 1000.0;
3144 
3145 	/* Overrides Clock levelsfrom CLK Mgr table entries as reported by PM FW */
3146 	if (bw_params->clk_table.entries[0].memclk_mhz) {
3147 		if (dc->debug.use_legacy_soc_bb_mechanism) {
3148 			unsigned int i = 0, j = 0, num_states = 0;
3149 
3150 			unsigned int dcfclk_mhz[DC__VOLTAGE_STATES] = {0};
3151 			unsigned int dram_speed_mts[DC__VOLTAGE_STATES] = {0};
3152 			unsigned int optimal_uclk_for_dcfclk_sta_targets[DC__VOLTAGE_STATES] = {0};
3153 			unsigned int optimal_dcfclk_for_uclk[DC__VOLTAGE_STATES] = {0};
3154 			unsigned int min_dcfclk = UINT_MAX;
3155 			/* Set 199 as first value in STA target array to have a minimum DCFCLK value.
3156 			 * For DCN32 we set min to 199 so minimum FCLK DPM0 (300Mhz can be achieved) */
3157 			unsigned int dcfclk_sta_targets[DC__VOLTAGE_STATES] = {199, 615, 906, 1324, 1564};
3158 			unsigned int num_dcfclk_sta_targets = 4, num_uclk_states = 0;
3159 			unsigned int max_dcfclk_mhz = 0, max_dispclk_mhz = 0, max_dppclk_mhz = 0, max_phyclk_mhz = 0;
3160 
3161 			for (i = 0; i < MAX_NUM_DPM_LVL; i++) {
3162 				if (bw_params->clk_table.entries[i].dcfclk_mhz > max_dcfclk_mhz)
3163 					max_dcfclk_mhz = bw_params->clk_table.entries[i].dcfclk_mhz;
3164 				if (bw_params->clk_table.entries[i].dcfclk_mhz != 0 &&
3165 						bw_params->clk_table.entries[i].dcfclk_mhz < min_dcfclk)
3166 					min_dcfclk = bw_params->clk_table.entries[i].dcfclk_mhz;
3167 				if (bw_params->clk_table.entries[i].dispclk_mhz > max_dispclk_mhz)
3168 					max_dispclk_mhz = bw_params->clk_table.entries[i].dispclk_mhz;
3169 				if (bw_params->clk_table.entries[i].dppclk_mhz > max_dppclk_mhz)
3170 					max_dppclk_mhz = bw_params->clk_table.entries[i].dppclk_mhz;
3171 				if (bw_params->clk_table.entries[i].phyclk_mhz > max_phyclk_mhz)
3172 					max_phyclk_mhz = bw_params->clk_table.entries[i].phyclk_mhz;
3173 			}
3174 			if (min_dcfclk > dcfclk_sta_targets[0])
3175 				dcfclk_sta_targets[0] = min_dcfclk;
3176 			if (!max_dcfclk_mhz)
3177 				max_dcfclk_mhz = dcn3_2_soc.clock_limits[0].dcfclk_mhz;
3178 			if (!max_dispclk_mhz)
3179 				max_dispclk_mhz = dcn3_2_soc.clock_limits[0].dispclk_mhz;
3180 			if (!max_dppclk_mhz)
3181 				max_dppclk_mhz = dcn3_2_soc.clock_limits[0].dppclk_mhz;
3182 			if (!max_phyclk_mhz)
3183 				max_phyclk_mhz = dcn3_2_soc.clock_limits[0].phyclk_mhz;
3184 
3185 			if (max_dcfclk_mhz > dcfclk_sta_targets[num_dcfclk_sta_targets-1]) {
3186 				// If max DCFCLK is greater than the max DCFCLK STA target, insert into the DCFCLK STA target array
3187 				dcfclk_sta_targets[num_dcfclk_sta_targets] = max_dcfclk_mhz;
3188 				num_dcfclk_sta_targets++;
3189 			} else if (max_dcfclk_mhz < dcfclk_sta_targets[num_dcfclk_sta_targets-1]) {
3190 				// If max DCFCLK is less than the max DCFCLK STA target, cap values and remove duplicates
3191 				for (i = 0; i < num_dcfclk_sta_targets; i++) {
3192 					if (dcfclk_sta_targets[i] > max_dcfclk_mhz) {
3193 						dcfclk_sta_targets[i] = max_dcfclk_mhz;
3194 						break;
3195 					}
3196 				}
3197 				// Update size of array since we "removed" duplicates
3198 				num_dcfclk_sta_targets = i + 1;
3199 			}
3200 
3201 			num_uclk_states = bw_params->clk_table.num_entries;
3202 
3203 			// Calculate optimal dcfclk for each uclk
3204 			for (i = 0; i < num_uclk_states; i++) {
3205 				dcn32_get_optimal_dcfclk_fclk_for_uclk(bw_params->clk_table.entries[i].memclk_mhz * 16,
3206 						&optimal_dcfclk_for_uclk[i], NULL);
3207 				if (optimal_dcfclk_for_uclk[i] < bw_params->clk_table.entries[0].dcfclk_mhz) {
3208 					optimal_dcfclk_for_uclk[i] = bw_params->clk_table.entries[0].dcfclk_mhz;
3209 				}
3210 			}
3211 
3212 			// Calculate optimal uclk for each dcfclk sta target
3213 			for (i = 0; i < num_dcfclk_sta_targets; i++) {
3214 				for (j = 0; j < num_uclk_states; j++) {
3215 					if (dcfclk_sta_targets[i] < optimal_dcfclk_for_uclk[j]) {
3216 						optimal_uclk_for_dcfclk_sta_targets[i] =
3217 								bw_params->clk_table.entries[j].memclk_mhz * 16;
3218 						break;
3219 					}
3220 				}
3221 			}
3222 
3223 			i = 0;
3224 			j = 0;
3225 			// create the final dcfclk and uclk table
3226 			while (i < num_dcfclk_sta_targets && j < num_uclk_states && num_states < DC__VOLTAGE_STATES) {
3227 				if (dcfclk_sta_targets[i] < optimal_dcfclk_for_uclk[j] && i < num_dcfclk_sta_targets) {
3228 					dcfclk_mhz[num_states] = dcfclk_sta_targets[i];
3229 					dram_speed_mts[num_states++] = optimal_uclk_for_dcfclk_sta_targets[i++];
3230 				} else {
3231 					if (j < num_uclk_states && optimal_dcfclk_for_uclk[j] <= max_dcfclk_mhz) {
3232 						dcfclk_mhz[num_states] = optimal_dcfclk_for_uclk[j];
3233 						dram_speed_mts[num_states++] = bw_params->clk_table.entries[j++].memclk_mhz * 16;
3234 					} else {
3235 						j = num_uclk_states;
3236 					}
3237 				}
3238 			}
3239 
3240 			while (i < num_dcfclk_sta_targets && num_states < DC__VOLTAGE_STATES) {
3241 				dcfclk_mhz[num_states] = dcfclk_sta_targets[i];
3242 				dram_speed_mts[num_states++] = optimal_uclk_for_dcfclk_sta_targets[i++];
3243 			}
3244 
3245 			while (j < num_uclk_states && num_states < DC__VOLTAGE_STATES &&
3246 					optimal_dcfclk_for_uclk[j] <= max_dcfclk_mhz) {
3247 				dcfclk_mhz[num_states] = optimal_dcfclk_for_uclk[j];
3248 				dram_speed_mts[num_states++] = bw_params->clk_table.entries[j++].memclk_mhz * 16;
3249 			}
3250 
3251 			/* bw_params->clk_table.entries[MAX_NUM_DPM_LVL].
3252 			 * MAX_NUM_DPM_LVL is 8.
3253 			 * dcn3_02_soc.clock_limits[DC__VOLTAGE_STATES].
3254 			 * DC__VOLTAGE_STATES is 40.
3255 			 */
3256 			if (num_states > MAX_NUM_DPM_LVL) {
3257 				ASSERT(0);
3258 				return;
3259 			}
3260 
3261 			dcn3_2_soc.num_states = num_states;
3262 			for (i = 0; i < dcn3_2_soc.num_states; i++) {
3263 				dcn3_2_soc.clock_limits[i].state = i;
3264 				dcn3_2_soc.clock_limits[i].dcfclk_mhz = dcfclk_mhz[i];
3265 				dcn3_2_soc.clock_limits[i].fabricclk_mhz = dcfclk_mhz[i];
3266 
3267 				/* Fill all states with max values of all these clocks */
3268 				dcn3_2_soc.clock_limits[i].dispclk_mhz = max_dispclk_mhz;
3269 				dcn3_2_soc.clock_limits[i].dppclk_mhz  = max_dppclk_mhz;
3270 				dcn3_2_soc.clock_limits[i].phyclk_mhz  = max_phyclk_mhz;
3271 				dcn3_2_soc.clock_limits[i].dscclk_mhz  = max_dispclk_mhz / 3;
3272 
3273 				/* Populate from bw_params for DTBCLK, SOCCLK */
3274 				if (i > 0) {
3275 					if (!bw_params->clk_table.entries[i].dtbclk_mhz) {
3276 						dcn3_2_soc.clock_limits[i].dtbclk_mhz  = dcn3_2_soc.clock_limits[i-1].dtbclk_mhz;
3277 					} else {
3278 						dcn3_2_soc.clock_limits[i].dtbclk_mhz  = bw_params->clk_table.entries[i].dtbclk_mhz;
3279 					}
3280 				} else if (bw_params->clk_table.entries[i].dtbclk_mhz) {
3281 					dcn3_2_soc.clock_limits[i].dtbclk_mhz  = bw_params->clk_table.entries[i].dtbclk_mhz;
3282 				}
3283 
3284 				if (!bw_params->clk_table.entries[i].socclk_mhz && i > 0)
3285 					dcn3_2_soc.clock_limits[i].socclk_mhz = dcn3_2_soc.clock_limits[i-1].socclk_mhz;
3286 				else
3287 					dcn3_2_soc.clock_limits[i].socclk_mhz = bw_params->clk_table.entries[i].socclk_mhz;
3288 
3289 				if (!dram_speed_mts[i] && i > 0)
3290 					dcn3_2_soc.clock_limits[i].dram_speed_mts = dcn3_2_soc.clock_limits[i-1].dram_speed_mts;
3291 				else
3292 					dcn3_2_soc.clock_limits[i].dram_speed_mts = dram_speed_mts[i];
3293 
3294 				/* These clocks cannot come from bw_params, always fill from dcn3_2_soc[0] */
3295 				/* PHYCLK_D18, PHYCLK_D32 */
3296 				dcn3_2_soc.clock_limits[i].phyclk_d18_mhz = dcn3_2_soc.clock_limits[0].phyclk_d18_mhz;
3297 				dcn3_2_soc.clock_limits[i].phyclk_d32_mhz = dcn3_2_soc.clock_limits[0].phyclk_d32_mhz;
3298 			}
3299 		} else {
3300 			build_synthetic_soc_states(dc->debug.disable_dc_mode_overwrite, bw_params,
3301 					dcn3_2_soc.clock_limits, &dcn3_2_soc.num_states);
3302 		}
3303 
3304 		/* Re-init DML with updated bb */
3305 		dml_init_instance(&dc->dml, &dcn3_2_soc, &dcn3_2_ip, DML_PROJECT_DCN32);
3306 		if (dc->current_state)
3307 			dml_init_instance(&dc->current_state->bw_ctx.dml, &dcn3_2_soc, &dcn3_2_ip, DML_PROJECT_DCN32);
3308 	}
3309 
3310 	if (dc->clk_mgr->bw_params->clk_table.num_entries > 1) {
3311 		unsigned int i = 0;
3312 
3313 		dc->dml2_options.bbox_overrides.clks_table.num_states = dc->clk_mgr->bw_params->clk_table.num_entries;
3314 
3315 		dc->dml2_options.bbox_overrides.clks_table.num_entries_per_clk.num_dcfclk_levels =
3316 			dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_dcfclk_levels;
3317 
3318 		dc->dml2_options.bbox_overrides.clks_table.num_entries_per_clk.num_fclk_levels =
3319 			dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_fclk_levels;
3320 
3321 		dc->dml2_options.bbox_overrides.clks_table.num_entries_per_clk.num_memclk_levels =
3322 			dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_memclk_levels;
3323 
3324 		dc->dml2_options.bbox_overrides.clks_table.num_entries_per_clk.num_socclk_levels =
3325 			dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_socclk_levels;
3326 
3327 		dc->dml2_options.bbox_overrides.clks_table.num_entries_per_clk.num_dtbclk_levels =
3328 			dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_dtbclk_levels;
3329 
3330 		dc->dml2_options.bbox_overrides.clks_table.num_entries_per_clk.num_dispclk_levels =
3331 			dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_dispclk_levels;
3332 
3333 		dc->dml2_options.bbox_overrides.clks_table.num_entries_per_clk.num_dppclk_levels =
3334 			dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_dppclk_levels;
3335 
3336 		for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_dcfclk_levels; i++) {
3337 			if (dc->clk_mgr->bw_params->clk_table.entries[i].dcfclk_mhz)
3338 				dc->dml2_options.bbox_overrides.clks_table.clk_entries[i].dcfclk_mhz =
3339 					dc->clk_mgr->bw_params->clk_table.entries[i].dcfclk_mhz;
3340 		}
3341 
3342 		for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_fclk_levels; i++) {
3343 			if (dc->clk_mgr->bw_params->clk_table.entries[i].fclk_mhz)
3344 				dc->dml2_options.bbox_overrides.clks_table.clk_entries[i].fclk_mhz =
3345 					dc->clk_mgr->bw_params->clk_table.entries[i].fclk_mhz;
3346 		}
3347 
3348 		for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_memclk_levels; i++) {
3349 			if (dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz)
3350 				dc->dml2_options.bbox_overrides.clks_table.clk_entries[i].memclk_mhz =
3351 					dc->clk_mgr->bw_params->clk_table.entries[i].memclk_mhz;
3352 		}
3353 
3354 		for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_socclk_levels; i++) {
3355 			if (dc->clk_mgr->bw_params->clk_table.entries[i].socclk_mhz)
3356 				dc->dml2_options.bbox_overrides.clks_table.clk_entries[i].socclk_mhz =
3357 					dc->clk_mgr->bw_params->clk_table.entries[i].socclk_mhz;
3358 		}
3359 
3360 		for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_dtbclk_levels; i++) {
3361 			if (dc->clk_mgr->bw_params->clk_table.entries[i].dtbclk_mhz)
3362 				dc->dml2_options.bbox_overrides.clks_table.clk_entries[i].dtbclk_mhz =
3363 					dc->clk_mgr->bw_params->clk_table.entries[i].dtbclk_mhz;
3364 		}
3365 
3366 		for (i = 0; i < dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_dispclk_levels; i++) {
3367 			if (dc->clk_mgr->bw_params->clk_table.entries[i].dispclk_mhz) {
3368 				dc->dml2_options.bbox_overrides.clks_table.clk_entries[i].dispclk_mhz =
3369 					dc->clk_mgr->bw_params->clk_table.entries[i].dispclk_mhz;
3370 				dc->dml2_options.bbox_overrides.clks_table.clk_entries[i].dppclk_mhz =
3371 					dc->clk_mgr->bw_params->clk_table.entries[i].dispclk_mhz;
3372 			}
3373 		}
3374 	}
3375 }
3376 
dcn32_zero_pipe_dcc_fraction(display_e2e_pipe_params_st * pipes,int pipe_cnt)3377 void dcn32_zero_pipe_dcc_fraction(display_e2e_pipe_params_st *pipes,
3378 				  int pipe_cnt)
3379 {
3380 	dc_assert_fp_enabled();
3381 
3382 	pipes[pipe_cnt].pipe.src.dcc_fraction_of_zs_req_luma = 0;
3383 	pipes[pipe_cnt].pipe.src.dcc_fraction_of_zs_req_chroma = 0;
3384 }
3385 
dcn32_allow_subvp_with_active_margin(struct pipe_ctx * pipe)3386 bool dcn32_allow_subvp_with_active_margin(struct pipe_ctx *pipe)
3387 {
3388 	bool allow = false;
3389 	uint32_t refresh_rate = 0;
3390 	uint32_t min_refresh = subvp_active_margin_list.min_refresh;
3391 	uint32_t max_refresh = subvp_active_margin_list.max_refresh;
3392 	uint32_t i;
3393 
3394 	for (i = 0; i < SUBVP_ACTIVE_MARGIN_LIST_LEN; i++) {
3395 		uint32_t width = subvp_active_margin_list.res[i].width;
3396 		uint32_t height = subvp_active_margin_list.res[i].height;
3397 
3398 		refresh_rate = (pipe->stream->timing.pix_clk_100hz * (uint64_t)100 +
3399 			pipe->stream->timing.v_total * pipe->stream->timing.h_total - (uint64_t)1);
3400 		refresh_rate = div_u64(refresh_rate, pipe->stream->timing.v_total);
3401 		refresh_rate = div_u64(refresh_rate, pipe->stream->timing.h_total);
3402 
3403 		if (refresh_rate >= min_refresh && refresh_rate <= max_refresh &&
3404 				dcn32_check_native_scaling_for_res(pipe, width, height)) {
3405 			allow = true;
3406 			break;
3407 		}
3408 	}
3409 	return allow;
3410 }
3411 
3412 /**
3413  * dcn32_allow_subvp_high_refresh_rate: Determine if the high refresh rate config will allow subvp
3414  *
3415  * @dc: Current DC state
3416  * @context: New DC state to be programmed
3417  * @pipe: Pipe to be considered for use in subvp
3418  *
3419  * On high refresh rate display configs, we will allow subvp under the following conditions:
3420  * 1. Resolution is 3840x2160, 3440x1440, or 2560x1440
3421  * 2. Refresh rate is between 120hz - 165hz
3422  * 3. No scaling
3423  * 4. Freesync is inactive
3424  * 5. For single display cases, freesync must be disabled
3425  *
3426  * Return: True if pipe can be used for subvp, false otherwise
3427  */
dcn32_allow_subvp_high_refresh_rate(struct dc * dc,struct dc_state * context,struct pipe_ctx * pipe)3428 bool dcn32_allow_subvp_high_refresh_rate(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe)
3429 {
3430 	bool allow = false;
3431 	uint32_t refresh_rate = 0;
3432 	uint32_t subvp_min_refresh = subvp_high_refresh_list.min_refresh;
3433 	uint32_t subvp_max_refresh = subvp_high_refresh_list.max_refresh;
3434 	uint32_t min_refresh = subvp_max_refresh;
3435 	uint32_t i;
3436 
3437 	/* Only allow SubVP on high refresh displays if all connected displays
3438 	 * are considered "high refresh" (i.e. >= 120hz). We do not want to
3439 	 * allow combinations such as 120hz (SubVP) + 60hz (SubVP).
3440 	 */
3441 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3442 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
3443 
3444 		if (!pipe_ctx->stream)
3445 			continue;
3446 		refresh_rate = (pipe_ctx->stream->timing.pix_clk_100hz * 100 +
3447 				pipe_ctx->stream->timing.v_total * pipe_ctx->stream->timing.h_total - 1)
3448 						/ (double)(pipe_ctx->stream->timing.v_total * pipe_ctx->stream->timing.h_total);
3449 
3450 		if (refresh_rate < min_refresh)
3451 			min_refresh = refresh_rate;
3452 	}
3453 
3454 	if (!dc->debug.disable_subvp_high_refresh && min_refresh >= subvp_min_refresh && pipe->stream &&
3455 			pipe->plane_state && !(pipe->stream->vrr_active_variable || pipe->stream->vrr_active_fixed)) {
3456 		refresh_rate = (pipe->stream->timing.pix_clk_100hz * 100 +
3457 						pipe->stream->timing.v_total * pipe->stream->timing.h_total - 1)
3458 						/ (double)(pipe->stream->timing.v_total * pipe->stream->timing.h_total);
3459 		if (refresh_rate >= subvp_min_refresh && refresh_rate <= subvp_max_refresh) {
3460 			for (i = 0; i < SUBVP_HIGH_REFRESH_LIST_LEN; i++) {
3461 				uint32_t width = subvp_high_refresh_list.res[i].width;
3462 				uint32_t height = subvp_high_refresh_list.res[i].height;
3463 
3464 				if (dcn32_check_native_scaling_for_res(pipe, width, height)) {
3465 					if ((context->stream_count == 1 && !pipe->stream->allow_freesync) || context->stream_count > 1) {
3466 						allow = true;
3467 						break;
3468 					}
3469 				}
3470 			}
3471 		}
3472 	}
3473 	return allow;
3474 }
3475 
3476 /**
3477  * dcn32_determine_max_vratio_prefetch: Determine max Vratio for prefetch by driver policy
3478  *
3479  * @dc: Current DC state
3480  * @context: New DC state to be programmed
3481  *
3482  * Return: Max vratio for prefetch
3483  */
dcn32_determine_max_vratio_prefetch(struct dc * dc,struct dc_state * context)3484 double dcn32_determine_max_vratio_prefetch(struct dc *dc, struct dc_state *context)
3485 {
3486 	double max_vratio_pre = __DML_MAX_BW_RATIO_PRE__; // Default value is 4
3487 	int i;
3488 
3489 	/* For single display MPO configs, allow the max vratio to be 8
3490 	 * if any plane is YUV420 format
3491 	 */
3492 	if (context->stream_count == 1 && context->stream_status[0].plane_count > 1) {
3493 		for (i = 0; i < context->stream_status[0].plane_count; i++) {
3494 			if (context->stream_status[0].plane_states[i]->format == SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr ||
3495 					context->stream_status[0].plane_states[i]->format == SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb) {
3496 				max_vratio_pre = __DML_MAX_VRATIO_PRE__;
3497 			}
3498 		}
3499 	}
3500 	return max_vratio_pre;
3501 }
3502 
3503 /**
3504  * dcn32_assign_fpo_vactive_candidate - Assign the FPO stream candidate for FPO + VActive case
3505  *
3506  * This function chooses the FPO candidate stream for FPO + VActive cases (2 stream config).
3507  * For FPO + VAtive cases, the assumption is that one display has ActiveMargin > 0, and the
3508  * other display has ActiveMargin <= 0. This function will choose the pipe/stream that has
3509  * ActiveMargin <= 0 to be the FPO stream candidate if found.
3510  *
3511  *
3512  * @dc: current dc state
3513  * @context: new dc state
3514  * @fpo_candidate_stream: pointer to FPO stream candidate if one is found
3515  *
3516  * Return: void
3517  */
dcn32_assign_fpo_vactive_candidate(struct dc * dc,const struct dc_state * context,struct dc_stream_state ** fpo_candidate_stream)3518 void dcn32_assign_fpo_vactive_candidate(struct dc *dc, const struct dc_state *context, struct dc_stream_state **fpo_candidate_stream)
3519 {
3520 	unsigned int i, pipe_idx;
3521 	const struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
3522 
3523 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
3524 		const struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3525 
3526 		/* In DCN32/321, FPO uses per-pipe P-State force.
3527 		 * If there's no planes, HUBP is power gated and
3528 		 * therefore programming UCLK_PSTATE_FORCE does
3529 		 * nothing (P-State will always be asserted naturally
3530 		 * on a pipe that has HUBP power gated. Therefore we
3531 		 * only want to enable FPO if the FPO pipe has both
3532 		 * a stream and a plane.
3533 		 */
3534 		if (!pipe->stream || !pipe->plane_state)
3535 			continue;
3536 
3537 		if (vba->ActiveDRAMClockChangeLatencyMarginPerState[vba->VoltageLevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]] <= 0) {
3538 			*fpo_candidate_stream = pipe->stream;
3539 			break;
3540 		}
3541 		pipe_idx++;
3542 	}
3543 }
3544 
3545 /**
3546  * dcn32_find_vactive_pipe - Determines if the config has a pipe that can switch in VACTIVE
3547  *
3548  * @dc: current dc state
3549  * @context: new dc state
3550  * @fpo_candidate_stream: candidate stream to be chosen for FPO
3551  * @vactive_margin_req_us: The vactive marign required for a vactive pipe to be considered "found"
3552  *
3553  * Return: True if VACTIVE display is found, false otherwise
3554  */
dcn32_find_vactive_pipe(struct dc * dc,const struct dc_state * context,struct dc_stream_state * fpo_candidate_stream,uint32_t vactive_margin_req_us)3555 bool dcn32_find_vactive_pipe(struct dc *dc, const struct dc_state *context, struct dc_stream_state *fpo_candidate_stream, uint32_t vactive_margin_req_us)
3556 {
3557 	unsigned int i, pipe_idx;
3558 	const struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
3559 	bool vactive_found = true;
3560 	unsigned int blank_us = 0;
3561 
3562 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
3563 		const struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
3564 
3565 		if (!pipe->stream)
3566 			continue;
3567 
3568 		/* Don't need to check for vactive margin on the FPO candidate stream */
3569 		if (fpo_candidate_stream && pipe->stream == fpo_candidate_stream) {
3570 			pipe_idx++;
3571 			continue;
3572 		}
3573 
3574 		/* Every plane (apart from the ones driven by the FPO pipes) needs to have active margin
3575 		 * in order for us to have found a valid "vactive" config for FPO + Vactive
3576 		 */
3577 		blank_us = ((pipe->stream->timing.v_total - pipe->stream->timing.v_addressable) * pipe->stream->timing.h_total /
3578 				(double)(pipe->stream->timing.pix_clk_100hz * 100)) * 1000000;
3579 		if (vba->ActiveDRAMClockChangeLatencyMarginPerState[vba->VoltageLevel][vba->maxMpcComb][vba->pipe_plane[pipe_idx]] < vactive_margin_req_us ||
3580 				pipe->stream->vrr_active_variable || pipe->stream->vrr_active_fixed || blank_us >= dc->debug.fpo_vactive_max_blank_us) {
3581 			vactive_found = false;
3582 			break;
3583 		}
3584 		pipe_idx++;
3585 	}
3586 	return vactive_found;
3587 }
3588 
dcn32_set_clock_limits(const struct _vcs_dpi_soc_bounding_box_st * soc_bb)3589 void dcn32_set_clock_limits(const struct _vcs_dpi_soc_bounding_box_st *soc_bb)
3590 {
3591 	dc_assert_fp_enabled();
3592 	dcn3_2_soc.clock_limits[0].dcfclk_mhz = 1200.0;
3593 }
3594 
dcn32_override_min_req_memclk(struct dc * dc,struct dc_state * context)3595 void dcn32_override_min_req_memclk(struct dc *dc, struct dc_state *context)
3596 {
3597 	// WA: restrict FPO and SubVP to use first non-strobe mode (DCN32 BW issue)
3598 	if ((context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching || dcn32_subvp_in_use(dc, context)) &&
3599 			dc->dml.soc.num_chans <= 8) {
3600 		int num_mclk_levels = dc->clk_mgr->bw_params->clk_table.num_entries_per_clk.num_memclk_levels;
3601 
3602 		if (context->bw_ctx.dml.vba.DRAMSpeed <= dc->clk_mgr->bw_params->clk_table.entries[0].memclk_mhz * 16 &&
3603 				num_mclk_levels > 1) {
3604 			context->bw_ctx.dml.vba.DRAMSpeed = dc->clk_mgr->bw_params->clk_table.entries[1].memclk_mhz * 16;
3605 			context->bw_ctx.bw.dcn.clk.dramclk_khz = context->bw_ctx.dml.vba.DRAMSpeed * 1000 / 16;
3606 		}
3607 	}
3608 }
3609