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 
27 #include "dm_services.h"
28 #include "dc.h"
29 
30 #include "dcn32_init.h"
31 
32 #include "resource.h"
33 #include "include/irq_service_interface.h"
34 #include "dcn32_resource.h"
35 
36 #include "dcn20/dcn20_resource.h"
37 #include "dcn30/dcn30_resource.h"
38 
39 #include "dcn10/dcn10_ipp.h"
40 #include "dcn30/dcn30_hubbub.h"
41 #include "dcn31/dcn31_hubbub.h"
42 #include "dcn32/dcn32_hubbub.h"
43 #include "dcn32/dcn32_mpc.h"
44 #include "dcn32_hubp.h"
45 #include "irq/dcn32/irq_service_dcn32.h"
46 #include "dcn32/dcn32_dpp.h"
47 #include "dcn32/dcn32_optc.h"
48 #include "dcn20/dcn20_hwseq.h"
49 #include "dcn30/dcn30_hwseq.h"
50 #include "dce110/dce110_hw_sequencer.h"
51 #include "dcn30/dcn30_opp.h"
52 #include "dcn20/dcn20_dsc.h"
53 #include "dcn30/dcn30_vpg.h"
54 #include "dcn30/dcn30_afmt.h"
55 #include "dcn30/dcn30_dio_stream_encoder.h"
56 #include "dcn32/dcn32_dio_stream_encoder.h"
57 #include "dcn31/dcn31_hpo_dp_stream_encoder.h"
58 #include "dcn31/dcn31_hpo_dp_link_encoder.h"
59 #include "dcn32/dcn32_hpo_dp_link_encoder.h"
60 #include "dc_link_dp.h"
61 #include "dcn31/dcn31_apg.h"
62 #include "dcn31/dcn31_dio_link_encoder.h"
63 #include "dcn32/dcn32_dio_link_encoder.h"
64 #include "dce/dce_clock_source.h"
65 #include "dce/dce_audio.h"
66 #include "dce/dce_hwseq.h"
67 #include "clk_mgr.h"
68 #include "virtual/virtual_stream_encoder.h"
69 #include "dml/display_mode_vba.h"
70 #include "dcn32/dcn32_dccg.h"
71 #include "dcn10/dcn10_resource.h"
72 #include "dc_link_ddc.h"
73 #include "dcn31/dcn31_panel_cntl.h"
74 
75 #include "dcn30/dcn30_dwb.h"
76 #include "dcn32/dcn32_mmhubbub.h"
77 
78 #include "dcn/dcn_3_2_0_offset.h"
79 #include "dcn/dcn_3_2_0_sh_mask.h"
80 #include "nbio/nbio_4_3_0_offset.h"
81 
82 #include "reg_helper.h"
83 #include "dce/dmub_abm.h"
84 #include "dce/dmub_psr.h"
85 #include "dce/dce_aux.h"
86 #include "dce/dce_i2c.h"
87 
88 #include "dml/dcn30/display_mode_vba_30.h"
89 #include "vm_helper.h"
90 #include "dcn20/dcn20_vmid.h"
91 
92 #define DCN_BASE__INST0_SEG1                       0x000000C0
93 #define DCN_BASE__INST0_SEG2                       0x000034C0
94 #define DCN_BASE__INST0_SEG3                       0x00009000
95 #define NBIO_BASE__INST0_SEG1                      0x00000014
96 
97 #define MAX_INSTANCE                                        6
98 #define MAX_SEGMENT                                         6
99 
100 struct IP_BASE_INSTANCE {
101 	unsigned int segment[MAX_SEGMENT];
102 };
103 
104 struct IP_BASE {
105 	struct IP_BASE_INSTANCE instance[MAX_INSTANCE];
106 };
107 
108 static const struct IP_BASE DCN_BASE = { { { { 0x00000012, 0x000000C0, 0x000034C0, 0x00009000, 0x02403C00, 0 } },
109 					{ { 0, 0, 0, 0, 0, 0 } },
110 					{ { 0, 0, 0, 0, 0, 0 } },
111 					{ { 0, 0, 0, 0, 0, 0 } },
112 					{ { 0, 0, 0, 0, 0, 0 } },
113 					{ { 0, 0, 0, 0, 0, 0 } } } };
114 
115 #define DC_LOGGER_INIT(logger)
116 
117 #define DCN3_2_DEFAULT_DET_SIZE 256
118 #define DCN3_2_MAX_DET_SIZE 1152
119 #define DCN3_2_MIN_DET_SIZE 128
120 #define DCN3_2_MIN_COMPBUF_SIZE_KB 128
121 
122 struct _vcs_dpi_ip_params_st dcn3_2_ip = {
123 	.gpuvm_enable = 1,
124 	.gpuvm_max_page_table_levels = 4,
125 	.hostvm_enable = 0,
126 	.rob_buffer_size_kbytes = 128,
127 	.det_buffer_size_kbytes = DCN3_2_DEFAULT_DET_SIZE,
128 	.config_return_buffer_size_in_kbytes = 1280,
129 	.compressed_buffer_segment_size_in_kbytes = 64,
130 	.meta_fifo_size_in_kentries = 22,
131 	.zero_size_buffer_entries = 512,
132 	.compbuf_reserved_space_64b = 256,
133 	.compbuf_reserved_space_zs = 64,
134 	.dpp_output_buffer_pixels = 2560,
135 	.opp_output_buffer_lines = 1,
136 	.pixel_chunk_size_kbytes = 8,
137 	.alpha_pixel_chunk_size_kbytes = 4, // not appearing in spreadsheet, match c code from hw team
138 	.min_pixel_chunk_size_bytes = 1024,
139 	.dcc_meta_buffer_size_bytes = 6272,
140 	.meta_chunk_size_kbytes = 2,
141 	.min_meta_chunk_size_bytes = 256,
142 	.writeback_chunk_size_kbytes = 8,
143 	.ptoi_supported = false,
144 	.num_dsc = 4,
145 	.maximum_dsc_bits_per_component = 12,
146 	.maximum_pixels_per_line_per_dsc_unit = 6016,
147 	.dsc422_native_support = true,
148 	.is_line_buffer_bpp_fixed = true,
149 	.line_buffer_fixed_bpp = 57,
150 	.line_buffer_size_bits = 1171920, //DPP doc, DCN3_2_DisplayMode_73.xlsm still shows as 986880 bits with 48 bpp
151 	.max_line_buffer_lines = 32,
152 	.writeback_interface_buffer_size_kbytes = 90,
153 	.max_num_dpp = 4,
154 	.max_num_otg = 4,
155 	.max_num_hdmi_frl_outputs = 1,
156 	.max_num_wb = 1,
157 	.max_dchub_pscl_bw_pix_per_clk = 4,
158 	.max_pscl_lb_bw_pix_per_clk = 2,
159 	.max_lb_vscl_bw_pix_per_clk = 4,
160 	.max_vscl_hscl_bw_pix_per_clk = 4,
161 	.max_hscl_ratio = 6,
162 	.max_vscl_ratio = 6,
163 	.max_hscl_taps = 8,
164 	.max_vscl_taps = 8,
165 	.dpte_buffer_size_in_pte_reqs_luma = 64,
166 	.dpte_buffer_size_in_pte_reqs_chroma = 34,
167 	.dispclk_ramp_margin_percent = 1,
168 	.max_inter_dcn_tile_repeaters = 8,
169 	.cursor_buffer_size = 16,
170 	.cursor_chunk_size = 2,
171 	.writeback_line_buffer_buffer_size = 0,
172 	.writeback_min_hscl_ratio = 1,
173 	.writeback_min_vscl_ratio = 1,
174 	.writeback_max_hscl_ratio = 1,
175 	.writeback_max_vscl_ratio = 1,
176 	.writeback_max_hscl_taps = 1,
177 	.writeback_max_vscl_taps = 1,
178 	.dppclk_delay_subtotal = 47,
179 	.dppclk_delay_scl = 50,
180 	.dppclk_delay_scl_lb_only = 16,
181 	.dppclk_delay_cnvc_formatter = 28,
182 	.dppclk_delay_cnvc_cursor = 6,
183 	.dispclk_delay_subtotal = 125,
184 	.dynamic_metadata_vm_enabled = false,
185 	.odm_combine_4to1_supported = false,
186 	.dcc_supported = true,
187 	.max_num_dp2p0_outputs = 2,
188 	.max_num_dp2p0_streams = 4,
189 };
190 
191 struct _vcs_dpi_soc_bounding_box_st dcn3_2_soc = {
192 	.clock_limits = {
193 		{
194 			.state = 0,
195 			.dcfclk_mhz = 1564.0,
196 			.fabricclk_mhz = 400.0,
197 			.dispclk_mhz = 2150.0,
198 			.dppclk_mhz = 2150.0,
199 			.phyclk_mhz = 810.0,
200 			.phyclk_d18_mhz = 667.0,
201 			.phyclk_d32_mhz = 625.0,
202 			.socclk_mhz = 1200.0,
203 			.dscclk_mhz = 716.667,
204 			.dram_speed_mts = 1600.0,
205 			.dtbclk_mhz = 1564.0,
206 		},
207 	},
208 	.num_states = 1,
209 	.sr_exit_time_us = 5.20,
210 	.sr_enter_plus_exit_time_us = 9.60,
211 	.sr_exit_z8_time_us = 285.0,
212 	.sr_enter_plus_exit_z8_time_us = 320,
213 	.writeback_latency_us = 12.0,
214 	.round_trip_ping_latency_dcfclk_cycles = 263,
215 	.urgent_latency_pixel_data_only_us = 4.0,
216 	.urgent_latency_pixel_mixed_with_vm_data_us = 4.0,
217 	.urgent_latency_vm_data_only_us = 4.0,
218 	.fclk_change_latency_us = 20,
219 	.usr_retraining_latency_us = 2,
220 	.smn_latency_us = 2,
221 	.mall_allocated_for_dcn_mbytes = 64,
222 	.urgent_out_of_order_return_per_channel_pixel_only_bytes = 4096,
223 	.urgent_out_of_order_return_per_channel_pixel_and_vm_bytes = 4096,
224 	.urgent_out_of_order_return_per_channel_vm_only_bytes = 4096,
225 	.pct_ideal_sdp_bw_after_urgent = 100.0,
226 	.pct_ideal_fabric_bw_after_urgent = 67.0,
227 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_only = 20.0,
228 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_and_vm = 60.0, // N/A, for now keep as is until DML implemented
229 	.pct_ideal_dram_sdp_bw_after_urgent_vm_only = 30.0, // N/A, for now keep as is until DML implemented
230 	.pct_ideal_dram_bw_after_urgent_strobe = 67.0,
231 	.max_avg_sdp_bw_use_normal_percent = 80.0,
232 	.max_avg_fabric_bw_use_normal_percent = 60.0,
233 	.max_avg_dram_bw_use_normal_strobe_percent = 50.0,
234 	.max_avg_dram_bw_use_normal_percent = 15.0,
235 	.num_chans = 8,
236 	.dram_channel_width_bytes = 2,
237 	.fabric_datapath_to_dcn_data_return_bytes = 64,
238 	.return_bus_width_bytes = 64,
239 	.downspread_percent = 0.38,
240 	.dcn_downspread_percent = 0.5,
241 	.dram_clock_change_latency_us = 400,
242 	.dispclk_dppclk_vco_speed_mhz = 4300.0,
243 	.do_urgent_latency_adjustment = true,
244 	.urgent_latency_adjustment_fabric_clock_component_us = 1.0,
245 	.urgent_latency_adjustment_fabric_clock_reference_mhz = 1000,
246 };
247 
248 enum dcn32_clk_src_array_id {
249 	DCN32_CLK_SRC_PLL0,
250 	DCN32_CLK_SRC_PLL1,
251 	DCN32_CLK_SRC_PLL2,
252 	DCN32_CLK_SRC_PLL3,
253 	DCN32_CLK_SRC_PLL4,
254 	DCN32_CLK_SRC_TOTAL
255 };
256 
257 /* begin *********************
258  * macros to expend register list macro defined in HW object header file
259  */
260 
261 /* DCN */
262 /* TODO awful hack. fixup dcn20_dwb.h */
263 #undef BASE_INNER
264 #define BASE_INNER(seg) DCN_BASE__INST0_SEG ## seg
265 
266 #define BASE(seg) BASE_INNER(seg)
267 
268 #define SR(reg_name)\
269 		.reg_name = BASE(reg ## reg_name ## _BASE_IDX) +  \
270 					reg ## reg_name
271 
272 #define SRI(reg_name, block, id)\
273 	.reg_name = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
274 					reg ## block ## id ## _ ## reg_name
275 
276 #define SRI2(reg_name, block, id)\
277 	.reg_name = BASE(reg ## reg_name ## _BASE_IDX) + \
278 					reg ## reg_name
279 
280 #define SRIR(var_name, reg_name, block, id)\
281 	.var_name = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
282 					reg ## block ## id ## _ ## reg_name
283 
284 #define SRII(reg_name, block, id)\
285 	.reg_name[id] = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
286 					reg ## block ## id ## _ ## reg_name
287 
288 #define SRII_MPC_RMU(reg_name, block, id)\
289 	.RMU##_##reg_name[id] = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
290 					reg ## block ## id ## _ ## reg_name
291 
292 #define SRII_DWB(reg_name, temp_name, block, id)\
293 	.reg_name[id] = BASE(reg ## block ## id ## _ ## temp_name ## _BASE_IDX) + \
294 					reg ## block ## id ## _ ## temp_name
295 
296 #define DCCG_SRII(reg_name, block, id)\
297 	.block ## _ ## reg_name[id] = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
298 					reg ## block ## id ## _ ## reg_name
299 
300 #define VUPDATE_SRII(reg_name, block, id)\
301 	.reg_name[id] = BASE(reg ## reg_name ## _ ## block ## id ## _BASE_IDX) + \
302 					reg ## reg_name ## _ ## block ## id
303 
304 /* NBIO */
305 #define NBIO_BASE_INNER(seg) \
306 	NBIO_BASE__INST0_SEG ## seg
307 
308 #define NBIO_BASE(seg) \
309 	NBIO_BASE_INNER(seg)
310 
311 #define NBIO_SR(reg_name)\
312 		.reg_name = NBIO_BASE(regBIF_BX0_ ## reg_name ## _BASE_IDX) + \
313 					regBIF_BX0_ ## reg_name
314 
315 #define CTX ctx
316 #define REG(reg_name) \
317 	(DCN_BASE.instance[0].segment[reg ## reg_name ## _BASE_IDX] + reg ## reg_name)
318 
319 static const struct bios_registers bios_regs = {
320 		NBIO_SR(BIOS_SCRATCH_3),
321 		NBIO_SR(BIOS_SCRATCH_6)
322 };
323 
324 #define clk_src_regs(index, pllid)\
325 [index] = {\
326 	CS_COMMON_REG_LIST_DCN3_0(index, pllid),\
327 }
328 
329 static const struct dce110_clk_src_regs clk_src_regs[] = {
330 	clk_src_regs(0, A),
331 	clk_src_regs(1, B),
332 	clk_src_regs(2, C),
333 	clk_src_regs(3, D),
334 	clk_src_regs(4, E)
335 };
336 
337 static const struct dce110_clk_src_shift cs_shift = {
338 		CS_COMMON_MASK_SH_LIST_DCN3_2(__SHIFT)
339 };
340 
341 static const struct dce110_clk_src_mask cs_mask = {
342 		CS_COMMON_MASK_SH_LIST_DCN3_2(_MASK)
343 };
344 
345 #define abm_regs(id)\
346 [id] = {\
347 		ABM_DCN32_REG_LIST(id)\
348 }
349 
350 static const struct dce_abm_registers abm_regs[] = {
351 		abm_regs(0),
352 		abm_regs(1),
353 		abm_regs(2),
354 		abm_regs(3),
355 };
356 
357 static const struct dce_abm_shift abm_shift = {
358 		ABM_MASK_SH_LIST_DCN32(__SHIFT)
359 };
360 
361 static const struct dce_abm_mask abm_mask = {
362 		ABM_MASK_SH_LIST_DCN32(_MASK)
363 };
364 
365 #define audio_regs(id)\
366 [id] = {\
367 		AUD_COMMON_REG_LIST(id)\
368 }
369 
370 static const struct dce_audio_registers audio_regs[] = {
371 	audio_regs(0),
372 	audio_regs(1),
373 	audio_regs(2),
374 	audio_regs(3),
375 	audio_regs(4)
376 };
377 
378 #define DCE120_AUD_COMMON_MASK_SH_LIST(mask_sh)\
379 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_INDEX, AZALIA_ENDPOINT_REG_INDEX, mask_sh),\
380 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_DATA, AZALIA_ENDPOINT_REG_DATA, mask_sh),\
381 		AUD_COMMON_MASK_SH_LIST_BASE(mask_sh)
382 
383 static const struct dce_audio_shift audio_shift = {
384 		DCE120_AUD_COMMON_MASK_SH_LIST(__SHIFT)
385 };
386 
387 static const struct dce_audio_mask audio_mask = {
388 		DCE120_AUD_COMMON_MASK_SH_LIST(_MASK)
389 };
390 
391 #define vpg_regs(id)\
392 [id] = {\
393 	VPG_DCN3_REG_LIST(id)\
394 }
395 
396 static const struct dcn30_vpg_registers vpg_regs[] = {
397 	vpg_regs(0),
398 	vpg_regs(1),
399 	vpg_regs(2),
400 	vpg_regs(3),
401 	vpg_regs(4),
402 	vpg_regs(5),
403 	vpg_regs(6),
404 	vpg_regs(7),
405 	vpg_regs(8),
406 	vpg_regs(9),
407 };
408 
409 static const struct dcn30_vpg_shift vpg_shift = {
410 	DCN3_VPG_MASK_SH_LIST(__SHIFT)
411 };
412 
413 static const struct dcn30_vpg_mask vpg_mask = {
414 	DCN3_VPG_MASK_SH_LIST(_MASK)
415 };
416 
417 #define afmt_regs(id)\
418 [id] = {\
419 	AFMT_DCN3_REG_LIST(id)\
420 }
421 
422 static const struct dcn30_afmt_registers afmt_regs[] = {
423 	afmt_regs(0),
424 	afmt_regs(1),
425 	afmt_regs(2),
426 	afmt_regs(3),
427 	afmt_regs(4),
428 	afmt_regs(5)
429 };
430 
431 static const struct dcn30_afmt_shift afmt_shift = {
432 	DCN3_AFMT_MASK_SH_LIST(__SHIFT)
433 };
434 
435 static const struct dcn30_afmt_mask afmt_mask = {
436 	DCN3_AFMT_MASK_SH_LIST(_MASK)
437 };
438 
439 #define apg_regs(id)\
440 [id] = {\
441 	APG_DCN31_REG_LIST(id)\
442 }
443 
444 static const struct dcn31_apg_registers apg_regs[] = {
445 	apg_regs(0),
446 	apg_regs(1),
447 	apg_regs(2),
448 	apg_regs(3)
449 };
450 
451 static const struct dcn31_apg_shift apg_shift = {
452 	DCN31_APG_MASK_SH_LIST(__SHIFT)
453 };
454 
455 static const struct dcn31_apg_mask apg_mask = {
456 		DCN31_APG_MASK_SH_LIST(_MASK)
457 };
458 
459 #define stream_enc_regs(id)\
460 [id] = {\
461 	SE_DCN32_REG_LIST(id)\
462 }
463 
464 static const struct dcn10_stream_enc_registers stream_enc_regs[] = {
465 	stream_enc_regs(0),
466 	stream_enc_regs(1),
467 	stream_enc_regs(2),
468 	stream_enc_regs(3),
469 	stream_enc_regs(4)
470 };
471 
472 static const struct dcn10_stream_encoder_shift se_shift = {
473 		SE_COMMON_MASK_SH_LIST_DCN32(__SHIFT)
474 };
475 
476 static const struct dcn10_stream_encoder_mask se_mask = {
477 		SE_COMMON_MASK_SH_LIST_DCN32(_MASK)
478 };
479 
480 
481 #define aux_regs(id)\
482 [id] = {\
483 	DCN2_AUX_REG_LIST(id)\
484 }
485 
486 static const struct dcn10_link_enc_aux_registers link_enc_aux_regs[] = {
487 		aux_regs(0),
488 		aux_regs(1),
489 		aux_regs(2),
490 		aux_regs(3),
491 		aux_regs(4)
492 };
493 
494 #define hpd_regs(id)\
495 [id] = {\
496 	HPD_REG_LIST(id)\
497 }
498 
499 static const struct dcn10_link_enc_hpd_registers link_enc_hpd_regs[] = {
500 		hpd_regs(0),
501 		hpd_regs(1),
502 		hpd_regs(2),
503 		hpd_regs(3),
504 		hpd_regs(4)
505 };
506 
507 #define link_regs(id, phyid)\
508 [id] = {\
509 	LE_DCN31_REG_LIST(id), \
510 	UNIPHY_DCN2_REG_LIST(phyid), \
511 	/*DPCS_DCN31_REG_LIST(id),*/ \
512 }
513 
514 static const struct dcn10_link_enc_registers link_enc_regs[] = {
515 	link_regs(0, A),
516 	link_regs(1, B),
517 	link_regs(2, C),
518 	link_regs(3, D),
519 	link_regs(4, E)
520 };
521 
522 static const struct dcn10_link_enc_shift le_shift = {
523 	LINK_ENCODER_MASK_SH_LIST_DCN31(__SHIFT), \
524 	//DPCS_DCN31_MASK_SH_LIST(__SHIFT)
525 };
526 
527 static const struct dcn10_link_enc_mask le_mask = {
528 	LINK_ENCODER_MASK_SH_LIST_DCN31(_MASK), \
529 
530 	//DPCS_DCN31_MASK_SH_LIST(_MASK)
531 };
532 
533 #define hpo_dp_stream_encoder_reg_list(id)\
534 [id] = {\
535 	DCN3_1_HPO_DP_STREAM_ENC_REG_LIST(id)\
536 }
537 
538 static const struct dcn31_hpo_dp_stream_encoder_registers hpo_dp_stream_enc_regs[] = {
539 	hpo_dp_stream_encoder_reg_list(0),
540 	hpo_dp_stream_encoder_reg_list(1),
541 	hpo_dp_stream_encoder_reg_list(2),
542 	hpo_dp_stream_encoder_reg_list(3),
543 };
544 
545 static const struct dcn31_hpo_dp_stream_encoder_shift hpo_dp_se_shift = {
546 	DCN3_1_HPO_DP_STREAM_ENC_MASK_SH_LIST(__SHIFT)
547 };
548 
549 static const struct dcn31_hpo_dp_stream_encoder_mask hpo_dp_se_mask = {
550 	DCN3_1_HPO_DP_STREAM_ENC_MASK_SH_LIST(_MASK)
551 };
552 
553 
554 #define hpo_dp_link_encoder_reg_list(id)\
555 [id] = {\
556 	DCN3_1_HPO_DP_LINK_ENC_REG_LIST(id),\
557 	/*DCN3_1_RDPCSTX_REG_LIST(0),*/\
558 	/*DCN3_1_RDPCSTX_REG_LIST(1),*/\
559 	/*DCN3_1_RDPCSTX_REG_LIST(2),*/\
560 	/*DCN3_1_RDPCSTX_REG_LIST(3),*/\
561 	/*DCN3_1_RDPCSTX_REG_LIST(4)*/\
562 }
563 
564 static const struct dcn31_hpo_dp_link_encoder_registers hpo_dp_link_enc_regs[] = {
565 	hpo_dp_link_encoder_reg_list(0),
566 	hpo_dp_link_encoder_reg_list(1),
567 };
568 
569 static const struct dcn31_hpo_dp_link_encoder_shift hpo_dp_le_shift = {
570 	DCN3_2_HPO_DP_LINK_ENC_MASK_SH_LIST(__SHIFT)
571 };
572 
573 static const struct dcn31_hpo_dp_link_encoder_mask hpo_dp_le_mask = {
574 	DCN3_2_HPO_DP_LINK_ENC_MASK_SH_LIST(_MASK)
575 };
576 
577 #define dpp_regs(id)\
578 [id] = {\
579 	DPP_REG_LIST_DCN30_COMMON(id),\
580 }
581 
582 static const struct dcn3_dpp_registers dpp_regs[] = {
583 	dpp_regs(0),
584 	dpp_regs(1),
585 	dpp_regs(2),
586 	dpp_regs(3)
587 };
588 
589 static const struct dcn3_dpp_shift tf_shift = {
590 		DPP_REG_LIST_SH_MASK_DCN30_COMMON(__SHIFT)
591 };
592 
593 static const struct dcn3_dpp_mask tf_mask = {
594 		DPP_REG_LIST_SH_MASK_DCN30_COMMON(_MASK)
595 };
596 
597 
598 #define opp_regs(id)\
599 [id] = {\
600 	OPP_REG_LIST_DCN30(id),\
601 }
602 
603 static const struct dcn20_opp_registers opp_regs[] = {
604 	opp_regs(0),
605 	opp_regs(1),
606 	opp_regs(2),
607 	opp_regs(3)
608 };
609 
610 static const struct dcn20_opp_shift opp_shift = {
611 	OPP_MASK_SH_LIST_DCN20(__SHIFT)
612 };
613 
614 static const struct dcn20_opp_mask opp_mask = {
615 	OPP_MASK_SH_LIST_DCN20(_MASK)
616 };
617 
618 #define aux_engine_regs(id)\
619 [id] = {\
620 	AUX_COMMON_REG_LIST0(id), \
621 	.AUXN_IMPCAL = 0, \
622 	.AUXP_IMPCAL = 0, \
623 	.AUX_RESET_MASK = DP_AUX0_AUX_CONTROL__AUX_RESET_MASK, \
624 }
625 
626 static const struct dce110_aux_registers aux_engine_regs[] = {
627 		aux_engine_regs(0),
628 		aux_engine_regs(1),
629 		aux_engine_regs(2),
630 		aux_engine_regs(3),
631 		aux_engine_regs(4)
632 };
633 
634 static const struct dce110_aux_registers_shift aux_shift = {
635 	DCN_AUX_MASK_SH_LIST(__SHIFT)
636 };
637 
638 static const struct dce110_aux_registers_mask aux_mask = {
639 	DCN_AUX_MASK_SH_LIST(_MASK)
640 };
641 
642 
643 #define dwbc_regs_dcn3(id)\
644 [id] = {\
645 	DWBC_COMMON_REG_LIST_DCN30(id),\
646 }
647 
648 static const struct dcn30_dwbc_registers dwbc30_regs[] = {
649 	dwbc_regs_dcn3(0),
650 };
651 
652 static const struct dcn30_dwbc_shift dwbc30_shift = {
653 	DWBC_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
654 };
655 
656 static const struct dcn30_dwbc_mask dwbc30_mask = {
657 	DWBC_COMMON_MASK_SH_LIST_DCN30(_MASK)
658 };
659 
660 #define mcif_wb_regs_dcn3(id)\
661 [id] = {\
662 	MCIF_WB_COMMON_REG_LIST_DCN32(id),\
663 }
664 
665 static const struct dcn30_mmhubbub_registers mcif_wb30_regs[] = {
666 	mcif_wb_regs_dcn3(0)
667 };
668 
669 static const struct dcn30_mmhubbub_shift mcif_wb30_shift = {
670 	MCIF_WB_COMMON_MASK_SH_LIST_DCN32(__SHIFT)
671 };
672 
673 static const struct dcn30_mmhubbub_mask mcif_wb30_mask = {
674 	MCIF_WB_COMMON_MASK_SH_LIST_DCN32(_MASK)
675 };
676 
677 #define dsc_regsDCN20(id)\
678 [id] = {\
679 	DSC_REG_LIST_DCN20(id)\
680 }
681 
682 static const struct dcn20_dsc_registers dsc_regs[] = {
683 	dsc_regsDCN20(0),
684 	dsc_regsDCN20(1),
685 	dsc_regsDCN20(2),
686 	dsc_regsDCN20(3)
687 };
688 
689 static const struct dcn20_dsc_shift dsc_shift = {
690 	DSC_REG_LIST_SH_MASK_DCN20(__SHIFT)
691 };
692 
693 static const struct dcn20_dsc_mask dsc_mask = {
694 	DSC_REG_LIST_SH_MASK_DCN20(_MASK)
695 };
696 
697 static const struct dcn30_mpc_registers mpc_regs = {
698 		MPC_REG_LIST_DCN3_2(0),
699 		MPC_REG_LIST_DCN3_2(1),
700 		MPC_REG_LIST_DCN3_2(2),
701 		MPC_REG_LIST_DCN3_2(3),
702 		MPC_OUT_MUX_REG_LIST_DCN3_0(0),
703 		MPC_OUT_MUX_REG_LIST_DCN3_0(1),
704 		MPC_OUT_MUX_REG_LIST_DCN3_0(2),
705 		MPC_OUT_MUX_REG_LIST_DCN3_0(3),
706 		MPC_DWB_MUX_REG_LIST_DCN3_0(0),
707 };
708 
709 static const struct dcn30_mpc_shift mpc_shift = {
710 	MPC_COMMON_MASK_SH_LIST_DCN32(__SHIFT)
711 };
712 
713 static const struct dcn30_mpc_mask mpc_mask = {
714 	MPC_COMMON_MASK_SH_LIST_DCN32(_MASK)
715 };
716 
717 #define optc_regs(id)\
718 [id] = {OPTC_COMMON_REG_LIST_DCN3_2(id)}
719 
720 //#ifdef DIAGS_BUILD
721 //static struct dcn_optc_registers optc_regs[] = {
722 //#else
723 static const struct dcn_optc_registers optc_regs[] = {
724 //#endif
725 	optc_regs(0),
726 	optc_regs(1),
727 	optc_regs(2),
728 	optc_regs(3)
729 };
730 
731 static const struct dcn_optc_shift optc_shift = {
732 	OPTC_COMMON_MASK_SH_LIST_DCN3_2(__SHIFT)
733 };
734 
735 static const struct dcn_optc_mask optc_mask = {
736 	OPTC_COMMON_MASK_SH_LIST_DCN3_2(_MASK)
737 };
738 
739 #define hubp_regs(id)\
740 [id] = {\
741 	HUBP_REG_LIST_DCN32(id)\
742 }
743 
744 static const struct dcn_hubp2_registers hubp_regs[] = {
745 		hubp_regs(0),
746 		hubp_regs(1),
747 		hubp_regs(2),
748 		hubp_regs(3)
749 };
750 
751 
752 static const struct dcn_hubp2_shift hubp_shift = {
753 		HUBP_MASK_SH_LIST_DCN32(__SHIFT)
754 };
755 
756 static const struct dcn_hubp2_mask hubp_mask = {
757 		HUBP_MASK_SH_LIST_DCN32(_MASK)
758 };
759 static const struct dcn_hubbub_registers hubbub_reg = {
760 		HUBBUB_REG_LIST_DCN32(0)
761 };
762 
763 static const struct dcn_hubbub_shift hubbub_shift = {
764 		HUBBUB_MASK_SH_LIST_DCN32(__SHIFT)
765 };
766 
767 static const struct dcn_hubbub_mask hubbub_mask = {
768 		HUBBUB_MASK_SH_LIST_DCN32(_MASK)
769 };
770 
771 static const struct dccg_registers dccg_regs = {
772 		DCCG_REG_LIST_DCN32()
773 };
774 
775 static const struct dccg_shift dccg_shift = {
776 		DCCG_MASK_SH_LIST_DCN32(__SHIFT)
777 };
778 
779 static const struct dccg_mask dccg_mask = {
780 		DCCG_MASK_SH_LIST_DCN32(_MASK)
781 };
782 
783 
784 #define SRII2(reg_name_pre, reg_name_post, id)\
785 	.reg_name_pre ## _ ##  reg_name_post[id] = BASE(reg ## reg_name_pre \
786 			## id ## _ ## reg_name_post ## _BASE_IDX) + \
787 			reg ## reg_name_pre ## id ## _ ## reg_name_post
788 
789 
790 #define HWSEQ_DCN32_REG_LIST()\
791 	SR(DCHUBBUB_GLOBAL_TIMER_CNTL), \
792 	SR(DIO_MEM_PWR_CTRL), \
793 	SR(ODM_MEM_PWR_CTRL3), \
794 	SR(MMHUBBUB_MEM_PWR_CNTL), \
795 	SR(DCCG_GATE_DISABLE_CNTL), \
796 	SR(DCCG_GATE_DISABLE_CNTL2), \
797 	SR(DCFCLK_CNTL),\
798 	SR(DC_MEM_GLOBAL_PWR_REQ_CNTL), \
799 	SRII(PIXEL_RATE_CNTL, OTG, 0), \
800 	SRII(PIXEL_RATE_CNTL, OTG, 1),\
801 	SRII(PIXEL_RATE_CNTL, OTG, 2),\
802 	SRII(PIXEL_RATE_CNTL, OTG, 3),\
803 	SRII(PHYPLL_PIXEL_RATE_CNTL, OTG, 0),\
804 	SRII(PHYPLL_PIXEL_RATE_CNTL, OTG, 1),\
805 	SRII(PHYPLL_PIXEL_RATE_CNTL, OTG, 2),\
806 	SRII(PHYPLL_PIXEL_RATE_CNTL, OTG, 3),\
807 	SR(MICROSECOND_TIME_BASE_DIV), \
808 	SR(MILLISECOND_TIME_BASE_DIV), \
809 	SR(DISPCLK_FREQ_CHANGE_CNTL), \
810 	SR(RBBMIF_TIMEOUT_DIS), \
811 	SR(RBBMIF_TIMEOUT_DIS_2), \
812 	SR(DCHUBBUB_CRC_CTRL), \
813 	SR(DPP_TOP0_DPP_CRC_CTRL), \
814 	SR(DPP_TOP0_DPP_CRC_VAL_B_A), \
815 	SR(DPP_TOP0_DPP_CRC_VAL_R_G), \
816 	SR(MPC_CRC_CTRL), \
817 	SR(MPC_CRC_RESULT_GB), \
818 	SR(MPC_CRC_RESULT_C), \
819 	SR(MPC_CRC_RESULT_AR), \
820 	SR(DOMAIN0_PG_CONFIG), \
821 	SR(DOMAIN1_PG_CONFIG), \
822 	SR(DOMAIN2_PG_CONFIG), \
823 	SR(DOMAIN3_PG_CONFIG), \
824 	SR(DOMAIN16_PG_CONFIG), \
825 	SR(DOMAIN17_PG_CONFIG), \
826 	SR(DOMAIN18_PG_CONFIG), \
827 	SR(DOMAIN19_PG_CONFIG), \
828 	SR(DOMAIN0_PG_STATUS), \
829 	SR(DOMAIN1_PG_STATUS), \
830 	SR(DOMAIN2_PG_STATUS), \
831 	SR(DOMAIN3_PG_STATUS), \
832 	SR(DOMAIN16_PG_STATUS), \
833 	SR(DOMAIN17_PG_STATUS), \
834 	SR(DOMAIN18_PG_STATUS), \
835 	SR(DOMAIN19_PG_STATUS), \
836 	SR(D1VGA_CONTROL), \
837 	SR(D2VGA_CONTROL), \
838 	SR(D3VGA_CONTROL), \
839 	SR(D4VGA_CONTROL), \
840 	SR(D5VGA_CONTROL), \
841 	SR(D6VGA_CONTROL), \
842 	SR(DC_IP_REQUEST_CNTL), \
843 	SR(AZALIA_AUDIO_DTO), \
844 	SR(AZALIA_CONTROLLER_CLOCK_GATING)
845 
846 static const struct dce_hwseq_registers hwseq_reg = {
847 		HWSEQ_DCN32_REG_LIST()
848 };
849 
850 #define HWSEQ_DCN32_MASK_SH_LIST(mask_sh)\
851 	HWSEQ_DCN_MASK_SH_LIST(mask_sh), \
852 	HWS_SF(, DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_REFDIV, mask_sh), \
853 	HWS_SF(, DOMAIN0_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
854 	HWS_SF(, DOMAIN0_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
855 	HWS_SF(, DOMAIN1_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
856 	HWS_SF(, DOMAIN1_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
857 	HWS_SF(, DOMAIN2_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
858 	HWS_SF(, DOMAIN2_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
859 	HWS_SF(, DOMAIN3_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
860 	HWS_SF(, DOMAIN3_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
861 	HWS_SF(, DOMAIN16_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
862 	HWS_SF(, DOMAIN16_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
863 	HWS_SF(, DOMAIN17_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
864 	HWS_SF(, DOMAIN17_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
865 	HWS_SF(, DOMAIN18_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
866 	HWS_SF(, DOMAIN18_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
867 	HWS_SF(, DOMAIN19_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
868 	HWS_SF(, DOMAIN19_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
869 	HWS_SF(, DOMAIN0_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
870 	HWS_SF(, DOMAIN1_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
871 	HWS_SF(, DOMAIN2_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
872 	HWS_SF(, DOMAIN3_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
873 	HWS_SF(, DOMAIN16_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
874 	HWS_SF(, DOMAIN17_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
875 	HWS_SF(, DOMAIN18_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
876 	HWS_SF(, DOMAIN19_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
877 	HWS_SF(, DC_IP_REQUEST_CNTL, IP_REQUEST_EN, mask_sh), \
878 	HWS_SF(, AZALIA_AUDIO_DTO, AZALIA_AUDIO_DTO_MODULE, mask_sh), \
879 	HWS_SF(, HPO_TOP_CLOCK_CONTROL, HPO_HDMISTREAMCLK_G_GATE_DIS, mask_sh), \
880 	HWS_SF(, ODM_MEM_PWR_CTRL3, ODM_MEM_UNASSIGNED_PWR_MODE, mask_sh), \
881 	HWS_SF(, ODM_MEM_PWR_CTRL3, ODM_MEM_VBLANK_PWR_MODE, mask_sh), \
882 	HWS_SF(, MMHUBBUB_MEM_PWR_CNTL, VGA_MEM_PWR_FORCE, mask_sh)
883 
884 static const struct dce_hwseq_shift hwseq_shift = {
885 		HWSEQ_DCN32_MASK_SH_LIST(__SHIFT)
886 };
887 
888 static const struct dce_hwseq_mask hwseq_mask = {
889 		HWSEQ_DCN32_MASK_SH_LIST(_MASK)
890 };
891 #define vmid_regs(id)\
892 [id] = {\
893 		DCN20_VMID_REG_LIST(id)\
894 }
895 
896 static const struct dcn_vmid_registers vmid_regs[] = {
897 	vmid_regs(0),
898 	vmid_regs(1),
899 	vmid_regs(2),
900 	vmid_regs(3),
901 	vmid_regs(4),
902 	vmid_regs(5),
903 	vmid_regs(6),
904 	vmid_regs(7),
905 	vmid_regs(8),
906 	vmid_regs(9),
907 	vmid_regs(10),
908 	vmid_regs(11),
909 	vmid_regs(12),
910 	vmid_regs(13),
911 	vmid_regs(14),
912 	vmid_regs(15)
913 };
914 
915 static const struct dcn20_vmid_shift vmid_shifts = {
916 		DCN20_VMID_MASK_SH_LIST(__SHIFT)
917 };
918 
919 static const struct dcn20_vmid_mask vmid_masks = {
920 		DCN20_VMID_MASK_SH_LIST(_MASK)
921 };
922 
923 static const struct resource_caps res_cap_dcn32 = {
924 	.num_timing_generator = 4,
925 	.num_opp = 4,
926 	.num_video_plane = 4,
927 	.num_audio = 5,
928 	.num_stream_encoder = 5,
929 	.num_hpo_dp_stream_encoder = 4,
930 	.num_hpo_dp_link_encoder = 2,
931 	.num_pll = 5,
932 	.num_dwb = 1,
933 	.num_ddc = 5,
934 	.num_vmid = 16,
935 	.num_mpc_3dlut = 4,
936 	.num_dsc = 4,
937 };
938 
939 static const struct dc_plane_cap plane_cap = {
940 	.type = DC_PLANE_TYPE_DCN_UNIVERSAL,
941 	.blends_with_above = true,
942 	.blends_with_below = true,
943 	.per_pixel_alpha = true,
944 
945 	.pixel_format_support = {
946 			.argb8888 = true,
947 			.nv12 = true,
948 			.fp16 = true,
949 			.p010 = true,
950 			.ayuv = false,
951 	},
952 
953 	.max_upscale_factor = {
954 			.argb8888 = 16000,
955 			.nv12 = 16000,
956 			.fp16 = 16000
957 	},
958 
959 	// 6:1 downscaling ratio: 1000/6 = 166.666
960 	.max_downscale_factor = {
961 			.argb8888 = 167,
962 			.nv12 = 167,
963 			.fp16 = 167
964 	},
965 	64,
966 	64
967 };
968 
969 static const struct dc_debug_options debug_defaults_drv = {
970 	.disable_dmcu = true,
971 	.force_abm_enable = false,
972 	.timing_trace = false,
973 	.clock_trace = true,
974 	.disable_pplib_clock_request = false,
975 	.disable_idle_power_optimizations = true,
976 	.pipe_split_policy = MPC_SPLIT_DYNAMIC,
977 	.force_single_disp_pipe_split = false,
978 	.disable_dcc = DCC_ENABLE,
979 	.vsr_support = true,
980 	.performance_trace = false,
981 	.max_downscale_src_width = 7680,/*upto 8K*/
982 	.disable_pplib_wm_range = false,
983 	.scl_reset_length10 = true,
984 	.sanity_checks = false,
985 	.underflow_assert_delay_us = 0xFFFFFFFF,
986 	.dwb_fi_phase = -1, // -1 = disable,
987 	.dmub_command_table = true,
988 	.enable_mem_low_power = {
989 		.bits = {
990 			.vga = false,
991 			.i2c = false,
992 			.dmcu = false, // This is previously known to cause hang on S3 cycles if enabled
993 			.dscl = false,
994 			.cm = false,
995 			.mpc = false,
996 			.optc = true,
997 		}
998 	},
999 	.use_max_lb = true,
1000 	.force_disable_subvp = true
1001 };
1002 
1003 static const struct dc_debug_options debug_defaults_diags = {
1004 	.disable_dmcu = true,
1005 	.force_abm_enable = false,
1006 	.timing_trace = true,
1007 	.clock_trace = true,
1008 	.disable_dpp_power_gate = true,
1009 	.disable_hubp_power_gate = true,
1010 	.disable_dsc_power_gate = true,
1011 	.disable_clock_gate = true,
1012 	.disable_pplib_clock_request = true,
1013 	.disable_pplib_wm_range = true,
1014 	.disable_stutter = false,
1015 	.scl_reset_length10 = true,
1016 	.dwb_fi_phase = -1, // -1 = disable
1017 	.dmub_command_table = true,
1018 	.enable_tri_buf = true,
1019 	.use_max_lb = true,
1020 	.force_disable_subvp = true
1021 };
1022 
1023 static struct dce_aux *dcn32_aux_engine_create(
1024 	struct dc_context *ctx,
1025 	uint32_t inst)
1026 {
1027 	struct aux_engine_dce110 *aux_engine =
1028 		kzalloc(sizeof(struct aux_engine_dce110), GFP_KERNEL);
1029 
1030 	if (!aux_engine)
1031 		return NULL;
1032 
1033 	dce110_aux_engine_construct(aux_engine, ctx, inst,
1034 				    SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD,
1035 				    &aux_engine_regs[inst],
1036 					&aux_mask,
1037 					&aux_shift,
1038 					ctx->dc->caps.extended_aux_timeout_support);
1039 
1040 	return &aux_engine->base;
1041 }
1042 #define i2c_inst_regs(id) { I2C_HW_ENGINE_COMMON_REG_LIST_DCN30(id) }
1043 
1044 static const struct dce_i2c_registers i2c_hw_regs[] = {
1045 		i2c_inst_regs(1),
1046 		i2c_inst_regs(2),
1047 		i2c_inst_regs(3),
1048 		i2c_inst_regs(4),
1049 		i2c_inst_regs(5),
1050 };
1051 
1052 static const struct dce_i2c_shift i2c_shifts = {
1053 		I2C_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
1054 };
1055 
1056 static const struct dce_i2c_mask i2c_masks = {
1057 		I2C_COMMON_MASK_SH_LIST_DCN30(_MASK)
1058 };
1059 
1060 static struct dce_i2c_hw *dcn32_i2c_hw_create(
1061 	struct dc_context *ctx,
1062 	uint32_t inst)
1063 {
1064 	struct dce_i2c_hw *dce_i2c_hw =
1065 		kzalloc(sizeof(struct dce_i2c_hw), GFP_KERNEL);
1066 
1067 	if (!dce_i2c_hw)
1068 		return NULL;
1069 
1070 	dcn2_i2c_hw_construct(dce_i2c_hw, ctx, inst,
1071 				    &i2c_hw_regs[inst], &i2c_shifts, &i2c_masks);
1072 
1073 	return dce_i2c_hw;
1074 }
1075 
1076 static struct clock_source *dcn32_clock_source_create(
1077 		struct dc_context *ctx,
1078 		struct dc_bios *bios,
1079 		enum clock_source_id id,
1080 		const struct dce110_clk_src_regs *regs,
1081 		bool dp_clk_src)
1082 {
1083 	struct dce110_clk_src *clk_src =
1084 		kzalloc(sizeof(struct dce110_clk_src), GFP_KERNEL);
1085 
1086 	if (!clk_src)
1087 		return NULL;
1088 
1089 	if (dcn31_clk_src_construct(clk_src, ctx, bios, id,
1090 			regs, &cs_shift, &cs_mask)) {
1091 		clk_src->base.dp_clk_src = dp_clk_src;
1092 		return &clk_src->base;
1093 	}
1094 
1095 	BREAK_TO_DEBUGGER();
1096 	return NULL;
1097 }
1098 
1099 static struct hubbub *dcn32_hubbub_create(struct dc_context *ctx)
1100 {
1101 	int i;
1102 
1103 	struct dcn20_hubbub *hubbub2 = kzalloc(sizeof(struct dcn20_hubbub),
1104 					  GFP_KERNEL);
1105 
1106 	if (!hubbub2)
1107 		return NULL;
1108 
1109 	hubbub32_construct(hubbub2, ctx,
1110 			&hubbub_reg,
1111 			&hubbub_shift,
1112 			&hubbub_mask,
1113 			ctx->dc->dml.ip.det_buffer_size_kbytes,
1114 			ctx->dc->dml.ip.pixel_chunk_size_kbytes,
1115 			ctx->dc->dml.ip.config_return_buffer_size_in_kbytes);
1116 
1117 
1118 	for (i = 0; i < res_cap_dcn32.num_vmid; i++) {
1119 		struct dcn20_vmid *vmid = &hubbub2->vmid[i];
1120 
1121 		vmid->ctx = ctx;
1122 
1123 		vmid->regs = &vmid_regs[i];
1124 		vmid->shifts = &vmid_shifts;
1125 		vmid->masks = &vmid_masks;
1126 	}
1127 
1128 	return &hubbub2->base;
1129 }
1130 
1131 static struct hubp *dcn32_hubp_create(
1132 	struct dc_context *ctx,
1133 	uint32_t inst)
1134 {
1135 	struct dcn20_hubp *hubp2 =
1136 		kzalloc(sizeof(struct dcn20_hubp), GFP_KERNEL);
1137 
1138 	if (!hubp2)
1139 		return NULL;
1140 
1141 	if (hubp32_construct(hubp2, ctx, inst,
1142 			&hubp_regs[inst], &hubp_shift, &hubp_mask))
1143 		return &hubp2->base;
1144 
1145 	BREAK_TO_DEBUGGER();
1146 	kfree(hubp2);
1147 	return NULL;
1148 }
1149 
1150 static void dcn32_dpp_destroy(struct dpp **dpp)
1151 {
1152 	kfree(TO_DCN30_DPP(*dpp));
1153 	*dpp = NULL;
1154 }
1155 
1156 static struct dpp *dcn32_dpp_create(
1157 	struct dc_context *ctx,
1158 	uint32_t inst)
1159 {
1160 	struct dcn3_dpp *dpp3 =
1161 		kzalloc(sizeof(struct dcn3_dpp), GFP_KERNEL);
1162 
1163 	if (!dpp3)
1164 		return NULL;
1165 
1166 	if (dpp32_construct(dpp3, ctx, inst,
1167 			&dpp_regs[inst], &tf_shift, &tf_mask))
1168 		return &dpp3->base;
1169 
1170 	BREAK_TO_DEBUGGER();
1171 	kfree(dpp3);
1172 	return NULL;
1173 }
1174 
1175 static struct mpc *dcn32_mpc_create(
1176 		struct dc_context *ctx,
1177 		int num_mpcc,
1178 		int num_rmu)
1179 {
1180 	struct dcn30_mpc *mpc30 = kzalloc(sizeof(struct dcn30_mpc),
1181 					  GFP_KERNEL);
1182 
1183 	if (!mpc30)
1184 		return NULL;
1185 
1186 	dcn32_mpc_construct(mpc30, ctx,
1187 			&mpc_regs,
1188 			&mpc_shift,
1189 			&mpc_mask,
1190 			num_mpcc,
1191 			num_rmu);
1192 
1193 	return &mpc30->base;
1194 }
1195 
1196 static struct output_pixel_processor *dcn32_opp_create(
1197 	struct dc_context *ctx, uint32_t inst)
1198 {
1199 	struct dcn20_opp *opp2 =
1200 		kzalloc(sizeof(struct dcn20_opp), GFP_KERNEL);
1201 
1202 	if (!opp2) {
1203 		BREAK_TO_DEBUGGER();
1204 		return NULL;
1205 	}
1206 
1207 	dcn20_opp_construct(opp2, ctx, inst,
1208 			&opp_regs[inst], &opp_shift, &opp_mask);
1209 	return &opp2->base;
1210 }
1211 
1212 
1213 static struct timing_generator *dcn32_timing_generator_create(
1214 		struct dc_context *ctx,
1215 		uint32_t instance)
1216 {
1217 	struct optc *tgn10 =
1218 		kzalloc(sizeof(struct optc), GFP_KERNEL);
1219 
1220 	if (!tgn10)
1221 		return NULL;
1222 
1223 	tgn10->base.inst = instance;
1224 	tgn10->base.ctx = ctx;
1225 
1226 	tgn10->tg_regs = &optc_regs[instance];
1227 	tgn10->tg_shift = &optc_shift;
1228 	tgn10->tg_mask = &optc_mask;
1229 
1230 	dcn32_timing_generator_init(tgn10);
1231 
1232 	return &tgn10->base;
1233 }
1234 
1235 static const struct encoder_feature_support link_enc_feature = {
1236 		.max_hdmi_deep_color = COLOR_DEPTH_121212,
1237 		.max_hdmi_pixel_clock = 600000,
1238 		.hdmi_ycbcr420_supported = true,
1239 		.dp_ycbcr420_supported = true,
1240 		.fec_supported = true,
1241 		.flags.bits.IS_HBR2_CAPABLE = true,
1242 		.flags.bits.IS_HBR3_CAPABLE = true,
1243 		.flags.bits.IS_TPS3_CAPABLE = true,
1244 		.flags.bits.IS_TPS4_CAPABLE = true
1245 };
1246 
1247 static struct link_encoder *dcn32_link_encoder_create(
1248 	const struct encoder_init_data *enc_init_data)
1249 {
1250 	struct dcn20_link_encoder *enc20 =
1251 		kzalloc(sizeof(struct dcn20_link_encoder), GFP_KERNEL);
1252 
1253 	if (!enc20)
1254 		return NULL;
1255 
1256 	dcn32_link_encoder_construct(enc20,
1257 			enc_init_data,
1258 			&link_enc_feature,
1259 			&link_enc_regs[enc_init_data->transmitter],
1260 			&link_enc_aux_regs[enc_init_data->channel - 1],
1261 			&link_enc_hpd_regs[enc_init_data->hpd_source],
1262 			&le_shift,
1263 			&le_mask);
1264 
1265 	return &enc20->enc10.base;
1266 }
1267 
1268 struct panel_cntl *dcn32_panel_cntl_create(const struct panel_cntl_init_data *init_data)
1269 {
1270 	struct dcn31_panel_cntl *panel_cntl =
1271 		kzalloc(sizeof(struct dcn31_panel_cntl), GFP_KERNEL);
1272 
1273 	if (!panel_cntl)
1274 		return NULL;
1275 
1276 	dcn31_panel_cntl_construct(panel_cntl, init_data);
1277 
1278 	return &panel_cntl->base;
1279 }
1280 
1281 static void read_dce_straps(
1282 	struct dc_context *ctx,
1283 	struct resource_straps *straps)
1284 {
1285 	generic_reg_get(ctx, regDC_PINSTRAPS + BASE(regDC_PINSTRAPS_BASE_IDX),
1286 		FN(DC_PINSTRAPS, DC_PINSTRAPS_AUDIO), &straps->dc_pinstraps_audio);
1287 
1288 }
1289 
1290 static struct audio *dcn32_create_audio(
1291 		struct dc_context *ctx, unsigned int inst)
1292 {
1293 	return dce_audio_create(ctx, inst,
1294 			&audio_regs[inst], &audio_shift, &audio_mask);
1295 }
1296 
1297 static struct vpg *dcn32_vpg_create(
1298 	struct dc_context *ctx,
1299 	uint32_t inst)
1300 {
1301 	struct dcn30_vpg *vpg3 = kzalloc(sizeof(struct dcn30_vpg), GFP_KERNEL);
1302 
1303 	if (!vpg3)
1304 		return NULL;
1305 
1306 	vpg3_construct(vpg3, ctx, inst,
1307 			&vpg_regs[inst],
1308 			&vpg_shift,
1309 			&vpg_mask);
1310 
1311 	return &vpg3->base;
1312 }
1313 
1314 static struct afmt *dcn32_afmt_create(
1315 	struct dc_context *ctx,
1316 	uint32_t inst)
1317 {
1318 	struct dcn30_afmt *afmt3 = kzalloc(sizeof(struct dcn30_afmt), GFP_KERNEL);
1319 
1320 	if (!afmt3)
1321 		return NULL;
1322 
1323 	afmt3_construct(afmt3, ctx, inst,
1324 			&afmt_regs[inst],
1325 			&afmt_shift,
1326 			&afmt_mask);
1327 
1328 	return &afmt3->base;
1329 }
1330 
1331 static struct apg *dcn31_apg_create(
1332 	struct dc_context *ctx,
1333 	uint32_t inst)
1334 {
1335 	struct dcn31_apg *apg31 = kzalloc(sizeof(struct dcn31_apg), GFP_KERNEL);
1336 
1337 	if (!apg31)
1338 		return NULL;
1339 
1340 	apg31_construct(apg31, ctx, inst,
1341 			&apg_regs[inst],
1342 			&apg_shift,
1343 			&apg_mask);
1344 
1345 	return &apg31->base;
1346 }
1347 
1348 static struct stream_encoder *dcn32_stream_encoder_create(
1349 	enum engine_id eng_id,
1350 	struct dc_context *ctx)
1351 {
1352 	struct dcn10_stream_encoder *enc1;
1353 	struct vpg *vpg;
1354 	struct afmt *afmt;
1355 	int vpg_inst;
1356 	int afmt_inst;
1357 
1358 	/* Mapping of VPG, AFMT, DME register blocks to DIO block instance */
1359 	if (eng_id <= ENGINE_ID_DIGF) {
1360 		vpg_inst = eng_id;
1361 		afmt_inst = eng_id;
1362 	} else
1363 		return NULL;
1364 
1365 	enc1 = kzalloc(sizeof(struct dcn10_stream_encoder), GFP_KERNEL);
1366 	vpg = dcn32_vpg_create(ctx, vpg_inst);
1367 	afmt = dcn32_afmt_create(ctx, afmt_inst);
1368 
1369 	if (!enc1 || !vpg || !afmt) {
1370 		kfree(enc1);
1371 		kfree(vpg);
1372 		kfree(afmt);
1373 		return NULL;
1374 	}
1375 
1376 	dcn32_dio_stream_encoder_construct(enc1, ctx, ctx->dc_bios,
1377 					eng_id, vpg, afmt,
1378 					&stream_enc_regs[eng_id],
1379 					&se_shift, &se_mask);
1380 
1381 	return &enc1->base;
1382 }
1383 
1384 static struct hpo_dp_stream_encoder *dcn32_hpo_dp_stream_encoder_create(
1385 	enum engine_id eng_id,
1386 	struct dc_context *ctx)
1387 {
1388 	struct dcn31_hpo_dp_stream_encoder *hpo_dp_enc31;
1389 	struct vpg *vpg;
1390 	struct apg *apg;
1391 	uint32_t hpo_dp_inst;
1392 	uint32_t vpg_inst;
1393 	uint32_t apg_inst;
1394 
1395 	ASSERT((eng_id >= ENGINE_ID_HPO_DP_0) && (eng_id <= ENGINE_ID_HPO_DP_3));
1396 	hpo_dp_inst = eng_id - ENGINE_ID_HPO_DP_0;
1397 
1398 	/* Mapping of VPG register blocks to HPO DP block instance:
1399 	 * VPG[6] -> HPO_DP[0]
1400 	 * VPG[7] -> HPO_DP[1]
1401 	 * VPG[8] -> HPO_DP[2]
1402 	 * VPG[9] -> HPO_DP[3]
1403 	 */
1404 	vpg_inst = hpo_dp_inst + 6;
1405 
1406 	/* Mapping of APG register blocks to HPO DP block instance:
1407 	 * APG[0] -> HPO_DP[0]
1408 	 * APG[1] -> HPO_DP[1]
1409 	 * APG[2] -> HPO_DP[2]
1410 	 * APG[3] -> HPO_DP[3]
1411 	 */
1412 	apg_inst = hpo_dp_inst;
1413 
1414 	/* allocate HPO stream encoder and create VPG sub-block */
1415 	hpo_dp_enc31 = kzalloc(sizeof(struct dcn31_hpo_dp_stream_encoder), GFP_KERNEL);
1416 	vpg = dcn32_vpg_create(ctx, vpg_inst);
1417 	apg = dcn31_apg_create(ctx, apg_inst);
1418 
1419 	if (!hpo_dp_enc31 || !vpg || !apg) {
1420 		kfree(hpo_dp_enc31);
1421 		kfree(vpg);
1422 		kfree(apg);
1423 		return NULL;
1424 	}
1425 
1426 	dcn31_hpo_dp_stream_encoder_construct(hpo_dp_enc31, ctx, ctx->dc_bios,
1427 					hpo_dp_inst, eng_id, vpg, apg,
1428 					&hpo_dp_stream_enc_regs[hpo_dp_inst],
1429 					&hpo_dp_se_shift, &hpo_dp_se_mask);
1430 
1431 	return &hpo_dp_enc31->base;
1432 }
1433 
1434 static struct hpo_dp_link_encoder *dcn32_hpo_dp_link_encoder_create(
1435 	uint8_t inst,
1436 	struct dc_context *ctx)
1437 {
1438 	struct dcn31_hpo_dp_link_encoder *hpo_dp_enc31;
1439 
1440 	/* allocate HPO link encoder */
1441 	hpo_dp_enc31 = kzalloc(sizeof(struct dcn31_hpo_dp_link_encoder), GFP_KERNEL);
1442 
1443 	hpo_dp_link_encoder32_construct(hpo_dp_enc31, ctx, inst,
1444 					&hpo_dp_link_enc_regs[inst],
1445 					&hpo_dp_le_shift, &hpo_dp_le_mask);
1446 
1447 	return &hpo_dp_enc31->base;
1448 }
1449 
1450 static struct dce_hwseq *dcn32_hwseq_create(
1451 	struct dc_context *ctx)
1452 {
1453 	struct dce_hwseq *hws = kzalloc(sizeof(struct dce_hwseq), GFP_KERNEL);
1454 
1455 	if (hws) {
1456 		hws->ctx = ctx;
1457 		hws->regs = &hwseq_reg;
1458 		hws->shifts = &hwseq_shift;
1459 		hws->masks = &hwseq_mask;
1460 	}
1461 	return hws;
1462 }
1463 static const struct resource_create_funcs res_create_funcs = {
1464 	.read_dce_straps = read_dce_straps,
1465 	.create_audio = dcn32_create_audio,
1466 	.create_stream_encoder = dcn32_stream_encoder_create,
1467 	.create_hpo_dp_stream_encoder = dcn32_hpo_dp_stream_encoder_create,
1468 	.create_hpo_dp_link_encoder = dcn32_hpo_dp_link_encoder_create,
1469 	.create_hwseq = dcn32_hwseq_create,
1470 };
1471 
1472 static const struct resource_create_funcs res_create_maximus_funcs = {
1473 	.read_dce_straps = NULL,
1474 	.create_audio = NULL,
1475 	.create_stream_encoder = NULL,
1476 	.create_hpo_dp_stream_encoder = dcn32_hpo_dp_stream_encoder_create,
1477 	.create_hpo_dp_link_encoder = dcn32_hpo_dp_link_encoder_create,
1478 	.create_hwseq = dcn32_hwseq_create,
1479 };
1480 
1481 static void dcn32_resource_destruct(struct dcn32_resource_pool *pool)
1482 {
1483 	unsigned int i;
1484 
1485 	for (i = 0; i < pool->base.stream_enc_count; i++) {
1486 		if (pool->base.stream_enc[i] != NULL) {
1487 			if (pool->base.stream_enc[i]->vpg != NULL) {
1488 				kfree(DCN30_VPG_FROM_VPG(pool->base.stream_enc[i]->vpg));
1489 				pool->base.stream_enc[i]->vpg = NULL;
1490 			}
1491 			if (pool->base.stream_enc[i]->afmt != NULL) {
1492 				kfree(DCN30_AFMT_FROM_AFMT(pool->base.stream_enc[i]->afmt));
1493 				pool->base.stream_enc[i]->afmt = NULL;
1494 			}
1495 			kfree(DCN10STRENC_FROM_STRENC(pool->base.stream_enc[i]));
1496 			pool->base.stream_enc[i] = NULL;
1497 		}
1498 	}
1499 
1500 	for (i = 0; i < pool->base.hpo_dp_stream_enc_count; i++) {
1501 		if (pool->base.hpo_dp_stream_enc[i] != NULL) {
1502 			if (pool->base.hpo_dp_stream_enc[i]->vpg != NULL) {
1503 				kfree(DCN30_VPG_FROM_VPG(pool->base.hpo_dp_stream_enc[i]->vpg));
1504 				pool->base.hpo_dp_stream_enc[i]->vpg = NULL;
1505 			}
1506 			if (pool->base.hpo_dp_stream_enc[i]->apg != NULL) {
1507 				kfree(DCN31_APG_FROM_APG(pool->base.hpo_dp_stream_enc[i]->apg));
1508 				pool->base.hpo_dp_stream_enc[i]->apg = NULL;
1509 			}
1510 			kfree(DCN3_1_HPO_DP_STREAM_ENC_FROM_HPO_STREAM_ENC(pool->base.hpo_dp_stream_enc[i]));
1511 			pool->base.hpo_dp_stream_enc[i] = NULL;
1512 		}
1513 	}
1514 
1515 	for (i = 0; i < pool->base.hpo_dp_link_enc_count; i++) {
1516 		if (pool->base.hpo_dp_link_enc[i] != NULL) {
1517 			kfree(DCN3_1_HPO_DP_LINK_ENC_FROM_HPO_LINK_ENC(pool->base.hpo_dp_link_enc[i]));
1518 			pool->base.hpo_dp_link_enc[i] = NULL;
1519 		}
1520 	}
1521 
1522 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
1523 		if (pool->base.dscs[i] != NULL)
1524 			dcn20_dsc_destroy(&pool->base.dscs[i]);
1525 	}
1526 
1527 	if (pool->base.mpc != NULL) {
1528 		kfree(TO_DCN20_MPC(pool->base.mpc));
1529 		pool->base.mpc = NULL;
1530 	}
1531 	if (pool->base.hubbub != NULL) {
1532 		kfree(TO_DCN20_HUBBUB(pool->base.hubbub));
1533 		pool->base.hubbub = NULL;
1534 	}
1535 	for (i = 0; i < pool->base.pipe_count; i++) {
1536 		if (pool->base.dpps[i] != NULL)
1537 			dcn32_dpp_destroy(&pool->base.dpps[i]);
1538 
1539 		if (pool->base.ipps[i] != NULL)
1540 			pool->base.ipps[i]->funcs->ipp_destroy(&pool->base.ipps[i]);
1541 
1542 		if (pool->base.hubps[i] != NULL) {
1543 			kfree(TO_DCN20_HUBP(pool->base.hubps[i]));
1544 			pool->base.hubps[i] = NULL;
1545 		}
1546 
1547 		if (pool->base.irqs != NULL) {
1548 			dal_irq_service_destroy(&pool->base.irqs);
1549 		}
1550 	}
1551 
1552 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1553 		if (pool->base.engines[i] != NULL)
1554 			dce110_engine_destroy(&pool->base.engines[i]);
1555 		if (pool->base.hw_i2cs[i] != NULL) {
1556 			kfree(pool->base.hw_i2cs[i]);
1557 			pool->base.hw_i2cs[i] = NULL;
1558 		}
1559 		if (pool->base.sw_i2cs[i] != NULL) {
1560 			kfree(pool->base.sw_i2cs[i]);
1561 			pool->base.sw_i2cs[i] = NULL;
1562 		}
1563 	}
1564 
1565 	for (i = 0; i < pool->base.res_cap->num_opp; i++) {
1566 		if (pool->base.opps[i] != NULL)
1567 			pool->base.opps[i]->funcs->opp_destroy(&pool->base.opps[i]);
1568 	}
1569 
1570 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1571 		if (pool->base.timing_generators[i] != NULL)	{
1572 			kfree(DCN10TG_FROM_TG(pool->base.timing_generators[i]));
1573 			pool->base.timing_generators[i] = NULL;
1574 		}
1575 	}
1576 
1577 	for (i = 0; i < pool->base.res_cap->num_dwb; i++) {
1578 		if (pool->base.dwbc[i] != NULL) {
1579 			kfree(TO_DCN30_DWBC(pool->base.dwbc[i]));
1580 			pool->base.dwbc[i] = NULL;
1581 		}
1582 		if (pool->base.mcif_wb[i] != NULL) {
1583 			kfree(TO_DCN30_MMHUBBUB(pool->base.mcif_wb[i]));
1584 			pool->base.mcif_wb[i] = NULL;
1585 		}
1586 	}
1587 
1588 	for (i = 0; i < pool->base.audio_count; i++) {
1589 		if (pool->base.audios[i])
1590 			dce_aud_destroy(&pool->base.audios[i]);
1591 	}
1592 
1593 	for (i = 0; i < pool->base.clk_src_count; i++) {
1594 		if (pool->base.clock_sources[i] != NULL) {
1595 			dcn20_clock_source_destroy(&pool->base.clock_sources[i]);
1596 			pool->base.clock_sources[i] = NULL;
1597 		}
1598 	}
1599 
1600 	for (i = 0; i < pool->base.res_cap->num_mpc_3dlut; i++) {
1601 		if (pool->base.mpc_lut[i] != NULL) {
1602 			dc_3dlut_func_release(pool->base.mpc_lut[i]);
1603 			pool->base.mpc_lut[i] = NULL;
1604 		}
1605 		if (pool->base.mpc_shaper[i] != NULL) {
1606 			dc_transfer_func_release(pool->base.mpc_shaper[i]);
1607 			pool->base.mpc_shaper[i] = NULL;
1608 		}
1609 	}
1610 
1611 	if (pool->base.dp_clock_source != NULL) {
1612 		dcn20_clock_source_destroy(&pool->base.dp_clock_source);
1613 		pool->base.dp_clock_source = NULL;
1614 	}
1615 
1616 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1617 		if (pool->base.multiple_abms[i] != NULL)
1618 			dce_abm_destroy(&pool->base.multiple_abms[i]);
1619 	}
1620 
1621 	if (pool->base.psr != NULL)
1622 		dmub_psr_destroy(&pool->base.psr);
1623 
1624 	if (pool->base.dccg != NULL)
1625 		dcn_dccg_destroy(&pool->base.dccg);
1626 
1627 	if (pool->base.oem_device != NULL)
1628 		dal_ddc_service_destroy(&pool->base.oem_device);
1629 }
1630 
1631 
1632 static bool dcn32_dwbc_create(struct dc_context *ctx, struct resource_pool *pool)
1633 {
1634 	int i;
1635 	uint32_t dwb_count = pool->res_cap->num_dwb;
1636 
1637 	for (i = 0; i < dwb_count; i++) {
1638 		struct dcn30_dwbc *dwbc30 = kzalloc(sizeof(struct dcn30_dwbc),
1639 						    GFP_KERNEL);
1640 
1641 		if (!dwbc30) {
1642 			dm_error("DC: failed to create dwbc30!\n");
1643 			return false;
1644 		}
1645 
1646 		dcn30_dwbc_construct(dwbc30, ctx,
1647 				&dwbc30_regs[i],
1648 				&dwbc30_shift,
1649 				&dwbc30_mask,
1650 				i);
1651 
1652 		pool->dwbc[i] = &dwbc30->base;
1653 	}
1654 	return true;
1655 }
1656 
1657 static bool dcn32_mmhubbub_create(struct dc_context *ctx, struct resource_pool *pool)
1658 {
1659 	int i;
1660 	uint32_t dwb_count = pool->res_cap->num_dwb;
1661 
1662 	for (i = 0; i < dwb_count; i++) {
1663 		struct dcn30_mmhubbub *mcif_wb30 = kzalloc(sizeof(struct dcn30_mmhubbub),
1664 						    GFP_KERNEL);
1665 
1666 		if (!mcif_wb30) {
1667 			dm_error("DC: failed to create mcif_wb30!\n");
1668 			return false;
1669 		}
1670 
1671 		dcn32_mmhubbub_construct(mcif_wb30, ctx,
1672 				&mcif_wb30_regs[i],
1673 				&mcif_wb30_shift,
1674 				&mcif_wb30_mask,
1675 				i);
1676 
1677 		pool->mcif_wb[i] = &mcif_wb30->base;
1678 	}
1679 	return true;
1680 }
1681 
1682 static struct display_stream_compressor *dcn32_dsc_create(
1683 	struct dc_context *ctx, uint32_t inst)
1684 {
1685 	struct dcn20_dsc *dsc =
1686 		kzalloc(sizeof(struct dcn20_dsc), GFP_KERNEL);
1687 
1688 	if (!dsc) {
1689 		BREAK_TO_DEBUGGER();
1690 		return NULL;
1691 	}
1692 
1693 	dsc2_construct(dsc, ctx, inst, &dsc_regs[inst], &dsc_shift, &dsc_mask);
1694 
1695 	dsc->max_image_width = 6016;
1696 
1697 	return &dsc->base;
1698 }
1699 
1700 static void dcn32_destroy_resource_pool(struct resource_pool **pool)
1701 {
1702 	struct dcn32_resource_pool *dcn32_pool = TO_DCN32_RES_POOL(*pool);
1703 
1704 	dcn32_resource_destruct(dcn32_pool);
1705 	kfree(dcn32_pool);
1706 	*pool = NULL;
1707 }
1708 
1709 bool dcn32_acquire_post_bldn_3dlut(
1710 		struct resource_context *res_ctx,
1711 		const struct resource_pool *pool,
1712 		int mpcc_id,
1713 		struct dc_3dlut **lut,
1714 		struct dc_transfer_func **shaper)
1715 {
1716 	bool ret = false;
1717 	union dc_3dlut_state *state;
1718 
1719 	ASSERT(*lut == NULL && *shaper == NULL);
1720 	*lut = NULL;
1721 	*shaper = NULL;
1722 
1723 	if (!res_ctx->is_mpc_3dlut_acquired[mpcc_id]) {
1724 		*lut = pool->mpc_lut[mpcc_id];
1725 		*shaper = pool->mpc_shaper[mpcc_id];
1726 		state = &pool->mpc_lut[mpcc_id]->state;
1727 		res_ctx->is_mpc_3dlut_acquired[mpcc_id] = true;
1728 		ret = true;
1729 	}
1730 	return ret;
1731 }
1732 
1733 bool dcn32_release_post_bldn_3dlut(
1734 		struct resource_context *res_ctx,
1735 		const struct resource_pool *pool,
1736 		struct dc_3dlut **lut,
1737 		struct dc_transfer_func **shaper)
1738 {
1739 	int i;
1740 	bool ret = false;
1741 
1742 	for (i = 0; i < pool->res_cap->num_mpc_3dlut; i++) {
1743 		if (pool->mpc_lut[i] == *lut && pool->mpc_shaper[i] == *shaper) {
1744 			res_ctx->is_mpc_3dlut_acquired[i] = false;
1745 			pool->mpc_lut[i]->state.raw = 0;
1746 			*lut = NULL;
1747 			*shaper = NULL;
1748 			ret = true;
1749 			break;
1750 		}
1751 	}
1752 	return ret;
1753 }
1754 
1755 /**
1756  ********************************************************************************************
1757  * dcn32_get_num_free_pipes: Calculate number of free pipes
1758  *
1759  * This function assumes that a "used" pipe is a pipe that has
1760  * both a stream and a plane assigned to it.
1761  *
1762  * @param [in] dc: current dc state
1763  * @param [in] context: new dc state
1764  *
1765  * @return: Number of free pipes available in the context
1766  *
1767  ********************************************************************************************
1768  */
1769 static unsigned int dcn32_get_num_free_pipes(struct dc *dc, struct dc_state *context)
1770 {
1771 	unsigned int i;
1772 	unsigned int free_pipes = 0;
1773 	unsigned int num_pipes = 0;
1774 
1775 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1776 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1777 
1778 		if (pipe->stream && !pipe->top_pipe) {
1779 			while (pipe) {
1780 				num_pipes++;
1781 				pipe = pipe->bottom_pipe;
1782 			}
1783 		}
1784 	}
1785 
1786 	free_pipes = dc->res_pool->pipe_count - num_pipes;
1787 	return free_pipes;
1788 }
1789 
1790 /**
1791  ********************************************************************************************
1792  * dcn32_assign_subvp_pipe: Function to decide which pipe will use Sub-VP.
1793  *
1794  * We enter this function if we are Sub-VP capable (i.e. enough pipes available)
1795  * and regular P-State switching (i.e. VACTIVE/VBLANK) is not supported, or if
1796  * we are forcing SubVP P-State switching on the current config.
1797  *
1798  * The number of pipes used for the chosen surface must be less than or equal to the
1799  * number of free pipes available.
1800  *
1801  * In general we choose surfaces with the longest frame time first (better for SubVP + VBLANK).
1802  * For multi-display cases the ActiveDRAMClockChangeMargin doesn't provide enough info on its own
1803  * for determining which should be the SubVP pipe (need a way to determine if a pipe / plane doesn't
1804  * support MCLK switching naturally [i.e. ACTIVE or VBLANK]).
1805  *
1806  * @param [in] dc: current dc state
1807  * @param [in] context: new dc state
1808  * @param [out] index: dc pipe index for the pipe chosen to have phantom pipes assigned
1809  *
1810  * @return: True if a valid pipe assignment was found for Sub-VP. Otherwise false.
1811  *
1812  ********************************************************************************************
1813  */
1814 
1815 static bool dcn32_assign_subvp_pipe(struct dc *dc,
1816 		struct dc_state *context,
1817 		unsigned int *index)
1818 {
1819 	unsigned int i, pipe_idx;
1820 	unsigned int max_frame_time = 0;
1821 	bool valid_assignment_found = false;
1822 	unsigned int free_pipes = dcn32_get_num_free_pipes(dc, context);
1823 	bool current_assignment_freesync = false;
1824 
1825 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1826 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1827 		unsigned int num_pipes = 0;
1828 
1829 		if (!pipe->stream)
1830 			continue;
1831 
1832 		if (pipe->plane_state && !pipe->top_pipe &&
1833 				pipe->stream->mall_stream_config.type == SUBVP_NONE) {
1834 			while (pipe) {
1835 				num_pipes++;
1836 				pipe = pipe->bottom_pipe;
1837 			}
1838 
1839 			pipe = &context->res_ctx.pipe_ctx[i];
1840 			if (num_pipes <= free_pipes) {
1841 				struct dc_stream_state *stream = pipe->stream;
1842 				unsigned int frame_us = (stream->timing.v_total * stream->timing.h_total /
1843 						(double)(stream->timing.pix_clk_100hz * 100)) * 1000000;
1844 				if (frame_us > max_frame_time && !stream->ignore_msa_timing_param) {
1845 					*index = i;
1846 					max_frame_time = frame_us;
1847 					valid_assignment_found = true;
1848 					current_assignment_freesync = false;
1849 				/* For the 2-Freesync display case, still choose the one with the
1850 			     * longest frame time
1851 			     */
1852 				} else if (stream->ignore_msa_timing_param && (!valid_assignment_found ||
1853 						(current_assignment_freesync && frame_us > max_frame_time))) {
1854 					*index = i;
1855 					valid_assignment_found = true;
1856 					current_assignment_freesync = true;
1857 				}
1858 			}
1859 		}
1860 		pipe_idx++;
1861 	}
1862 	return valid_assignment_found;
1863 }
1864 
1865 /**
1866  * ***************************************************************************************
1867  * dcn32_enough_pipes_for_subvp: Function to check if there are "enough" pipes for SubVP.
1868  *
1869  * This function returns true if there are enough free pipes
1870  * to create the required phantom pipes for any given stream
1871  * (that does not already have phantom pipe assigned).
1872  *
1873  * e.g. For a 2 stream config where the first stream uses one
1874  * pipe and the second stream uses 2 pipes (i.e. pipe split),
1875  * this function will return true because there is 1 remaining
1876  * pipe which can be used as the phantom pipe for the non pipe
1877  * split pipe.
1878  *
1879  * @param [in] dc: current dc state
1880  * @param [in] context: new dc state
1881  *
1882  * @return: True if there are enough free pipes to assign phantom pipes to at least one
1883  *          stream that does not already have phantom pipes assigned. Otherwise false.
1884  *
1885  * ***************************************************************************************
1886  */
1887 static bool dcn32_enough_pipes_for_subvp(struct dc *dc, struct dc_state *context)
1888 {
1889 	unsigned int i, split_cnt, free_pipes;
1890 	unsigned int min_pipe_split = dc->res_pool->pipe_count + 1; // init as max number of pipes + 1
1891 	bool subvp_possible = false;
1892 
1893 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1894 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1895 
1896 		// Find the minimum pipe split count for non SubVP pipes
1897 		if (pipe->stream && !pipe->top_pipe &&
1898 				pipe->stream->mall_stream_config.type == SUBVP_NONE) {
1899 			split_cnt = 0;
1900 			while (pipe) {
1901 				split_cnt++;
1902 				pipe = pipe->bottom_pipe;
1903 			}
1904 
1905 			if (split_cnt < min_pipe_split)
1906 				min_pipe_split = split_cnt;
1907 		}
1908 	}
1909 
1910 	free_pipes = dcn32_get_num_free_pipes(dc, context);
1911 
1912 	// SubVP only possible if at least one pipe is being used (i.e. free_pipes
1913 	// should not equal to the pipe_count)
1914 	if (free_pipes >= min_pipe_split && free_pipes < dc->res_pool->pipe_count)
1915 		subvp_possible = true;
1916 
1917 	return subvp_possible;
1918 }
1919 
1920 static void dcn32_enable_phantom_plane(struct dc *dc,
1921 		struct dc_state *context,
1922 		struct dc_stream_state *phantom_stream,
1923 		unsigned int dc_pipe_idx)
1924 {
1925 	struct dc_plane_state *phantom_plane = NULL;
1926 	struct dc_plane_state *prev_phantom_plane = NULL;
1927 	struct pipe_ctx *curr_pipe = &context->res_ctx.pipe_ctx[dc_pipe_idx];
1928 
1929 	while (curr_pipe) {
1930 		if (curr_pipe->top_pipe && curr_pipe->top_pipe->plane_state == curr_pipe->plane_state)
1931 			phantom_plane = prev_phantom_plane;
1932 		else
1933 			phantom_plane = dc_create_plane_state(dc);
1934 
1935 		memcpy(&phantom_plane->address, &curr_pipe->plane_state->address, sizeof(phantom_plane->address));
1936 		memcpy(&phantom_plane->scaling_quality, &curr_pipe->plane_state->scaling_quality,
1937 				sizeof(phantom_plane->scaling_quality));
1938 		memcpy(&phantom_plane->src_rect, &curr_pipe->plane_state->src_rect, sizeof(phantom_plane->src_rect));
1939 		memcpy(&phantom_plane->dst_rect, &curr_pipe->plane_state->dst_rect, sizeof(phantom_plane->dst_rect));
1940 		memcpy(&phantom_plane->clip_rect, &curr_pipe->plane_state->clip_rect, sizeof(phantom_plane->clip_rect));
1941 		memcpy(&phantom_plane->plane_size, &curr_pipe->plane_state->plane_size,
1942 				sizeof(phantom_plane->plane_size));
1943 		memcpy(&phantom_plane->tiling_info, &curr_pipe->plane_state->tiling_info,
1944 				sizeof(phantom_plane->tiling_info));
1945 		memcpy(&phantom_plane->dcc, &curr_pipe->plane_state->dcc, sizeof(phantom_plane->dcc));
1946 		phantom_plane->format = curr_pipe->plane_state->format;
1947 		phantom_plane->rotation = curr_pipe->plane_state->rotation;
1948 		phantom_plane->visible = curr_pipe->plane_state->visible;
1949 
1950 		/* Shadow pipe has small viewport. */
1951 		phantom_plane->clip_rect.y = 0;
1952 		phantom_plane->clip_rect.height = phantom_stream->timing.v_addressable;
1953 
1954 		dc_add_plane_to_context(dc, phantom_stream, phantom_plane, context);
1955 
1956 		curr_pipe = curr_pipe->bottom_pipe;
1957 		prev_phantom_plane = phantom_plane;
1958 	}
1959 }
1960 
1961 /**
1962  * ***************************************************************************************
1963  * dcn32_set_phantom_stream_timing: Set timing params for the phantom stream
1964  *
1965  * Set timing params of the phantom stream based on calculated output from DML.
1966  * This function first gets the DML pipe index using the DC pipe index, then
1967  * calls into DML (get_subviewport_lines_needed_in_mall) to get the number of
1968  * lines required for SubVP MCLK switching and assigns to the phantom stream
1969  * accordingly.
1970  *
1971  * - The number of SubVP lines calculated in DML does not take into account
1972  * FW processing delays and required pstate allow width, so we must include
1973  * that separately.
1974  *
1975  * - Set phantom backporch = vstartup of main pipe
1976  *
1977  * @param [in] dc: current dc state
1978  * @param [in] context: new dc state
1979  * @param [in] ref_pipe: Main pipe for the phantom stream
1980  * @param [in] pipes: DML pipe params
1981  * @param [in] pipe_cnt: number of DML pipes
1982  * @param [in] dc_pipe_idx: DC pipe index for the main pipe (i.e. ref_pipe)
1983  *
1984  * @return: void
1985  *
1986  * ***************************************************************************************
1987  */
1988 static void dcn32_set_phantom_stream_timing(struct dc *dc,
1989 		struct dc_state *context,
1990 		struct pipe_ctx *ref_pipe,
1991 		struct dc_stream_state *phantom_stream,
1992 		display_e2e_pipe_params_st *pipes,
1993 		unsigned int pipe_cnt,
1994 		unsigned int dc_pipe_idx)
1995 {
1996 	unsigned int i, pipe_idx;
1997 	struct pipe_ctx *pipe;
1998 	uint32_t phantom_vactive, phantom_bp, pstate_width_fw_delay_lines;
1999 	unsigned int vlevel = context->bw_ctx.dml.vba.VoltageLevel;
2000 	unsigned int dcfclk = context->bw_ctx.dml.vba.DCFCLKState[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
2001 	unsigned int socclk = context->bw_ctx.dml.vba.SOCCLKPerState[vlevel];
2002 
2003 	// Find DML pipe index (pipe_idx) using dc_pipe_idx
2004 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2005 		pipe = &context->res_ctx.pipe_ctx[i];
2006 
2007 		if (!pipe->stream)
2008 			continue;
2009 
2010 		if (i == dc_pipe_idx)
2011 			break;
2012 
2013 		pipe_idx++;
2014 	}
2015 
2016 	// Calculate lines required for pstate allow width and FW processing delays
2017 	pstate_width_fw_delay_lines = ((double)(dc->caps.subvp_fw_processing_delay_us +
2018 			dc->caps.subvp_pstate_allow_width_us) / 1000000) *
2019 			(ref_pipe->stream->timing.pix_clk_100hz * 100) /
2020 			(double)ref_pipe->stream->timing.h_total;
2021 
2022 	// Update clks_cfg for calling into recalculate
2023 	pipes[0].clks_cfg.voltage = vlevel;
2024 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
2025 	pipes[0].clks_cfg.socclk_mhz = socclk;
2026 
2027 	// DML calculation for MALL region doesn't take into account FW delay
2028 	// and required pstate allow width for multi-display cases
2029 	phantom_vactive = get_subviewport_lines_needed_in_mall(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx) +
2030 				pstate_width_fw_delay_lines;
2031 
2032 	// For backporch of phantom pipe, use vstartup of the main pipe
2033 	phantom_bp = get_vstartup(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
2034 
2035 	phantom_stream->dst.y = 0;
2036 	phantom_stream->dst.height = phantom_vactive;
2037 	phantom_stream->src.y = 0;
2038 	phantom_stream->src.height = phantom_vactive;
2039 
2040 	phantom_stream->timing.v_addressable = phantom_vactive;
2041 	phantom_stream->timing.v_front_porch = 1;
2042 	phantom_stream->timing.v_total = phantom_stream->timing.v_addressable +
2043 						phantom_stream->timing.v_front_porch +
2044 						phantom_stream->timing.v_sync_width +
2045 						phantom_bp;
2046 }
2047 
2048 static struct dc_stream_state *dcn32_enable_phantom_stream(struct dc *dc,
2049 		struct dc_state *context,
2050 		display_e2e_pipe_params_st *pipes,
2051 		unsigned int pipe_cnt,
2052 		unsigned int dc_pipe_idx)
2053 {
2054 	struct dc_stream_state *phantom_stream = NULL;
2055 	struct pipe_ctx *ref_pipe = &context->res_ctx.pipe_ctx[dc_pipe_idx];
2056 
2057 	phantom_stream = dc_create_stream_for_sink(ref_pipe->stream->sink);
2058 	phantom_stream->signal = SIGNAL_TYPE_VIRTUAL;
2059 	phantom_stream->dpms_off = true;
2060 	phantom_stream->mall_stream_config.type = SUBVP_PHANTOM;
2061 	phantom_stream->mall_stream_config.paired_stream = ref_pipe->stream;
2062 	ref_pipe->stream->mall_stream_config.type = SUBVP_MAIN;
2063 	ref_pipe->stream->mall_stream_config.paired_stream = phantom_stream;
2064 
2065 	/* stream has limited viewport and small timing */
2066 	memcpy(&phantom_stream->timing, &ref_pipe->stream->timing, sizeof(phantom_stream->timing));
2067 	memcpy(&phantom_stream->src, &ref_pipe->stream->src, sizeof(phantom_stream->src));
2068 	memcpy(&phantom_stream->dst, &ref_pipe->stream->dst, sizeof(phantom_stream->dst));
2069 	dcn32_set_phantom_stream_timing(dc, context, ref_pipe, phantom_stream, pipes, pipe_cnt, dc_pipe_idx);
2070 
2071 	dc_add_stream_to_ctx(dc, context, phantom_stream);
2072 	return phantom_stream;
2073 }
2074 
2075 // return true if removed piped from ctx, false otherwise
2076 bool dcn32_remove_phantom_pipes(struct dc *dc, struct dc_state *context)
2077 {
2078 	int i;
2079 	bool removed_pipe = false;
2080 
2081 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2082 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2083 		// build scaling params for phantom pipes
2084 		if (pipe->plane_state && pipe->stream && pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
2085 			dc_rem_all_planes_for_stream(dc, pipe->stream, context);
2086 			dc_remove_stream_from_ctx(dc, context, pipe->stream);
2087 			removed_pipe = true;
2088 		}
2089 
2090 		// Clear all phantom stream info
2091 		if (pipe->stream) {
2092 			pipe->stream->mall_stream_config.type = SUBVP_NONE;
2093 			pipe->stream->mall_stream_config.paired_stream = NULL;
2094 		}
2095 	}
2096 	return removed_pipe;
2097 }
2098 
2099 /* TODO: Input to this function should indicate which pipe indexes (or streams)
2100  * require a phantom pipe / stream
2101  */
2102 void dcn32_add_phantom_pipes(struct dc *dc, struct dc_state *context,
2103 		display_e2e_pipe_params_st *pipes,
2104 		unsigned int pipe_cnt,
2105 		unsigned int index)
2106 {
2107 	struct dc_stream_state *phantom_stream = NULL;
2108 	unsigned int i;
2109 
2110 	// The index of the DC pipe passed into this function is guarenteed to
2111 	// be a valid candidate for SubVP (i.e. has a plane, stream, doesn't
2112 	// already have phantom pipe assigned, etc.) by previous checks.
2113 	phantom_stream = dcn32_enable_phantom_stream(dc, context, pipes, pipe_cnt, index);
2114 	dcn32_enable_phantom_plane(dc, context, phantom_stream, index);
2115 
2116 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2117 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2118 
2119 		// Build scaling params for phantom pipes which were newly added.
2120 		// We determine which phantom pipes were added by comparing with
2121 		// the phantom stream.
2122 		if (pipe->plane_state && pipe->stream && pipe->stream == phantom_stream &&
2123 				pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
2124 			pipe->stream->use_dynamic_meta = false;
2125 			pipe->plane_state->flip_immediate = false;
2126 			if (!resource_build_scaling_params(pipe)) {
2127 				// Log / remove phantom pipes since failed to build scaling params
2128 			}
2129 		}
2130 	}
2131 }
2132 
2133 static bool dcn32_split_stream_for_mpc_or_odm(
2134 		const struct dc *dc,
2135 		struct resource_context *res_ctx,
2136 		struct pipe_ctx *pri_pipe,
2137 		struct pipe_ctx *sec_pipe,
2138 		bool odm)
2139 {
2140 	int pipe_idx = sec_pipe->pipe_idx;
2141 	const struct resource_pool *pool = dc->res_pool;
2142 
2143 	if (pri_pipe->plane_state) {
2144 		/* ODM + window MPO, where MPO window is on left half only */
2145 		if (pri_pipe->plane_state->clip_rect.x + pri_pipe->plane_state->clip_rect.width <=
2146 				pri_pipe->stream->src.x + pri_pipe->stream->src.width/2)
2147 			return true;
2148 
2149 		/* ODM + window MPO, where MPO window is on right half only */
2150 		if (pri_pipe->plane_state->clip_rect.x >= pri_pipe->stream->src.width/2)
2151 			return true;
2152 	}
2153 
2154 	*sec_pipe = *pri_pipe;
2155 
2156 	sec_pipe->pipe_idx = pipe_idx;
2157 	sec_pipe->plane_res.mi = pool->mis[pipe_idx];
2158 	sec_pipe->plane_res.hubp = pool->hubps[pipe_idx];
2159 	sec_pipe->plane_res.ipp = pool->ipps[pipe_idx];
2160 	sec_pipe->plane_res.xfm = pool->transforms[pipe_idx];
2161 	sec_pipe->plane_res.dpp = pool->dpps[pipe_idx];
2162 	sec_pipe->plane_res.mpcc_inst = pool->dpps[pipe_idx]->inst;
2163 	sec_pipe->stream_res.dsc = NULL;
2164 	if (odm) {
2165 		if (pri_pipe->next_odm_pipe) {
2166 			ASSERT(pri_pipe->next_odm_pipe != sec_pipe);
2167 			sec_pipe->next_odm_pipe = pri_pipe->next_odm_pipe;
2168 			sec_pipe->next_odm_pipe->prev_odm_pipe = sec_pipe;
2169 		}
2170 		if (pri_pipe->top_pipe && pri_pipe->top_pipe->next_odm_pipe) {
2171 			pri_pipe->top_pipe->next_odm_pipe->bottom_pipe = sec_pipe;
2172 			sec_pipe->top_pipe = pri_pipe->top_pipe->next_odm_pipe;
2173 		}
2174 		if (pri_pipe->bottom_pipe && pri_pipe->bottom_pipe->next_odm_pipe) {
2175 			pri_pipe->bottom_pipe->next_odm_pipe->top_pipe = sec_pipe;
2176 			sec_pipe->bottom_pipe = pri_pipe->bottom_pipe->next_odm_pipe;
2177 		}
2178 		pri_pipe->next_odm_pipe = sec_pipe;
2179 		sec_pipe->prev_odm_pipe = pri_pipe;
2180 		ASSERT(sec_pipe->top_pipe == NULL);
2181 
2182 		if (!sec_pipe->top_pipe)
2183 			sec_pipe->stream_res.opp = pool->opps[pipe_idx];
2184 		else
2185 			sec_pipe->stream_res.opp = sec_pipe->top_pipe->stream_res.opp;
2186 		if (sec_pipe->stream->timing.flags.DSC == 1) {
2187 			dcn20_acquire_dsc(dc, res_ctx, &sec_pipe->stream_res.dsc, pipe_idx);
2188 			ASSERT(sec_pipe->stream_res.dsc);
2189 			if (sec_pipe->stream_res.dsc == NULL)
2190 				return false;
2191 		}
2192 	} else {
2193 		if (pri_pipe->bottom_pipe) {
2194 			ASSERT(pri_pipe->bottom_pipe != sec_pipe);
2195 			sec_pipe->bottom_pipe = pri_pipe->bottom_pipe;
2196 			sec_pipe->bottom_pipe->top_pipe = sec_pipe;
2197 		}
2198 		pri_pipe->bottom_pipe = sec_pipe;
2199 		sec_pipe->top_pipe = pri_pipe;
2200 
2201 		ASSERT(pri_pipe->plane_state);
2202 	}
2203 
2204 	return true;
2205 }
2206 
2207 static struct pipe_ctx *dcn32_find_split_pipe(
2208 		struct dc *dc,
2209 		struct dc_state *context,
2210 		int old_index)
2211 {
2212 	struct pipe_ctx *pipe = NULL;
2213 	int i;
2214 
2215 	if (old_index >= 0 && context->res_ctx.pipe_ctx[old_index].stream == NULL) {
2216 		pipe = &context->res_ctx.pipe_ctx[old_index];
2217 		pipe->pipe_idx = old_index;
2218 	}
2219 
2220 	if (!pipe)
2221 		for (i = dc->res_pool->pipe_count - 1; i >= 0; i--) {
2222 			if (dc->current_state->res_ctx.pipe_ctx[i].top_pipe == NULL
2223 					&& dc->current_state->res_ctx.pipe_ctx[i].prev_odm_pipe == NULL) {
2224 				if (context->res_ctx.pipe_ctx[i].stream == NULL) {
2225 					pipe = &context->res_ctx.pipe_ctx[i];
2226 					pipe->pipe_idx = i;
2227 					break;
2228 				}
2229 			}
2230 		}
2231 
2232 	/*
2233 	 * May need to fix pipes getting tossed from 1 opp to another on flip
2234 	 * Add for debugging transient underflow during topology updates:
2235 	 * ASSERT(pipe);
2236 	 */
2237 	if (!pipe)
2238 		for (i = dc->res_pool->pipe_count - 1; i >= 0; i--) {
2239 			if (context->res_ctx.pipe_ctx[i].stream == NULL) {
2240 				pipe = &context->res_ctx.pipe_ctx[i];
2241 				pipe->pipe_idx = i;
2242 				break;
2243 			}
2244 		}
2245 
2246 	return pipe;
2247 }
2248 
2249 
2250 /**
2251  * ***************************************************************************************
2252  * subvp_subvp_schedulable: Determine if SubVP + SubVP config is schedulable
2253  *
2254  * High level algorithm:
2255  * 1. Find longest microschedule length (in us) between the two SubVP pipes
2256  * 2. Check if the worst case overlap (VBLANK in middle of ACTIVE) for both
2257  * pipes still allows for the maximum microschedule to fit in the active
2258  * region for both pipes.
2259  *
2260  * @param [in] dc: current dc state
2261  * @param [in] context: new dc state
2262  *
2263  * @return: bool - True if the SubVP + SubVP config is schedulable, false otherwise
2264  *
2265  * ***************************************************************************************
2266  */
2267 static bool subvp_subvp_schedulable(struct dc *dc, struct dc_state *context)
2268 {
2269 	struct pipe_ctx *subvp_pipes[2];
2270 	struct dc_stream_state *phantom = NULL;
2271 	uint32_t microschedule_lines = 0;
2272 	uint32_t index = 0;
2273 	uint32_t i;
2274 	uint32_t max_microschedule_us = 0;
2275 	int32_t vactive1_us, vactive2_us, vblank1_us, vblank2_us;
2276 
2277 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2278 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2279 		uint32_t time_us = 0;
2280 
2281 		/* Loop to calculate the maximum microschedule time between the two SubVP pipes,
2282 		 * and also to store the two main SubVP pipe pointers in subvp_pipes[2].
2283 		 */
2284 		if (pipe->stream && pipe->plane_state && !pipe->top_pipe &&
2285 				pipe->stream->mall_stream_config.type == SUBVP_MAIN) {
2286 			phantom = pipe->stream->mall_stream_config.paired_stream;
2287 			microschedule_lines = (phantom->timing.v_total - phantom->timing.v_front_porch) +
2288 					phantom->timing.v_addressable;
2289 
2290 			// Round up when calculating microschedule time (+ 1 at the end)
2291 			time_us = (microschedule_lines * phantom->timing.h_total) /
2292 					(double)(phantom->timing.pix_clk_100hz * 100) * 1000000 +
2293 						dc->caps.subvp_prefetch_end_to_mall_start_us +
2294 						dc->caps.subvp_fw_processing_delay_us + 1;
2295 			if (time_us > max_microschedule_us)
2296 				max_microschedule_us = time_us;
2297 
2298 			subvp_pipes[index] = pipe;
2299 			index++;
2300 
2301 			// Maximum 2 SubVP pipes
2302 			if (index == 2)
2303 				break;
2304 		}
2305 	}
2306 	vactive1_us = ((subvp_pipes[0]->stream->timing.v_addressable * subvp_pipes[0]->stream->timing.h_total) /
2307 			(double)(subvp_pipes[0]->stream->timing.pix_clk_100hz * 100)) * 1000000;
2308 	vactive2_us = ((subvp_pipes[1]->stream->timing.v_addressable * subvp_pipes[1]->stream->timing.h_total) /
2309 				(double)(subvp_pipes[1]->stream->timing.pix_clk_100hz * 100)) * 1000000;
2310 	vblank1_us = (((subvp_pipes[0]->stream->timing.v_total - subvp_pipes[0]->stream->timing.v_addressable) *
2311 			subvp_pipes[0]->stream->timing.h_total) /
2312 			(double)(subvp_pipes[0]->stream->timing.pix_clk_100hz * 100)) * 1000000;
2313 	vblank2_us = (((subvp_pipes[1]->stream->timing.v_total - subvp_pipes[1]->stream->timing.v_addressable) *
2314 			subvp_pipes[1]->stream->timing.h_total) /
2315 			(double)(subvp_pipes[1]->stream->timing.pix_clk_100hz * 100)) * 1000000;
2316 
2317 	if ((vactive1_us - vblank2_us) / 2 > max_microschedule_us &&
2318 			(vactive2_us - vblank1_us) / 2 > max_microschedule_us)
2319 		return true;
2320 
2321 	return false;
2322 }
2323 
2324 /**
2325  * ***************************************************************************************
2326  * subvp_drr_schedulable: Determine if SubVP + DRR config is schedulable
2327  *
2328  * High level algorithm:
2329  * 1. Get timing for SubVP pipe, phantom pipe, and DRR pipe
2330  * 2. Determine the frame time for the DRR display when adding required margin for MCLK switching
2331  * (the margin is equal to the MALL region + DRR margin (500us))
2332  * 3.If (SubVP Active - Prefetch > Stretched DRR frame + max(MALL region, Stretched DRR frame))
2333  * then report the configuration as supported
2334  *
2335  * @param [in] dc: current dc state
2336  * @param [in] context: new dc state
2337  * @param [in] drr_pipe: DRR pipe_ctx for the SubVP + DRR config
2338  *
2339  * @return: bool - True if the SubVP + DRR config is schedulable, false otherwise
2340  *
2341  * ***************************************************************************************
2342  */
2343 static bool subvp_drr_schedulable(struct dc *dc, struct dc_state *context, struct pipe_ctx *drr_pipe)
2344 {
2345 	bool schedulable = false;
2346 	uint32_t i;
2347 	struct pipe_ctx *pipe = NULL;
2348 	struct dc_crtc_timing *main_timing = NULL;
2349 	struct dc_crtc_timing *phantom_timing = NULL;
2350 	struct dc_crtc_timing *drr_timing = NULL;
2351 	int16_t prefetch_us = 0;
2352 	int16_t mall_region_us = 0;
2353 	int16_t drr_frame_us = 0;	// nominal frame time
2354 	int16_t subvp_active_us = 0;
2355 	int16_t stretched_drr_us = 0;
2356 	int16_t drr_stretched_vblank_us = 0;
2357 	int16_t max_vblank_mallregion = 0;
2358 
2359 	// Find SubVP pipe
2360 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2361 		pipe = &context->res_ctx.pipe_ctx[i];
2362 
2363 		// We check for master pipe, but it shouldn't matter since we only need
2364 		// the pipe for timing info (stream should be same for any pipe splits)
2365 		if (!pipe->stream || !pipe->plane_state || pipe->top_pipe || pipe->prev_odm_pipe)
2366 			continue;
2367 
2368 		// Find the SubVP pipe
2369 		if (pipe->stream->mall_stream_config.type == SUBVP_MAIN)
2370 			break;
2371 	}
2372 
2373 	main_timing = &pipe->stream->timing;
2374 	phantom_timing = &pipe->stream->mall_stream_config.paired_stream->timing;
2375 	drr_timing = &drr_pipe->stream->timing;
2376 	prefetch_us = (phantom_timing->v_total - phantom_timing->v_front_porch) * phantom_timing->h_total /
2377 			(double)(phantom_timing->pix_clk_100hz * 100) * 1000000 +
2378 			dc->caps.subvp_prefetch_end_to_mall_start_us;
2379 	subvp_active_us = main_timing->v_addressable * main_timing->h_total /
2380 			(double)(main_timing->pix_clk_100hz * 100) * 1000000;
2381 	drr_frame_us = drr_timing->v_total * drr_timing->h_total /
2382 			(double)(drr_timing->pix_clk_100hz * 100) * 1000000;
2383 	// P-State allow width and FW delays already included phantom_timing->v_addressable
2384 	mall_region_us = phantom_timing->v_addressable * phantom_timing->h_total /
2385 			(double)(phantom_timing->pix_clk_100hz * 100) * 1000000;
2386 	stretched_drr_us = drr_frame_us + mall_region_us + SUBVP_DRR_MARGIN_US;
2387 	drr_stretched_vblank_us = (drr_timing->v_total - drr_timing->v_addressable) * drr_timing->h_total /
2388 			(double)(drr_timing->pix_clk_100hz * 100) * 1000000 + (stretched_drr_us - drr_frame_us);
2389 	max_vblank_mallregion = drr_stretched_vblank_us > mall_region_us ? drr_stretched_vblank_us : mall_region_us;
2390 
2391 	/* We consider SubVP + DRR schedulable if the stretched frame duration of the DRR display (i.e. the
2392 	 * highest refresh rate + margin that can support UCLK P-State switch) passes the static analysis
2393 	 * for VBLANK: (VACTIVE region of the SubVP pipe can fit the MALL prefetch, VBLANK frame time,
2394 	 * and the max of (VBLANK blanking time, MALL region)).
2395 	 */
2396 	if (stretched_drr_us < (1 / (double)drr_timing->min_refresh_in_uhz) * 1000000 * 1000000 &&
2397 			subvp_active_us - prefetch_us - stretched_drr_us - max_vblank_mallregion > 0)
2398 		schedulable = true;
2399 
2400 	return schedulable;
2401 }
2402 
2403 /**
2404  * ***************************************************************************************
2405  * subvp_vblank_schedulable: Determine if SubVP + VBLANK config is schedulable
2406  *
2407  * High level algorithm:
2408  * 1. Get timing for SubVP pipe, phantom pipe, and VBLANK pipe
2409  * 2. If (SubVP Active - Prefetch > Vblank Frame Time + max(MALL region, Vblank blanking time))
2410  * then report the configuration as supported
2411  * 3. If the VBLANK display is DRR, then take the DRR static schedulability path
2412  *
2413  * @param [in] dc: current dc state
2414  * @param [in] context: new dc state
2415  *
2416  * @return: bool - True if the SubVP + VBLANK/DRR config is schedulable, false otherwise
2417  *
2418  * ***************************************************************************************
2419  */
2420 static bool subvp_vblank_schedulable(struct dc *dc, struct dc_state *context)
2421 {
2422 	struct pipe_ctx *pipe = NULL;
2423 	struct pipe_ctx *subvp_pipe = NULL;
2424 	bool found = false;
2425 	bool schedulable = false;
2426 	uint32_t i = 0;
2427 	uint8_t vblank_index = 0;
2428 	uint16_t prefetch_us = 0;
2429 	uint16_t mall_region_us = 0;
2430 	uint16_t vblank_frame_us = 0;
2431 	uint16_t subvp_active_us = 0;
2432 	uint16_t vblank_blank_us = 0;
2433 	uint16_t max_vblank_mallregion = 0;
2434 	struct dc_crtc_timing *main_timing = NULL;
2435 	struct dc_crtc_timing *phantom_timing = NULL;
2436 	struct dc_crtc_timing *vblank_timing = NULL;
2437 
2438 	/* For SubVP + VBLANK/DRR cases, we assume there can only be
2439 	 * a single VBLANK/DRR display. If DML outputs SubVP + VBLANK
2440 	 * is supported, it is either a single VBLANK case or two VBLANK
2441 	 * displays which are synchronized (in which case they have identical
2442 	 * timings).
2443 	 */
2444 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2445 		pipe = &context->res_ctx.pipe_ctx[i];
2446 
2447 		// We check for master pipe, but it shouldn't matter since we only need
2448 		// the pipe for timing info (stream should be same for any pipe splits)
2449 		if (!pipe->stream || !pipe->plane_state || pipe->top_pipe || pipe->prev_odm_pipe)
2450 			continue;
2451 
2452 		if (!found && pipe->stream->mall_stream_config.type == SUBVP_NONE) {
2453 			// Found pipe which is not SubVP or Phantom (i.e. the VBLANK pipe).
2454 			vblank_index = i;
2455 			found = true;
2456 		}
2457 
2458 		if (!subvp_pipe && pipe->stream->mall_stream_config.type == SUBVP_MAIN)
2459 			subvp_pipe = pipe;
2460 	}
2461 	// Use ignore_msa_timing_param flag to identify as DRR
2462 	if (found && context->res_ctx.pipe_ctx[vblank_index].stream->ignore_msa_timing_param) {
2463 		// SUBVP + DRR case
2464 		schedulable = subvp_drr_schedulable(dc, context, &context->res_ctx.pipe_ctx[vblank_index]);
2465 	} else if (found) {
2466 		main_timing = &subvp_pipe->stream->timing;
2467 		phantom_timing = &subvp_pipe->stream->mall_stream_config.paired_stream->timing;
2468 		vblank_timing = &context->res_ctx.pipe_ctx[vblank_index].stream->timing;
2469 		// Prefetch time is equal to VACTIVE + BP + VSYNC of the phantom pipe
2470 		// Also include the prefetch end to mallstart delay time
2471 		prefetch_us = (phantom_timing->v_total - phantom_timing->v_front_porch) * phantom_timing->h_total /
2472 				(double)(phantom_timing->pix_clk_100hz * 100) * 1000000 +
2473 				dc->caps.subvp_prefetch_end_to_mall_start_us;
2474 		// P-State allow width and FW delays already included phantom_timing->v_addressable
2475 		mall_region_us = phantom_timing->v_addressable * phantom_timing->h_total /
2476 				(double)(phantom_timing->pix_clk_100hz * 100) * 1000000;
2477 		vblank_frame_us = vblank_timing->v_total * vblank_timing->h_total /
2478 				(double)(vblank_timing->pix_clk_100hz * 100) * 1000000;
2479 		vblank_blank_us =  (vblank_timing->v_total - vblank_timing->v_addressable) * vblank_timing->h_total /
2480 				(double)(vblank_timing->pix_clk_100hz * 100) * 1000000;
2481 		subvp_active_us = main_timing->v_addressable * main_timing->h_total /
2482 				(double)(main_timing->pix_clk_100hz * 100) * 1000000;
2483 		max_vblank_mallregion = vblank_blank_us > mall_region_us ? vblank_blank_us : mall_region_us;
2484 
2485 		// Schedulable if VACTIVE region of the SubVP pipe can fit the MALL prefetch, VBLANK frame time,
2486 		// and the max of (VBLANK blanking time, MALL region)
2487 		// TODO: Possibly add some margin (i.e. the below conditions should be [...] > X instead of [...] > 0)
2488 		if (subvp_active_us - prefetch_us - vblank_frame_us - max_vblank_mallregion > 0)
2489 			schedulable = true;
2490 	}
2491 	return schedulable;
2492 }
2493 
2494 /**
2495  * ********************************************************************************************
2496  * subvp_validate_static_schedulability: Check which SubVP case is calculated and handle
2497  * static analysis based on the case.
2498  *
2499  * Three cases:
2500  * 1. SubVP + SubVP
2501  * 2. SubVP + VBLANK (DRR checked internally)
2502  * 3. SubVP + VACTIVE (currently unsupported)
2503  *
2504  * @param [in] dc: current dc state
2505  * @param [in] context: new dc state
2506  * @param [in] vlevel: Voltage level calculated by DML
2507  *
2508  * @return: bool - True if statically schedulable, false otherwise
2509  *
2510  * ********************************************************************************************
2511  */
2512 static bool subvp_validate_static_schedulability(struct dc *dc,
2513 				struct dc_state *context,
2514 				int vlevel)
2515 {
2516 	bool schedulable = true;	// true by default for single display case
2517 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
2518 	uint32_t i, pipe_idx;
2519 	uint8_t subvp_count = 0;
2520 	uint8_t vactive_count = 0;
2521 
2522 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2523 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2524 
2525 		if (!pipe->stream)
2526 			continue;
2527 
2528 		if (pipe->plane_state && !pipe->top_pipe &&
2529 				pipe->stream->mall_stream_config.type == SUBVP_MAIN)
2530 			subvp_count++;
2531 
2532 		// Count how many planes are capable of VACTIVE switching (SubVP + VACTIVE unsupported)
2533 		if (vba->ActiveDRAMClockChangeLatencyMargin[vba->pipe_plane[pipe_idx]] > 0) {
2534 			vactive_count++;
2535 		}
2536 		pipe_idx++;
2537 	}
2538 
2539 	if (subvp_count == 2) {
2540 		// Static schedulability check for SubVP + SubVP case
2541 		schedulable = subvp_subvp_schedulable(dc, context);
2542 	} else if (vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_vblank_w_mall_sub_vp) {
2543 		// Static schedulability check for SubVP + VBLANK case. Also handle the case where
2544 		// DML outputs SubVP + VBLANK + VACTIVE (DML will report as SubVP + VBLANK)
2545 		if (vactive_count > 0)
2546 			schedulable = false;
2547 		else
2548 			schedulable = subvp_vblank_schedulable(dc, context);
2549 	} else if (vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_vactive_w_mall_sub_vp) {
2550 		// SubVP + VACTIVE currently unsupported
2551 		schedulable = false;
2552 	}
2553 	return schedulable;
2554 }
2555 
2556 static void dcn32_full_validate_bw_helper(struct dc *dc,
2557 		struct dc_state *context,
2558 		display_e2e_pipe_params_st *pipes,
2559 		int *vlevel,
2560 		int *split,
2561 		bool *merge,
2562 		int *pipe_cnt)
2563 {
2564 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
2565 	unsigned int dc_pipe_idx = 0;
2566 	bool found_supported_config = false;
2567 	struct pipe_ctx *pipe = NULL;
2568 	uint32_t non_subvp_pipes = 0;
2569 	bool drr_pipe_found = false;
2570 	uint32_t drr_pipe_index = 0;
2571 	uint32_t i = 0;
2572 
2573 	/*
2574 	 * DML favors voltage over p-state, but we're more interested in
2575 	 * supporting p-state over voltage. We can't support p-state in
2576 	 * prefetch mode > 0 so try capping the prefetch mode to start.
2577 	 */
2578 	context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
2579 			dm_prefetch_support_uclk_fclk_and_stutter;
2580 	*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
2581 	/* This may adjust vlevel and maxMpcComb */
2582 	if (*vlevel < context->bw_ctx.dml.soc.num_states)
2583 		*vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, *vlevel, split, merge);
2584 
2585 	/* Conditions for setting up phantom pipes for SubVP:
2586 	 * 1. Not force disable SubVP
2587 	 * 2. Full update (i.e. !fast_validate)
2588 	 * 3. Enough pipes are available to support SubVP (TODO: Which pipes will use VACTIVE / VBLANK / SUBVP?)
2589 	 * 4. Display configuration passes validation
2590 	 * 5. (Config doesn't support MCLK in VACTIVE/VBLANK || dc->debug.force_subvp_mclk_switch)
2591 	 */
2592 	if (!dc->debug.force_disable_subvp && dcn32_all_pipes_have_stream_and_plane(dc, context) &&
2593 			(*vlevel == context->bw_ctx.dml.soc.num_states ||
2594 			vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported ||
2595 			dc->debug.force_subvp_mclk_switch)) {
2596 
2597 		dcn32_merge_pipes_for_subvp(dc, context);
2598 
2599 		while (!found_supported_config && dcn32_enough_pipes_for_subvp(dc, context) &&
2600 				dcn32_assign_subvp_pipe(dc, context, &dc_pipe_idx)) {
2601 
2602 			/* For the case where *vlevel = num_states, bandwidth validation has failed for this config.
2603 			 * Adding phantom pipes won't change the validation result, so change the DML input param
2604 			 * for P-State support before adding phantom pipes and recalculating the DML result.
2605 			 * However, this case is only applicable for SubVP + DRR cases because the prefetch mode
2606 			 * will not allow for switch in VBLANK. The DRR display must have it's VBLANK stretched
2607 			 * enough to support support MCLK switching.
2608 			 */
2609 			if (*vlevel == context->bw_ctx.dml.soc.num_states) {
2610 				context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
2611 								dm_prefetch_support_stutter;
2612 				/* There are params (such as FabricClock) that need to be recalculated
2613 				 * after validation fails (otherwise it will be 0). Calculation for
2614 				 * phantom vactive requires call into DML, so we must ensure all the
2615 				 * vba params are valid otherwise we'll get incorrect phantom vactive.
2616 				 */
2617 				*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
2618 			}
2619 
2620 			dc->res_pool->funcs->add_phantom_pipes(dc, context, pipes, *pipe_cnt, dc_pipe_idx);
2621 
2622 			*pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, false);
2623 			*vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, *pipe_cnt);
2624 
2625 			if (*vlevel < context->bw_ctx.dml.soc.num_states &&
2626 					vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] != dm_dram_clock_change_unsupported
2627 					&& subvp_validate_static_schedulability(dc, context, *vlevel)) {
2628 				found_supported_config = true;
2629 			} else if (*vlevel < context->bw_ctx.dml.soc.num_states &&
2630 					vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported) {
2631 				/* Case where 1 SubVP is added, and DML reports MCLK unsupported. This handles
2632 				 * the case for SubVP + DRR, where the DRR display does not support MCLK switch
2633 				 * at it's native refresh rate / timing.
2634 				 */
2635 				for (i = 0; i < dc->res_pool->pipe_count; i++) {
2636 					pipe = &context->res_ctx.pipe_ctx[i];
2637 					if (pipe->stream && pipe->plane_state && !pipe->top_pipe &&
2638 							pipe->stream->mall_stream_config.type == SUBVP_NONE) {
2639 						non_subvp_pipes++;
2640 						// Use ignore_msa_timing_param flag to identify as DRR
2641 						if (pipe->stream->ignore_msa_timing_param) {
2642 							drr_pipe_found = true;
2643 							drr_pipe_index = i;
2644 						}
2645 					}
2646 				}
2647 				// If there is only 1 remaining non SubVP pipe that is DRR, check static
2648 				// schedulability for SubVP + DRR.
2649 				if (non_subvp_pipes == 1 && drr_pipe_found) {
2650 					found_supported_config = subvp_drr_schedulable(dc,
2651 							context, &context->res_ctx.pipe_ctx[drr_pipe_index]);
2652 				}
2653 			}
2654 		}
2655 
2656 		// If SubVP pipe config is unsupported (or cannot be used for UCLK switching)
2657 		// remove phantom pipes and repopulate dml pipes
2658 		if (!found_supported_config) {
2659 			dc->res_pool->funcs->remove_phantom_pipes(dc, context);
2660 			vba->DRAMClockChangeSupport[*vlevel][vba->maxMpcComb] = dm_dram_clock_change_unsupported;
2661 			*pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, false);
2662 		} else {
2663 			// only call dcn20_validate_apply_pipe_split_flags if we found a supported config
2664 			memset(split, 0, MAX_PIPES * sizeof(int));
2665 			memset(merge, 0, MAX_PIPES * sizeof(bool));
2666 			*vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, *vlevel, split, merge);
2667 
2668 			// Note: We can't apply the phantom pipes to hardware at this time. We have to wait
2669 			// until driver has acquired the DMCUB lock to do it safely.
2670 		}
2671 	}
2672 }
2673 
2674 static bool dcn32_internal_validate_bw(
2675 		struct dc *dc,
2676 		struct dc_state *context,
2677 		display_e2e_pipe_params_st *pipes,
2678 		int *pipe_cnt_out,
2679 		int *vlevel_out,
2680 		bool fast_validate)
2681 {
2682 	bool out = false;
2683 	bool repopulate_pipes = false;
2684 	int split[MAX_PIPES] = { 0 };
2685 	bool merge[MAX_PIPES] = { false };
2686 	bool newly_split[MAX_PIPES] = { false };
2687 	int pipe_cnt, i, pipe_idx, vlevel;
2688 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
2689 
2690 	ASSERT(pipes);
2691 	if (!pipes)
2692 		return false;
2693 
2694 	// For each full update, remove all existing phantom pipes first
2695 	dc->res_pool->funcs->remove_phantom_pipes(dc, context);
2696 
2697 	dc->res_pool->funcs->update_soc_for_wm_a(dc, context);
2698 
2699 	pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, fast_validate);
2700 
2701 	if (!pipe_cnt) {
2702 		out = true;
2703 		goto validate_out;
2704 	}
2705 
2706 	dml_log_pipe_params(&context->bw_ctx.dml, pipes, pipe_cnt);
2707 
2708 	if (!fast_validate) {
2709 		dcn32_full_validate_bw_helper(dc, context, pipes, &vlevel, split, merge, &pipe_cnt);
2710 	}
2711 
2712 	if (fast_validate || vlevel == context->bw_ctx.dml.soc.num_states ||
2713 			vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported) {
2714 		/*
2715 		 * If mode is unsupported or there's still no p-state support then
2716 		 * fall back to favoring voltage.
2717 		 *
2718 		 * We don't actually support prefetch mode 2, so require that we
2719 		 * at least support prefetch mode 1.
2720 		 */
2721 		context->bw_ctx.dml.soc.allow_for_pstate_or_stutter_in_vblank_final =
2722 				dm_prefetch_support_stutter;
2723 
2724 		vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
2725 		if (vlevel < context->bw_ctx.dml.soc.num_states) {
2726 			memset(split, 0, MAX_PIPES * sizeof(int));
2727 			memset(merge, 0, MAX_PIPES * sizeof(bool));
2728 			vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, vlevel, split, merge);
2729 		}
2730 	}
2731 
2732 	dml_log_mode_support_params(&context->bw_ctx.dml);
2733 
2734 	if (vlevel == context->bw_ctx.dml.soc.num_states)
2735 		goto validate_fail;
2736 
2737 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2738 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2739 		struct pipe_ctx *mpo_pipe = pipe->bottom_pipe;
2740 
2741 		if (!pipe->stream)
2742 			continue;
2743 
2744 		/* We only support full screen mpo with ODM */
2745 		if (vba->ODMCombineEnabled[vba->pipe_plane[pipe_idx]] != dm_odm_combine_mode_disabled
2746 				&& pipe->plane_state && mpo_pipe
2747 				&& memcmp(&mpo_pipe->plane_res.scl_data.recout,
2748 						&pipe->plane_res.scl_data.recout,
2749 						sizeof(struct rect)) != 0) {
2750 			ASSERT(mpo_pipe->plane_state != pipe->plane_state);
2751 			goto validate_fail;
2752 		}
2753 		pipe_idx++;
2754 	}
2755 
2756 	/* merge pipes if necessary */
2757 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2758 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2759 
2760 		/*skip pipes that don't need merging*/
2761 		if (!merge[i])
2762 			continue;
2763 
2764 		/* if ODM merge we ignore mpc tree, mpo pipes will have their own flags */
2765 		if (pipe->prev_odm_pipe) {
2766 			/*split off odm pipe*/
2767 			pipe->prev_odm_pipe->next_odm_pipe = pipe->next_odm_pipe;
2768 			if (pipe->next_odm_pipe)
2769 				pipe->next_odm_pipe->prev_odm_pipe = pipe->prev_odm_pipe;
2770 
2771 			pipe->bottom_pipe = NULL;
2772 			pipe->next_odm_pipe = NULL;
2773 			pipe->plane_state = NULL;
2774 			pipe->stream = NULL;
2775 			pipe->top_pipe = NULL;
2776 			pipe->prev_odm_pipe = NULL;
2777 			if (pipe->stream_res.dsc)
2778 				dcn20_release_dsc(&context->res_ctx, dc->res_pool, &pipe->stream_res.dsc);
2779 			memset(&pipe->plane_res, 0, sizeof(pipe->plane_res));
2780 			memset(&pipe->stream_res, 0, sizeof(pipe->stream_res));
2781 			repopulate_pipes = true;
2782 		} else if (pipe->top_pipe && pipe->top_pipe->plane_state == pipe->plane_state) {
2783 			struct pipe_ctx *top_pipe = pipe->top_pipe;
2784 			struct pipe_ctx *bottom_pipe = pipe->bottom_pipe;
2785 
2786 			top_pipe->bottom_pipe = bottom_pipe;
2787 			if (bottom_pipe)
2788 				bottom_pipe->top_pipe = top_pipe;
2789 
2790 			pipe->top_pipe = NULL;
2791 			pipe->bottom_pipe = NULL;
2792 			pipe->plane_state = NULL;
2793 			pipe->stream = NULL;
2794 			memset(&pipe->plane_res, 0, sizeof(pipe->plane_res));
2795 			memset(&pipe->stream_res, 0, sizeof(pipe->stream_res));
2796 			repopulate_pipes = true;
2797 		} else
2798 			ASSERT(0); /* Should never try to merge master pipe */
2799 
2800 	}
2801 
2802 	for (i = 0, pipe_idx = -1; i < dc->res_pool->pipe_count; i++) {
2803 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2804 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2805 		struct pipe_ctx *hsplit_pipe = NULL;
2806 		bool odm;
2807 		int old_index = -1;
2808 
2809 		if (!pipe->stream || newly_split[i])
2810 			continue;
2811 
2812 		pipe_idx++;
2813 		odm = vba->ODMCombineEnabled[vba->pipe_plane[pipe_idx]] != dm_odm_combine_mode_disabled;
2814 
2815 		if (!pipe->plane_state && !odm)
2816 			continue;
2817 
2818 		if (split[i]) {
2819 			if (odm) {
2820 				if (split[i] == 4 && old_pipe->next_odm_pipe && old_pipe->next_odm_pipe->next_odm_pipe)
2821 					old_index = old_pipe->next_odm_pipe->next_odm_pipe->pipe_idx;
2822 				else if (old_pipe->next_odm_pipe)
2823 					old_index = old_pipe->next_odm_pipe->pipe_idx;
2824 			} else {
2825 				if (split[i] == 4 && old_pipe->bottom_pipe && old_pipe->bottom_pipe->bottom_pipe &&
2826 						old_pipe->bottom_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2827 					old_index = old_pipe->bottom_pipe->bottom_pipe->pipe_idx;
2828 				else if (old_pipe->bottom_pipe &&
2829 						old_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2830 					old_index = old_pipe->bottom_pipe->pipe_idx;
2831 			}
2832 			hsplit_pipe = dcn32_find_split_pipe(dc, context, old_index);
2833 			ASSERT(hsplit_pipe);
2834 			if (!hsplit_pipe)
2835 				goto validate_fail;
2836 
2837 			if (!dcn32_split_stream_for_mpc_or_odm(
2838 					dc, &context->res_ctx,
2839 					pipe, hsplit_pipe, odm))
2840 				goto validate_fail;
2841 
2842 			newly_split[hsplit_pipe->pipe_idx] = true;
2843 			repopulate_pipes = true;
2844 		}
2845 		if (split[i] == 4) {
2846 			struct pipe_ctx *pipe_4to1;
2847 
2848 			if (odm && old_pipe->next_odm_pipe)
2849 				old_index = old_pipe->next_odm_pipe->pipe_idx;
2850 			else if (!odm && old_pipe->bottom_pipe &&
2851 						old_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2852 				old_index = old_pipe->bottom_pipe->pipe_idx;
2853 			else
2854 				old_index = -1;
2855 			pipe_4to1 = dcn32_find_split_pipe(dc, context, old_index);
2856 			ASSERT(pipe_4to1);
2857 			if (!pipe_4to1)
2858 				goto validate_fail;
2859 			if (!dcn32_split_stream_for_mpc_or_odm(
2860 					dc, &context->res_ctx,
2861 					pipe, pipe_4to1, odm))
2862 				goto validate_fail;
2863 			newly_split[pipe_4to1->pipe_idx] = true;
2864 
2865 			if (odm && old_pipe->next_odm_pipe && old_pipe->next_odm_pipe->next_odm_pipe
2866 					&& old_pipe->next_odm_pipe->next_odm_pipe->next_odm_pipe)
2867 				old_index = old_pipe->next_odm_pipe->next_odm_pipe->next_odm_pipe->pipe_idx;
2868 			else if (!odm && old_pipe->bottom_pipe && old_pipe->bottom_pipe->bottom_pipe &&
2869 					old_pipe->bottom_pipe->bottom_pipe->bottom_pipe &&
2870 					old_pipe->bottom_pipe->bottom_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2871 				old_index = old_pipe->bottom_pipe->bottom_pipe->bottom_pipe->pipe_idx;
2872 			else
2873 				old_index = -1;
2874 			pipe_4to1 = dcn32_find_split_pipe(dc, context, old_index);
2875 			ASSERT(pipe_4to1);
2876 			if (!pipe_4to1)
2877 				goto validate_fail;
2878 			if (!dcn32_split_stream_for_mpc_or_odm(
2879 					dc, &context->res_ctx,
2880 					hsplit_pipe, pipe_4to1, odm))
2881 				goto validate_fail;
2882 			newly_split[pipe_4to1->pipe_idx] = true;
2883 		}
2884 		if (odm)
2885 			dcn20_build_mapped_resource(dc, context, pipe->stream);
2886 	}
2887 
2888 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2889 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2890 
2891 		if (pipe->plane_state) {
2892 			if (!resource_build_scaling_params(pipe))
2893 				goto validate_fail;
2894 		}
2895 	}
2896 
2897 	/* Actual dsc count per stream dsc validation*/
2898 	if (!dcn20_validate_dsc(dc, context)) {
2899 		vba->ValidationStatus[vba->soc.num_states] = DML_FAIL_DSC_VALIDATION_FAILURE;
2900 		goto validate_fail;
2901 	}
2902 
2903 	if (repopulate_pipes)
2904 		pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, fast_validate);
2905 	*vlevel_out = vlevel;
2906 	*pipe_cnt_out = pipe_cnt;
2907 
2908 	out = true;
2909 	goto validate_out;
2910 
2911 validate_fail:
2912 	out = false;
2913 
2914 validate_out:
2915 	return out;
2916 }
2917 
2918 bool dcn32_validate_bandwidth(struct dc *dc,
2919 		struct dc_state *context,
2920 		bool fast_validate)
2921 {
2922 	bool out = false;
2923 
2924 	BW_VAL_TRACE_SETUP();
2925 
2926 	int vlevel = 0;
2927 	int pipe_cnt = 0;
2928 	display_e2e_pipe_params_st *pipes = kzalloc(dc->res_pool->pipe_count * sizeof(display_e2e_pipe_params_st), GFP_KERNEL);
2929 	DC_LOGGER_INIT(dc->ctx->logger);
2930 
2931 	BW_VAL_TRACE_COUNT();
2932 
2933     DC_FP_START();
2934 	out = dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel, fast_validate);
2935     DC_FP_END();
2936 
2937 	if (pipe_cnt == 0)
2938 		goto validate_out;
2939 
2940 	if (!out)
2941 		goto validate_fail;
2942 
2943 	BW_VAL_TRACE_END_VOLTAGE_LEVEL();
2944 
2945 	if (fast_validate) {
2946 		BW_VAL_TRACE_SKIP(fast);
2947 		goto validate_out;
2948 	}
2949 
2950 	dc->res_pool->funcs->calculate_wm_and_dlg(dc, context, pipes, pipe_cnt, vlevel);
2951 
2952 	BW_VAL_TRACE_END_WATERMARKS();
2953 
2954 	goto validate_out;
2955 
2956 validate_fail:
2957 	DC_LOG_WARNING("Mode Validation Warning: %s failed validation.\n",
2958 		dml_get_status_message(context->bw_ctx.dml.vba.ValidationStatus[context->bw_ctx.dml.vba.soc.num_states]));
2959 
2960 	BW_VAL_TRACE_SKIP(fail);
2961 	out = false;
2962 
2963 validate_out:
2964 	kfree(pipes);
2965 
2966 	BW_VAL_TRACE_FINISH();
2967 
2968 	return out;
2969 }
2970 
2971 
2972 static bool is_dual_plane(enum surface_pixel_format format)
2973 {
2974 	return format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN || format == SURFACE_PIXEL_FORMAT_GRPH_RGBE_ALPHA;
2975 }
2976 
2977 int dcn32_populate_dml_pipes_from_context(
2978 	struct dc *dc, struct dc_state *context,
2979 	display_e2e_pipe_params_st *pipes,
2980 	bool fast_validate)
2981 {
2982 	int i, pipe_cnt;
2983 	struct resource_context *res_ctx = &context->res_ctx;
2984 	struct pipe_ctx *pipe;
2985 
2986 	dcn20_populate_dml_pipes_from_context(dc, context, pipes, fast_validate);
2987 
2988 	for (i = 0, pipe_cnt = 0; i < dc->res_pool->pipe_count; i++) {
2989 		struct dc_crtc_timing *timing;
2990 
2991 		if (!res_ctx->pipe_ctx[i].stream)
2992 			continue;
2993 		pipe = &res_ctx->pipe_ctx[i];
2994 		timing = &pipe->stream->timing;
2995 
2996 		pipes[pipe_cnt].pipe.src.gpuvm = true;
2997 		pipes[pipe_cnt].pipe.src.dcc_fraction_of_zs_req_luma = 0;
2998 		pipes[pipe_cnt].pipe.src.dcc_fraction_of_zs_req_chroma = 0;
2999 		pipes[pipe_cnt].pipe.dest.vfront_porch = timing->v_front_porch;
3000 		pipes[pipe_cnt].pipe.src.gpuvm_min_page_size_kbytes = 256; // according to spreadsheet
3001 		pipes[pipe_cnt].pipe.src.unbounded_req_mode = false;
3002 		pipes[pipe_cnt].pipe.scale_ratio_depth.lb_depth = dm_lb_19;
3003 
3004 		switch (pipe->stream->mall_stream_config.type) {
3005 		case SUBVP_MAIN:
3006 			pipes[pipe_cnt].pipe.src.use_mall_for_pstate_change = dm_use_mall_pstate_change_sub_viewport;
3007 			break;
3008 		case SUBVP_PHANTOM:
3009 			pipes[pipe_cnt].pipe.src.use_mall_for_pstate_change = dm_use_mall_pstate_change_phantom_pipe;
3010 			pipes[pipe_cnt].pipe.src.use_mall_for_static_screen = dm_use_mall_static_screen_disable;
3011 			// Disallow unbounded req for SubVP according to DCHUB programming guide
3012 			pipes[pipe_cnt].pipe.src.unbounded_req_mode = false;
3013 			break;
3014 		case SUBVP_NONE:
3015 			pipes[pipe_cnt].pipe.src.use_mall_for_pstate_change = dm_use_mall_pstate_change_disable;
3016 			pipes[pipe_cnt].pipe.src.use_mall_for_static_screen = dm_use_mall_static_screen_disable;
3017 			break;
3018 		default:
3019 			break;
3020 		}
3021 
3022 		pipes[pipe_cnt].dout.dsc_input_bpc = 0;
3023 		if (pipes[pipe_cnt].dout.dsc_enable) {
3024 			switch (timing->display_color_depth) {
3025 			case COLOR_DEPTH_888:
3026 				pipes[pipe_cnt].dout.dsc_input_bpc = 8;
3027 				break;
3028 			case COLOR_DEPTH_101010:
3029 				pipes[pipe_cnt].dout.dsc_input_bpc = 10;
3030 				break;
3031 			case COLOR_DEPTH_121212:
3032 				pipes[pipe_cnt].dout.dsc_input_bpc = 12;
3033 				break;
3034 			default:
3035 				ASSERT(0);
3036 				break;
3037 			}
3038 		}
3039 
3040 		pipes[pipe_cnt].pipe.dest.odm_combine_policy = dm_odm_combine_policy_dal;
3041 		if (context->stream_count == 1) {
3042 			if (dc->debug.enable_single_display_2to1_odm_policy)
3043 				pipes[pipe_cnt].pipe.dest.odm_combine_policy = dm_odm_combine_policy_2to1;
3044 		}
3045 		pipe_cnt++;
3046 	}
3047 
3048 	/* For DET allocation, we don't want to use DML policy (not optimal for utilizing all
3049 	 * the DET available for each pipe). Use the DET override input to maintain our driver
3050 	 * policy.
3051 	 */
3052 	switch (pipe_cnt) {
3053 	case 1:
3054 		pipes[0].pipe.src.det_size_override = DCN3_2_MAX_DET_SIZE;
3055 		if (pipe->plane_state && !dc->debug.disable_z9_mpc) {
3056 			if (!is_dual_plane(pipe->plane_state->format)) {
3057 				pipes[0].pipe.src.det_size_override = DCN3_2_DEFAULT_DET_SIZE;
3058 				pipes[0].pipe.src.unbounded_req_mode = true;
3059 				if (pipe->plane_state->src_rect.width >= 5120 &&
3060 					pipe->plane_state->src_rect.height >= 2880)
3061 					pipes[0].pipe.src.det_size_override = 320; // 5K or higher
3062 			}
3063 		}
3064 		break;
3065 	case 2:
3066 	case 3:
3067 	case 4:
3068 		// For 2 and 3 pipes, use (MAX_DET_SIZE / pipe_cnt), for 4 pipes use default size for each pipe
3069 		for (i = 0; i < pipe_cnt; i++) {
3070 			pipes[i].pipe.src.det_size_override = (pipe_cnt < 4) ? (DCN3_2_MAX_DET_SIZE / pipe_cnt) : DCN3_2_DEFAULT_DET_SIZE;
3071 		}
3072 		break;
3073 	}
3074 
3075 	dcn32_update_det_override_for_mpo(dc, context, pipes);
3076 
3077 	return pipe_cnt;
3078 }
3079 
3080 void dcn32_calculate_wm_and_dlg_fp(
3081 		struct dc *dc, struct dc_state *context,
3082 		display_e2e_pipe_params_st *pipes,
3083 		int pipe_cnt,
3084 		int vlevel)
3085 {
3086 	int i, pipe_idx, vlevel_temp = 0;
3087 	double dcfclk = dcn3_2_soc.clock_limits[0].dcfclk_mhz;
3088 	double dcfclk_from_validation = context->bw_ctx.dml.vba.DCFCLKState[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
3089 	unsigned int min_dram_speed_mts = context->bw_ctx.dml.vba.DRAMSpeed;
3090 	bool pstate_en = context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] !=
3091 			dm_dram_clock_change_unsupported;
3092 
3093 	// Override DRAMClockChangeSupport for SubVP + DRR case where the DRR cannot switch without stretching it's VBLANK
3094 	if (!pstate_en && dcn32_subvp_in_use(dc, context)) {
3095 		context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] = dm_dram_clock_change_vblank_w_mall_sub_vp;
3096 		pstate_en = true;
3097 	}
3098 
3099 	/* Set B:
3100 	 * For Set B calculations use clocks from clock_limits[2] when available i.e. when SMU is present,
3101 	 * otherwise use arbitrary low value from spreadsheet for DCFCLK as lower is safer for watermark
3102 	 * calculations to cover bootup clocks.
3103 	 * DCFCLK: soc.clock_limits[2] when available
3104 	 * UCLK: soc.clock_limits[2] when available
3105 	 */
3106 	if (dcn3_2_soc.num_states > 2) {
3107 		vlevel_temp = 2;
3108 		dcfclk = dcn3_2_soc.clock_limits[2].dcfclk_mhz;
3109 	} else
3110 		dcfclk = 615; //DCFCLK Vmin_lv
3111 
3112 	pipes[0].clks_cfg.voltage = vlevel_temp;
3113 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
3114 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel_temp].socclk_mhz;
3115 
3116 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].valid) {
3117 		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;
3118 		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;
3119 		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;
3120 		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;
3121 	}
3122 	context->bw_ctx.bw.dcn.watermarks.b.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3123 	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;
3124 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3125 	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;
3126 	context->bw_ctx.bw.dcn.watermarks.b.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3127 	context->bw_ctx.bw.dcn.watermarks.b.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3128 	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;
3129 	context->bw_ctx.bw.dcn.watermarks.b.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3130 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3131 	context->bw_ctx.bw.dcn.watermarks.b.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3132 
3133 	/* Set D:
3134 	 * All clocks min.
3135 	 * DCFCLK: Min, as reported by PM FW when available
3136 	 * UCLK  : Min, as reported by PM FW when available
3137 	 * sr_enter_exit/sr_exit should be lower than used for DRAM (TBD after bringup or later, use as decided in Clk Mgr)
3138 	 */
3139 
3140 	if (dcn3_2_soc.num_states > 2) {
3141 		vlevel_temp = 0;
3142 		dcfclk = dc->clk_mgr->bw_params->clk_table.entries[0].dcfclk_mhz;
3143 	} else
3144 		dcfclk = 615; //DCFCLK Vmin_lv
3145 
3146 	pipes[0].clks_cfg.voltage = vlevel_temp;
3147 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
3148 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel_temp].socclk_mhz;
3149 
3150 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].valid) {
3151 		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;
3152 		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;
3153 		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;
3154 		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;
3155 	}
3156 	context->bw_ctx.bw.dcn.watermarks.d.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3157 	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;
3158 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3159 	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;
3160 	context->bw_ctx.bw.dcn.watermarks.d.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3161 	context->bw_ctx.bw.dcn.watermarks.d.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3162 	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;
3163 	context->bw_ctx.bw.dcn.watermarks.d.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3164 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3165 	context->bw_ctx.bw.dcn.watermarks.d.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3166 
3167 	/* Set C, for Dummy P-State:
3168 	 * All clocks min.
3169 	 * DCFCLK: Min, as reported by PM FW, when available
3170 	 * UCLK  : Min,  as reported by PM FW, when available
3171 	 * pstate latency as per UCLK state dummy pstate latency
3172 	 */
3173 	// For Set A and Set C use values from validation
3174 	pipes[0].clks_cfg.voltage = vlevel;
3175 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk_from_validation;
3176 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel].socclk_mhz;
3177 
3178 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].valid) {
3179 		unsigned int min_dram_speed_mts_margin = 160;
3180 
3181 		if ((!pstate_en))
3182 			min_dram_speed_mts = dc->clk_mgr->bw_params->clk_table.entries[dc->clk_mgr->bw_params->clk_table.num_entries - 1].memclk_mhz * 16;
3183 
3184 		/* find largest table entry that is lower than dram speed, but lower than DPM0 still uses DPM0 */
3185 		for (i = 3; i > 0; i--)
3186 			if (min_dram_speed_mts + min_dram_speed_mts_margin > dc->clk_mgr->bw_params->dummy_pstate_table[i].dram_speed_mts)
3187 				break;
3188 
3189 		context->bw_ctx.dml.soc.dram_clock_change_latency_us = dc->clk_mgr->bw_params->dummy_pstate_table[i].dummy_pstate_latency_us;
3190 		context->bw_ctx.dml.soc.dummy_pstate_latency_us = dc->clk_mgr->bw_params->dummy_pstate_table[i].dummy_pstate_latency_us;
3191 		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;
3192 		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;
3193 		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;
3194 	}
3195 	context->bw_ctx.bw.dcn.watermarks.c.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3196 	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;
3197 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3198 	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;
3199 	context->bw_ctx.bw.dcn.watermarks.c.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3200 	context->bw_ctx.bw.dcn.watermarks.c.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3201 	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;
3202 	context->bw_ctx.bw.dcn.watermarks.c.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3203 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3204 	context->bw_ctx.bw.dcn.watermarks.c.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3205 
3206 	if ((!pstate_en) && (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].valid)) {
3207 		/* The only difference between A and C is p-state latency, if p-state is not supported
3208 		 * with full p-state latency we want to calculate DLG based on dummy p-state latency,
3209 		 * Set A p-state watermark set to 0 on DCN32, when p-state unsupported, for now keep as DCN32.
3210 		 */
3211 		context->bw_ctx.bw.dcn.watermarks.a = context->bw_ctx.bw.dcn.watermarks.c;
3212 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns = 0;
3213 	} else {
3214 		/* Set A:
3215 		 * All clocks min.
3216 		 * DCFCLK: Min, as reported by PM FW, when available
3217 		 * UCLK: Min, as reported by PM FW, when available
3218 		 */
3219 		dc->res_pool->funcs->update_soc_for_wm_a(dc, context);
3220 		context->bw_ctx.bw.dcn.watermarks.a.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3221 		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;
3222 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3223 		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;
3224 		context->bw_ctx.bw.dcn.watermarks.a.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3225 		context->bw_ctx.bw.dcn.watermarks.a.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3226 		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;
3227 		context->bw_ctx.bw.dcn.watermarks.a.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3228 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.fclk_pstate_change_ns = get_fclk_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3229 		context->bw_ctx.bw.dcn.watermarks.a.usr_retraining_ns = get_usr_retraining_watermark(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
3230 	}
3231 
3232 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
3233 		if (!context->res_ctx.pipe_ctx[i].stream)
3234 			continue;
3235 
3236 		pipes[pipe_idx].clks_cfg.dispclk_mhz = get_dispclk_calculated(&context->bw_ctx.dml, pipes, pipe_cnt);
3237 		pipes[pipe_idx].clks_cfg.dppclk_mhz = get_dppclk_calculated(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
3238 
3239 		if (dc->config.forced_clocks) {
3240 			pipes[pipe_idx].clks_cfg.dispclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dispclk_mhz;
3241 			pipes[pipe_idx].clks_cfg.dppclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dppclk_mhz;
3242 		}
3243 		if (dc->debug.min_disp_clk_khz > pipes[pipe_idx].clks_cfg.dispclk_mhz * 1000)
3244 			pipes[pipe_idx].clks_cfg.dispclk_mhz = dc->debug.min_disp_clk_khz / 1000.0;
3245 		if (dc->debug.min_dpp_clk_khz > pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000)
3246 			pipes[pipe_idx].clks_cfg.dppclk_mhz = dc->debug.min_dpp_clk_khz / 1000.0;
3247 
3248 		pipe_idx++;
3249 	}
3250 
3251 	context->perf_params.stutter_period_us = context->bw_ctx.dml.vba.StutterPeriod;
3252 
3253 	dcn32_calculate_dlg_params(dc, context, pipes, pipe_cnt, vlevel);
3254 
3255 	if (!pstate_en)
3256 		/* Restore full p-state latency */
3257 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
3258 				dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
3259 }
3260 
3261 static struct dc_cap_funcs cap_funcs = {
3262 	.get_dcc_compression_cap = dcn20_get_dcc_compression_cap
3263 };
3264 
3265 
3266 static void dcn32_get_optimal_dcfclk_fclk_for_uclk(unsigned int uclk_mts,
3267 		unsigned int *optimal_dcfclk,
3268 		unsigned int *optimal_fclk)
3269 {
3270 	double bw_from_dram, bw_from_dram1, bw_from_dram2;
3271 
3272 	bw_from_dram1 = uclk_mts * dcn3_2_soc.num_chans *
3273 		dcn3_2_soc.dram_channel_width_bytes * (dcn3_2_soc.max_avg_dram_bw_use_normal_percent / 100);
3274 	bw_from_dram2 = uclk_mts * dcn3_2_soc.num_chans *
3275 		dcn3_2_soc.dram_channel_width_bytes * (dcn3_2_soc.max_avg_sdp_bw_use_normal_percent / 100);
3276 
3277 	bw_from_dram = (bw_from_dram1 < bw_from_dram2) ? bw_from_dram1 : bw_from_dram2;
3278 
3279 	if (optimal_fclk)
3280 		*optimal_fclk = bw_from_dram /
3281 		(dcn3_2_soc.fabric_datapath_to_dcn_data_return_bytes * (dcn3_2_soc.max_avg_sdp_bw_use_normal_percent / 100));
3282 
3283 	if (optimal_dcfclk)
3284 		*optimal_dcfclk =  bw_from_dram /
3285 		(dcn3_2_soc.return_bus_width_bytes * (dcn3_2_soc.max_avg_sdp_bw_use_normal_percent / 100));
3286 }
3287 
3288 void dcn32_calculate_wm_and_dlg(
3289 		struct dc *dc, struct dc_state *context,
3290 		display_e2e_pipe_params_st *pipes,
3291 		int pipe_cnt,
3292 		int vlevel)
3293 {
3294     DC_FP_START();
3295     dcn32_calculate_wm_and_dlg_fp(
3296 		dc, context,
3297 		pipes,
3298 		pipe_cnt,
3299 		vlevel);
3300     DC_FP_END();
3301 }
3302 
3303 static bool is_dtbclk_required(struct dc *dc, struct dc_state *context)
3304 {
3305 	int i;
3306 
3307 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3308 		if (!context->res_ctx.pipe_ctx[i].stream)
3309 			continue;
3310 		if (is_dp_128b_132b_signal(&context->res_ctx.pipe_ctx[i]))
3311 			return true;
3312 	}
3313 	return false;
3314 }
3315 
3316 void dcn32_calculate_dlg_params(struct dc *dc, struct dc_state *context, display_e2e_pipe_params_st *pipes,
3317 		int pipe_cnt, int vlevel)
3318 {
3319 	int i, pipe_idx;
3320 	bool usr_retraining_support = false;
3321 
3322 	/* Writeback MCIF_WB arbitration parameters */
3323 	dc->res_pool->funcs->set_mcif_arb_params(dc, context, pipes, pipe_cnt);
3324 
3325 	context->bw_ctx.bw.dcn.clk.dispclk_khz = context->bw_ctx.dml.vba.DISPCLK * 1000;
3326 	context->bw_ctx.bw.dcn.clk.dcfclk_khz = context->bw_ctx.dml.vba.DCFCLK * 1000;
3327 	context->bw_ctx.bw.dcn.clk.socclk_khz = context->bw_ctx.dml.vba.SOCCLK * 1000;
3328 	context->bw_ctx.bw.dcn.clk.dramclk_khz = context->bw_ctx.dml.vba.DRAMSpeed * 1000 / 16;
3329 	context->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz = context->bw_ctx.dml.vba.DCFCLKDeepSleep * 1000;
3330 	context->bw_ctx.bw.dcn.clk.fclk_khz = context->bw_ctx.dml.vba.FabricClock * 1000;
3331 	context->bw_ctx.bw.dcn.clk.p_state_change_support =
3332 			context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb]
3333 					!= dm_dram_clock_change_unsupported;
3334 	context->bw_ctx.bw.dcn.clk.num_ways = dcn32_helper_calculate_num_ways_for_subvp(dc, context);
3335 	/*
3336  *
3337 	 * TODO: needs FAMS
3338 	 * Pstate change might not be supported by hardware, but it might be
3339 	 * possible with firmware driven vertical blank stretching.
3340 	 */
3341 	// context->bw_ctx.bw.dcn.clk.p_state_change_support |= context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching;
3342 	context->bw_ctx.bw.dcn.clk.dppclk_khz = 0;
3343 	context->bw_ctx.bw.dcn.clk.dtbclk_en = is_dtbclk_required(dc, context);
3344 	context->bw_ctx.bw.dcn.clk.ref_dtbclk_khz = context->bw_ctx.dml.vba.DTBCLKPerState[vlevel] * 1000;
3345 	if (context->bw_ctx.dml.vba.FCLKChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] == dm_fclock_change_unsupported)
3346 		context->bw_ctx.bw.dcn.clk.fclk_p_state_change_support = false;
3347 	else
3348 		context->bw_ctx.bw.dcn.clk.fclk_p_state_change_support = true;
3349 
3350 	usr_retraining_support = context->bw_ctx.dml.vba.USRRetrainingSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
3351 	ASSERT(usr_retraining_support);
3352 
3353 	if (context->bw_ctx.bw.dcn.clk.dispclk_khz < dc->debug.min_disp_clk_khz)
3354 		context->bw_ctx.bw.dcn.clk.dispclk_khz = dc->debug.min_disp_clk_khz;
3355 
3356 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
3357 		if (!context->res_ctx.pipe_ctx[i].stream)
3358 			continue;
3359 		pipes[pipe_idx].pipe.dest.vstartup_start = get_vstartup(&context->bw_ctx.dml, pipes, pipe_cnt,
3360 				pipe_idx);
3361 		pipes[pipe_idx].pipe.dest.vupdate_offset = get_vupdate_offset(&context->bw_ctx.dml, pipes, pipe_cnt,
3362 				pipe_idx);
3363 		pipes[pipe_idx].pipe.dest.vupdate_width = get_vupdate_width(&context->bw_ctx.dml, pipes, pipe_cnt,
3364 				pipe_idx);
3365 		pipes[pipe_idx].pipe.dest.vready_offset = get_vready_offset(&context->bw_ctx.dml, pipes, pipe_cnt,
3366 				pipe_idx);
3367 		if (context->res_ctx.pipe_ctx[i].stream->mall_stream_config.type == SUBVP_PHANTOM) {
3368 			// Phantom pipe requires that DET_SIZE = 0 and no unbounded requests
3369 			context->res_ctx.pipe_ctx[i].det_buffer_size_kb = 0;
3370 			context->res_ctx.pipe_ctx[i].unbounded_req = false;
3371 		} else {
3372 			context->res_ctx.pipe_ctx[i].det_buffer_size_kb = get_det_buffer_size_kbytes(&context->bw_ctx.dml, pipes, pipe_cnt,
3373 							pipe_idx);
3374 			context->res_ctx.pipe_ctx[i].unbounded_req = pipes[pipe_idx].pipe.src.unbounded_req_mode;
3375 		}
3376 		if (context->bw_ctx.bw.dcn.clk.dppclk_khz < pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000)
3377 			context->bw_ctx.bw.dcn.clk.dppclk_khz = pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000;
3378 		context->res_ctx.pipe_ctx[i].plane_res.bw.dppclk_khz = pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000;
3379 		context->res_ctx.pipe_ctx[i].pipe_dlg_param = pipes[pipe_idx].pipe.dest;
3380 		pipe_idx++;
3381 	}
3382 	/*save a original dppclock copy*/
3383 	context->bw_ctx.bw.dcn.clk.bw_dppclk_khz = context->bw_ctx.bw.dcn.clk.dppclk_khz;
3384 	context->bw_ctx.bw.dcn.clk.bw_dispclk_khz = context->bw_ctx.bw.dcn.clk.dispclk_khz;
3385 	context->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz = context->bw_ctx.dml.soc.clock_limits[vlevel].dppclk_mhz
3386 			* 1000;
3387 	context->bw_ctx.bw.dcn.clk.max_supported_dispclk_khz = context->bw_ctx.dml.soc.clock_limits[vlevel].dispclk_mhz
3388 			* 1000;
3389 
3390 	context->bw_ctx.bw.dcn.compbuf_size_kb = context->bw_ctx.dml.ip.config_return_buffer_size_in_kbytes;
3391 
3392 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3393 		if (context->res_ctx.pipe_ctx[i].stream)
3394 			context->bw_ctx.bw.dcn.compbuf_size_kb -= context->res_ctx.pipe_ctx[i].det_buffer_size_kb;
3395 	}
3396 
3397 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
3398 
3399 		if (!context->res_ctx.pipe_ctx[i].stream)
3400 			continue;
3401 
3402 		context->bw_ctx.dml.funcs.rq_dlg_get_dlg_reg_v2(&context->bw_ctx.dml,
3403 				&context->res_ctx.pipe_ctx[i].dlg_regs, &context->res_ctx.pipe_ctx[i].ttu_regs, pipes,
3404 				pipe_cnt, pipe_idx);
3405 
3406 		context->bw_ctx.dml.funcs.rq_dlg_get_rq_reg_v2(&context->res_ctx.pipe_ctx[i].rq_regs,
3407 				&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
3408 
3409 		pipe_idx++;
3410 	}
3411 }
3412 
3413 static void get_optimal_ntuple(struct _vcs_dpi_voltage_scaling_st *entry)
3414 {
3415 	if (entry->dcfclk_mhz > 0) {
3416 		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);
3417 
3418 		entry->fabricclk_mhz = bw_on_sdp / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100));
3419 		entry->dram_speed_mts = bw_on_sdp / (dcn3_2_soc.num_chans *
3420 				dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100));
3421 	} else if (entry->fabricclk_mhz > 0) {
3422 		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);
3423 
3424 		entry->dcfclk_mhz = bw_on_fabric / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100));
3425 		entry->dram_speed_mts = bw_on_fabric / (dcn3_2_soc.num_chans *
3426 				dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100));
3427 	} else if (entry->dram_speed_mts > 0) {
3428 		float bw_on_dram = entry->dram_speed_mts * dcn3_2_soc.num_chans *
3429 				dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100);
3430 
3431 		entry->fabricclk_mhz = bw_on_dram / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100));
3432 		entry->dcfclk_mhz = bw_on_dram / (dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100));
3433 	}
3434 }
3435 
3436 static float calculate_net_bw_in_kbytes_sec(struct _vcs_dpi_voltage_scaling_st *entry)
3437 {
3438 	float memory_bw_kbytes_sec = entry->dram_speed_mts * dcn3_2_soc.num_chans *
3439 			dcn3_2_soc.dram_channel_width_bytes * ((float)dcn3_2_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only / 100);
3440 
3441 	float fabric_bw_kbytes_sec = entry->fabricclk_mhz * dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_fabric_bw_after_urgent / 100);
3442 
3443 	float sdp_bw_kbytes_sec = entry->dcfclk_mhz * dcn3_2_soc.return_bus_width_bytes * ((float)dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / 100);
3444 
3445 	float limiting_bw_kbytes_sec = memory_bw_kbytes_sec;
3446 
3447 	if (fabric_bw_kbytes_sec < limiting_bw_kbytes_sec)
3448 		limiting_bw_kbytes_sec = fabric_bw_kbytes_sec;
3449 
3450 	if (sdp_bw_kbytes_sec < limiting_bw_kbytes_sec)
3451 		limiting_bw_kbytes_sec = sdp_bw_kbytes_sec;
3452 
3453 	return limiting_bw_kbytes_sec;
3454 }
3455 
3456 static void insert_entry_into_table_sorted(struct _vcs_dpi_voltage_scaling_st *table, unsigned int *num_entries,
3457 		struct _vcs_dpi_voltage_scaling_st *entry)
3458 {
3459 	int index = 0;
3460 	int i = 0;
3461 	float net_bw_of_new_state = 0;
3462 
3463 	if (*num_entries == 0) {
3464 		table[0] = *entry;
3465 		(*num_entries)++;
3466 	} else {
3467 		net_bw_of_new_state = calculate_net_bw_in_kbytes_sec(entry);
3468 		while (net_bw_of_new_state > calculate_net_bw_in_kbytes_sec(&table[index])) {
3469 			index++;
3470 			if (index >= *num_entries)
3471 				break;
3472 		}
3473 
3474 		for (i = *num_entries; i > index; i--) {
3475 			table[i] = table[i - 1];
3476 		}
3477 
3478 		table[index] = *entry;
3479 		(*num_entries)++;
3480 	}
3481 }
3482 
3483 static void remove_entry_from_table_at_index(struct _vcs_dpi_voltage_scaling_st *table, unsigned int *num_entries,
3484 		unsigned int index)
3485 {
3486 	int i;
3487 
3488 	if (*num_entries == 0)
3489 		return;
3490 
3491 	for (i = index; i < *num_entries - 1; i++) {
3492 		table[i] = table[i + 1];
3493 	}
3494 	memset(&table[--(*num_entries)], 0, sizeof(struct _vcs_dpi_voltage_scaling_st));
3495 }
3496 
3497 static int build_synthetic_soc_states(struct clk_bw_params *bw_params,
3498 		struct _vcs_dpi_voltage_scaling_st *table, unsigned int *num_entries)
3499 {
3500 	int i, j;
3501 	struct _vcs_dpi_voltage_scaling_st entry = {0};
3502 
3503 	unsigned int max_dcfclk_mhz = 0, max_dispclk_mhz = 0, max_dppclk_mhz = 0,
3504 			max_phyclk_mhz = 0, max_dtbclk_mhz = 0, max_fclk_mhz = 0, max_uclk_mhz = 0;
3505 
3506 	unsigned int min_dcfclk_mhz = 199, min_fclk_mhz = 299;
3507 
3508 	static const unsigned int num_dcfclk_stas = 5;
3509 	unsigned int dcfclk_sta_targets[DC__VOLTAGE_STATES] = {199, 615, 906, 1324, 1564};
3510 
3511 	unsigned int num_uclk_dpms = 0;
3512 	unsigned int num_fclk_dpms = 0;
3513 	unsigned int num_dcfclk_dpms = 0;
3514 
3515 	for (i = 0; i < MAX_NUM_DPM_LVL; i++) {
3516 		if (bw_params->clk_table.entries[i].dcfclk_mhz > max_dcfclk_mhz)
3517 			max_dcfclk_mhz = bw_params->clk_table.entries[i].dcfclk_mhz;
3518 		if (bw_params->clk_table.entries[i].fclk_mhz > max_fclk_mhz)
3519 			max_fclk_mhz = bw_params->clk_table.entries[i].fclk_mhz;
3520 		if (bw_params->clk_table.entries[i].memclk_mhz > max_uclk_mhz)
3521 			max_uclk_mhz = bw_params->clk_table.entries[i].memclk_mhz;
3522 		if (bw_params->clk_table.entries[i].dispclk_mhz > max_dispclk_mhz)
3523 			max_dispclk_mhz = bw_params->clk_table.entries[i].dispclk_mhz;
3524 		if (bw_params->clk_table.entries[i].dppclk_mhz > max_dppclk_mhz)
3525 			max_dppclk_mhz = bw_params->clk_table.entries[i].dppclk_mhz;
3526 		if (bw_params->clk_table.entries[i].phyclk_mhz > max_phyclk_mhz)
3527 			max_phyclk_mhz = bw_params->clk_table.entries[i].phyclk_mhz;
3528 		if (bw_params->clk_table.entries[i].dtbclk_mhz > max_dtbclk_mhz)
3529 			max_dtbclk_mhz = bw_params->clk_table.entries[i].dtbclk_mhz;
3530 
3531 		if (bw_params->clk_table.entries[i].memclk_mhz > 0)
3532 			num_uclk_dpms++;
3533 		if (bw_params->clk_table.entries[i].fclk_mhz > 0)
3534 			num_fclk_dpms++;
3535 		if (bw_params->clk_table.entries[i].dcfclk_mhz > 0)
3536 			num_dcfclk_dpms++;
3537 	}
3538 
3539 	if (!max_dcfclk_mhz || !max_dispclk_mhz || !max_dtbclk_mhz)
3540 		return -1;
3541 
3542 	if (max_dppclk_mhz == 0)
3543 		max_dppclk_mhz = max_dispclk_mhz;
3544 
3545 	if (max_fclk_mhz == 0)
3546 		max_fclk_mhz = max_dcfclk_mhz * dcn3_2_soc.pct_ideal_sdp_bw_after_urgent / dcn3_2_soc.pct_ideal_fabric_bw_after_urgent;
3547 
3548 	if (max_phyclk_mhz == 0)
3549 		max_phyclk_mhz = dcn3_2_soc.clock_limits[0].phyclk_mhz;
3550 
3551 	*num_entries = 0;
3552 	entry.dispclk_mhz = max_dispclk_mhz;
3553 	entry.dscclk_mhz = max_dispclk_mhz / 3;
3554 	entry.dppclk_mhz = max_dppclk_mhz;
3555 	entry.dtbclk_mhz = max_dtbclk_mhz;
3556 	entry.phyclk_mhz = max_phyclk_mhz;
3557 	entry.phyclk_d18_mhz = dcn3_2_soc.clock_limits[0].phyclk_d18_mhz;
3558 	entry.phyclk_d32_mhz = dcn3_2_soc.clock_limits[0].phyclk_d32_mhz;
3559 
3560 	// Insert all the DCFCLK STAs
3561 	for (i = 0; i < num_dcfclk_stas; i++) {
3562 		entry.dcfclk_mhz = dcfclk_sta_targets[i];
3563 		entry.fabricclk_mhz = 0;
3564 		entry.dram_speed_mts = 0;
3565 
3566 		get_optimal_ntuple(&entry);
3567 		insert_entry_into_table_sorted(table, num_entries, &entry);
3568 	}
3569 
3570 	// Insert the max DCFCLK
3571 	entry.dcfclk_mhz = max_dcfclk_mhz;
3572 	entry.fabricclk_mhz = 0;
3573 	entry.dram_speed_mts = 0;
3574 
3575 	get_optimal_ntuple(&entry);
3576 	insert_entry_into_table_sorted(table, num_entries, &entry);
3577 
3578 	// Insert the UCLK DPMS
3579 	for (i = 0; i < num_uclk_dpms; i++) {
3580 		entry.dcfclk_mhz = 0;
3581 		entry.fabricclk_mhz = 0;
3582 		entry.dram_speed_mts = bw_params->clk_table.entries[i].memclk_mhz * 16;
3583 
3584 		get_optimal_ntuple(&entry);
3585 		insert_entry_into_table_sorted(table, num_entries, &entry);
3586 	}
3587 
3588 	// If FCLK is coarse grained, insert individual DPMs.
3589 	if (num_fclk_dpms > 2) {
3590 		for (i = 0; i < num_fclk_dpms; i++) {
3591 			entry.dcfclk_mhz = 0;
3592 			entry.fabricclk_mhz = bw_params->clk_table.entries[i].fclk_mhz;
3593 			entry.dram_speed_mts = 0;
3594 
3595 			get_optimal_ntuple(&entry);
3596 			insert_entry_into_table_sorted(table, num_entries, &entry);
3597 		}
3598 	}
3599 	// If FCLK fine grained, only insert max
3600 	else {
3601 		entry.dcfclk_mhz = 0;
3602 		entry.fabricclk_mhz = max_fclk_mhz;
3603 		entry.dram_speed_mts = 0;
3604 
3605 		get_optimal_ntuple(&entry);
3606 		insert_entry_into_table_sorted(table, num_entries, &entry);
3607 	}
3608 
3609 	// At this point, the table contains all "points of interest" based on
3610 	// DPMs from PMFW, and STAs.  Table is sorted by BW, and all clock
3611 	// ratios (by derate, are exact).
3612 
3613 	// Remove states that require higher clocks than are supported
3614 	for (i = *num_entries - 1; i >= 0 ; i--) {
3615 		if (table[i].dcfclk_mhz > max_dcfclk_mhz ||
3616 				table[i].fabricclk_mhz > max_fclk_mhz ||
3617 				table[i].dram_speed_mts > max_uclk_mhz * 16)
3618 			remove_entry_from_table_at_index(table, num_entries, i);
3619 	}
3620 
3621 	// At this point, the table only contains supported points of interest
3622 	// it could be used as is, but some states may be redundant due to
3623 	// coarse grained nature of some clocks, so we want to round up to
3624 	// coarse grained DPMs and remove duplicates.
3625 
3626 	// Round up UCLKs
3627 	for (i = *num_entries - 1; i >= 0 ; i--) {
3628 		for (j = 0; j < num_uclk_dpms; j++) {
3629 			if (bw_params->clk_table.entries[j].memclk_mhz * 16 >= table[i].dram_speed_mts) {
3630 				table[i].dram_speed_mts = bw_params->clk_table.entries[j].memclk_mhz * 16;
3631 				break;
3632 			}
3633 		}
3634 	}
3635 
3636 	// If FCLK is coarse grained, round up to next DPMs
3637 	if (num_fclk_dpms > 2) {
3638 		for (i = *num_entries - 1; i >= 0 ; i--) {
3639 			for (j = 0; j < num_fclk_dpms; j++) {
3640 				if (bw_params->clk_table.entries[j].fclk_mhz >= table[i].fabricclk_mhz) {
3641 					table[i].fabricclk_mhz = bw_params->clk_table.entries[j].fclk_mhz;
3642 					break;
3643 				}
3644 			}
3645 		}
3646 	}
3647 	// Otherwise, round up to minimum.
3648 	else {
3649 		for (i = *num_entries - 1; i >= 0 ; i--) {
3650 			if (table[i].fabricclk_mhz < min_fclk_mhz) {
3651 				table[i].fabricclk_mhz = min_fclk_mhz;
3652 				break;
3653 			}
3654 		}
3655 	}
3656 
3657 	// Round DCFCLKs up to minimum
3658 	for (i = *num_entries - 1; i >= 0 ; i--) {
3659 		if (table[i].dcfclk_mhz < min_dcfclk_mhz) {
3660 			table[i].dcfclk_mhz = min_dcfclk_mhz;
3661 			break;
3662 		}
3663 	}
3664 
3665 	// Remove duplicate states, note duplicate states are always neighbouring since table is sorted.
3666 	i = 0;
3667 	while (i < *num_entries - 1) {
3668 		if (table[i].dcfclk_mhz == table[i + 1].dcfclk_mhz &&
3669 				table[i].fabricclk_mhz == table[i + 1].fabricclk_mhz &&
3670 				table[i].dram_speed_mts == table[i + 1].dram_speed_mts)
3671 			remove_entry_from_table_at_index(table, num_entries, i + 1);
3672 		else
3673 			i++;
3674 	}
3675 
3676 	// Fix up the state indicies
3677 	for (i = *num_entries - 1; i >= 0 ; i--) {
3678 		table[i].state = i;
3679 	}
3680 
3681 	return 0;
3682 }
3683 
3684 /* dcn32_update_bw_bounding_box
3685  * This would override some dcn3_2 ip_or_soc initial parameters hardcoded from spreadsheet
3686  * with actual values as per dGPU SKU:
3687  * -with passed few options from dc->config
3688  * -with dentist_vco_frequency from Clk Mgr (currently hardcoded, but might need to get it from PM FW)
3689  * -with passed latency values (passed in ns units) in dc-> bb override for debugging purposes
3690  * -with passed latencies from VBIOS (in 100_ns units) if available for certain dGPU SKU
3691  * -with number of DRAM channels from VBIOS (which differ for certain dGPU SKU of the same ASIC)
3692  * -clocks levels with passed clk_table entries from Clk Mgr as reported by PM FW for different
3693  *  clocks (which might differ for certain dGPU SKU of the same ASIC)
3694  */
3695 static void dcn32_update_bw_bounding_box(struct dc *dc, struct clk_bw_params *bw_params)
3696 {
3697 	if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
3698 
3699 		/* Overrides from dc->config options */
3700 		dcn3_2_ip.clamp_min_dcfclk = dc->config.clamp_min_dcfclk;
3701 
3702 		/* Override from passed dc->bb_overrides if available*/
3703 		if ((int)(dcn3_2_soc.sr_exit_time_us * 1000) != dc->bb_overrides.sr_exit_time_ns
3704 				&& dc->bb_overrides.sr_exit_time_ns) {
3705 			dcn3_2_soc.sr_exit_time_us = dc->bb_overrides.sr_exit_time_ns / 1000.0;
3706 		}
3707 
3708 		if ((int)(dcn3_2_soc.sr_enter_plus_exit_time_us * 1000)
3709 				!= dc->bb_overrides.sr_enter_plus_exit_time_ns
3710 				&& dc->bb_overrides.sr_enter_plus_exit_time_ns) {
3711 			dcn3_2_soc.sr_enter_plus_exit_time_us =
3712 				dc->bb_overrides.sr_enter_plus_exit_time_ns / 1000.0;
3713 		}
3714 
3715 		if ((int)(dcn3_2_soc.urgent_latency_us * 1000) != dc->bb_overrides.urgent_latency_ns
3716 			&& dc->bb_overrides.urgent_latency_ns) {
3717 			dcn3_2_soc.urgent_latency_us = dc->bb_overrides.urgent_latency_ns / 1000.0;
3718 		}
3719 
3720 		if ((int)(dcn3_2_soc.dram_clock_change_latency_us * 1000)
3721 				!= dc->bb_overrides.dram_clock_change_latency_ns
3722 				&& dc->bb_overrides.dram_clock_change_latency_ns) {
3723 			dcn3_2_soc.dram_clock_change_latency_us =
3724 				dc->bb_overrides.dram_clock_change_latency_ns / 1000.0;
3725 		}
3726 
3727 		if ((int)(dcn3_2_soc.dummy_pstate_latency_us * 1000)
3728 				!= dc->bb_overrides.dummy_clock_change_latency_ns
3729 				&& dc->bb_overrides.dummy_clock_change_latency_ns) {
3730 			dcn3_2_soc.dummy_pstate_latency_us =
3731 				dc->bb_overrides.dummy_clock_change_latency_ns / 1000.0;
3732 		}
3733 
3734 		/* Override from VBIOS if VBIOS bb_info available */
3735 		if (dc->ctx->dc_bios->funcs->get_soc_bb_info) {
3736 			struct bp_soc_bb_info bb_info = {0};
3737 
3738 			if (dc->ctx->dc_bios->funcs->get_soc_bb_info(dc->ctx->dc_bios, &bb_info) == BP_RESULT_OK) {
3739 				if (bb_info.dram_clock_change_latency_100ns > 0)
3740 					dcn3_2_soc.dram_clock_change_latency_us = bb_info.dram_clock_change_latency_100ns * 10;
3741 
3742 			if (bb_info.dram_sr_enter_exit_latency_100ns > 0)
3743 				dcn3_2_soc.sr_enter_plus_exit_time_us = bb_info.dram_sr_enter_exit_latency_100ns * 10;
3744 
3745 			if (bb_info.dram_sr_exit_latency_100ns > 0)
3746 				dcn3_2_soc.sr_exit_time_us = bb_info.dram_sr_exit_latency_100ns * 10;
3747 			}
3748 		}
3749 
3750 		/* Override from VBIOS for num_chan */
3751 		if (dc->ctx->dc_bios->vram_info.num_chans)
3752 			dcn3_2_soc.num_chans = dc->ctx->dc_bios->vram_info.num_chans;
3753 
3754 		if (dc->ctx->dc_bios->vram_info.dram_channel_width_bytes)
3755 			dcn3_2_soc.dram_channel_width_bytes = dc->ctx->dc_bios->vram_info.dram_channel_width_bytes;
3756 
3757 	}
3758 
3759 	/* Override dispclk_dppclk_vco_speed_mhz from Clk Mgr */
3760 	dcn3_2_soc.dispclk_dppclk_vco_speed_mhz = dc->clk_mgr->dentist_vco_freq_khz / 1000.0;
3761 	dc->dml.soc.dispclk_dppclk_vco_speed_mhz = dc->clk_mgr->dentist_vco_freq_khz / 1000.0;
3762 
3763 	/* Overrides Clock levelsfrom CLK Mgr table entries as reported by PM FW */
3764 	if ((!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) && (bw_params->clk_table.entries[0].memclk_mhz)) {
3765 		if (dc->debug.use_legacy_soc_bb_mechanism) {
3766 			unsigned int i = 0, j = 0, num_states = 0;
3767 
3768 			unsigned int dcfclk_mhz[DC__VOLTAGE_STATES] = {0};
3769 			unsigned int dram_speed_mts[DC__VOLTAGE_STATES] = {0};
3770 			unsigned int optimal_uclk_for_dcfclk_sta_targets[DC__VOLTAGE_STATES] = {0};
3771 			unsigned int optimal_dcfclk_for_uclk[DC__VOLTAGE_STATES] = {0};
3772 			unsigned int min_dcfclk = UINT_MAX;
3773 			/* Set 199 as first value in STA target array to have a minimum DCFCLK value.
3774 			 * For DCN32 we set min to 199 so minimum FCLK DPM0 (300Mhz can be achieved) */
3775 			unsigned int dcfclk_sta_targets[DC__VOLTAGE_STATES] = {199, 615, 906, 1324, 1564};
3776 			unsigned int num_dcfclk_sta_targets = 4, num_uclk_states = 0;
3777 			unsigned int max_dcfclk_mhz = 0, max_dispclk_mhz = 0, max_dppclk_mhz = 0, max_phyclk_mhz = 0;
3778 
3779 			for (i = 0; i < MAX_NUM_DPM_LVL; i++) {
3780 				if (bw_params->clk_table.entries[i].dcfclk_mhz > max_dcfclk_mhz)
3781 					max_dcfclk_mhz = bw_params->clk_table.entries[i].dcfclk_mhz;
3782 				if (bw_params->clk_table.entries[i].dcfclk_mhz != 0 &&
3783 						bw_params->clk_table.entries[i].dcfclk_mhz < min_dcfclk)
3784 					min_dcfclk = bw_params->clk_table.entries[i].dcfclk_mhz;
3785 				if (bw_params->clk_table.entries[i].dispclk_mhz > max_dispclk_mhz)
3786 					max_dispclk_mhz = bw_params->clk_table.entries[i].dispclk_mhz;
3787 				if (bw_params->clk_table.entries[i].dppclk_mhz > max_dppclk_mhz)
3788 					max_dppclk_mhz = bw_params->clk_table.entries[i].dppclk_mhz;
3789 				if (bw_params->clk_table.entries[i].phyclk_mhz > max_phyclk_mhz)
3790 					max_phyclk_mhz = bw_params->clk_table.entries[i].phyclk_mhz;
3791 			}
3792 			if (min_dcfclk > dcfclk_sta_targets[0])
3793 				dcfclk_sta_targets[0] = min_dcfclk;
3794 			if (!max_dcfclk_mhz)
3795 				max_dcfclk_mhz = dcn3_2_soc.clock_limits[0].dcfclk_mhz;
3796 			if (!max_dispclk_mhz)
3797 				max_dispclk_mhz = dcn3_2_soc.clock_limits[0].dispclk_mhz;
3798 			if (!max_dppclk_mhz)
3799 				max_dppclk_mhz = dcn3_2_soc.clock_limits[0].dppclk_mhz;
3800 			if (!max_phyclk_mhz)
3801 				max_phyclk_mhz = dcn3_2_soc.clock_limits[0].phyclk_mhz;
3802 
3803 			if (max_dcfclk_mhz > dcfclk_sta_targets[num_dcfclk_sta_targets-1]) {
3804 				// If max DCFCLK is greater than the max DCFCLK STA target, insert into the DCFCLK STA target array
3805 				dcfclk_sta_targets[num_dcfclk_sta_targets] = max_dcfclk_mhz;
3806 				num_dcfclk_sta_targets++;
3807 			} else if (max_dcfclk_mhz < dcfclk_sta_targets[num_dcfclk_sta_targets-1]) {
3808 				// If max DCFCLK is less than the max DCFCLK STA target, cap values and remove duplicates
3809 				for (i = 0; i < num_dcfclk_sta_targets; i++) {
3810 					if (dcfclk_sta_targets[i] > max_dcfclk_mhz) {
3811 						dcfclk_sta_targets[i] = max_dcfclk_mhz;
3812 						break;
3813 					}
3814 				}
3815 				// Update size of array since we "removed" duplicates
3816 				num_dcfclk_sta_targets = i + 1;
3817 			}
3818 
3819 			num_uclk_states = bw_params->clk_table.num_entries;
3820 
3821 			// Calculate optimal dcfclk for each uclk
3822 			for (i = 0; i < num_uclk_states; i++) {
3823 				dcn32_get_optimal_dcfclk_fclk_for_uclk(bw_params->clk_table.entries[i].memclk_mhz * 16,
3824 						&optimal_dcfclk_for_uclk[i], NULL);
3825 				if (optimal_dcfclk_for_uclk[i] < bw_params->clk_table.entries[0].dcfclk_mhz) {
3826 					optimal_dcfclk_for_uclk[i] = bw_params->clk_table.entries[0].dcfclk_mhz;
3827 				}
3828 			}
3829 
3830 			// Calculate optimal uclk for each dcfclk sta target
3831 			for (i = 0; i < num_dcfclk_sta_targets; i++) {
3832 				for (j = 0; j < num_uclk_states; j++) {
3833 					if (dcfclk_sta_targets[i] < optimal_dcfclk_for_uclk[j]) {
3834 						optimal_uclk_for_dcfclk_sta_targets[i] =
3835 								bw_params->clk_table.entries[j].memclk_mhz * 16;
3836 						break;
3837 					}
3838 				}
3839 			}
3840 
3841 			i = 0;
3842 			j = 0;
3843 			// create the final dcfclk and uclk table
3844 			while (i < num_dcfclk_sta_targets && j < num_uclk_states && num_states < DC__VOLTAGE_STATES) {
3845 				if (dcfclk_sta_targets[i] < optimal_dcfclk_for_uclk[j] && i < num_dcfclk_sta_targets) {
3846 					dcfclk_mhz[num_states] = dcfclk_sta_targets[i];
3847 					dram_speed_mts[num_states++] = optimal_uclk_for_dcfclk_sta_targets[i++];
3848 				} else {
3849 					if (j < num_uclk_states && optimal_dcfclk_for_uclk[j] <= max_dcfclk_mhz) {
3850 						dcfclk_mhz[num_states] = optimal_dcfclk_for_uclk[j];
3851 						dram_speed_mts[num_states++] = bw_params->clk_table.entries[j++].memclk_mhz * 16;
3852 					} else {
3853 						j = num_uclk_states;
3854 					}
3855 				}
3856 			}
3857 
3858 			while (i < num_dcfclk_sta_targets && num_states < DC__VOLTAGE_STATES) {
3859 				dcfclk_mhz[num_states] = dcfclk_sta_targets[i];
3860 				dram_speed_mts[num_states++] = optimal_uclk_for_dcfclk_sta_targets[i++];
3861 			}
3862 
3863 			while (j < num_uclk_states && num_states < DC__VOLTAGE_STATES &&
3864 					optimal_dcfclk_for_uclk[j] <= max_dcfclk_mhz) {
3865 				dcfclk_mhz[num_states] = optimal_dcfclk_for_uclk[j];
3866 				dram_speed_mts[num_states++] = bw_params->clk_table.entries[j++].memclk_mhz * 16;
3867 			}
3868 
3869 			dcn3_2_soc.num_states = num_states;
3870 			for (i = 0; i < dcn3_2_soc.num_states; i++) {
3871 				dcn3_2_soc.clock_limits[i].state = i;
3872 				dcn3_2_soc.clock_limits[i].dcfclk_mhz = dcfclk_mhz[i];
3873 				dcn3_2_soc.clock_limits[i].fabricclk_mhz = dcfclk_mhz[i];
3874 
3875 				/* Fill all states with max values of all these clocks */
3876 				dcn3_2_soc.clock_limits[i].dispclk_mhz = max_dispclk_mhz;
3877 				dcn3_2_soc.clock_limits[i].dppclk_mhz  = max_dppclk_mhz;
3878 				dcn3_2_soc.clock_limits[i].phyclk_mhz  = max_phyclk_mhz;
3879 				dcn3_2_soc.clock_limits[i].dscclk_mhz  = max_dispclk_mhz / 3;
3880 
3881 				/* Populate from bw_params for DTBCLK, SOCCLK */
3882 				if (i > 0) {
3883 					if (!bw_params->clk_table.entries[i].dtbclk_mhz) {
3884 						dcn3_2_soc.clock_limits[i].dtbclk_mhz  = dcn3_2_soc.clock_limits[i-1].dtbclk_mhz;
3885 					} else {
3886 						dcn3_2_soc.clock_limits[i].dtbclk_mhz  = bw_params->clk_table.entries[i].dtbclk_mhz;
3887 					}
3888 				} else if (bw_params->clk_table.entries[i].dtbclk_mhz) {
3889 					dcn3_2_soc.clock_limits[i].dtbclk_mhz  = bw_params->clk_table.entries[i].dtbclk_mhz;
3890 				}
3891 
3892 				if (!bw_params->clk_table.entries[i].socclk_mhz && i > 0)
3893 					dcn3_2_soc.clock_limits[i].socclk_mhz = dcn3_2_soc.clock_limits[i-1].socclk_mhz;
3894 				else
3895 					dcn3_2_soc.clock_limits[i].socclk_mhz = bw_params->clk_table.entries[i].socclk_mhz;
3896 
3897 				if (!dram_speed_mts[i] && i > 0)
3898 					dcn3_2_soc.clock_limits[i].dram_speed_mts = dcn3_2_soc.clock_limits[i-1].dram_speed_mts;
3899 				else
3900 					dcn3_2_soc.clock_limits[i].dram_speed_mts = dram_speed_mts[i];
3901 
3902 				/* These clocks cannot come from bw_params, always fill from dcn3_2_soc[0] */
3903 				/* PHYCLK_D18, PHYCLK_D32 */
3904 				dcn3_2_soc.clock_limits[i].phyclk_d18_mhz = dcn3_2_soc.clock_limits[0].phyclk_d18_mhz;
3905 				dcn3_2_soc.clock_limits[i].phyclk_d32_mhz = dcn3_2_soc.clock_limits[0].phyclk_d32_mhz;
3906 			}
3907 		} else {
3908 			build_synthetic_soc_states(bw_params, dcn3_2_soc.clock_limits, &dcn3_2_soc.num_states);
3909 		}
3910 
3911 		/* Re-init DML with updated bb */
3912 		dml_init_instance(&dc->dml, &dcn3_2_soc, &dcn3_2_ip, DML_PROJECT_DCN32);
3913 		if (dc->current_state)
3914 			dml_init_instance(&dc->current_state->bw_ctx.dml, &dcn3_2_soc, &dcn3_2_ip, DML_PROJECT_DCN32);
3915 	}
3916 }
3917 
3918 static struct resource_funcs dcn32_res_pool_funcs = {
3919 	.destroy = dcn32_destroy_resource_pool,
3920 	.link_enc_create = dcn32_link_encoder_create,
3921 	.link_enc_create_minimal = NULL,
3922 	.panel_cntl_create = dcn32_panel_cntl_create,
3923 	.validate_bandwidth = dcn32_validate_bandwidth,
3924 	.calculate_wm_and_dlg = dcn32_calculate_wm_and_dlg,
3925 	.populate_dml_pipes = dcn32_populate_dml_pipes_from_context,
3926 	.acquire_idle_pipe_for_layer = dcn20_acquire_idle_pipe_for_layer,
3927 	.add_stream_to_ctx = dcn30_add_stream_to_ctx,
3928 	.add_dsc_to_stream_resource = dcn20_add_dsc_to_stream_resource,
3929 	.remove_stream_from_ctx = dcn20_remove_stream_from_ctx,
3930 	.populate_dml_writeback_from_context = dcn30_populate_dml_writeback_from_context,
3931 	.set_mcif_arb_params = dcn30_set_mcif_arb_params,
3932 	.find_first_free_match_stream_enc_for_link = dcn10_find_first_free_match_stream_enc_for_link,
3933 	.acquire_post_bldn_3dlut = dcn32_acquire_post_bldn_3dlut,
3934 	.release_post_bldn_3dlut = dcn32_release_post_bldn_3dlut,
3935 	.update_bw_bounding_box = dcn32_update_bw_bounding_box,
3936 	.patch_unknown_plane_state = dcn20_patch_unknown_plane_state,
3937 	.update_soc_for_wm_a = dcn30_update_soc_for_wm_a,
3938 	.add_phantom_pipes = dcn32_add_phantom_pipes,
3939 	.remove_phantom_pipes = dcn32_remove_phantom_pipes,
3940 };
3941 
3942 
3943 static bool dcn32_resource_construct(
3944 	uint8_t num_virtual_links,
3945 	struct dc *dc,
3946 	struct dcn32_resource_pool *pool)
3947 {
3948 	int i, j;
3949 	struct dc_context *ctx = dc->ctx;
3950 	struct irq_service_init_data init_data;
3951 	struct ddc_service_init_data ddc_init_data = {0};
3952 	uint32_t pipe_fuses = 0;
3953 	uint32_t num_pipes  = 4;
3954 
3955     DC_FP_START();
3956 
3957 	ctx->dc_bios->regs = &bios_regs;
3958 
3959 	pool->base.res_cap = &res_cap_dcn32;
3960 	/* max number of pipes for ASIC before checking for pipe fuses */
3961 	num_pipes  = pool->base.res_cap->num_timing_generator;
3962 	pipe_fuses = REG_READ(CC_DC_PIPE_DIS);
3963 
3964 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++)
3965 		if (pipe_fuses & 1 << i)
3966 			num_pipes--;
3967 
3968 	if (pipe_fuses & 1)
3969 		ASSERT(0); //Unexpected - Pipe 0 should always be fully functional!
3970 
3971 	if (pipe_fuses & CC_DC_PIPE_DIS__DC_FULL_DIS_MASK)
3972 		ASSERT(0); //Entire DCN is harvested!
3973 
3974 	/* within dml lib, initial value is hard coded, if ASIC pipe is fused, the
3975 	 * value will be changed, update max_num_dpp and max_num_otg for dml.
3976 	 */
3977 	dcn3_2_ip.max_num_dpp = num_pipes;
3978 	dcn3_2_ip.max_num_otg = num_pipes;
3979 
3980 	pool->base.funcs = &dcn32_res_pool_funcs;
3981 
3982 	/*************************************************
3983 	 *  Resource + asic cap harcoding                *
3984 	 *************************************************/
3985 	pool->base.underlay_pipe_index = NO_UNDERLAY_PIPE;
3986 	pool->base.timing_generator_count = num_pipes;
3987 	pool->base.pipe_count = num_pipes;
3988 	pool->base.mpcc_count = num_pipes;
3989 	dc->caps.max_downscale_ratio = 600;
3990 	dc->caps.i2c_speed_in_khz = 100;
3991 	dc->caps.i2c_speed_in_khz_hdcp = 100; /*1.4 w/a applied by default*/
3992 	dc->caps.max_cursor_size = 256;
3993 	dc->caps.min_horizontal_blanking_period = 80;
3994 	dc->caps.dmdata_alloc_size = 2048;
3995 	dc->caps.mall_size_per_mem_channel = 0;
3996 	dc->caps.mall_size_total = 0;
3997 	dc->caps.cursor_cache_size = dc->caps.max_cursor_size * dc->caps.max_cursor_size * 8;
3998 
3999 	dc->caps.cache_line_size = 64;
4000 	dc->caps.cache_num_ways = 16;
4001 	dc->caps.max_cab_allocation_bytes = 67108864; // 64MB = 1024 * 1024 * 64
4002 	dc->caps.subvp_fw_processing_delay_us = 15;
4003 	dc->caps.subvp_prefetch_end_to_mall_start_us = 15;
4004 	dc->caps.subvp_pstate_allow_width_us = 20;
4005 	dc->caps.subvp_vertical_int_margin_us = 30;
4006 
4007 	dc->caps.max_slave_planes = 2;
4008 	dc->caps.max_slave_yuv_planes = 2;
4009 	dc->caps.max_slave_rgb_planes = 2;
4010 	dc->caps.post_blend_color_processing = true;
4011 	dc->caps.force_dp_tps4_for_cp2520 = true;
4012 	dc->caps.dp_hpo = true;
4013 	dc->caps.edp_dsc_support = true;
4014 	dc->caps.extended_aux_timeout_support = true;
4015 	dc->caps.dmcub_support = true;
4016 
4017 	/* Color pipeline capabilities */
4018 	dc->caps.color.dpp.dcn_arch = 1;
4019 	dc->caps.color.dpp.input_lut_shared = 0;
4020 	dc->caps.color.dpp.icsc = 1;
4021 	dc->caps.color.dpp.dgam_ram = 0; // must use gamma_corr
4022 	dc->caps.color.dpp.dgam_rom_caps.srgb = 1;
4023 	dc->caps.color.dpp.dgam_rom_caps.bt2020 = 1;
4024 	dc->caps.color.dpp.dgam_rom_caps.gamma2_2 = 1;
4025 	dc->caps.color.dpp.dgam_rom_caps.pq = 1;
4026 	dc->caps.color.dpp.dgam_rom_caps.hlg = 1;
4027 	dc->caps.color.dpp.post_csc = 1;
4028 	dc->caps.color.dpp.gamma_corr = 1;
4029 	dc->caps.color.dpp.dgam_rom_for_yuv = 0;
4030 
4031 	dc->caps.color.dpp.hw_3d_lut = 1;
4032 	dc->caps.color.dpp.ogam_ram = 0;  // no OGAM in DPP since DCN1
4033 	// no OGAM ROM on DCN2 and later ASICs
4034 	dc->caps.color.dpp.ogam_rom_caps.srgb = 0;
4035 	dc->caps.color.dpp.ogam_rom_caps.bt2020 = 0;
4036 	dc->caps.color.dpp.ogam_rom_caps.gamma2_2 = 0;
4037 	dc->caps.color.dpp.ogam_rom_caps.pq = 0;
4038 	dc->caps.color.dpp.ogam_rom_caps.hlg = 0;
4039 	dc->caps.color.dpp.ocsc = 0;
4040 
4041 	dc->caps.color.mpc.gamut_remap = 1;
4042 	dc->caps.color.mpc.num_3dluts = pool->base.res_cap->num_mpc_3dlut; //4, configurable to be before or after BLND in MPCC
4043 	dc->caps.color.mpc.ogam_ram = 1;
4044 	dc->caps.color.mpc.ogam_rom_caps.srgb = 0;
4045 	dc->caps.color.mpc.ogam_rom_caps.bt2020 = 0;
4046 	dc->caps.color.mpc.ogam_rom_caps.gamma2_2 = 0;
4047 	dc->caps.color.mpc.ogam_rom_caps.pq = 0;
4048 	dc->caps.color.mpc.ogam_rom_caps.hlg = 0;
4049 	dc->caps.color.mpc.ocsc = 1;
4050 
4051 	/* Use pipe context based otg sync logic */
4052 	dc->config.use_pipe_ctx_sync_logic = true;
4053 
4054 	/* read VBIOS LTTPR caps */
4055 	{
4056 		if (ctx->dc_bios->funcs->get_lttpr_caps) {
4057 			enum bp_result bp_query_result;
4058 			uint8_t is_vbios_lttpr_enable = 0;
4059 
4060 			bp_query_result = ctx->dc_bios->funcs->get_lttpr_caps(ctx->dc_bios, &is_vbios_lttpr_enable);
4061 			dc->caps.vbios_lttpr_enable = (bp_query_result == BP_RESULT_OK) && !!is_vbios_lttpr_enable;
4062 		}
4063 
4064 		/* interop bit is implicit */
4065 		{
4066 			dc->caps.vbios_lttpr_aware = true;
4067 		}
4068 	}
4069 
4070 	if (dc->ctx->dce_environment == DCE_ENV_PRODUCTION_DRV)
4071 		dc->debug = debug_defaults_drv;
4072 	else if (dc->ctx->dce_environment == DCE_ENV_FPGA_MAXIMUS) {
4073 		dc->debug = debug_defaults_diags;
4074 	} else
4075 		dc->debug = debug_defaults_diags;
4076 	// Init the vm_helper
4077 	if (dc->vm_helper)
4078 		vm_helper_init(dc->vm_helper, 16);
4079 
4080 	/*************************************************
4081 	 *  Create resources                             *
4082 	 *************************************************/
4083 
4084 	/* Clock Sources for Pixel Clock*/
4085 	pool->base.clock_sources[DCN32_CLK_SRC_PLL0] =
4086 			dcn32_clock_source_create(ctx, ctx->dc_bios,
4087 				CLOCK_SOURCE_COMBO_PHY_PLL0,
4088 				&clk_src_regs[0], false);
4089 	pool->base.clock_sources[DCN32_CLK_SRC_PLL1] =
4090 			dcn32_clock_source_create(ctx, ctx->dc_bios,
4091 				CLOCK_SOURCE_COMBO_PHY_PLL1,
4092 				&clk_src_regs[1], false);
4093 	pool->base.clock_sources[DCN32_CLK_SRC_PLL2] =
4094 			dcn32_clock_source_create(ctx, ctx->dc_bios,
4095 				CLOCK_SOURCE_COMBO_PHY_PLL2,
4096 				&clk_src_regs[2], false);
4097 	pool->base.clock_sources[DCN32_CLK_SRC_PLL3] =
4098 			dcn32_clock_source_create(ctx, ctx->dc_bios,
4099 				CLOCK_SOURCE_COMBO_PHY_PLL3,
4100 				&clk_src_regs[3], false);
4101 	pool->base.clock_sources[DCN32_CLK_SRC_PLL4] =
4102 			dcn32_clock_source_create(ctx, ctx->dc_bios,
4103 				CLOCK_SOURCE_COMBO_PHY_PLL4,
4104 				&clk_src_regs[4], false);
4105 
4106 	pool->base.clk_src_count = DCN32_CLK_SRC_TOTAL;
4107 
4108 	/* todo: not reuse phy_pll registers */
4109 	pool->base.dp_clock_source =
4110 			dcn32_clock_source_create(ctx, ctx->dc_bios,
4111 				CLOCK_SOURCE_ID_DP_DTO,
4112 				&clk_src_regs[0], true);
4113 
4114 	for (i = 0; i < pool->base.clk_src_count; i++) {
4115 		if (pool->base.clock_sources[i] == NULL) {
4116 			dm_error("DC: failed to create clock sources!\n");
4117 			BREAK_TO_DEBUGGER();
4118 			goto create_fail;
4119 		}
4120 	}
4121 
4122 	/* DCCG */
4123 	pool->base.dccg = dccg32_create(ctx, &dccg_regs, &dccg_shift, &dccg_mask);
4124 	if (pool->base.dccg == NULL) {
4125 		dm_error("DC: failed to create dccg!\n");
4126 		BREAK_TO_DEBUGGER();
4127 		goto create_fail;
4128 	}
4129 
4130 	/* DML */
4131 	if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment))
4132 		dml_init_instance(&dc->dml, &dcn3_2_soc, &dcn3_2_ip, DML_PROJECT_DCN32);
4133 
4134 	/* IRQ Service */
4135 	init_data.ctx = dc->ctx;
4136 	pool->base.irqs = dal_irq_service_dcn32_create(&init_data);
4137 	if (!pool->base.irqs)
4138 		goto create_fail;
4139 
4140 	/* HUBBUB */
4141 	pool->base.hubbub = dcn32_hubbub_create(ctx);
4142 	if (pool->base.hubbub == NULL) {
4143 		BREAK_TO_DEBUGGER();
4144 		dm_error("DC: failed to create hubbub!\n");
4145 		goto create_fail;
4146 	}
4147 
4148 	/* HUBPs, DPPs, OPPs, TGs, ABMs */
4149 	for (i = 0, j = 0; i < pool->base.res_cap->num_timing_generator; i++) {
4150 
4151 		/* if pipe is disabled, skip instance of HW pipe,
4152 		 * i.e, skip ASIC register instance
4153 		 */
4154 		if (pipe_fuses & 1 << i)
4155 			continue;
4156 
4157 		/* HUBPs */
4158 		pool->base.hubps[j] = dcn32_hubp_create(ctx, i);
4159 		if (pool->base.hubps[j] == NULL) {
4160 			BREAK_TO_DEBUGGER();
4161 			dm_error(
4162 				"DC: failed to create hubps!\n");
4163 			goto create_fail;
4164 		}
4165 
4166 		/* DPPs */
4167 		pool->base.dpps[j] = dcn32_dpp_create(ctx, i);
4168 		if (pool->base.dpps[j] == NULL) {
4169 			BREAK_TO_DEBUGGER();
4170 			dm_error(
4171 				"DC: failed to create dpps!\n");
4172 			goto create_fail;
4173 		}
4174 
4175 		/* OPPs */
4176 		pool->base.opps[j] = dcn32_opp_create(ctx, i);
4177 		if (pool->base.opps[j] == NULL) {
4178 			BREAK_TO_DEBUGGER();
4179 			dm_error(
4180 				"DC: failed to create output pixel processor!\n");
4181 			goto create_fail;
4182 		}
4183 
4184 		/* TGs */
4185 		pool->base.timing_generators[j] = dcn32_timing_generator_create(
4186 				ctx, i);
4187 		if (pool->base.timing_generators[j] == NULL) {
4188 			BREAK_TO_DEBUGGER();
4189 			dm_error("DC: failed to create tg!\n");
4190 			goto create_fail;
4191 		}
4192 
4193 		/* ABMs */
4194 		pool->base.multiple_abms[j] = dmub_abm_create(ctx,
4195 				&abm_regs[i],
4196 				&abm_shift,
4197 				&abm_mask);
4198 		if (pool->base.multiple_abms[j] == NULL) {
4199 			dm_error("DC: failed to create abm for pipe %d!\n", i);
4200 			BREAK_TO_DEBUGGER();
4201 			goto create_fail;
4202 		}
4203 
4204 		/* index for resource pool arrays for next valid pipe */
4205 		j++;
4206 	}
4207 
4208 	/* PSR */
4209 	pool->base.psr = dmub_psr_create(ctx);
4210 	if (pool->base.psr == NULL) {
4211 		dm_error("DC: failed to create psr obj!\n");
4212 		BREAK_TO_DEBUGGER();
4213 		goto create_fail;
4214 	}
4215 
4216 	/* MPCCs */
4217 	pool->base.mpc = dcn32_mpc_create(ctx, pool->base.res_cap->num_timing_generator, pool->base.res_cap->num_mpc_3dlut);
4218 	if (pool->base.mpc == NULL) {
4219 		BREAK_TO_DEBUGGER();
4220 		dm_error("DC: failed to create mpc!\n");
4221 		goto create_fail;
4222 	}
4223 
4224 	/* DSCs */
4225 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
4226 		pool->base.dscs[i] = dcn32_dsc_create(ctx, i);
4227 		if (pool->base.dscs[i] == NULL) {
4228 			BREAK_TO_DEBUGGER();
4229 			dm_error("DC: failed to create display stream compressor %d!\n", i);
4230 			goto create_fail;
4231 		}
4232 	}
4233 
4234 	/* DWB */
4235 	if (!dcn32_dwbc_create(ctx, &pool->base)) {
4236 		BREAK_TO_DEBUGGER();
4237 		dm_error("DC: failed to create dwbc!\n");
4238 		goto create_fail;
4239 	}
4240 
4241 	/* MMHUBBUB */
4242 	if (!dcn32_mmhubbub_create(ctx, &pool->base)) {
4243 		BREAK_TO_DEBUGGER();
4244 		dm_error("DC: failed to create mcif_wb!\n");
4245 		goto create_fail;
4246 	}
4247 
4248 	/* AUX and I2C */
4249 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
4250 		pool->base.engines[i] = dcn32_aux_engine_create(ctx, i);
4251 		if (pool->base.engines[i] == NULL) {
4252 			BREAK_TO_DEBUGGER();
4253 			dm_error(
4254 				"DC:failed to create aux engine!!\n");
4255 			goto create_fail;
4256 		}
4257 		pool->base.hw_i2cs[i] = dcn32_i2c_hw_create(ctx, i);
4258 		if (pool->base.hw_i2cs[i] == NULL) {
4259 			BREAK_TO_DEBUGGER();
4260 			dm_error(
4261 				"DC:failed to create hw i2c!!\n");
4262 			goto create_fail;
4263 		}
4264 		pool->base.sw_i2cs[i] = NULL;
4265 	}
4266 
4267 	/* Audio, HWSeq, Stream Encoders including HPO and virtual, MPC 3D LUTs */
4268 	if (!resource_construct(num_virtual_links, dc, &pool->base,
4269 			(!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment) ?
4270 			&res_create_funcs : &res_create_maximus_funcs)))
4271 			goto create_fail;
4272 
4273 	/* HW Sequencer init functions and Plane caps */
4274 	dcn32_hw_sequencer_init_functions(dc);
4275 
4276 	dc->caps.max_planes =  pool->base.pipe_count;
4277 
4278 	for (i = 0; i < dc->caps.max_planes; ++i)
4279 		dc->caps.planes[i] = plane_cap;
4280 
4281 	dc->cap_funcs = cap_funcs;
4282 
4283 	if (dc->ctx->dc_bios->fw_info.oem_i2c_present) {
4284 		ddc_init_data.ctx = dc->ctx;
4285 		ddc_init_data.link = NULL;
4286 		ddc_init_data.id.id = dc->ctx->dc_bios->fw_info.oem_i2c_obj_id;
4287 		ddc_init_data.id.enum_id = 0;
4288 		ddc_init_data.id.type = OBJECT_TYPE_GENERIC;
4289 		pool->base.oem_device = dal_ddc_service_create(&ddc_init_data);
4290 	} else {
4291 		pool->base.oem_device = NULL;
4292 	}
4293 
4294     DC_FP_END();
4295 
4296 	return true;
4297 
4298 create_fail:
4299 
4300     DC_FP_END();
4301 
4302 	dcn32_resource_destruct(pool);
4303 
4304 	return false;
4305 }
4306 
4307 struct resource_pool *dcn32_create_resource_pool(
4308 		const struct dc_init_data *init_data,
4309 		struct dc *dc)
4310 {
4311 	struct dcn32_resource_pool *pool =
4312 		kzalloc(sizeof(struct dcn32_resource_pool), GFP_KERNEL);
4313 
4314 	if (!pool)
4315 		return NULL;
4316 
4317 	if (dcn32_resource_construct(init_data->num_virtual_links, dc, pool))
4318 		return &pool->base;
4319 
4320 	BREAK_TO_DEBUGGER();
4321 	kfree(pool);
4322 	return NULL;
4323 }
4324