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 "link.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 #include "dml/dcn32/dcn32_fpu.h"
92 
93 #define DC_LOGGER_INIT(logger)
94 
95 enum dcn32_clk_src_array_id {
96 	DCN32_CLK_SRC_PLL0,
97 	DCN32_CLK_SRC_PLL1,
98 	DCN32_CLK_SRC_PLL2,
99 	DCN32_CLK_SRC_PLL3,
100 	DCN32_CLK_SRC_PLL4,
101 	DCN32_CLK_SRC_TOTAL
102 };
103 
104 /* begin *********************
105  * macros to expend register list macro defined in HW object header file
106  */
107 
108 /* DCN */
109 #define BASE_INNER(seg) ctx->dcn_reg_offsets[seg]
110 
111 #define BASE(seg) BASE_INNER(seg)
112 
113 #define SR(reg_name)\
114 		REG_STRUCT.reg_name = BASE(reg ## reg_name ## _BASE_IDX) +  \
115 					reg ## reg_name
116 #define SR_ARR(reg_name, id) \
117 	REG_STRUCT[id].reg_name = BASE(reg##reg_name##_BASE_IDX) + reg##reg_name
118 
119 #define SR_ARR_INIT(reg_name, id, value) \
120 	REG_STRUCT[id].reg_name = value
121 
122 #define SRI(reg_name, block, id)\
123 	REG_STRUCT.reg_name = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
124 		reg ## block ## id ## _ ## reg_name
125 
126 #define SRI_ARR(reg_name, block, id)\
127 	REG_STRUCT[id].reg_name = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
128 		reg ## block ## id ## _ ## reg_name
129 
130 #define SR_ARR_I2C(reg_name, id) \
131 	REG_STRUCT[id-1].reg_name = BASE(reg##reg_name##_BASE_IDX) + reg##reg_name
132 
133 #define SRI_ARR_I2C(reg_name, block, id)\
134 	REG_STRUCT[id-1].reg_name = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
135 		reg ## block ## id ## _ ## reg_name
136 
137 #define SRI_ARR_ALPHABET(reg_name, block, index, id)\
138 	REG_STRUCT[index].reg_name = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
139 		reg ## block ## id ## _ ## reg_name
140 
141 #define SRI2(reg_name, block, id)\
142 	.reg_name = BASE(reg ## reg_name ## _BASE_IDX) +	\
143 		reg ## reg_name
144 #define SRI2_ARR(reg_name, block, id)\
145 	REG_STRUCT[id].reg_name = BASE(reg ## reg_name ## _BASE_IDX) +	\
146 		reg ## reg_name
147 
148 #define SRIR(var_name, reg_name, block, id)\
149 	.var_name = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
150 		reg ## block ## id ## _ ## reg_name
151 
152 #define SRII(reg_name, block, id)\
153 	REG_STRUCT.reg_name[id] = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
154 					reg ## block ## id ## _ ## reg_name
155 
156 #define SRII_ARR_2(reg_name, block, id, inst)\
157 	REG_STRUCT[inst].reg_name[id] = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
158 		reg ## block ## id ## _ ## reg_name
159 
160 #define SRII_MPC_RMU(reg_name, block, id)\
161 	.RMU##_##reg_name[id] = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
162 		reg ## block ## id ## _ ## reg_name
163 
164 #define SRII_DWB(reg_name, temp_name, block, id)\
165 	REG_STRUCT.reg_name[id] = BASE(reg ## block ## id ## _ ## temp_name ## _BASE_IDX) + \
166 		reg ## block ## id ## _ ## temp_name
167 
168 #define SF_DWB2(reg_name, block, id, field_name, post_fix)	\
169 	.field_name = reg_name ## __ ## field_name ## post_fix
170 
171 #define DCCG_SRII(reg_name, block, id)\
172 	REG_STRUCT.block ## _ ## reg_name[id] = BASE(reg ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
173 		reg ## block ## id ## _ ## reg_name
174 
175 #define VUPDATE_SRII(reg_name, block, id)\
176 	REG_STRUCT.reg_name[id] = BASE(reg ## reg_name ## _ ## block ## id ## _BASE_IDX) + \
177 		reg ## reg_name ## _ ## block ## id
178 
179 /* NBIO */
180 #define NBIO_BASE_INNER(seg) ctx->nbio_reg_offsets[seg]
181 
182 #define NBIO_BASE(seg) \
183 	NBIO_BASE_INNER(seg)
184 
185 #define NBIO_SR(reg_name)\
186 	REG_STRUCT.reg_name = NBIO_BASE(regBIF_BX0_ ## reg_name ## _BASE_IDX) + \
187 			regBIF_BX0_ ## reg_name
188 #define NBIO_SR_ARR(reg_name, id)\
189 	REG_STRUCT[id].reg_name = NBIO_BASE(regBIF_BX0_ ## reg_name ## _BASE_IDX) + \
190 		regBIF_BX0_ ## reg_name
191 
192 #undef CTX
193 #define CTX ctx
194 #define REG(reg_name) \
195 	(ctx->dcn_reg_offsets[reg ## reg_name ## _BASE_IDX] + reg ## reg_name)
196 
197 static struct bios_registers bios_regs;
198 
199 #define bios_regs_init() \
200 		( \
201 		NBIO_SR(BIOS_SCRATCH_3),\
202 		NBIO_SR(BIOS_SCRATCH_6)\
203 		)
204 
205 #define clk_src_regs_init(index, pllid)\
206 	CS_COMMON_REG_LIST_DCN3_0_RI(index, pllid)
207 
208 static struct dce110_clk_src_regs clk_src_regs[5];
209 
210 static const struct dce110_clk_src_shift cs_shift = {
211 		CS_COMMON_MASK_SH_LIST_DCN3_2(__SHIFT)
212 };
213 
214 static const struct dce110_clk_src_mask cs_mask = {
215 		CS_COMMON_MASK_SH_LIST_DCN3_2(_MASK)
216 };
217 
218 #define abm_regs_init(id)\
219 		ABM_DCN32_REG_LIST_RI(id)
220 
221 static struct dce_abm_registers abm_regs[4];
222 
223 static const struct dce_abm_shift abm_shift = {
224 		ABM_MASK_SH_LIST_DCN32(__SHIFT)
225 };
226 
227 static const struct dce_abm_mask abm_mask = {
228 		ABM_MASK_SH_LIST_DCN32(_MASK)
229 };
230 
231 #define audio_regs_init(id)\
232 		AUD_COMMON_REG_LIST_RI(id)
233 
234 static struct dce_audio_registers audio_regs[5];
235 
236 #define DCE120_AUD_COMMON_MASK_SH_LIST(mask_sh)\
237 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_INDEX, AZALIA_ENDPOINT_REG_INDEX, mask_sh),\
238 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_DATA, AZALIA_ENDPOINT_REG_DATA, mask_sh),\
239 		AUD_COMMON_MASK_SH_LIST_BASE(mask_sh)
240 
241 static const struct dce_audio_shift audio_shift = {
242 		DCE120_AUD_COMMON_MASK_SH_LIST(__SHIFT)
243 };
244 
245 static const struct dce_audio_mask audio_mask = {
246 		DCE120_AUD_COMMON_MASK_SH_LIST(_MASK)
247 };
248 
249 #define vpg_regs_init(id)\
250 	VPG_DCN3_REG_LIST_RI(id)
251 
252 static struct dcn30_vpg_registers vpg_regs[10];
253 
254 static const struct dcn30_vpg_shift vpg_shift = {
255 	DCN3_VPG_MASK_SH_LIST(__SHIFT)
256 };
257 
258 static const struct dcn30_vpg_mask vpg_mask = {
259 	DCN3_VPG_MASK_SH_LIST(_MASK)
260 };
261 
262 #define afmt_regs_init(id)\
263 	AFMT_DCN3_REG_LIST_RI(id)
264 
265 static struct dcn30_afmt_registers afmt_regs[6];
266 
267 static const struct dcn30_afmt_shift afmt_shift = {
268 	DCN3_AFMT_MASK_SH_LIST(__SHIFT)
269 };
270 
271 static const struct dcn30_afmt_mask afmt_mask = {
272 	DCN3_AFMT_MASK_SH_LIST(_MASK)
273 };
274 
275 #define apg_regs_init(id)\
276 	APG_DCN31_REG_LIST_RI(id)
277 
278 static struct dcn31_apg_registers apg_regs[4];
279 
280 static const struct dcn31_apg_shift apg_shift = {
281 	DCN31_APG_MASK_SH_LIST(__SHIFT)
282 };
283 
284 static const struct dcn31_apg_mask apg_mask = {
285 		DCN31_APG_MASK_SH_LIST(_MASK)
286 };
287 
288 #define stream_enc_regs_init(id)\
289 	SE_DCN32_REG_LIST_RI(id)
290 
291 static struct dcn10_stream_enc_registers stream_enc_regs[5];
292 
293 static const struct dcn10_stream_encoder_shift se_shift = {
294 		SE_COMMON_MASK_SH_LIST_DCN32(__SHIFT)
295 };
296 
297 static const struct dcn10_stream_encoder_mask se_mask = {
298 		SE_COMMON_MASK_SH_LIST_DCN32(_MASK)
299 };
300 
301 
302 #define aux_regs_init(id)\
303 	DCN2_AUX_REG_LIST_RI(id)
304 
305 static struct dcn10_link_enc_aux_registers link_enc_aux_regs[5];
306 
307 #define hpd_regs_init(id)\
308 	HPD_REG_LIST_RI(id)
309 
310 static struct dcn10_link_enc_hpd_registers link_enc_hpd_regs[5];
311 
312 #define link_regs_init(id, phyid)\
313 	( \
314 	LE_DCN31_REG_LIST_RI(id), \
315 	UNIPHY_DCN2_REG_LIST_RI(id, phyid)\
316 	)
317 	/*DPCS_DCN31_REG_LIST(id),*/ \
318 
319 static struct dcn10_link_enc_registers link_enc_regs[5];
320 
321 static const struct dcn10_link_enc_shift le_shift = {
322 	LINK_ENCODER_MASK_SH_LIST_DCN31(__SHIFT), \
323 	//DPCS_DCN31_MASK_SH_LIST(__SHIFT)
324 };
325 
326 static const struct dcn10_link_enc_mask le_mask = {
327 	LINK_ENCODER_MASK_SH_LIST_DCN31(_MASK), \
328 
329 	//DPCS_DCN31_MASK_SH_LIST(_MASK)
330 };
331 
332 #define hpo_dp_stream_encoder_reg_init(id)\
333 	DCN3_1_HPO_DP_STREAM_ENC_REG_LIST_RI(id)
334 
335 static struct dcn31_hpo_dp_stream_encoder_registers hpo_dp_stream_enc_regs[4];
336 
337 static const struct dcn31_hpo_dp_stream_encoder_shift hpo_dp_se_shift = {
338 	DCN3_1_HPO_DP_STREAM_ENC_MASK_SH_LIST(__SHIFT)
339 };
340 
341 static const struct dcn31_hpo_dp_stream_encoder_mask hpo_dp_se_mask = {
342 	DCN3_1_HPO_DP_STREAM_ENC_MASK_SH_LIST(_MASK)
343 };
344 
345 
346 #define hpo_dp_link_encoder_reg_init(id)\
347 	DCN3_1_HPO_DP_LINK_ENC_REG_LIST_RI(id)
348 	/*DCN3_1_RDPCSTX_REG_LIST(0),*/
349 	/*DCN3_1_RDPCSTX_REG_LIST(1),*/
350 	/*DCN3_1_RDPCSTX_REG_LIST(2),*/
351 	/*DCN3_1_RDPCSTX_REG_LIST(3),*/
352 
353 static struct dcn31_hpo_dp_link_encoder_registers hpo_dp_link_enc_regs[2];
354 
355 static const struct dcn31_hpo_dp_link_encoder_shift hpo_dp_le_shift = {
356 	DCN3_2_HPO_DP_LINK_ENC_MASK_SH_LIST(__SHIFT)
357 };
358 
359 static const struct dcn31_hpo_dp_link_encoder_mask hpo_dp_le_mask = {
360 	DCN3_2_HPO_DP_LINK_ENC_MASK_SH_LIST(_MASK)
361 };
362 
363 #define dpp_regs_init(id)\
364 	DPP_REG_LIST_DCN30_COMMON_RI(id)
365 
366 static struct dcn3_dpp_registers dpp_regs[4];
367 
368 static const struct dcn3_dpp_shift tf_shift = {
369 		DPP_REG_LIST_SH_MASK_DCN30_COMMON(__SHIFT)
370 };
371 
372 static const struct dcn3_dpp_mask tf_mask = {
373 		DPP_REG_LIST_SH_MASK_DCN30_COMMON(_MASK)
374 };
375 
376 
377 #define opp_regs_init(id)\
378 	OPP_REG_LIST_DCN30_RI(id)
379 
380 static struct dcn20_opp_registers opp_regs[4];
381 
382 static const struct dcn20_opp_shift opp_shift = {
383 	OPP_MASK_SH_LIST_DCN20(__SHIFT)
384 };
385 
386 static const struct dcn20_opp_mask opp_mask = {
387 	OPP_MASK_SH_LIST_DCN20(_MASK)
388 };
389 
390 #define aux_engine_regs_init(id)\
391 	( \
392 	AUX_COMMON_REG_LIST0_RI(id), \
393 	SR_ARR_INIT(AUXN_IMPCAL, id, 0), \
394 	SR_ARR_INIT(AUXP_IMPCAL, id, 0), \
395 	SR_ARR_INIT(AUX_RESET_MASK, id, DP_AUX0_AUX_CONTROL__AUX_RESET_MASK), \
396 	SR_ARR_INIT(AUX_RESET_MASK, id, DP_AUX0_AUX_CONTROL__AUX_RESET_MASK)\
397 	)
398 
399 static struct dce110_aux_registers aux_engine_regs[5];
400 
401 static const struct dce110_aux_registers_shift aux_shift = {
402 	DCN_AUX_MASK_SH_LIST(__SHIFT)
403 };
404 
405 static const struct dce110_aux_registers_mask aux_mask = {
406 	DCN_AUX_MASK_SH_LIST(_MASK)
407 };
408 
409 #define dwbc_regs_dcn3_init(id)\
410 	DWBC_COMMON_REG_LIST_DCN30_RI(id)
411 
412 static struct dcn30_dwbc_registers dwbc30_regs[1];
413 
414 static const struct dcn30_dwbc_shift dwbc30_shift = {
415 	DWBC_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
416 };
417 
418 static const struct dcn30_dwbc_mask dwbc30_mask = {
419 	DWBC_COMMON_MASK_SH_LIST_DCN30(_MASK)
420 };
421 
422 #define mcif_wb_regs_dcn3_init(id)\
423 	MCIF_WB_COMMON_REG_LIST_DCN32_RI(id)
424 
425 static struct dcn30_mmhubbub_registers mcif_wb30_regs[1];
426 
427 static const struct dcn30_mmhubbub_shift mcif_wb30_shift = {
428 	MCIF_WB_COMMON_MASK_SH_LIST_DCN32(__SHIFT)
429 };
430 
431 static const struct dcn30_mmhubbub_mask mcif_wb30_mask = {
432 	MCIF_WB_COMMON_MASK_SH_LIST_DCN32(_MASK)
433 };
434 
435 #define dsc_regsDCN20_init(id)\
436 	DSC_REG_LIST_DCN20_RI(id)
437 
438 static struct dcn20_dsc_registers dsc_regs[4];
439 
440 static const struct dcn20_dsc_shift dsc_shift = {
441 	DSC_REG_LIST_SH_MASK_DCN20(__SHIFT)
442 };
443 
444 static const struct dcn20_dsc_mask dsc_mask = {
445 	DSC_REG_LIST_SH_MASK_DCN20(_MASK)
446 };
447 
448 static struct dcn30_mpc_registers mpc_regs;
449 
450 #define dcn_mpc_regs_init() \
451 	MPC_REG_LIST_DCN3_2_RI(0),\
452 	MPC_REG_LIST_DCN3_2_RI(1),\
453 	MPC_REG_LIST_DCN3_2_RI(2),\
454 	MPC_REG_LIST_DCN3_2_RI(3),\
455 	MPC_OUT_MUX_REG_LIST_DCN3_0_RI(0),\
456 	MPC_OUT_MUX_REG_LIST_DCN3_0_RI(1),\
457 	MPC_OUT_MUX_REG_LIST_DCN3_0_RI(2),\
458 	MPC_OUT_MUX_REG_LIST_DCN3_0_RI(3),\
459 	MPC_DWB_MUX_REG_LIST_DCN3_0_RI(0)
460 
461 static const struct dcn30_mpc_shift mpc_shift = {
462 	MPC_COMMON_MASK_SH_LIST_DCN32(__SHIFT)
463 };
464 
465 static const struct dcn30_mpc_mask mpc_mask = {
466 	MPC_COMMON_MASK_SH_LIST_DCN32(_MASK)
467 };
468 
469 #define optc_regs_init(id)\
470 	OPTC_COMMON_REG_LIST_DCN3_2_RI(id)
471 
472 static struct dcn_optc_registers optc_regs[4];
473 
474 static const struct dcn_optc_shift optc_shift = {
475 	OPTC_COMMON_MASK_SH_LIST_DCN3_2(__SHIFT)
476 };
477 
478 static const struct dcn_optc_mask optc_mask = {
479 	OPTC_COMMON_MASK_SH_LIST_DCN3_2(_MASK)
480 };
481 
482 #define hubp_regs_init(id)\
483 	HUBP_REG_LIST_DCN32_RI(id)
484 
485 static struct dcn_hubp2_registers hubp_regs[4];
486 
487 
488 static const struct dcn_hubp2_shift hubp_shift = {
489 		HUBP_MASK_SH_LIST_DCN32(__SHIFT)
490 };
491 
492 static const struct dcn_hubp2_mask hubp_mask = {
493 		HUBP_MASK_SH_LIST_DCN32(_MASK)
494 };
495 
496 static struct dcn_hubbub_registers hubbub_reg;
497 #define hubbub_reg_init()\
498 		HUBBUB_REG_LIST_DCN32_RI(0)
499 
500 static const struct dcn_hubbub_shift hubbub_shift = {
501 		HUBBUB_MASK_SH_LIST_DCN32(__SHIFT)
502 };
503 
504 static const struct dcn_hubbub_mask hubbub_mask = {
505 		HUBBUB_MASK_SH_LIST_DCN32(_MASK)
506 };
507 
508 static struct dccg_registers dccg_regs;
509 
510 #define dccg_regs_init()\
511 	DCCG_REG_LIST_DCN32_RI()
512 
513 static const struct dccg_shift dccg_shift = {
514 		DCCG_MASK_SH_LIST_DCN32(__SHIFT)
515 };
516 
517 static const struct dccg_mask dccg_mask = {
518 		DCCG_MASK_SH_LIST_DCN32(_MASK)
519 };
520 
521 
522 #define SRII2(reg_name_pre, reg_name_post, id)\
523 	.reg_name_pre ## _ ##  reg_name_post[id] = BASE(reg ## reg_name_pre \
524 			## id ## _ ## reg_name_post ## _BASE_IDX) + \
525 			reg ## reg_name_pre ## id ## _ ## reg_name_post
526 
527 
528 #define HWSEQ_DCN32_REG_LIST()\
529 	SR(DCHUBBUB_GLOBAL_TIMER_CNTL), \
530 	SR(DIO_MEM_PWR_CTRL), \
531 	SR(ODM_MEM_PWR_CTRL3), \
532 	SR(MMHUBBUB_MEM_PWR_CNTL), \
533 	SR(DCCG_GATE_DISABLE_CNTL), \
534 	SR(DCCG_GATE_DISABLE_CNTL2), \
535 	SR(DCFCLK_CNTL),\
536 	SR(DC_MEM_GLOBAL_PWR_REQ_CNTL), \
537 	SRII(PIXEL_RATE_CNTL, OTG, 0), \
538 	SRII(PIXEL_RATE_CNTL, OTG, 1),\
539 	SRII(PIXEL_RATE_CNTL, OTG, 2),\
540 	SRII(PIXEL_RATE_CNTL, OTG, 3),\
541 	SRII(PHYPLL_PIXEL_RATE_CNTL, OTG, 0),\
542 	SRII(PHYPLL_PIXEL_RATE_CNTL, OTG, 1),\
543 	SRII(PHYPLL_PIXEL_RATE_CNTL, OTG, 2),\
544 	SRII(PHYPLL_PIXEL_RATE_CNTL, OTG, 3),\
545 	SR(MICROSECOND_TIME_BASE_DIV), \
546 	SR(MILLISECOND_TIME_BASE_DIV), \
547 	SR(DISPCLK_FREQ_CHANGE_CNTL), \
548 	SR(RBBMIF_TIMEOUT_DIS), \
549 	SR(RBBMIF_TIMEOUT_DIS_2), \
550 	SR(DCHUBBUB_CRC_CTRL), \
551 	SR(DPP_TOP0_DPP_CRC_CTRL), \
552 	SR(DPP_TOP0_DPP_CRC_VAL_B_A), \
553 	SR(DPP_TOP0_DPP_CRC_VAL_R_G), \
554 	SR(MPC_CRC_CTRL), \
555 	SR(MPC_CRC_RESULT_GB), \
556 	SR(MPC_CRC_RESULT_C), \
557 	SR(MPC_CRC_RESULT_AR), \
558 	SR(DOMAIN0_PG_CONFIG), \
559 	SR(DOMAIN1_PG_CONFIG), \
560 	SR(DOMAIN2_PG_CONFIG), \
561 	SR(DOMAIN3_PG_CONFIG), \
562 	SR(DOMAIN16_PG_CONFIG), \
563 	SR(DOMAIN17_PG_CONFIG), \
564 	SR(DOMAIN18_PG_CONFIG), \
565 	SR(DOMAIN19_PG_CONFIG), \
566 	SR(DOMAIN0_PG_STATUS), \
567 	SR(DOMAIN1_PG_STATUS), \
568 	SR(DOMAIN2_PG_STATUS), \
569 	SR(DOMAIN3_PG_STATUS), \
570 	SR(DOMAIN16_PG_STATUS), \
571 	SR(DOMAIN17_PG_STATUS), \
572 	SR(DOMAIN18_PG_STATUS), \
573 	SR(DOMAIN19_PG_STATUS), \
574 	SR(D1VGA_CONTROL), \
575 	SR(D2VGA_CONTROL), \
576 	SR(D3VGA_CONTROL), \
577 	SR(D4VGA_CONTROL), \
578 	SR(D5VGA_CONTROL), \
579 	SR(D6VGA_CONTROL), \
580 	SR(DC_IP_REQUEST_CNTL), \
581 	SR(AZALIA_AUDIO_DTO), \
582 	SR(AZALIA_CONTROLLER_CLOCK_GATING)
583 
584 static struct dce_hwseq_registers hwseq_reg;
585 
586 #define hwseq_reg_init()\
587 	HWSEQ_DCN32_REG_LIST()
588 
589 #define HWSEQ_DCN32_MASK_SH_LIST(mask_sh)\
590 	HWSEQ_DCN_MASK_SH_LIST(mask_sh), \
591 	HWS_SF(, DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_REFDIV, mask_sh), \
592 	HWS_SF(, DOMAIN0_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
593 	HWS_SF(, DOMAIN0_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
594 	HWS_SF(, DOMAIN1_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
595 	HWS_SF(, DOMAIN1_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
596 	HWS_SF(, DOMAIN2_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
597 	HWS_SF(, DOMAIN2_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
598 	HWS_SF(, DOMAIN3_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
599 	HWS_SF(, DOMAIN3_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
600 	HWS_SF(, DOMAIN16_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
601 	HWS_SF(, DOMAIN16_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
602 	HWS_SF(, DOMAIN17_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
603 	HWS_SF(, DOMAIN17_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
604 	HWS_SF(, DOMAIN18_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
605 	HWS_SF(, DOMAIN18_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
606 	HWS_SF(, DOMAIN19_PG_CONFIG, DOMAIN_POWER_FORCEON, mask_sh), \
607 	HWS_SF(, DOMAIN19_PG_CONFIG, DOMAIN_POWER_GATE, mask_sh), \
608 	HWS_SF(, DOMAIN0_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
609 	HWS_SF(, DOMAIN1_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
610 	HWS_SF(, DOMAIN2_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
611 	HWS_SF(, DOMAIN3_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
612 	HWS_SF(, DOMAIN16_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
613 	HWS_SF(, DOMAIN17_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
614 	HWS_SF(, DOMAIN18_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
615 	HWS_SF(, DOMAIN19_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, mask_sh), \
616 	HWS_SF(, DC_IP_REQUEST_CNTL, IP_REQUEST_EN, mask_sh), \
617 	HWS_SF(, AZALIA_AUDIO_DTO, AZALIA_AUDIO_DTO_MODULE, mask_sh), \
618 	HWS_SF(, HPO_TOP_CLOCK_CONTROL, HPO_HDMISTREAMCLK_G_GATE_DIS, mask_sh), \
619 	HWS_SF(, ODM_MEM_PWR_CTRL3, ODM_MEM_UNASSIGNED_PWR_MODE, mask_sh), \
620 	HWS_SF(, ODM_MEM_PWR_CTRL3, ODM_MEM_VBLANK_PWR_MODE, mask_sh), \
621 	HWS_SF(, MMHUBBUB_MEM_PWR_CNTL, VGA_MEM_PWR_FORCE, mask_sh)
622 
623 static const struct dce_hwseq_shift hwseq_shift = {
624 		HWSEQ_DCN32_MASK_SH_LIST(__SHIFT)
625 };
626 
627 static const struct dce_hwseq_mask hwseq_mask = {
628 		HWSEQ_DCN32_MASK_SH_LIST(_MASK)
629 };
630 #define vmid_regs_init(id)\
631 		DCN20_VMID_REG_LIST_RI(id)
632 
633 static struct dcn_vmid_registers vmid_regs[16];
634 
635 static const struct dcn20_vmid_shift vmid_shifts = {
636 		DCN20_VMID_MASK_SH_LIST(__SHIFT)
637 };
638 
639 static const struct dcn20_vmid_mask vmid_masks = {
640 		DCN20_VMID_MASK_SH_LIST(_MASK)
641 };
642 
643 static const struct resource_caps res_cap_dcn32 = {
644 	.num_timing_generator = 4,
645 	.num_opp = 4,
646 	.num_video_plane = 4,
647 	.num_audio = 5,
648 	.num_stream_encoder = 5,
649 	.num_hpo_dp_stream_encoder = 4,
650 	.num_hpo_dp_link_encoder = 2,
651 	.num_pll = 5,
652 	.num_dwb = 1,
653 	.num_ddc = 5,
654 	.num_vmid = 16,
655 	.num_mpc_3dlut = 4,
656 	.num_dsc = 4,
657 };
658 
659 static const struct dc_plane_cap plane_cap = {
660 	.type = DC_PLANE_TYPE_DCN_UNIVERSAL,
661 	.blends_with_above = true,
662 	.blends_with_below = true,
663 	.per_pixel_alpha = true,
664 
665 	.pixel_format_support = {
666 			.argb8888 = true,
667 			.nv12 = true,
668 			.fp16 = true,
669 			.p010 = true,
670 			.ayuv = false,
671 	},
672 
673 	.max_upscale_factor = {
674 			.argb8888 = 16000,
675 			.nv12 = 16000,
676 			.fp16 = 16000
677 	},
678 
679 	// 6:1 downscaling ratio: 1000/6 = 166.666
680 	.max_downscale_factor = {
681 			.argb8888 = 167,
682 			.nv12 = 167,
683 			.fp16 = 167
684 	},
685 	64,
686 	64
687 };
688 
689 static const struct dc_debug_options debug_defaults_drv = {
690 	.disable_dmcu = true,
691 	.force_abm_enable = false,
692 	.timing_trace = false,
693 	.clock_trace = true,
694 	.disable_pplib_clock_request = false,
695 	.pipe_split_policy = MPC_SPLIT_AVOID, // Due to CRB, no need to MPC split anymore
696 	.force_single_disp_pipe_split = false,
697 	.disable_dcc = DCC_ENABLE,
698 	.vsr_support = true,
699 	.performance_trace = false,
700 	.max_downscale_src_width = 7680,/*upto 8K*/
701 	.disable_pplib_wm_range = false,
702 	.scl_reset_length10 = true,
703 	.sanity_checks = false,
704 	.underflow_assert_delay_us = 0xFFFFFFFF,
705 	.dwb_fi_phase = -1, // -1 = disable,
706 	.dmub_command_table = true,
707 	.enable_mem_low_power = {
708 		.bits = {
709 			.vga = false,
710 			.i2c = false,
711 			.dmcu = false, // This is previously known to cause hang on S3 cycles if enabled
712 			.dscl = false,
713 			.cm = false,
714 			.mpc = false,
715 			.optc = true,
716 		}
717 	},
718 	.use_max_lb = true,
719 	.force_disable_subvp = false,
720 	.exit_idle_opt_for_cursor_updates = true,
721 	.enable_single_display_2to1_odm_policy = true,
722 
723 	/* Must match enable_single_display_2to1_odm_policy to support dynamic ODM transitions*/
724 	.enable_double_buffered_dsc_pg_support = true,
725 	.enable_dp_dig_pixel_rate_div_policy = 1,
726 	.allow_sw_cursor_fallback = false, // Linux can't do SW cursor "fallback"
727 	.alloc_extra_way_for_cursor = true,
728 	.min_prefetch_in_strobe_ns = 60000, // 60us
729 	.disable_unbounded_requesting = false,
730 };
731 
732 static const struct dc_debug_options debug_defaults_diags = {
733 	.disable_dmcu = true,
734 	.force_abm_enable = false,
735 	.timing_trace = true,
736 	.clock_trace = true,
737 	.disable_dpp_power_gate = true,
738 	.disable_hubp_power_gate = true,
739 	.disable_dsc_power_gate = true,
740 	.disable_clock_gate = true,
741 	.disable_pplib_clock_request = true,
742 	.disable_pplib_wm_range = true,
743 	.disable_stutter = false,
744 	.scl_reset_length10 = true,
745 	.dwb_fi_phase = -1, // -1 = disable
746 	.dmub_command_table = true,
747 	.enable_tri_buf = true,
748 	.use_max_lb = true,
749 	.force_disable_subvp = true
750 };
751 
752 static struct dce_aux *dcn32_aux_engine_create(
753 	struct dc_context *ctx,
754 	uint32_t inst)
755 {
756 	struct aux_engine_dce110 *aux_engine =
757 		kzalloc(sizeof(struct aux_engine_dce110), GFP_KERNEL);
758 
759 	if (!aux_engine)
760 		return NULL;
761 
762 #undef REG_STRUCT
763 #define REG_STRUCT aux_engine_regs
764 	aux_engine_regs_init(0),
765 	aux_engine_regs_init(1),
766 	aux_engine_regs_init(2),
767 	aux_engine_regs_init(3),
768 	aux_engine_regs_init(4);
769 
770 	dce110_aux_engine_construct(aux_engine, ctx, inst,
771 				    SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD,
772 				    &aux_engine_regs[inst],
773 					&aux_mask,
774 					&aux_shift,
775 					ctx->dc->caps.extended_aux_timeout_support);
776 
777 	return &aux_engine->base;
778 }
779 #define i2c_inst_regs_init(id)\
780 	I2C_HW_ENGINE_COMMON_REG_LIST_DCN30_RI(id)
781 
782 static struct dce_i2c_registers i2c_hw_regs[5];
783 
784 static const struct dce_i2c_shift i2c_shifts = {
785 		I2C_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
786 };
787 
788 static const struct dce_i2c_mask i2c_masks = {
789 		I2C_COMMON_MASK_SH_LIST_DCN30(_MASK)
790 };
791 
792 static struct dce_i2c_hw *dcn32_i2c_hw_create(
793 	struct dc_context *ctx,
794 	uint32_t inst)
795 {
796 	struct dce_i2c_hw *dce_i2c_hw =
797 		kzalloc(sizeof(struct dce_i2c_hw), GFP_KERNEL);
798 
799 	if (!dce_i2c_hw)
800 		return NULL;
801 
802 #undef REG_STRUCT
803 #define REG_STRUCT i2c_hw_regs
804 	i2c_inst_regs_init(1),
805 	i2c_inst_regs_init(2),
806 	i2c_inst_regs_init(3),
807 	i2c_inst_regs_init(4),
808 	i2c_inst_regs_init(5);
809 
810 	dcn2_i2c_hw_construct(dce_i2c_hw, ctx, inst,
811 				    &i2c_hw_regs[inst], &i2c_shifts, &i2c_masks);
812 
813 	return dce_i2c_hw;
814 }
815 
816 static struct clock_source *dcn32_clock_source_create(
817 		struct dc_context *ctx,
818 		struct dc_bios *bios,
819 		enum clock_source_id id,
820 		const struct dce110_clk_src_regs *regs,
821 		bool dp_clk_src)
822 {
823 	struct dce110_clk_src *clk_src =
824 		kzalloc(sizeof(struct dce110_clk_src), GFP_KERNEL);
825 
826 	if (!clk_src)
827 		return NULL;
828 
829 	if (dcn31_clk_src_construct(clk_src, ctx, bios, id,
830 			regs, &cs_shift, &cs_mask)) {
831 		clk_src->base.dp_clk_src = dp_clk_src;
832 		return &clk_src->base;
833 	}
834 
835 	kfree(clk_src);
836 	BREAK_TO_DEBUGGER();
837 	return NULL;
838 }
839 
840 static struct hubbub *dcn32_hubbub_create(struct dc_context *ctx)
841 {
842 	int i;
843 
844 	struct dcn20_hubbub *hubbub2 = kzalloc(sizeof(struct dcn20_hubbub),
845 					  GFP_KERNEL);
846 
847 	if (!hubbub2)
848 		return NULL;
849 
850 #undef REG_STRUCT
851 #define REG_STRUCT hubbub_reg
852 	hubbub_reg_init();
853 
854 #undef REG_STRUCT
855 #define REG_STRUCT vmid_regs
856 	vmid_regs_init(0),
857 	vmid_regs_init(1),
858 	vmid_regs_init(2),
859 	vmid_regs_init(3),
860 	vmid_regs_init(4),
861 	vmid_regs_init(5),
862 	vmid_regs_init(6),
863 	vmid_regs_init(7),
864 	vmid_regs_init(8),
865 	vmid_regs_init(9),
866 	vmid_regs_init(10),
867 	vmid_regs_init(11),
868 	vmid_regs_init(12),
869 	vmid_regs_init(13),
870 	vmid_regs_init(14),
871 	vmid_regs_init(15);
872 
873 	hubbub32_construct(hubbub2, ctx,
874 			&hubbub_reg,
875 			&hubbub_shift,
876 			&hubbub_mask,
877 			ctx->dc->dml.ip.det_buffer_size_kbytes,
878 			ctx->dc->dml.ip.pixel_chunk_size_kbytes,
879 			ctx->dc->dml.ip.config_return_buffer_size_in_kbytes);
880 
881 
882 	for (i = 0; i < res_cap_dcn32.num_vmid; i++) {
883 		struct dcn20_vmid *vmid = &hubbub2->vmid[i];
884 
885 		vmid->ctx = ctx;
886 
887 		vmid->regs = &vmid_regs[i];
888 		vmid->shifts = &vmid_shifts;
889 		vmid->masks = &vmid_masks;
890 	}
891 
892 	return &hubbub2->base;
893 }
894 
895 static struct hubp *dcn32_hubp_create(
896 	struct dc_context *ctx,
897 	uint32_t inst)
898 {
899 	struct dcn20_hubp *hubp2 =
900 		kzalloc(sizeof(struct dcn20_hubp), GFP_KERNEL);
901 
902 	if (!hubp2)
903 		return NULL;
904 
905 #undef REG_STRUCT
906 #define REG_STRUCT hubp_regs
907 	hubp_regs_init(0),
908 	hubp_regs_init(1),
909 	hubp_regs_init(2),
910 	hubp_regs_init(3);
911 
912 	if (hubp32_construct(hubp2, ctx, inst,
913 			&hubp_regs[inst], &hubp_shift, &hubp_mask))
914 		return &hubp2->base;
915 
916 	BREAK_TO_DEBUGGER();
917 	kfree(hubp2);
918 	return NULL;
919 }
920 
921 static void dcn32_dpp_destroy(struct dpp **dpp)
922 {
923 	kfree(TO_DCN30_DPP(*dpp));
924 	*dpp = NULL;
925 }
926 
927 static struct dpp *dcn32_dpp_create(
928 	struct dc_context *ctx,
929 	uint32_t inst)
930 {
931 	struct dcn3_dpp *dpp3 =
932 		kzalloc(sizeof(struct dcn3_dpp), GFP_KERNEL);
933 
934 	if (!dpp3)
935 		return NULL;
936 
937 #undef REG_STRUCT
938 #define REG_STRUCT dpp_regs
939 	dpp_regs_init(0),
940 	dpp_regs_init(1),
941 	dpp_regs_init(2),
942 	dpp_regs_init(3);
943 
944 	if (dpp32_construct(dpp3, ctx, inst,
945 			&dpp_regs[inst], &tf_shift, &tf_mask))
946 		return &dpp3->base;
947 
948 	BREAK_TO_DEBUGGER();
949 	kfree(dpp3);
950 	return NULL;
951 }
952 
953 static struct mpc *dcn32_mpc_create(
954 		struct dc_context *ctx,
955 		int num_mpcc,
956 		int num_rmu)
957 {
958 	struct dcn30_mpc *mpc30 = kzalloc(sizeof(struct dcn30_mpc),
959 					  GFP_KERNEL);
960 
961 	if (!mpc30)
962 		return NULL;
963 
964 #undef REG_STRUCT
965 #define REG_STRUCT mpc_regs
966 	dcn_mpc_regs_init();
967 
968 	dcn32_mpc_construct(mpc30, ctx,
969 			&mpc_regs,
970 			&mpc_shift,
971 			&mpc_mask,
972 			num_mpcc,
973 			num_rmu);
974 
975 	return &mpc30->base;
976 }
977 
978 static struct output_pixel_processor *dcn32_opp_create(
979 	struct dc_context *ctx, uint32_t inst)
980 {
981 	struct dcn20_opp *opp2 =
982 		kzalloc(sizeof(struct dcn20_opp), GFP_KERNEL);
983 
984 	if (!opp2) {
985 		BREAK_TO_DEBUGGER();
986 		return NULL;
987 	}
988 
989 #undef REG_STRUCT
990 #define REG_STRUCT opp_regs
991 	opp_regs_init(0),
992 	opp_regs_init(1),
993 	opp_regs_init(2),
994 	opp_regs_init(3);
995 
996 	dcn20_opp_construct(opp2, ctx, inst,
997 			&opp_regs[inst], &opp_shift, &opp_mask);
998 	return &opp2->base;
999 }
1000 
1001 
1002 static struct timing_generator *dcn32_timing_generator_create(
1003 		struct dc_context *ctx,
1004 		uint32_t instance)
1005 {
1006 	struct optc *tgn10 =
1007 		kzalloc(sizeof(struct optc), GFP_KERNEL);
1008 
1009 	if (!tgn10)
1010 		return NULL;
1011 
1012 #undef REG_STRUCT
1013 #define REG_STRUCT optc_regs
1014 	optc_regs_init(0),
1015 	optc_regs_init(1),
1016 	optc_regs_init(2),
1017 	optc_regs_init(3);
1018 
1019 	tgn10->base.inst = instance;
1020 	tgn10->base.ctx = ctx;
1021 
1022 	tgn10->tg_regs = &optc_regs[instance];
1023 	tgn10->tg_shift = &optc_shift;
1024 	tgn10->tg_mask = &optc_mask;
1025 
1026 	dcn32_timing_generator_init(tgn10);
1027 
1028 	return &tgn10->base;
1029 }
1030 
1031 static const struct encoder_feature_support link_enc_feature = {
1032 		.max_hdmi_deep_color = COLOR_DEPTH_121212,
1033 		.max_hdmi_pixel_clock = 600000,
1034 		.hdmi_ycbcr420_supported = true,
1035 		.dp_ycbcr420_supported = true,
1036 		.fec_supported = true,
1037 		.flags.bits.IS_HBR2_CAPABLE = true,
1038 		.flags.bits.IS_HBR3_CAPABLE = true,
1039 		.flags.bits.IS_TPS3_CAPABLE = true,
1040 		.flags.bits.IS_TPS4_CAPABLE = true
1041 };
1042 
1043 static struct link_encoder *dcn32_link_encoder_create(
1044 	struct dc_context *ctx,
1045 	const struct encoder_init_data *enc_init_data)
1046 {
1047 	struct dcn20_link_encoder *enc20 =
1048 		kzalloc(sizeof(struct dcn20_link_encoder), GFP_KERNEL);
1049 
1050 	if (!enc20)
1051 		return NULL;
1052 
1053 #undef REG_STRUCT
1054 #define REG_STRUCT link_enc_aux_regs
1055 	aux_regs_init(0),
1056 	aux_regs_init(1),
1057 	aux_regs_init(2),
1058 	aux_regs_init(3),
1059 	aux_regs_init(4);
1060 
1061 #undef REG_STRUCT
1062 #define REG_STRUCT link_enc_hpd_regs
1063 	hpd_regs_init(0),
1064 	hpd_regs_init(1),
1065 	hpd_regs_init(2),
1066 	hpd_regs_init(3),
1067 	hpd_regs_init(4);
1068 
1069 #undef REG_STRUCT
1070 #define REG_STRUCT link_enc_regs
1071 	link_regs_init(0, A),
1072 	link_regs_init(1, B),
1073 	link_regs_init(2, C),
1074 	link_regs_init(3, D),
1075 	link_regs_init(4, E);
1076 
1077 	dcn32_link_encoder_construct(enc20,
1078 			enc_init_data,
1079 			&link_enc_feature,
1080 			&link_enc_regs[enc_init_data->transmitter],
1081 			&link_enc_aux_regs[enc_init_data->channel - 1],
1082 			&link_enc_hpd_regs[enc_init_data->hpd_source],
1083 			&le_shift,
1084 			&le_mask);
1085 
1086 	return &enc20->enc10.base;
1087 }
1088 
1089 struct panel_cntl *dcn32_panel_cntl_create(const struct panel_cntl_init_data *init_data)
1090 {
1091 	struct dcn31_panel_cntl *panel_cntl =
1092 		kzalloc(sizeof(struct dcn31_panel_cntl), GFP_KERNEL);
1093 
1094 	if (!panel_cntl)
1095 		return NULL;
1096 
1097 	dcn31_panel_cntl_construct(panel_cntl, init_data);
1098 
1099 	return &panel_cntl->base;
1100 }
1101 
1102 static void read_dce_straps(
1103 	struct dc_context *ctx,
1104 	struct resource_straps *straps)
1105 {
1106 	generic_reg_get(ctx, ctx->dcn_reg_offsets[regDC_PINSTRAPS_BASE_IDX] + regDC_PINSTRAPS,
1107 		FN(DC_PINSTRAPS, DC_PINSTRAPS_AUDIO), &straps->dc_pinstraps_audio);
1108 
1109 }
1110 
1111 static struct audio *dcn32_create_audio(
1112 		struct dc_context *ctx, unsigned int inst)
1113 {
1114 
1115 #undef REG_STRUCT
1116 #define REG_STRUCT audio_regs
1117 	audio_regs_init(0),
1118 	audio_regs_init(1),
1119 	audio_regs_init(2),
1120 	audio_regs_init(3),
1121 	audio_regs_init(4);
1122 
1123 	return dce_audio_create(ctx, inst,
1124 			&audio_regs[inst], &audio_shift, &audio_mask);
1125 }
1126 
1127 static struct vpg *dcn32_vpg_create(
1128 	struct dc_context *ctx,
1129 	uint32_t inst)
1130 {
1131 	struct dcn30_vpg *vpg3 = kzalloc(sizeof(struct dcn30_vpg), GFP_KERNEL);
1132 
1133 	if (!vpg3)
1134 		return NULL;
1135 
1136 #undef REG_STRUCT
1137 #define REG_STRUCT vpg_regs
1138 	vpg_regs_init(0),
1139 	vpg_regs_init(1),
1140 	vpg_regs_init(2),
1141 	vpg_regs_init(3),
1142 	vpg_regs_init(4),
1143 	vpg_regs_init(5),
1144 	vpg_regs_init(6),
1145 	vpg_regs_init(7),
1146 	vpg_regs_init(8),
1147 	vpg_regs_init(9);
1148 
1149 	vpg3_construct(vpg3, ctx, inst,
1150 			&vpg_regs[inst],
1151 			&vpg_shift,
1152 			&vpg_mask);
1153 
1154 	return &vpg3->base;
1155 }
1156 
1157 static struct afmt *dcn32_afmt_create(
1158 	struct dc_context *ctx,
1159 	uint32_t inst)
1160 {
1161 	struct dcn30_afmt *afmt3 = kzalloc(sizeof(struct dcn30_afmt), GFP_KERNEL);
1162 
1163 	if (!afmt3)
1164 		return NULL;
1165 
1166 #undef REG_STRUCT
1167 #define REG_STRUCT afmt_regs
1168 	afmt_regs_init(0),
1169 	afmt_regs_init(1),
1170 	afmt_regs_init(2),
1171 	afmt_regs_init(3),
1172 	afmt_regs_init(4),
1173 	afmt_regs_init(5);
1174 
1175 	afmt3_construct(afmt3, ctx, inst,
1176 			&afmt_regs[inst],
1177 			&afmt_shift,
1178 			&afmt_mask);
1179 
1180 	return &afmt3->base;
1181 }
1182 
1183 static struct apg *dcn31_apg_create(
1184 	struct dc_context *ctx,
1185 	uint32_t inst)
1186 {
1187 	struct dcn31_apg *apg31 = kzalloc(sizeof(struct dcn31_apg), GFP_KERNEL);
1188 
1189 	if (!apg31)
1190 		return NULL;
1191 
1192 #undef REG_STRUCT
1193 #define REG_STRUCT apg_regs
1194 	apg_regs_init(0),
1195 	apg_regs_init(1),
1196 	apg_regs_init(2),
1197 	apg_regs_init(3);
1198 
1199 	apg31_construct(apg31, ctx, inst,
1200 			&apg_regs[inst],
1201 			&apg_shift,
1202 			&apg_mask);
1203 
1204 	return &apg31->base;
1205 }
1206 
1207 static struct stream_encoder *dcn32_stream_encoder_create(
1208 	enum engine_id eng_id,
1209 	struct dc_context *ctx)
1210 {
1211 	struct dcn10_stream_encoder *enc1;
1212 	struct vpg *vpg;
1213 	struct afmt *afmt;
1214 	int vpg_inst;
1215 	int afmt_inst;
1216 
1217 	/* Mapping of VPG, AFMT, DME register blocks to DIO block instance */
1218 	if (eng_id <= ENGINE_ID_DIGF) {
1219 		vpg_inst = eng_id;
1220 		afmt_inst = eng_id;
1221 	} else
1222 		return NULL;
1223 
1224 	enc1 = kzalloc(sizeof(struct dcn10_stream_encoder), GFP_KERNEL);
1225 	vpg = dcn32_vpg_create(ctx, vpg_inst);
1226 	afmt = dcn32_afmt_create(ctx, afmt_inst);
1227 
1228 	if (!enc1 || !vpg || !afmt) {
1229 		kfree(enc1);
1230 		kfree(vpg);
1231 		kfree(afmt);
1232 		return NULL;
1233 	}
1234 
1235 #undef REG_STRUCT
1236 #define REG_STRUCT stream_enc_regs
1237 	stream_enc_regs_init(0),
1238 	stream_enc_regs_init(1),
1239 	stream_enc_regs_init(2),
1240 	stream_enc_regs_init(3),
1241 	stream_enc_regs_init(4);
1242 
1243 	dcn32_dio_stream_encoder_construct(enc1, ctx, ctx->dc_bios,
1244 					eng_id, vpg, afmt,
1245 					&stream_enc_regs[eng_id],
1246 					&se_shift, &se_mask);
1247 
1248 	return &enc1->base;
1249 }
1250 
1251 static struct hpo_dp_stream_encoder *dcn32_hpo_dp_stream_encoder_create(
1252 	enum engine_id eng_id,
1253 	struct dc_context *ctx)
1254 {
1255 	struct dcn31_hpo_dp_stream_encoder *hpo_dp_enc31;
1256 	struct vpg *vpg;
1257 	struct apg *apg;
1258 	uint32_t hpo_dp_inst;
1259 	uint32_t vpg_inst;
1260 	uint32_t apg_inst;
1261 
1262 	ASSERT((eng_id >= ENGINE_ID_HPO_DP_0) && (eng_id <= ENGINE_ID_HPO_DP_3));
1263 	hpo_dp_inst = eng_id - ENGINE_ID_HPO_DP_0;
1264 
1265 	/* Mapping of VPG register blocks to HPO DP block instance:
1266 	 * VPG[6] -> HPO_DP[0]
1267 	 * VPG[7] -> HPO_DP[1]
1268 	 * VPG[8] -> HPO_DP[2]
1269 	 * VPG[9] -> HPO_DP[3]
1270 	 */
1271 	vpg_inst = hpo_dp_inst + 6;
1272 
1273 	/* Mapping of APG register blocks to HPO DP block instance:
1274 	 * APG[0] -> HPO_DP[0]
1275 	 * APG[1] -> HPO_DP[1]
1276 	 * APG[2] -> HPO_DP[2]
1277 	 * APG[3] -> HPO_DP[3]
1278 	 */
1279 	apg_inst = hpo_dp_inst;
1280 
1281 	/* allocate HPO stream encoder and create VPG sub-block */
1282 	hpo_dp_enc31 = kzalloc(sizeof(struct dcn31_hpo_dp_stream_encoder), GFP_KERNEL);
1283 	vpg = dcn32_vpg_create(ctx, vpg_inst);
1284 	apg = dcn31_apg_create(ctx, apg_inst);
1285 
1286 	if (!hpo_dp_enc31 || !vpg || !apg) {
1287 		kfree(hpo_dp_enc31);
1288 		kfree(vpg);
1289 		kfree(apg);
1290 		return NULL;
1291 	}
1292 
1293 #undef REG_STRUCT
1294 #define REG_STRUCT hpo_dp_stream_enc_regs
1295 	hpo_dp_stream_encoder_reg_init(0),
1296 	hpo_dp_stream_encoder_reg_init(1),
1297 	hpo_dp_stream_encoder_reg_init(2),
1298 	hpo_dp_stream_encoder_reg_init(3);
1299 
1300 	dcn31_hpo_dp_stream_encoder_construct(hpo_dp_enc31, ctx, ctx->dc_bios,
1301 					hpo_dp_inst, eng_id, vpg, apg,
1302 					&hpo_dp_stream_enc_regs[hpo_dp_inst],
1303 					&hpo_dp_se_shift, &hpo_dp_se_mask);
1304 
1305 	return &hpo_dp_enc31->base;
1306 }
1307 
1308 static struct hpo_dp_link_encoder *dcn32_hpo_dp_link_encoder_create(
1309 	uint8_t inst,
1310 	struct dc_context *ctx)
1311 {
1312 	struct dcn31_hpo_dp_link_encoder *hpo_dp_enc31;
1313 
1314 	/* allocate HPO link encoder */
1315 	hpo_dp_enc31 = kzalloc(sizeof(struct dcn31_hpo_dp_link_encoder), GFP_KERNEL);
1316 
1317 #undef REG_STRUCT
1318 #define REG_STRUCT hpo_dp_link_enc_regs
1319 	hpo_dp_link_encoder_reg_init(0),
1320 	hpo_dp_link_encoder_reg_init(1);
1321 
1322 	hpo_dp_link_encoder32_construct(hpo_dp_enc31, ctx, inst,
1323 					&hpo_dp_link_enc_regs[inst],
1324 					&hpo_dp_le_shift, &hpo_dp_le_mask);
1325 
1326 	return &hpo_dp_enc31->base;
1327 }
1328 
1329 static struct dce_hwseq *dcn32_hwseq_create(
1330 	struct dc_context *ctx)
1331 {
1332 	struct dce_hwseq *hws = kzalloc(sizeof(struct dce_hwseq), GFP_KERNEL);
1333 
1334 #undef REG_STRUCT
1335 #define REG_STRUCT hwseq_reg
1336 	hwseq_reg_init();
1337 
1338 	if (hws) {
1339 		hws->ctx = ctx;
1340 		hws->regs = &hwseq_reg;
1341 		hws->shifts = &hwseq_shift;
1342 		hws->masks = &hwseq_mask;
1343 	}
1344 	return hws;
1345 }
1346 static const struct resource_create_funcs res_create_funcs = {
1347 	.read_dce_straps = read_dce_straps,
1348 	.create_audio = dcn32_create_audio,
1349 	.create_stream_encoder = dcn32_stream_encoder_create,
1350 	.create_hpo_dp_stream_encoder = dcn32_hpo_dp_stream_encoder_create,
1351 	.create_hpo_dp_link_encoder = dcn32_hpo_dp_link_encoder_create,
1352 	.create_hwseq = dcn32_hwseq_create,
1353 };
1354 
1355 static const struct resource_create_funcs res_create_maximus_funcs = {
1356 	.read_dce_straps = NULL,
1357 	.create_audio = NULL,
1358 	.create_stream_encoder = NULL,
1359 	.create_hpo_dp_stream_encoder = dcn32_hpo_dp_stream_encoder_create,
1360 	.create_hpo_dp_link_encoder = dcn32_hpo_dp_link_encoder_create,
1361 	.create_hwseq = dcn32_hwseq_create,
1362 };
1363 
1364 static void dcn32_resource_destruct(struct dcn32_resource_pool *pool)
1365 {
1366 	unsigned int i;
1367 
1368 	for (i = 0; i < pool->base.stream_enc_count; i++) {
1369 		if (pool->base.stream_enc[i] != NULL) {
1370 			if (pool->base.stream_enc[i]->vpg != NULL) {
1371 				kfree(DCN30_VPG_FROM_VPG(pool->base.stream_enc[i]->vpg));
1372 				pool->base.stream_enc[i]->vpg = NULL;
1373 			}
1374 			if (pool->base.stream_enc[i]->afmt != NULL) {
1375 				kfree(DCN30_AFMT_FROM_AFMT(pool->base.stream_enc[i]->afmt));
1376 				pool->base.stream_enc[i]->afmt = NULL;
1377 			}
1378 			kfree(DCN10STRENC_FROM_STRENC(pool->base.stream_enc[i]));
1379 			pool->base.stream_enc[i] = NULL;
1380 		}
1381 	}
1382 
1383 	for (i = 0; i < pool->base.hpo_dp_stream_enc_count; i++) {
1384 		if (pool->base.hpo_dp_stream_enc[i] != NULL) {
1385 			if (pool->base.hpo_dp_stream_enc[i]->vpg != NULL) {
1386 				kfree(DCN30_VPG_FROM_VPG(pool->base.hpo_dp_stream_enc[i]->vpg));
1387 				pool->base.hpo_dp_stream_enc[i]->vpg = NULL;
1388 			}
1389 			if (pool->base.hpo_dp_stream_enc[i]->apg != NULL) {
1390 				kfree(DCN31_APG_FROM_APG(pool->base.hpo_dp_stream_enc[i]->apg));
1391 				pool->base.hpo_dp_stream_enc[i]->apg = NULL;
1392 			}
1393 			kfree(DCN3_1_HPO_DP_STREAM_ENC_FROM_HPO_STREAM_ENC(pool->base.hpo_dp_stream_enc[i]));
1394 			pool->base.hpo_dp_stream_enc[i] = NULL;
1395 		}
1396 	}
1397 
1398 	for (i = 0; i < pool->base.hpo_dp_link_enc_count; i++) {
1399 		if (pool->base.hpo_dp_link_enc[i] != NULL) {
1400 			kfree(DCN3_1_HPO_DP_LINK_ENC_FROM_HPO_LINK_ENC(pool->base.hpo_dp_link_enc[i]));
1401 			pool->base.hpo_dp_link_enc[i] = NULL;
1402 		}
1403 	}
1404 
1405 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
1406 		if (pool->base.dscs[i] != NULL)
1407 			dcn20_dsc_destroy(&pool->base.dscs[i]);
1408 	}
1409 
1410 	if (pool->base.mpc != NULL) {
1411 		kfree(TO_DCN20_MPC(pool->base.mpc));
1412 		pool->base.mpc = NULL;
1413 	}
1414 	if (pool->base.hubbub != NULL) {
1415 		kfree(TO_DCN20_HUBBUB(pool->base.hubbub));
1416 		pool->base.hubbub = NULL;
1417 	}
1418 	for (i = 0; i < pool->base.pipe_count; i++) {
1419 		if (pool->base.dpps[i] != NULL)
1420 			dcn32_dpp_destroy(&pool->base.dpps[i]);
1421 
1422 		if (pool->base.ipps[i] != NULL)
1423 			pool->base.ipps[i]->funcs->ipp_destroy(&pool->base.ipps[i]);
1424 
1425 		if (pool->base.hubps[i] != NULL) {
1426 			kfree(TO_DCN20_HUBP(pool->base.hubps[i]));
1427 			pool->base.hubps[i] = NULL;
1428 		}
1429 
1430 		if (pool->base.irqs != NULL) {
1431 			dal_irq_service_destroy(&pool->base.irqs);
1432 		}
1433 	}
1434 
1435 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1436 		if (pool->base.engines[i] != NULL)
1437 			dce110_engine_destroy(&pool->base.engines[i]);
1438 		if (pool->base.hw_i2cs[i] != NULL) {
1439 			kfree(pool->base.hw_i2cs[i]);
1440 			pool->base.hw_i2cs[i] = NULL;
1441 		}
1442 		if (pool->base.sw_i2cs[i] != NULL) {
1443 			kfree(pool->base.sw_i2cs[i]);
1444 			pool->base.sw_i2cs[i] = NULL;
1445 		}
1446 	}
1447 
1448 	for (i = 0; i < pool->base.res_cap->num_opp; i++) {
1449 		if (pool->base.opps[i] != NULL)
1450 			pool->base.opps[i]->funcs->opp_destroy(&pool->base.opps[i]);
1451 	}
1452 
1453 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1454 		if (pool->base.timing_generators[i] != NULL)	{
1455 			kfree(DCN10TG_FROM_TG(pool->base.timing_generators[i]));
1456 			pool->base.timing_generators[i] = NULL;
1457 		}
1458 	}
1459 
1460 	for (i = 0; i < pool->base.res_cap->num_dwb; i++) {
1461 		if (pool->base.dwbc[i] != NULL) {
1462 			kfree(TO_DCN30_DWBC(pool->base.dwbc[i]));
1463 			pool->base.dwbc[i] = NULL;
1464 		}
1465 		if (pool->base.mcif_wb[i] != NULL) {
1466 			kfree(TO_DCN30_MMHUBBUB(pool->base.mcif_wb[i]));
1467 			pool->base.mcif_wb[i] = NULL;
1468 		}
1469 	}
1470 
1471 	for (i = 0; i < pool->base.audio_count; i++) {
1472 		if (pool->base.audios[i])
1473 			dce_aud_destroy(&pool->base.audios[i]);
1474 	}
1475 
1476 	for (i = 0; i < pool->base.clk_src_count; i++) {
1477 		if (pool->base.clock_sources[i] != NULL) {
1478 			dcn20_clock_source_destroy(&pool->base.clock_sources[i]);
1479 			pool->base.clock_sources[i] = NULL;
1480 		}
1481 	}
1482 
1483 	for (i = 0; i < pool->base.res_cap->num_mpc_3dlut; i++) {
1484 		if (pool->base.mpc_lut[i] != NULL) {
1485 			dc_3dlut_func_release(pool->base.mpc_lut[i]);
1486 			pool->base.mpc_lut[i] = NULL;
1487 		}
1488 		if (pool->base.mpc_shaper[i] != NULL) {
1489 			dc_transfer_func_release(pool->base.mpc_shaper[i]);
1490 			pool->base.mpc_shaper[i] = NULL;
1491 		}
1492 	}
1493 
1494 	if (pool->base.dp_clock_source != NULL) {
1495 		dcn20_clock_source_destroy(&pool->base.dp_clock_source);
1496 		pool->base.dp_clock_source = NULL;
1497 	}
1498 
1499 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1500 		if (pool->base.multiple_abms[i] != NULL)
1501 			dce_abm_destroy(&pool->base.multiple_abms[i]);
1502 	}
1503 
1504 	if (pool->base.psr != NULL)
1505 		dmub_psr_destroy(&pool->base.psr);
1506 
1507 	if (pool->base.dccg != NULL)
1508 		dcn_dccg_destroy(&pool->base.dccg);
1509 
1510 	if (pool->base.oem_device != NULL)
1511 		link_destroy_ddc_service(&pool->base.oem_device);
1512 }
1513 
1514 
1515 static bool dcn32_dwbc_create(struct dc_context *ctx, struct resource_pool *pool)
1516 {
1517 	int i;
1518 	uint32_t dwb_count = pool->res_cap->num_dwb;
1519 
1520 	for (i = 0; i < dwb_count; i++) {
1521 		struct dcn30_dwbc *dwbc30 = kzalloc(sizeof(struct dcn30_dwbc),
1522 						    GFP_KERNEL);
1523 
1524 		if (!dwbc30) {
1525 			dm_error("DC: failed to create dwbc30!\n");
1526 			return false;
1527 		}
1528 
1529 #undef REG_STRUCT
1530 #define REG_STRUCT dwbc30_regs
1531 		dwbc_regs_dcn3_init(0);
1532 
1533 		dcn30_dwbc_construct(dwbc30, ctx,
1534 				&dwbc30_regs[i],
1535 				&dwbc30_shift,
1536 				&dwbc30_mask,
1537 				i);
1538 
1539 		pool->dwbc[i] = &dwbc30->base;
1540 	}
1541 	return true;
1542 }
1543 
1544 static bool dcn32_mmhubbub_create(struct dc_context *ctx, struct resource_pool *pool)
1545 {
1546 	int i;
1547 	uint32_t dwb_count = pool->res_cap->num_dwb;
1548 
1549 	for (i = 0; i < dwb_count; i++) {
1550 		struct dcn30_mmhubbub *mcif_wb30 = kzalloc(sizeof(struct dcn30_mmhubbub),
1551 						    GFP_KERNEL);
1552 
1553 		if (!mcif_wb30) {
1554 			dm_error("DC: failed to create mcif_wb30!\n");
1555 			return false;
1556 		}
1557 
1558 #undef REG_STRUCT
1559 #define REG_STRUCT mcif_wb30_regs
1560 		mcif_wb_regs_dcn3_init(0);
1561 
1562 		dcn32_mmhubbub_construct(mcif_wb30, ctx,
1563 				&mcif_wb30_regs[i],
1564 				&mcif_wb30_shift,
1565 				&mcif_wb30_mask,
1566 				i);
1567 
1568 		pool->mcif_wb[i] = &mcif_wb30->base;
1569 	}
1570 	return true;
1571 }
1572 
1573 static struct display_stream_compressor *dcn32_dsc_create(
1574 	struct dc_context *ctx, uint32_t inst)
1575 {
1576 	struct dcn20_dsc *dsc =
1577 		kzalloc(sizeof(struct dcn20_dsc), GFP_KERNEL);
1578 
1579 	if (!dsc) {
1580 		BREAK_TO_DEBUGGER();
1581 		return NULL;
1582 	}
1583 
1584 #undef REG_STRUCT
1585 #define REG_STRUCT dsc_regs
1586 	dsc_regsDCN20_init(0),
1587 	dsc_regsDCN20_init(1),
1588 	dsc_regsDCN20_init(2),
1589 	dsc_regsDCN20_init(3);
1590 
1591 	dsc2_construct(dsc, ctx, inst, &dsc_regs[inst], &dsc_shift, &dsc_mask);
1592 
1593 	dsc->max_image_width = 6016;
1594 
1595 	return &dsc->base;
1596 }
1597 
1598 static void dcn32_destroy_resource_pool(struct resource_pool **pool)
1599 {
1600 	struct dcn32_resource_pool *dcn32_pool = TO_DCN32_RES_POOL(*pool);
1601 
1602 	dcn32_resource_destruct(dcn32_pool);
1603 	kfree(dcn32_pool);
1604 	*pool = NULL;
1605 }
1606 
1607 bool dcn32_acquire_post_bldn_3dlut(
1608 		struct resource_context *res_ctx,
1609 		const struct resource_pool *pool,
1610 		int mpcc_id,
1611 		struct dc_3dlut **lut,
1612 		struct dc_transfer_func **shaper)
1613 {
1614 	bool ret = false;
1615 	union dc_3dlut_state *state;
1616 
1617 	ASSERT(*lut == NULL && *shaper == NULL);
1618 	*lut = NULL;
1619 	*shaper = NULL;
1620 
1621 	if (!res_ctx->is_mpc_3dlut_acquired[mpcc_id]) {
1622 		*lut = pool->mpc_lut[mpcc_id];
1623 		*shaper = pool->mpc_shaper[mpcc_id];
1624 		state = &pool->mpc_lut[mpcc_id]->state;
1625 		res_ctx->is_mpc_3dlut_acquired[mpcc_id] = true;
1626 		ret = true;
1627 	}
1628 	return ret;
1629 }
1630 
1631 bool dcn32_release_post_bldn_3dlut(
1632 		struct resource_context *res_ctx,
1633 		const struct resource_pool *pool,
1634 		struct dc_3dlut **lut,
1635 		struct dc_transfer_func **shaper)
1636 {
1637 	int i;
1638 	bool ret = false;
1639 
1640 	for (i = 0; i < pool->res_cap->num_mpc_3dlut; i++) {
1641 		if (pool->mpc_lut[i] == *lut && pool->mpc_shaper[i] == *shaper) {
1642 			res_ctx->is_mpc_3dlut_acquired[i] = false;
1643 			pool->mpc_lut[i]->state.raw = 0;
1644 			*lut = NULL;
1645 			*shaper = NULL;
1646 			ret = true;
1647 			break;
1648 		}
1649 	}
1650 	return ret;
1651 }
1652 
1653 static void dcn32_enable_phantom_plane(struct dc *dc,
1654 		struct dc_state *context,
1655 		struct dc_stream_state *phantom_stream,
1656 		unsigned int dc_pipe_idx)
1657 {
1658 	struct dc_plane_state *phantom_plane = NULL;
1659 	struct dc_plane_state *prev_phantom_plane = NULL;
1660 	struct pipe_ctx *curr_pipe = &context->res_ctx.pipe_ctx[dc_pipe_idx];
1661 
1662 	while (curr_pipe) {
1663 		if (curr_pipe->top_pipe && curr_pipe->top_pipe->plane_state == curr_pipe->plane_state)
1664 			phantom_plane = prev_phantom_plane;
1665 		else
1666 			phantom_plane = dc_create_plane_state(dc);
1667 
1668 		memcpy(&phantom_plane->address, &curr_pipe->plane_state->address, sizeof(phantom_plane->address));
1669 		memcpy(&phantom_plane->scaling_quality, &curr_pipe->plane_state->scaling_quality,
1670 				sizeof(phantom_plane->scaling_quality));
1671 		memcpy(&phantom_plane->src_rect, &curr_pipe->plane_state->src_rect, sizeof(phantom_plane->src_rect));
1672 		memcpy(&phantom_plane->dst_rect, &curr_pipe->plane_state->dst_rect, sizeof(phantom_plane->dst_rect));
1673 		memcpy(&phantom_plane->clip_rect, &curr_pipe->plane_state->clip_rect, sizeof(phantom_plane->clip_rect));
1674 		memcpy(&phantom_plane->plane_size, &curr_pipe->plane_state->plane_size,
1675 				sizeof(phantom_plane->plane_size));
1676 		memcpy(&phantom_plane->tiling_info, &curr_pipe->plane_state->tiling_info,
1677 				sizeof(phantom_plane->tiling_info));
1678 		memcpy(&phantom_plane->dcc, &curr_pipe->plane_state->dcc, sizeof(phantom_plane->dcc));
1679 		phantom_plane->format = curr_pipe->plane_state->format;
1680 		phantom_plane->rotation = curr_pipe->plane_state->rotation;
1681 		phantom_plane->visible = curr_pipe->plane_state->visible;
1682 
1683 		/* Shadow pipe has small viewport. */
1684 		phantom_plane->clip_rect.y = 0;
1685 		phantom_plane->clip_rect.height = phantom_stream->src.height;
1686 
1687 		phantom_plane->is_phantom = true;
1688 
1689 		dc_add_plane_to_context(dc, phantom_stream, phantom_plane, context);
1690 
1691 		curr_pipe = curr_pipe->bottom_pipe;
1692 		prev_phantom_plane = phantom_plane;
1693 	}
1694 }
1695 
1696 static struct dc_stream_state *dcn32_enable_phantom_stream(struct dc *dc,
1697 		struct dc_state *context,
1698 		display_e2e_pipe_params_st *pipes,
1699 		unsigned int pipe_cnt,
1700 		unsigned int dc_pipe_idx)
1701 {
1702 	struct dc_stream_state *phantom_stream = NULL;
1703 	struct pipe_ctx *ref_pipe = &context->res_ctx.pipe_ctx[dc_pipe_idx];
1704 
1705 	phantom_stream = dc_create_stream_for_sink(ref_pipe->stream->sink);
1706 	phantom_stream->signal = SIGNAL_TYPE_VIRTUAL;
1707 	phantom_stream->dpms_off = true;
1708 	phantom_stream->mall_stream_config.type = SUBVP_PHANTOM;
1709 	phantom_stream->mall_stream_config.paired_stream = ref_pipe->stream;
1710 	ref_pipe->stream->mall_stream_config.type = SUBVP_MAIN;
1711 	ref_pipe->stream->mall_stream_config.paired_stream = phantom_stream;
1712 
1713 	/* stream has limited viewport and small timing */
1714 	memcpy(&phantom_stream->timing, &ref_pipe->stream->timing, sizeof(phantom_stream->timing));
1715 	memcpy(&phantom_stream->src, &ref_pipe->stream->src, sizeof(phantom_stream->src));
1716 	memcpy(&phantom_stream->dst, &ref_pipe->stream->dst, sizeof(phantom_stream->dst));
1717 	DC_FP_START();
1718 	dcn32_set_phantom_stream_timing(dc, context, ref_pipe, phantom_stream, pipes, pipe_cnt, dc_pipe_idx);
1719 	DC_FP_END();
1720 
1721 	dc_add_stream_to_ctx(dc, context, phantom_stream);
1722 	return phantom_stream;
1723 }
1724 
1725 void dcn32_retain_phantom_pipes(struct dc *dc, struct dc_state *context)
1726 {
1727 	int i;
1728 	struct dc_plane_state *phantom_plane = NULL;
1729 	struct dc_stream_state *phantom_stream = NULL;
1730 
1731 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1732 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1733 
1734 		if (!pipe->top_pipe && !pipe->prev_odm_pipe &&
1735 				pipe->plane_state && pipe->stream &&
1736 				pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
1737 			phantom_plane = pipe->plane_state;
1738 			phantom_stream = pipe->stream;
1739 
1740 			dc_plane_state_retain(phantom_plane);
1741 			dc_stream_retain(phantom_stream);
1742 		}
1743 	}
1744 }
1745 
1746 // return true if removed piped from ctx, false otherwise
1747 bool dcn32_remove_phantom_pipes(struct dc *dc, struct dc_state *context, bool fast_update)
1748 {
1749 	int i;
1750 	bool removed_pipe = false;
1751 	struct dc_plane_state *phantom_plane = NULL;
1752 	struct dc_stream_state *phantom_stream = NULL;
1753 
1754 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1755 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1756 		// build scaling params for phantom pipes
1757 		if (pipe->plane_state && pipe->stream && pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
1758 			phantom_plane = pipe->plane_state;
1759 			phantom_stream = pipe->stream;
1760 
1761 			dc_rem_all_planes_for_stream(dc, pipe->stream, context);
1762 			dc_remove_stream_from_ctx(dc, context, pipe->stream);
1763 
1764 			/* Ref count is incremented on allocation and also when added to the context.
1765 			 * Therefore we must call release for the the phantom plane and stream once
1766 			 * they are removed from the ctx to finally decrement the refcount to 0 to free.
1767 			 */
1768 			dc_plane_state_release(phantom_plane);
1769 			dc_stream_release(phantom_stream);
1770 
1771 			removed_pipe = true;
1772 		}
1773 
1774 		/* For non-full updates, a shallow copy of the current state
1775 		 * is created. In this case we don't want to erase the current
1776 		 * state (there can be 2 HIRQL threads, one in flip, and one in
1777 		 * checkMPO) that can cause a race condition.
1778 		 *
1779 		 * This is just a workaround, needs a proper fix.
1780 		 */
1781 		if (!fast_update) {
1782 			// Clear all phantom stream info
1783 			if (pipe->stream) {
1784 				pipe->stream->mall_stream_config.type = SUBVP_NONE;
1785 				pipe->stream->mall_stream_config.paired_stream = NULL;
1786 			}
1787 
1788 			if (pipe->plane_state) {
1789 				pipe->plane_state->is_phantom = false;
1790 			}
1791 		}
1792 	}
1793 	return removed_pipe;
1794 }
1795 
1796 /* TODO: Input to this function should indicate which pipe indexes (or streams)
1797  * require a phantom pipe / stream
1798  */
1799 void dcn32_add_phantom_pipes(struct dc *dc, struct dc_state *context,
1800 		display_e2e_pipe_params_st *pipes,
1801 		unsigned int pipe_cnt,
1802 		unsigned int index)
1803 {
1804 	struct dc_stream_state *phantom_stream = NULL;
1805 	unsigned int i;
1806 
1807 	// The index of the DC pipe passed into this function is guarenteed to
1808 	// be a valid candidate for SubVP (i.e. has a plane, stream, doesn't
1809 	// already have phantom pipe assigned, etc.) by previous checks.
1810 	phantom_stream = dcn32_enable_phantom_stream(dc, context, pipes, pipe_cnt, index);
1811 	dcn32_enable_phantom_plane(dc, context, phantom_stream, index);
1812 
1813 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1814 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1815 
1816 		// Build scaling params for phantom pipes which were newly added.
1817 		// We determine which phantom pipes were added by comparing with
1818 		// the phantom stream.
1819 		if (pipe->plane_state && pipe->stream && pipe->stream == phantom_stream &&
1820 				pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
1821 			pipe->stream->use_dynamic_meta = false;
1822 			pipe->plane_state->flip_immediate = false;
1823 			if (!resource_build_scaling_params(pipe)) {
1824 				// Log / remove phantom pipes since failed to build scaling params
1825 			}
1826 		}
1827 	}
1828 }
1829 
1830 bool dcn32_validate_bandwidth(struct dc *dc,
1831 		struct dc_state *context,
1832 		bool fast_validate)
1833 {
1834 	bool out = false;
1835 
1836 	BW_VAL_TRACE_SETUP();
1837 
1838 	int vlevel = 0;
1839 	int pipe_cnt = 0;
1840 	display_e2e_pipe_params_st *pipes = kzalloc(dc->res_pool->pipe_count * sizeof(display_e2e_pipe_params_st), GFP_KERNEL);
1841 	struct mall_temp_config mall_temp_config;
1842 
1843 	/* To handle Freesync properly, setting FreeSync DML parameters
1844 	 * to its default state for the first stage of validation
1845 	 */
1846 	context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching = false;
1847 	context->bw_ctx.dml.soc.dram_clock_change_requirement_final = true;
1848 
1849 	DC_LOGGER_INIT(dc->ctx->logger);
1850 
1851 	/* For fast validation, there are situations where a shallow copy of
1852 	 * of the dc->current_state is created for the validation. In this case
1853 	 * we want to save and restore the mall config because we always
1854 	 * teardown subvp at the beginning of validation (and don't attempt
1855 	 * to add it back if it's fast validation). If we don't restore the
1856 	 * subvp config in cases of fast validation + shallow copy of the
1857 	 * dc->current_state, the dc->current_state will have a partially
1858 	 * removed subvp state when we did not intend to remove it.
1859 	 */
1860 	if (fast_validate) {
1861 		memset(&mall_temp_config, 0, sizeof(mall_temp_config));
1862 		dcn32_save_mall_state(dc, context, &mall_temp_config);
1863 	}
1864 
1865 	BW_VAL_TRACE_COUNT();
1866 
1867 	DC_FP_START();
1868 	out = dcn32_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel, fast_validate);
1869 	DC_FP_END();
1870 
1871 	if (fast_validate)
1872 		dcn32_restore_mall_state(dc, context, &mall_temp_config);
1873 
1874 	if (pipe_cnt == 0)
1875 		goto validate_out;
1876 
1877 	if (!out)
1878 		goto validate_fail;
1879 
1880 	BW_VAL_TRACE_END_VOLTAGE_LEVEL();
1881 
1882 	if (fast_validate) {
1883 		BW_VAL_TRACE_SKIP(fast);
1884 		goto validate_out;
1885 	}
1886 
1887 	dc->res_pool->funcs->calculate_wm_and_dlg(dc, context, pipes, pipe_cnt, vlevel);
1888 
1889 	BW_VAL_TRACE_END_WATERMARKS();
1890 
1891 	goto validate_out;
1892 
1893 validate_fail:
1894 	DC_LOG_WARNING("Mode Validation Warning: %s failed validation.\n",
1895 		dml_get_status_message(context->bw_ctx.dml.vba.ValidationStatus[context->bw_ctx.dml.vba.soc.num_states]));
1896 
1897 	BW_VAL_TRACE_SKIP(fail);
1898 	out = false;
1899 
1900 validate_out:
1901 	kfree(pipes);
1902 
1903 	BW_VAL_TRACE_FINISH();
1904 
1905 	return out;
1906 }
1907 
1908 int dcn32_populate_dml_pipes_from_context(
1909 	struct dc *dc, struct dc_state *context,
1910 	display_e2e_pipe_params_st *pipes,
1911 	bool fast_validate)
1912 {
1913 	int i, pipe_cnt;
1914 	struct resource_context *res_ctx = &context->res_ctx;
1915 	struct pipe_ctx *pipe;
1916 	bool subvp_in_use = false;
1917 	uint8_t is_pipe_split_expected[MAX_PIPES] = {0};
1918 	struct dc_crtc_timing *timing;
1919 
1920 	dcn20_populate_dml_pipes_from_context(dc, context, pipes, fast_validate);
1921 
1922 	/* Determine whether we will apply ODM 2to1 policy:
1923 	 * Applies to single display and where the number of planes is less than 3.
1924 	 * For 3 plane case ( 2 MPO planes ), we will not set the policy for the MPO pipes.
1925 	 *
1926 	 * Apply pipe split policy first so we can predict the pipe split correctly
1927 	 * (dcn32_predict_pipe_split).
1928 	 */
1929 	for (i = 0, pipe_cnt = 0; i < dc->res_pool->pipe_count; i++) {
1930 		if (!res_ctx->pipe_ctx[i].stream)
1931 			continue;
1932 		pipe = &res_ctx->pipe_ctx[i];
1933 		timing = &pipe->stream->timing;
1934 
1935 		pipes[pipe_cnt].pipe.dest.odm_combine_policy = dm_odm_combine_policy_dal;
1936 		if (context->stream_count == 1 &&
1937 				context->stream_status[0].plane_count == 1 &&
1938 				!dc_is_hdmi_signal(res_ctx->pipe_ctx[i].stream->signal) &&
1939 				is_h_timing_divisible_by_2(res_ctx->pipe_ctx[i].stream) &&
1940 				pipe->stream->timing.pix_clk_100hz * 100 > DCN3_2_VMIN_DISPCLK_HZ &&
1941 				dc->debug.enable_single_display_2to1_odm_policy) {
1942 			pipes[pipe_cnt].pipe.dest.odm_combine_policy = dm_odm_combine_policy_2to1;
1943 		}
1944 		pipe_cnt++;
1945 	}
1946 
1947 	for (i = 0, pipe_cnt = 0; i < dc->res_pool->pipe_count; i++) {
1948 
1949 		if (!res_ctx->pipe_ctx[i].stream)
1950 			continue;
1951 		pipe = &res_ctx->pipe_ctx[i];
1952 		timing = &pipe->stream->timing;
1953 
1954 		pipes[pipe_cnt].pipe.src.gpuvm = true;
1955 		DC_FP_START();
1956 		dcn32_zero_pipe_dcc_fraction(pipes, pipe_cnt);
1957 		DC_FP_END();
1958 		pipes[pipe_cnt].pipe.dest.vfront_porch = timing->v_front_porch;
1959 		pipes[pipe_cnt].pipe.src.gpuvm_min_page_size_kbytes = 256; // according to spreadsheet
1960 		pipes[pipe_cnt].pipe.src.unbounded_req_mode = false;
1961 		pipes[pipe_cnt].pipe.scale_ratio_depth.lb_depth = dm_lb_19;
1962 
1963 		/* Only populate DML input with subvp info for full updates.
1964 		 * This is just a workaround -- needs a proper fix.
1965 		 */
1966 		if (!fast_validate) {
1967 			switch (pipe->stream->mall_stream_config.type) {
1968 			case SUBVP_MAIN:
1969 				pipes[pipe_cnt].pipe.src.use_mall_for_pstate_change = dm_use_mall_pstate_change_sub_viewport;
1970 				subvp_in_use = true;
1971 				break;
1972 			case SUBVP_PHANTOM:
1973 				pipes[pipe_cnt].pipe.src.use_mall_for_pstate_change = dm_use_mall_pstate_change_phantom_pipe;
1974 				pipes[pipe_cnt].pipe.src.use_mall_for_static_screen = dm_use_mall_static_screen_disable;
1975 				// Disallow unbounded req for SubVP according to DCHUB programming guide
1976 				pipes[pipe_cnt].pipe.src.unbounded_req_mode = false;
1977 				break;
1978 			case SUBVP_NONE:
1979 				pipes[pipe_cnt].pipe.src.use_mall_for_pstate_change = dm_use_mall_pstate_change_disable;
1980 				pipes[pipe_cnt].pipe.src.use_mall_for_static_screen = dm_use_mall_static_screen_disable;
1981 				break;
1982 			default:
1983 				break;
1984 			}
1985 		}
1986 
1987 		pipes[pipe_cnt].dout.dsc_input_bpc = 0;
1988 		if (pipes[pipe_cnt].dout.dsc_enable) {
1989 			switch (timing->display_color_depth) {
1990 			case COLOR_DEPTH_888:
1991 				pipes[pipe_cnt].dout.dsc_input_bpc = 8;
1992 				break;
1993 			case COLOR_DEPTH_101010:
1994 				pipes[pipe_cnt].dout.dsc_input_bpc = 10;
1995 				break;
1996 			case COLOR_DEPTH_121212:
1997 				pipes[pipe_cnt].dout.dsc_input_bpc = 12;
1998 				break;
1999 			default:
2000 				ASSERT(0);
2001 				break;
2002 			}
2003 		}
2004 
2005 		DC_FP_START();
2006 		is_pipe_split_expected[i] = dcn32_predict_pipe_split(context, &pipes[pipe_cnt]);
2007 		DC_FP_END();
2008 
2009 		pipe_cnt++;
2010 	}
2011 
2012 	/* For DET allocation, we don't want to use DML policy (not optimal for utilizing all
2013 	 * the DET available for each pipe). Use the DET override input to maintain our driver
2014 	 * policy.
2015 	 */
2016 	dcn32_set_det_allocations(dc, context, pipes);
2017 
2018 	// In general cases we want to keep the dram clock change requirement
2019 	// (prefer configs that support MCLK switch). Only override to false
2020 	// for SubVP
2021 	if (subvp_in_use)
2022 		context->bw_ctx.dml.soc.dram_clock_change_requirement_final = false;
2023 	else
2024 		context->bw_ctx.dml.soc.dram_clock_change_requirement_final = true;
2025 
2026 	return pipe_cnt;
2027 }
2028 
2029 static struct dc_cap_funcs cap_funcs = {
2030 	.get_dcc_compression_cap = dcn20_get_dcc_compression_cap
2031 };
2032 
2033 void dcn32_calculate_wm_and_dlg(struct dc *dc, struct dc_state *context,
2034 				display_e2e_pipe_params_st *pipes,
2035 				int pipe_cnt,
2036 				int vlevel)
2037 {
2038     DC_FP_START();
2039     dcn32_calculate_wm_and_dlg_fpu(dc, context, pipes, pipe_cnt, vlevel);
2040     DC_FP_END();
2041 }
2042 
2043 static void dcn32_update_bw_bounding_box(struct dc *dc, struct clk_bw_params *bw_params)
2044 {
2045 	DC_FP_START();
2046 	dcn32_update_bw_bounding_box_fpu(dc, bw_params);
2047 	DC_FP_END();
2048 }
2049 
2050 static struct resource_funcs dcn32_res_pool_funcs = {
2051 	.destroy = dcn32_destroy_resource_pool,
2052 	.link_enc_create = dcn32_link_encoder_create,
2053 	.link_enc_create_minimal = NULL,
2054 	.panel_cntl_create = dcn32_panel_cntl_create,
2055 	.validate_bandwidth = dcn32_validate_bandwidth,
2056 	.calculate_wm_and_dlg = dcn32_calculate_wm_and_dlg,
2057 	.populate_dml_pipes = dcn32_populate_dml_pipes_from_context,
2058 	.acquire_idle_pipe_for_head_pipe_in_layer = dcn32_acquire_idle_pipe_for_head_pipe_in_layer,
2059 	.add_stream_to_ctx = dcn30_add_stream_to_ctx,
2060 	.add_dsc_to_stream_resource = dcn20_add_dsc_to_stream_resource,
2061 	.remove_stream_from_ctx = dcn20_remove_stream_from_ctx,
2062 	.populate_dml_writeback_from_context = dcn30_populate_dml_writeback_from_context,
2063 	.set_mcif_arb_params = dcn30_set_mcif_arb_params,
2064 	.find_first_free_match_stream_enc_for_link = dcn10_find_first_free_match_stream_enc_for_link,
2065 	.acquire_post_bldn_3dlut = dcn32_acquire_post_bldn_3dlut,
2066 	.release_post_bldn_3dlut = dcn32_release_post_bldn_3dlut,
2067 	.update_bw_bounding_box = dcn32_update_bw_bounding_box,
2068 	.patch_unknown_plane_state = dcn20_patch_unknown_plane_state,
2069 	.update_soc_for_wm_a = dcn30_update_soc_for_wm_a,
2070 	.add_phantom_pipes = dcn32_add_phantom_pipes,
2071 	.remove_phantom_pipes = dcn32_remove_phantom_pipes,
2072 	.retain_phantom_pipes = dcn32_retain_phantom_pipes,
2073 	.save_mall_state = dcn32_save_mall_state,
2074 	.restore_mall_state = dcn32_restore_mall_state,
2075 };
2076 
2077 
2078 static bool dcn32_resource_construct(
2079 	uint8_t num_virtual_links,
2080 	struct dc *dc,
2081 	struct dcn32_resource_pool *pool)
2082 {
2083 	int i, j;
2084 	struct dc_context *ctx = dc->ctx;
2085 	struct irq_service_init_data init_data;
2086 	struct ddc_service_init_data ddc_init_data = {0};
2087 	uint32_t pipe_fuses = 0;
2088 	uint32_t num_pipes  = 4;
2089 
2090 	#undef REG_STRUCT
2091 	#define REG_STRUCT bios_regs
2092 		bios_regs_init();
2093 
2094 	#undef REG_STRUCT
2095 	#define REG_STRUCT clk_src_regs
2096 		clk_src_regs_init(0, A),
2097 		clk_src_regs_init(1, B),
2098 		clk_src_regs_init(2, C),
2099 		clk_src_regs_init(3, D),
2100 		clk_src_regs_init(4, E);
2101 	#undef REG_STRUCT
2102 	#define REG_STRUCT abm_regs
2103 		abm_regs_init(0),
2104 		abm_regs_init(1),
2105 		abm_regs_init(2),
2106 		abm_regs_init(3);
2107 
2108 	#undef REG_STRUCT
2109 	#define REG_STRUCT dccg_regs
2110 		dccg_regs_init();
2111 
2112 	DC_FP_START();
2113 
2114 	ctx->dc_bios->regs = &bios_regs;
2115 
2116 	pool->base.res_cap = &res_cap_dcn32;
2117 	/* max number of pipes for ASIC before checking for pipe fuses */
2118 	num_pipes  = pool->base.res_cap->num_timing_generator;
2119 	pipe_fuses = REG_READ(CC_DC_PIPE_DIS);
2120 
2121 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++)
2122 		if (pipe_fuses & 1 << i)
2123 			num_pipes--;
2124 
2125 	if (pipe_fuses & 1)
2126 		ASSERT(0); //Unexpected - Pipe 0 should always be fully functional!
2127 
2128 	if (pipe_fuses & CC_DC_PIPE_DIS__DC_FULL_DIS_MASK)
2129 		ASSERT(0); //Entire DCN is harvested!
2130 
2131 	/* within dml lib, initial value is hard coded, if ASIC pipe is fused, the
2132 	 * value will be changed, update max_num_dpp and max_num_otg for dml.
2133 	 */
2134 	dcn3_2_ip.max_num_dpp = num_pipes;
2135 	dcn3_2_ip.max_num_otg = num_pipes;
2136 
2137 	pool->base.funcs = &dcn32_res_pool_funcs;
2138 
2139 	/*************************************************
2140 	 *  Resource + asic cap harcoding                *
2141 	 *************************************************/
2142 	pool->base.underlay_pipe_index = NO_UNDERLAY_PIPE;
2143 	pool->base.timing_generator_count = num_pipes;
2144 	pool->base.pipe_count = num_pipes;
2145 	pool->base.mpcc_count = num_pipes;
2146 	dc->caps.max_downscale_ratio = 600;
2147 	dc->caps.i2c_speed_in_khz = 100;
2148 	dc->caps.i2c_speed_in_khz_hdcp = 100; /*1.4 w/a applied by default*/
2149 	/* TODO: Bring max_cursor_size back to 256 after subvp cursor corruption is fixed*/
2150 	dc->caps.max_cursor_size = 64;
2151 	dc->caps.min_horizontal_blanking_period = 80;
2152 	dc->caps.dmdata_alloc_size = 2048;
2153 	dc->caps.mall_size_per_mem_channel = 0;
2154 	dc->caps.mall_size_total = 0;
2155 	dc->caps.cursor_cache_size = dc->caps.max_cursor_size * dc->caps.max_cursor_size * 8;
2156 
2157 	dc->caps.cache_line_size = 64;
2158 	dc->caps.cache_num_ways = 16;
2159 	dc->caps.max_cab_allocation_bytes = 67108864; // 64MB = 1024 * 1024 * 64
2160 	dc->caps.subvp_fw_processing_delay_us = 15;
2161 	dc->caps.subvp_drr_max_vblank_margin_us = 40;
2162 	dc->caps.subvp_prefetch_end_to_mall_start_us = 15;
2163 	dc->caps.subvp_swath_height_margin_lines = 16;
2164 	dc->caps.subvp_pstate_allow_width_us = 20;
2165 	dc->caps.subvp_vertical_int_margin_us = 30;
2166 	dc->caps.subvp_drr_vblank_start_margin_us = 100; // 100us margin
2167 
2168 	dc->caps.max_slave_planes = 2;
2169 	dc->caps.max_slave_yuv_planes = 2;
2170 	dc->caps.max_slave_rgb_planes = 2;
2171 	dc->caps.post_blend_color_processing = true;
2172 	dc->caps.force_dp_tps4_for_cp2520 = true;
2173 	if (dc->config.forceHBR2CP2520)
2174 		dc->caps.force_dp_tps4_for_cp2520 = false;
2175 	dc->caps.dp_hpo = true;
2176 	dc->caps.dp_hdmi21_pcon_support = true;
2177 	dc->caps.edp_dsc_support = true;
2178 	dc->caps.extended_aux_timeout_support = true;
2179 	dc->caps.dmcub_support = true;
2180 
2181 	/* Color pipeline capabilities */
2182 	dc->caps.color.dpp.dcn_arch = 1;
2183 	dc->caps.color.dpp.input_lut_shared = 0;
2184 	dc->caps.color.dpp.icsc = 1;
2185 	dc->caps.color.dpp.dgam_ram = 0; // must use gamma_corr
2186 	dc->caps.color.dpp.dgam_rom_caps.srgb = 1;
2187 	dc->caps.color.dpp.dgam_rom_caps.bt2020 = 1;
2188 	dc->caps.color.dpp.dgam_rom_caps.gamma2_2 = 1;
2189 	dc->caps.color.dpp.dgam_rom_caps.pq = 1;
2190 	dc->caps.color.dpp.dgam_rom_caps.hlg = 1;
2191 	dc->caps.color.dpp.post_csc = 1;
2192 	dc->caps.color.dpp.gamma_corr = 1;
2193 	dc->caps.color.dpp.dgam_rom_for_yuv = 0;
2194 
2195 	dc->caps.color.dpp.hw_3d_lut = 1;
2196 	dc->caps.color.dpp.ogam_ram = 0;  // no OGAM in DPP since DCN1
2197 	// no OGAM ROM on DCN2 and later ASICs
2198 	dc->caps.color.dpp.ogam_rom_caps.srgb = 0;
2199 	dc->caps.color.dpp.ogam_rom_caps.bt2020 = 0;
2200 	dc->caps.color.dpp.ogam_rom_caps.gamma2_2 = 0;
2201 	dc->caps.color.dpp.ogam_rom_caps.pq = 0;
2202 	dc->caps.color.dpp.ogam_rom_caps.hlg = 0;
2203 	dc->caps.color.dpp.ocsc = 0;
2204 
2205 	dc->caps.color.mpc.gamut_remap = 1;
2206 	dc->caps.color.mpc.num_3dluts = pool->base.res_cap->num_mpc_3dlut; //4, configurable to be before or after BLND in MPCC
2207 	dc->caps.color.mpc.ogam_ram = 1;
2208 	dc->caps.color.mpc.ogam_rom_caps.srgb = 0;
2209 	dc->caps.color.mpc.ogam_rom_caps.bt2020 = 0;
2210 	dc->caps.color.mpc.ogam_rom_caps.gamma2_2 = 0;
2211 	dc->caps.color.mpc.ogam_rom_caps.pq = 0;
2212 	dc->caps.color.mpc.ogam_rom_caps.hlg = 0;
2213 	dc->caps.color.mpc.ocsc = 1;
2214 
2215 	/* Use pipe context based otg sync logic */
2216 	dc->config.use_pipe_ctx_sync_logic = true;
2217 
2218 	/* read VBIOS LTTPR caps */
2219 	{
2220 		if (ctx->dc_bios->funcs->get_lttpr_caps) {
2221 			enum bp_result bp_query_result;
2222 			uint8_t is_vbios_lttpr_enable = 0;
2223 
2224 			bp_query_result = ctx->dc_bios->funcs->get_lttpr_caps(ctx->dc_bios, &is_vbios_lttpr_enable);
2225 			dc->caps.vbios_lttpr_enable = (bp_query_result == BP_RESULT_OK) && !!is_vbios_lttpr_enable;
2226 		}
2227 
2228 		/* interop bit is implicit */
2229 		{
2230 			dc->caps.vbios_lttpr_aware = true;
2231 		}
2232 	}
2233 
2234 	if (dc->ctx->dce_environment == DCE_ENV_PRODUCTION_DRV)
2235 		dc->debug = debug_defaults_drv;
2236 	else if (dc->ctx->dce_environment == DCE_ENV_FPGA_MAXIMUS) {
2237 		dc->debug = debug_defaults_diags;
2238 	} else
2239 		dc->debug = debug_defaults_diags;
2240 	// Init the vm_helper
2241 	if (dc->vm_helper)
2242 		vm_helper_init(dc->vm_helper, 16);
2243 
2244 	/*************************************************
2245 	 *  Create resources                             *
2246 	 *************************************************/
2247 
2248 	/* Clock Sources for Pixel Clock*/
2249 	pool->base.clock_sources[DCN32_CLK_SRC_PLL0] =
2250 			dcn32_clock_source_create(ctx, ctx->dc_bios,
2251 				CLOCK_SOURCE_COMBO_PHY_PLL0,
2252 				&clk_src_regs[0], false);
2253 	pool->base.clock_sources[DCN32_CLK_SRC_PLL1] =
2254 			dcn32_clock_source_create(ctx, ctx->dc_bios,
2255 				CLOCK_SOURCE_COMBO_PHY_PLL1,
2256 				&clk_src_regs[1], false);
2257 	pool->base.clock_sources[DCN32_CLK_SRC_PLL2] =
2258 			dcn32_clock_source_create(ctx, ctx->dc_bios,
2259 				CLOCK_SOURCE_COMBO_PHY_PLL2,
2260 				&clk_src_regs[2], false);
2261 	pool->base.clock_sources[DCN32_CLK_SRC_PLL3] =
2262 			dcn32_clock_source_create(ctx, ctx->dc_bios,
2263 				CLOCK_SOURCE_COMBO_PHY_PLL3,
2264 				&clk_src_regs[3], false);
2265 	pool->base.clock_sources[DCN32_CLK_SRC_PLL4] =
2266 			dcn32_clock_source_create(ctx, ctx->dc_bios,
2267 				CLOCK_SOURCE_COMBO_PHY_PLL4,
2268 				&clk_src_regs[4], false);
2269 
2270 	pool->base.clk_src_count = DCN32_CLK_SRC_TOTAL;
2271 
2272 	/* todo: not reuse phy_pll registers */
2273 	pool->base.dp_clock_source =
2274 			dcn32_clock_source_create(ctx, ctx->dc_bios,
2275 				CLOCK_SOURCE_ID_DP_DTO,
2276 				&clk_src_regs[0], true);
2277 
2278 	for (i = 0; i < pool->base.clk_src_count; i++) {
2279 		if (pool->base.clock_sources[i] == NULL) {
2280 			dm_error("DC: failed to create clock sources!\n");
2281 			BREAK_TO_DEBUGGER();
2282 			goto create_fail;
2283 		}
2284 	}
2285 
2286 	/* DCCG */
2287 	pool->base.dccg = dccg32_create(ctx, &dccg_regs, &dccg_shift, &dccg_mask);
2288 	if (pool->base.dccg == NULL) {
2289 		dm_error("DC: failed to create dccg!\n");
2290 		BREAK_TO_DEBUGGER();
2291 		goto create_fail;
2292 	}
2293 
2294 	/* DML */
2295 	if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment))
2296 		dml_init_instance(&dc->dml, &dcn3_2_soc, &dcn3_2_ip, DML_PROJECT_DCN32);
2297 
2298 	/* IRQ Service */
2299 	init_data.ctx = dc->ctx;
2300 	pool->base.irqs = dal_irq_service_dcn32_create(&init_data);
2301 	if (!pool->base.irqs)
2302 		goto create_fail;
2303 
2304 	/* HUBBUB */
2305 	pool->base.hubbub = dcn32_hubbub_create(ctx);
2306 	if (pool->base.hubbub == NULL) {
2307 		BREAK_TO_DEBUGGER();
2308 		dm_error("DC: failed to create hubbub!\n");
2309 		goto create_fail;
2310 	}
2311 
2312 	/* HUBPs, DPPs, OPPs, TGs, ABMs */
2313 	for (i = 0, j = 0; i < pool->base.res_cap->num_timing_generator; i++) {
2314 
2315 		/* if pipe is disabled, skip instance of HW pipe,
2316 		 * i.e, skip ASIC register instance
2317 		 */
2318 		if (pipe_fuses & 1 << i)
2319 			continue;
2320 
2321 		/* HUBPs */
2322 		pool->base.hubps[j] = dcn32_hubp_create(ctx, i);
2323 		if (pool->base.hubps[j] == NULL) {
2324 			BREAK_TO_DEBUGGER();
2325 			dm_error(
2326 				"DC: failed to create hubps!\n");
2327 			goto create_fail;
2328 		}
2329 
2330 		/* DPPs */
2331 		pool->base.dpps[j] = dcn32_dpp_create(ctx, i);
2332 		if (pool->base.dpps[j] == NULL) {
2333 			BREAK_TO_DEBUGGER();
2334 			dm_error(
2335 				"DC: failed to create dpps!\n");
2336 			goto create_fail;
2337 		}
2338 
2339 		/* OPPs */
2340 		pool->base.opps[j] = dcn32_opp_create(ctx, i);
2341 		if (pool->base.opps[j] == NULL) {
2342 			BREAK_TO_DEBUGGER();
2343 			dm_error(
2344 				"DC: failed to create output pixel processor!\n");
2345 			goto create_fail;
2346 		}
2347 
2348 		/* TGs */
2349 		pool->base.timing_generators[j] = dcn32_timing_generator_create(
2350 				ctx, i);
2351 		if (pool->base.timing_generators[j] == NULL) {
2352 			BREAK_TO_DEBUGGER();
2353 			dm_error("DC: failed to create tg!\n");
2354 			goto create_fail;
2355 		}
2356 
2357 		/* ABMs */
2358 		pool->base.multiple_abms[j] = dmub_abm_create(ctx,
2359 				&abm_regs[i],
2360 				&abm_shift,
2361 				&abm_mask);
2362 		if (pool->base.multiple_abms[j] == NULL) {
2363 			dm_error("DC: failed to create abm for pipe %d!\n", i);
2364 			BREAK_TO_DEBUGGER();
2365 			goto create_fail;
2366 		}
2367 
2368 		/* index for resource pool arrays for next valid pipe */
2369 		j++;
2370 	}
2371 
2372 	/* PSR */
2373 	pool->base.psr = dmub_psr_create(ctx);
2374 	if (pool->base.psr == NULL) {
2375 		dm_error("DC: failed to create psr obj!\n");
2376 		BREAK_TO_DEBUGGER();
2377 		goto create_fail;
2378 	}
2379 
2380 	/* MPCCs */
2381 	pool->base.mpc = dcn32_mpc_create(ctx, pool->base.res_cap->num_timing_generator, pool->base.res_cap->num_mpc_3dlut);
2382 	if (pool->base.mpc == NULL) {
2383 		BREAK_TO_DEBUGGER();
2384 		dm_error("DC: failed to create mpc!\n");
2385 		goto create_fail;
2386 	}
2387 
2388 	/* DSCs */
2389 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
2390 		pool->base.dscs[i] = dcn32_dsc_create(ctx, i);
2391 		if (pool->base.dscs[i] == NULL) {
2392 			BREAK_TO_DEBUGGER();
2393 			dm_error("DC: failed to create display stream compressor %d!\n", i);
2394 			goto create_fail;
2395 		}
2396 	}
2397 
2398 	/* DWB */
2399 	if (!dcn32_dwbc_create(ctx, &pool->base)) {
2400 		BREAK_TO_DEBUGGER();
2401 		dm_error("DC: failed to create dwbc!\n");
2402 		goto create_fail;
2403 	}
2404 
2405 	/* MMHUBBUB */
2406 	if (!dcn32_mmhubbub_create(ctx, &pool->base)) {
2407 		BREAK_TO_DEBUGGER();
2408 		dm_error("DC: failed to create mcif_wb!\n");
2409 		goto create_fail;
2410 	}
2411 
2412 	/* AUX and I2C */
2413 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
2414 		pool->base.engines[i] = dcn32_aux_engine_create(ctx, i);
2415 		if (pool->base.engines[i] == NULL) {
2416 			BREAK_TO_DEBUGGER();
2417 			dm_error(
2418 				"DC:failed to create aux engine!!\n");
2419 			goto create_fail;
2420 		}
2421 		pool->base.hw_i2cs[i] = dcn32_i2c_hw_create(ctx, i);
2422 		if (pool->base.hw_i2cs[i] == NULL) {
2423 			BREAK_TO_DEBUGGER();
2424 			dm_error(
2425 				"DC:failed to create hw i2c!!\n");
2426 			goto create_fail;
2427 		}
2428 		pool->base.sw_i2cs[i] = NULL;
2429 	}
2430 
2431 	/* Audio, HWSeq, Stream Encoders including HPO and virtual, MPC 3D LUTs */
2432 	if (!resource_construct(num_virtual_links, dc, &pool->base,
2433 			(!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment) ?
2434 			&res_create_funcs : &res_create_maximus_funcs)))
2435 			goto create_fail;
2436 
2437 	/* HW Sequencer init functions and Plane caps */
2438 	dcn32_hw_sequencer_init_functions(dc);
2439 
2440 	dc->caps.max_planes =  pool->base.pipe_count;
2441 
2442 	for (i = 0; i < dc->caps.max_planes; ++i)
2443 		dc->caps.planes[i] = plane_cap;
2444 
2445 	dc->cap_funcs = cap_funcs;
2446 
2447 	if (dc->ctx->dc_bios->fw_info.oem_i2c_present) {
2448 		ddc_init_data.ctx = dc->ctx;
2449 		ddc_init_data.link = NULL;
2450 		ddc_init_data.id.id = dc->ctx->dc_bios->fw_info.oem_i2c_obj_id;
2451 		ddc_init_data.id.enum_id = 0;
2452 		ddc_init_data.id.type = OBJECT_TYPE_GENERIC;
2453 		pool->base.oem_device = link_create_ddc_service(&ddc_init_data);
2454 	} else {
2455 		pool->base.oem_device = NULL;
2456 	}
2457 
2458 	if (ASICREV_IS_GC_11_0_3(dc->ctx->asic_id.hw_internal_rev) && (dc->config.sdpif_request_limit_words_per_umc == 0))
2459 		dc->config.sdpif_request_limit_words_per_umc = 16;
2460 
2461 	DC_FP_END();
2462 
2463 	return true;
2464 
2465 create_fail:
2466 
2467 	DC_FP_END();
2468 
2469 	dcn32_resource_destruct(pool);
2470 
2471 	return false;
2472 }
2473 
2474 struct resource_pool *dcn32_create_resource_pool(
2475 		const struct dc_init_data *init_data,
2476 		struct dc *dc)
2477 {
2478 	struct dcn32_resource_pool *pool =
2479 		kzalloc(sizeof(struct dcn32_resource_pool), GFP_KERNEL);
2480 
2481 	if (!pool)
2482 		return NULL;
2483 
2484 	if (dcn32_resource_construct(init_data->num_virtual_links, dc, pool))
2485 		return &pool->base;
2486 
2487 	BREAK_TO_DEBUGGER();
2488 	kfree(pool);
2489 	return NULL;
2490 }
2491 
2492 static struct pipe_ctx *find_idle_secondary_pipe_check_mpo(
2493 		struct resource_context *res_ctx,
2494 		const struct resource_pool *pool,
2495 		const struct pipe_ctx *primary_pipe)
2496 {
2497 	int i;
2498 	struct pipe_ctx *secondary_pipe = NULL;
2499 	struct pipe_ctx *next_odm_mpo_pipe = NULL;
2500 	int primary_index, preferred_pipe_idx;
2501 	struct pipe_ctx *old_primary_pipe = NULL;
2502 
2503 	/*
2504 	 * Modified from find_idle_secondary_pipe
2505 	 * With windowed MPO and ODM, we want to avoid the case where we want a
2506 	 *  free pipe for the left side but the free pipe is being used on the
2507 	 *  right side.
2508 	 * Add check on current_state if the primary_pipe is the left side,
2509 	 *  to check the right side ( primary_pipe->next_odm_pipe ) to see if
2510 	 *  it is using a pipe for MPO ( primary_pipe->next_odm_pipe->bottom_pipe )
2511 	 * - If so, then don't use this pipe
2512 	 * EXCEPTION - 3 plane ( 2 MPO plane ) case
2513 	 * - in this case, the primary pipe has already gotten a free pipe for the
2514 	 *  MPO window in the left
2515 	 * - when it tries to get a free pipe for the MPO window on the right,
2516 	 *  it will see that it is already assigned to the right side
2517 	 *  ( primary_pipe->next_odm_pipe ).  But in this case, we want this
2518 	 *  free pipe, since it will be for the right side.  So add an
2519 	 *  additional condition, that skipping the free pipe on the right only
2520 	 *  applies if the primary pipe has no bottom pipe currently assigned
2521 	 */
2522 	if (primary_pipe) {
2523 		primary_index = primary_pipe->pipe_idx;
2524 		old_primary_pipe = &primary_pipe->stream->ctx->dc->current_state->res_ctx.pipe_ctx[primary_index];
2525 		if ((old_primary_pipe->next_odm_pipe) && (old_primary_pipe->next_odm_pipe->bottom_pipe)
2526 			&& (!primary_pipe->bottom_pipe))
2527 			next_odm_mpo_pipe = old_primary_pipe->next_odm_pipe->bottom_pipe;
2528 
2529 		preferred_pipe_idx = (pool->pipe_count - 1) - primary_pipe->pipe_idx;
2530 		if ((res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) &&
2531 			!(next_odm_mpo_pipe && next_odm_mpo_pipe->pipe_idx == preferred_pipe_idx)) {
2532 			secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
2533 			secondary_pipe->pipe_idx = preferred_pipe_idx;
2534 		}
2535 	}
2536 
2537 	/*
2538 	 * search backwards for the second pipe to keep pipe
2539 	 * assignment more consistent
2540 	 */
2541 	if (!secondary_pipe)
2542 		for (i = pool->pipe_count - 1; i >= 0; i--) {
2543 			if ((res_ctx->pipe_ctx[i].stream == NULL) &&
2544 				!(next_odm_mpo_pipe && next_odm_mpo_pipe->pipe_idx == i)) {
2545 				secondary_pipe = &res_ctx->pipe_ctx[i];
2546 				secondary_pipe->pipe_idx = i;
2547 				break;
2548 			}
2549 		}
2550 
2551 	return secondary_pipe;
2552 }
2553 
2554 struct pipe_ctx *dcn32_acquire_idle_pipe_for_head_pipe_in_layer(
2555 		struct dc_state *state,
2556 		const struct resource_pool *pool,
2557 		struct dc_stream_state *stream,
2558 		struct pipe_ctx *head_pipe)
2559 {
2560 	struct resource_context *res_ctx = &state->res_ctx;
2561 	struct pipe_ctx *idle_pipe, *pipe;
2562 	struct resource_context *old_ctx = &stream->ctx->dc->current_state->res_ctx;
2563 	int head_index;
2564 
2565 	if (!head_pipe)
2566 		ASSERT(0);
2567 
2568 	/*
2569 	 * Modified from dcn20_acquire_idle_pipe_for_layer
2570 	 * Check if head_pipe in old_context already has bottom_pipe allocated.
2571 	 * - If so, check if that pipe is available in the current context.
2572 	 * --  If so, reuse pipe from old_context
2573 	 */
2574 	head_index = head_pipe->pipe_idx;
2575 	pipe = &old_ctx->pipe_ctx[head_index];
2576 	if (pipe->bottom_pipe && res_ctx->pipe_ctx[pipe->bottom_pipe->pipe_idx].stream == NULL) {
2577 		idle_pipe = &res_ctx->pipe_ctx[pipe->bottom_pipe->pipe_idx];
2578 		idle_pipe->pipe_idx = pipe->bottom_pipe->pipe_idx;
2579 	} else {
2580 		idle_pipe = find_idle_secondary_pipe_check_mpo(res_ctx, pool, head_pipe);
2581 		if (!idle_pipe)
2582 			return NULL;
2583 	}
2584 
2585 	idle_pipe->stream = head_pipe->stream;
2586 	idle_pipe->stream_res.tg = head_pipe->stream_res.tg;
2587 	idle_pipe->stream_res.opp = head_pipe->stream_res.opp;
2588 
2589 	idle_pipe->plane_res.hubp = pool->hubps[idle_pipe->pipe_idx];
2590 	idle_pipe->plane_res.ipp = pool->ipps[idle_pipe->pipe_idx];
2591 	idle_pipe->plane_res.dpp = pool->dpps[idle_pipe->pipe_idx];
2592 	idle_pipe->plane_res.mpcc_inst = pool->dpps[idle_pipe->pipe_idx]->inst;
2593 
2594 	return idle_pipe;
2595 }
2596