xref: /openbmc/linux/drivers/gpu/drm/amd/display/dc/dcn20/dcn20_resource.c (revision cd487b6d506329917bdd2a594b307aa469a53872)
1 /*
2 * Copyright 2016 Advanced Micro Devices, Inc.
3  * Copyright 2019 Raptor Engineering, LLC
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 <linux/slab.h>
28 
29 #include "dm_services.h"
30 #include "dc.h"
31 
32 #include "dcn20_init.h"
33 
34 #include "resource.h"
35 #include "include/irq_service_interface.h"
36 #include "dcn20/dcn20_resource.h"
37 
38 #include "dml/dcn20/dcn20_fpu.h"
39 
40 #include "dcn10/dcn10_hubp.h"
41 #include "dcn10/dcn10_ipp.h"
42 #include "dcn20_hubbub.h"
43 #include "dcn20_mpc.h"
44 #include "dcn20_hubp.h"
45 #include "irq/dcn20/irq_service_dcn20.h"
46 #include "dcn20_dpp.h"
47 #include "dcn20_optc.h"
48 #include "dcn20_hwseq.h"
49 #include "dce110/dce110_hw_sequencer.h"
50 #include "dcn10/dcn10_resource.h"
51 #include "dcn20_opp.h"
52 
53 #include "dcn20_dsc.h"
54 
55 #include "dcn20_link_encoder.h"
56 #include "dcn20_stream_encoder.h"
57 #include "dce/dce_clock_source.h"
58 #include "dce/dce_audio.h"
59 #include "dce/dce_hwseq.h"
60 #include "virtual/virtual_stream_encoder.h"
61 #include "dce110/dce110_resource.h"
62 #include "dml/display_mode_vba.h"
63 #include "dcn20_dccg.h"
64 #include "dcn20_vmid.h"
65 #include "dce/dce_panel_cntl.h"
66 
67 #include "navi10_ip_offset.h"
68 
69 #include "dcn/dcn_2_0_0_offset.h"
70 #include "dcn/dcn_2_0_0_sh_mask.h"
71 #include "dpcs/dpcs_2_0_0_offset.h"
72 #include "dpcs/dpcs_2_0_0_sh_mask.h"
73 
74 #include "nbio/nbio_2_3_offset.h"
75 
76 #include "dcn20/dcn20_dwb.h"
77 #include "dcn20/dcn20_mmhubbub.h"
78 
79 #include "mmhub/mmhub_2_0_0_offset.h"
80 #include "mmhub/mmhub_2_0_0_sh_mask.h"
81 
82 #include "reg_helper.h"
83 #include "dce/dce_abm.h"
84 #include "dce/dce_dmcu.h"
85 #include "dce/dce_aux.h"
86 #include "dce/dce_i2c.h"
87 #include "vm_helper.h"
88 #include "link_enc_cfg.h"
89 
90 #include "amdgpu_socbb.h"
91 
92 #include "link.h"
93 #define DC_LOGGER_INIT(logger)
94 
95 #ifndef mmDP0_DP_DPHY_INTERNAL_CTRL
96 	#define mmDP0_DP_DPHY_INTERNAL_CTRL		0x210f
97 	#define mmDP0_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
98 	#define mmDP1_DP_DPHY_INTERNAL_CTRL		0x220f
99 	#define mmDP1_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
100 	#define mmDP2_DP_DPHY_INTERNAL_CTRL		0x230f
101 	#define mmDP2_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
102 	#define mmDP3_DP_DPHY_INTERNAL_CTRL		0x240f
103 	#define mmDP3_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
104 	#define mmDP4_DP_DPHY_INTERNAL_CTRL		0x250f
105 	#define mmDP4_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
106 	#define mmDP5_DP_DPHY_INTERNAL_CTRL		0x260f
107 	#define mmDP5_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
108 	#define mmDP6_DP_DPHY_INTERNAL_CTRL		0x270f
109 	#define mmDP6_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
110 #endif
111 
112 
113 enum dcn20_clk_src_array_id {
114 	DCN20_CLK_SRC_PLL0,
115 	DCN20_CLK_SRC_PLL1,
116 	DCN20_CLK_SRC_PLL2,
117 	DCN20_CLK_SRC_PLL3,
118 	DCN20_CLK_SRC_PLL4,
119 	DCN20_CLK_SRC_PLL5,
120 	DCN20_CLK_SRC_TOTAL
121 };
122 
123 /* begin *********************
124  * macros to expend register list macro defined in HW object header file */
125 
126 /* DCN */
127 #define BASE_INNER(seg) DCN_BASE__INST0_SEG ## seg
128 
129 #define BASE(seg) BASE_INNER(seg)
130 
131 #define SR(reg_name)\
132 		.reg_name = BASE(mm ## reg_name ## _BASE_IDX) +  \
133 					mm ## reg_name
134 
135 #define SRI(reg_name, block, id)\
136 	.reg_name = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
137 					mm ## block ## id ## _ ## reg_name
138 
139 #define SRI2_DWB(reg_name, block, id)\
140 	.reg_name = BASE(mm ## reg_name ## _BASE_IDX) + \
141 					mm ## reg_name
142 #define SF_DWB(reg_name, field_name, post_fix)\
143 	.field_name = reg_name ## __ ## field_name ## post_fix
144 
145 #define SF_DWB2(reg_name, block, id, field_name, post_fix)	\
146 	.field_name = reg_name ## __ ## field_name ## post_fix
147 
148 #define SRIR(var_name, reg_name, block, id)\
149 	.var_name = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
150 					mm ## block ## id ## _ ## reg_name
151 
152 #define SRII(reg_name, block, id)\
153 	.reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
154 					mm ## block ## id ## _ ## reg_name
155 
156 #define DCCG_SRII(reg_name, block, id)\
157 	.block ## _ ## reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
158 					mm ## block ## id ## _ ## reg_name
159 
160 #define VUPDATE_SRII(reg_name, block, id)\
161 	.reg_name[id] = BASE(mm ## reg_name ## _ ## block ## id ## _BASE_IDX) + \
162 					mm ## reg_name ## _ ## block ## id
163 
164 /* NBIO */
165 #define NBIO_BASE_INNER(seg) \
166 	NBIO_BASE__INST0_SEG ## seg
167 
168 #define NBIO_BASE(seg) \
169 	NBIO_BASE_INNER(seg)
170 
171 #define NBIO_SR(reg_name)\
172 		.reg_name = NBIO_BASE(mm ## reg_name ## _BASE_IDX) + \
173 					mm ## reg_name
174 
175 /* MMHUB */
176 #define MMHUB_BASE_INNER(seg) \
177 	MMHUB_BASE__INST0_SEG ## seg
178 
179 #define MMHUB_BASE(seg) \
180 	MMHUB_BASE_INNER(seg)
181 
182 #define MMHUB_SR(reg_name)\
183 		.reg_name = MMHUB_BASE(mmMM ## reg_name ## _BASE_IDX) + \
184 					mmMM ## reg_name
185 
186 static const struct bios_registers bios_regs = {
187 		NBIO_SR(BIOS_SCRATCH_3),
188 		NBIO_SR(BIOS_SCRATCH_6)
189 };
190 
191 #define clk_src_regs(index, pllid)\
192 [index] = {\
193 	CS_COMMON_REG_LIST_DCN2_0(index, pllid),\
194 }
195 
196 static const struct dce110_clk_src_regs clk_src_regs[] = {
197 	clk_src_regs(0, A),
198 	clk_src_regs(1, B),
199 	clk_src_regs(2, C),
200 	clk_src_regs(3, D),
201 	clk_src_regs(4, E),
202 	clk_src_regs(5, F)
203 };
204 
205 static const struct dce110_clk_src_shift cs_shift = {
206 		CS_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
207 };
208 
209 static const struct dce110_clk_src_mask cs_mask = {
210 		CS_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
211 };
212 
213 static const struct dce_dmcu_registers dmcu_regs = {
214 		DMCU_DCN10_REG_LIST()
215 };
216 
217 static const struct dce_dmcu_shift dmcu_shift = {
218 		DMCU_MASK_SH_LIST_DCN10(__SHIFT)
219 };
220 
221 static const struct dce_dmcu_mask dmcu_mask = {
222 		DMCU_MASK_SH_LIST_DCN10(_MASK)
223 };
224 
225 static const struct dce_abm_registers abm_regs = {
226 		ABM_DCN20_REG_LIST()
227 };
228 
229 static const struct dce_abm_shift abm_shift = {
230 		ABM_MASK_SH_LIST_DCN20(__SHIFT)
231 };
232 
233 static const struct dce_abm_mask abm_mask = {
234 		ABM_MASK_SH_LIST_DCN20(_MASK)
235 };
236 
237 #define audio_regs(id)\
238 [id] = {\
239 		AUD_COMMON_REG_LIST(id)\
240 }
241 
242 static const struct dce_audio_registers audio_regs[] = {
243 	audio_regs(0),
244 	audio_regs(1),
245 	audio_regs(2),
246 	audio_regs(3),
247 	audio_regs(4),
248 	audio_regs(5),
249 	audio_regs(6),
250 };
251 
252 #define DCE120_AUD_COMMON_MASK_SH_LIST(mask_sh)\
253 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_INDEX, AZALIA_ENDPOINT_REG_INDEX, mask_sh),\
254 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_DATA, AZALIA_ENDPOINT_REG_DATA, mask_sh),\
255 		AUD_COMMON_MASK_SH_LIST_BASE(mask_sh)
256 
257 static const struct dce_audio_shift audio_shift = {
258 		DCE120_AUD_COMMON_MASK_SH_LIST(__SHIFT)
259 };
260 
261 static const struct dce_audio_mask audio_mask = {
262 		DCE120_AUD_COMMON_MASK_SH_LIST(_MASK)
263 };
264 
265 #define stream_enc_regs(id)\
266 [id] = {\
267 	SE_DCN2_REG_LIST(id)\
268 }
269 
270 static const struct dcn10_stream_enc_registers stream_enc_regs[] = {
271 	stream_enc_regs(0),
272 	stream_enc_regs(1),
273 	stream_enc_regs(2),
274 	stream_enc_regs(3),
275 	stream_enc_regs(4),
276 	stream_enc_regs(5),
277 };
278 
279 static const struct dcn10_stream_encoder_shift se_shift = {
280 		SE_COMMON_MASK_SH_LIST_DCN20(__SHIFT)
281 };
282 
283 static const struct dcn10_stream_encoder_mask se_mask = {
284 		SE_COMMON_MASK_SH_LIST_DCN20(_MASK)
285 };
286 
287 
288 #define aux_regs(id)\
289 [id] = {\
290 	DCN2_AUX_REG_LIST(id)\
291 }
292 
293 static const struct dcn10_link_enc_aux_registers link_enc_aux_regs[] = {
294 		aux_regs(0),
295 		aux_regs(1),
296 		aux_regs(2),
297 		aux_regs(3),
298 		aux_regs(4),
299 		aux_regs(5)
300 };
301 
302 #define hpd_regs(id)\
303 [id] = {\
304 	HPD_REG_LIST(id)\
305 }
306 
307 static const struct dcn10_link_enc_hpd_registers link_enc_hpd_regs[] = {
308 		hpd_regs(0),
309 		hpd_regs(1),
310 		hpd_regs(2),
311 		hpd_regs(3),
312 		hpd_regs(4),
313 		hpd_regs(5)
314 };
315 
316 #define link_regs(id, phyid)\
317 [id] = {\
318 	LE_DCN10_REG_LIST(id), \
319 	UNIPHY_DCN2_REG_LIST(phyid), \
320 	DPCS_DCN2_REG_LIST(id), \
321 	SRI(DP_DPHY_INTERNAL_CTRL, DP, id) \
322 }
323 
324 static const struct dcn10_link_enc_registers link_enc_regs[] = {
325 	link_regs(0, A),
326 	link_regs(1, B),
327 	link_regs(2, C),
328 	link_regs(3, D),
329 	link_regs(4, E),
330 	link_regs(5, F)
331 };
332 
333 static const struct dcn10_link_enc_shift le_shift = {
334 	LINK_ENCODER_MASK_SH_LIST_DCN20(__SHIFT),\
335 	DPCS_DCN2_MASK_SH_LIST(__SHIFT)
336 };
337 
338 static const struct dcn10_link_enc_mask le_mask = {
339 	LINK_ENCODER_MASK_SH_LIST_DCN20(_MASK),\
340 	DPCS_DCN2_MASK_SH_LIST(_MASK)
341 };
342 
343 static const struct dce_panel_cntl_registers panel_cntl_regs[] = {
344 	{ DCN_PANEL_CNTL_REG_LIST() }
345 };
346 
347 static const struct dce_panel_cntl_shift panel_cntl_shift = {
348 	DCE_PANEL_CNTL_MASK_SH_LIST(__SHIFT)
349 };
350 
351 static const struct dce_panel_cntl_mask panel_cntl_mask = {
352 	DCE_PANEL_CNTL_MASK_SH_LIST(_MASK)
353 };
354 
355 #define ipp_regs(id)\
356 [id] = {\
357 	IPP_REG_LIST_DCN20(id),\
358 }
359 
360 static const struct dcn10_ipp_registers ipp_regs[] = {
361 	ipp_regs(0),
362 	ipp_regs(1),
363 	ipp_regs(2),
364 	ipp_regs(3),
365 	ipp_regs(4),
366 	ipp_regs(5),
367 };
368 
369 static const struct dcn10_ipp_shift ipp_shift = {
370 		IPP_MASK_SH_LIST_DCN20(__SHIFT)
371 };
372 
373 static const struct dcn10_ipp_mask ipp_mask = {
374 		IPP_MASK_SH_LIST_DCN20(_MASK),
375 };
376 
377 #define opp_regs(id)\
378 [id] = {\
379 	OPP_REG_LIST_DCN20(id),\
380 }
381 
382 static const struct dcn20_opp_registers opp_regs[] = {
383 	opp_regs(0),
384 	opp_regs(1),
385 	opp_regs(2),
386 	opp_regs(3),
387 	opp_regs(4),
388 	opp_regs(5),
389 };
390 
391 static const struct dcn20_opp_shift opp_shift = {
392 		OPP_MASK_SH_LIST_DCN20(__SHIFT)
393 };
394 
395 static const struct dcn20_opp_mask opp_mask = {
396 		OPP_MASK_SH_LIST_DCN20(_MASK)
397 };
398 
399 #define aux_engine_regs(id)\
400 [id] = {\
401 	AUX_COMMON_REG_LIST0(id), \
402 	.AUXN_IMPCAL = 0, \
403 	.AUXP_IMPCAL = 0, \
404 	.AUX_RESET_MASK = DP_AUX0_AUX_CONTROL__AUX_RESET_MASK, \
405 }
406 
407 static const struct dce110_aux_registers aux_engine_regs[] = {
408 		aux_engine_regs(0),
409 		aux_engine_regs(1),
410 		aux_engine_regs(2),
411 		aux_engine_regs(3),
412 		aux_engine_regs(4),
413 		aux_engine_regs(5)
414 };
415 
416 #define tf_regs(id)\
417 [id] = {\
418 	TF_REG_LIST_DCN20(id),\
419 	TF_REG_LIST_DCN20_COMMON_APPEND(id),\
420 }
421 
422 static const struct dcn2_dpp_registers tf_regs[] = {
423 	tf_regs(0),
424 	tf_regs(1),
425 	tf_regs(2),
426 	tf_regs(3),
427 	tf_regs(4),
428 	tf_regs(5),
429 };
430 
431 static const struct dcn2_dpp_shift tf_shift = {
432 		TF_REG_LIST_SH_MASK_DCN20(__SHIFT),
433 		TF_DEBUG_REG_LIST_SH_DCN20
434 };
435 
436 static const struct dcn2_dpp_mask tf_mask = {
437 		TF_REG_LIST_SH_MASK_DCN20(_MASK),
438 		TF_DEBUG_REG_LIST_MASK_DCN20
439 };
440 
441 #define dwbc_regs_dcn2(id)\
442 [id] = {\
443 	DWBC_COMMON_REG_LIST_DCN2_0(id),\
444 		}
445 
446 static const struct dcn20_dwbc_registers dwbc20_regs[] = {
447 	dwbc_regs_dcn2(0),
448 };
449 
450 static const struct dcn20_dwbc_shift dwbc20_shift = {
451 	DWBC_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
452 };
453 
454 static const struct dcn20_dwbc_mask dwbc20_mask = {
455 	DWBC_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
456 };
457 
458 #define mcif_wb_regs_dcn2(id)\
459 [id] = {\
460 	MCIF_WB_COMMON_REG_LIST_DCN2_0(id),\
461 		}
462 
463 static const struct dcn20_mmhubbub_registers mcif_wb20_regs[] = {
464 	mcif_wb_regs_dcn2(0),
465 };
466 
467 static const struct dcn20_mmhubbub_shift mcif_wb20_shift = {
468 	MCIF_WB_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
469 };
470 
471 static const struct dcn20_mmhubbub_mask mcif_wb20_mask = {
472 	MCIF_WB_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
473 };
474 
475 static const struct dcn20_mpc_registers mpc_regs = {
476 		MPC_REG_LIST_DCN2_0(0),
477 		MPC_REG_LIST_DCN2_0(1),
478 		MPC_REG_LIST_DCN2_0(2),
479 		MPC_REG_LIST_DCN2_0(3),
480 		MPC_REG_LIST_DCN2_0(4),
481 		MPC_REG_LIST_DCN2_0(5),
482 		MPC_OUT_MUX_REG_LIST_DCN2_0(0),
483 		MPC_OUT_MUX_REG_LIST_DCN2_0(1),
484 		MPC_OUT_MUX_REG_LIST_DCN2_0(2),
485 		MPC_OUT_MUX_REG_LIST_DCN2_0(3),
486 		MPC_OUT_MUX_REG_LIST_DCN2_0(4),
487 		MPC_OUT_MUX_REG_LIST_DCN2_0(5),
488 		MPC_DBG_REG_LIST_DCN2_0()
489 };
490 
491 static const struct dcn20_mpc_shift mpc_shift = {
492 	MPC_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT),
493 	MPC_DEBUG_REG_LIST_SH_DCN20
494 };
495 
496 static const struct dcn20_mpc_mask mpc_mask = {
497 	MPC_COMMON_MASK_SH_LIST_DCN2_0(_MASK),
498 	MPC_DEBUG_REG_LIST_MASK_DCN20
499 };
500 
501 #define tg_regs(id)\
502 [id] = {TG_COMMON_REG_LIST_DCN2_0(id)}
503 
504 
505 static const struct dcn_optc_registers tg_regs[] = {
506 	tg_regs(0),
507 	tg_regs(1),
508 	tg_regs(2),
509 	tg_regs(3),
510 	tg_regs(4),
511 	tg_regs(5)
512 };
513 
514 static const struct dcn_optc_shift tg_shift = {
515 	TG_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
516 };
517 
518 static const struct dcn_optc_mask tg_mask = {
519 	TG_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
520 };
521 
522 #define hubp_regs(id)\
523 [id] = {\
524 	HUBP_REG_LIST_DCN20(id)\
525 }
526 
527 static const struct dcn_hubp2_registers hubp_regs[] = {
528 		hubp_regs(0),
529 		hubp_regs(1),
530 		hubp_regs(2),
531 		hubp_regs(3),
532 		hubp_regs(4),
533 		hubp_regs(5)
534 };
535 
536 static const struct dcn_hubp2_shift hubp_shift = {
537 		HUBP_MASK_SH_LIST_DCN20(__SHIFT)
538 };
539 
540 static const struct dcn_hubp2_mask hubp_mask = {
541 		HUBP_MASK_SH_LIST_DCN20(_MASK)
542 };
543 
544 static const struct dcn_hubbub_registers hubbub_reg = {
545 		HUBBUB_REG_LIST_DCN20(0)
546 };
547 
548 static const struct dcn_hubbub_shift hubbub_shift = {
549 		HUBBUB_MASK_SH_LIST_DCN20(__SHIFT)
550 };
551 
552 static const struct dcn_hubbub_mask hubbub_mask = {
553 		HUBBUB_MASK_SH_LIST_DCN20(_MASK)
554 };
555 
556 #define vmid_regs(id)\
557 [id] = {\
558 		DCN20_VMID_REG_LIST(id)\
559 }
560 
561 static const struct dcn_vmid_registers vmid_regs[] = {
562 	vmid_regs(0),
563 	vmid_regs(1),
564 	vmid_regs(2),
565 	vmid_regs(3),
566 	vmid_regs(4),
567 	vmid_regs(5),
568 	vmid_regs(6),
569 	vmid_regs(7),
570 	vmid_regs(8),
571 	vmid_regs(9),
572 	vmid_regs(10),
573 	vmid_regs(11),
574 	vmid_regs(12),
575 	vmid_regs(13),
576 	vmid_regs(14),
577 	vmid_regs(15)
578 };
579 
580 static const struct dcn20_vmid_shift vmid_shifts = {
581 		DCN20_VMID_MASK_SH_LIST(__SHIFT)
582 };
583 
584 static const struct dcn20_vmid_mask vmid_masks = {
585 		DCN20_VMID_MASK_SH_LIST(_MASK)
586 };
587 
588 static const struct dce110_aux_registers_shift aux_shift = {
589 		DCN_AUX_MASK_SH_LIST(__SHIFT)
590 };
591 
592 static const struct dce110_aux_registers_mask aux_mask = {
593 		DCN_AUX_MASK_SH_LIST(_MASK)
594 };
595 
596 static int map_transmitter_id_to_phy_instance(
597 	enum transmitter transmitter)
598 {
599 	switch (transmitter) {
600 	case TRANSMITTER_UNIPHY_A:
601 		return 0;
602 	break;
603 	case TRANSMITTER_UNIPHY_B:
604 		return 1;
605 	break;
606 	case TRANSMITTER_UNIPHY_C:
607 		return 2;
608 	break;
609 	case TRANSMITTER_UNIPHY_D:
610 		return 3;
611 	break;
612 	case TRANSMITTER_UNIPHY_E:
613 		return 4;
614 	break;
615 	case TRANSMITTER_UNIPHY_F:
616 		return 5;
617 	break;
618 	default:
619 		ASSERT(0);
620 		return 0;
621 	}
622 }
623 
624 #define dsc_regsDCN20(id)\
625 [id] = {\
626 	DSC_REG_LIST_DCN20(id)\
627 }
628 
629 static const struct dcn20_dsc_registers dsc_regs[] = {
630 	dsc_regsDCN20(0),
631 	dsc_regsDCN20(1),
632 	dsc_regsDCN20(2),
633 	dsc_regsDCN20(3),
634 	dsc_regsDCN20(4),
635 	dsc_regsDCN20(5)
636 };
637 
638 static const struct dcn20_dsc_shift dsc_shift = {
639 	DSC_REG_LIST_SH_MASK_DCN20(__SHIFT)
640 };
641 
642 static const struct dcn20_dsc_mask dsc_mask = {
643 	DSC_REG_LIST_SH_MASK_DCN20(_MASK)
644 };
645 
646 static const struct dccg_registers dccg_regs = {
647 		DCCG_REG_LIST_DCN2()
648 };
649 
650 static const struct dccg_shift dccg_shift = {
651 		DCCG_MASK_SH_LIST_DCN2(__SHIFT)
652 };
653 
654 static const struct dccg_mask dccg_mask = {
655 		DCCG_MASK_SH_LIST_DCN2(_MASK)
656 };
657 
658 static const struct resource_caps res_cap_nv10 = {
659 		.num_timing_generator = 6,
660 		.num_opp = 6,
661 		.num_video_plane = 6,
662 		.num_audio = 7,
663 		.num_stream_encoder = 6,
664 		.num_pll = 6,
665 		.num_dwb = 1,
666 		.num_ddc = 6,
667 		.num_vmid = 16,
668 		.num_dsc = 6,
669 };
670 
671 static const struct dc_plane_cap plane_cap = {
672 	.type = DC_PLANE_TYPE_DCN_UNIVERSAL,
673 	.per_pixel_alpha = true,
674 
675 	.pixel_format_support = {
676 			.argb8888 = true,
677 			.nv12 = true,
678 			.fp16 = true,
679 			.p010 = true
680 	},
681 
682 	.max_upscale_factor = {
683 			.argb8888 = 16000,
684 			.nv12 = 16000,
685 			.fp16 = 1
686 	},
687 
688 	.max_downscale_factor = {
689 			.argb8888 = 250,
690 			.nv12 = 250,
691 			.fp16 = 1
692 	},
693 	16,
694 	16
695 };
696 static const struct resource_caps res_cap_nv14 = {
697 		.num_timing_generator = 5,
698 		.num_opp = 5,
699 		.num_video_plane = 5,
700 		.num_audio = 6,
701 		.num_stream_encoder = 5,
702 		.num_pll = 5,
703 		.num_dwb = 1,
704 		.num_ddc = 5,
705 		.num_vmid = 16,
706 		.num_dsc = 5,
707 };
708 
709 static const struct dc_debug_options debug_defaults_drv = {
710 		.disable_dmcu = false,
711 		.force_abm_enable = false,
712 		.timing_trace = false,
713 		.clock_trace = true,
714 		.disable_pplib_clock_request = true,
715 		.pipe_split_policy = MPC_SPLIT_AVOID_MULT_DISP,
716 		.force_single_disp_pipe_split = false,
717 		.disable_dcc = DCC_ENABLE,
718 		.vsr_support = true,
719 		.performance_trace = false,
720 		.max_downscale_src_width = 5120,/*upto 5K*/
721 		.disable_pplib_wm_range = false,
722 		.scl_reset_length10 = true,
723 		.sanity_checks = false,
724 		.underflow_assert_delay_us = 0xFFFFFFFF,
725 };
726 
727 static const struct dc_debug_options debug_defaults_diags = {
728 		.disable_dmcu = false,
729 		.force_abm_enable = false,
730 		.timing_trace = true,
731 		.clock_trace = true,
732 		.disable_dpp_power_gate = true,
733 		.disable_hubp_power_gate = true,
734 		.disable_clock_gate = true,
735 		.disable_pplib_clock_request = true,
736 		.disable_pplib_wm_range = true,
737 		.disable_stutter = true,
738 		.scl_reset_length10 = true,
739 		.underflow_assert_delay_us = 0xFFFFFFFF,
740 		.enable_tri_buf = true,
741 };
742 
743 void dcn20_dpp_destroy(struct dpp **dpp)
744 {
745 	kfree(TO_DCN20_DPP(*dpp));
746 	*dpp = NULL;
747 }
748 
749 struct dpp *dcn20_dpp_create(
750 	struct dc_context *ctx,
751 	uint32_t inst)
752 {
753 	struct dcn20_dpp *dpp =
754 		kzalloc(sizeof(struct dcn20_dpp), GFP_ATOMIC);
755 
756 	if (!dpp)
757 		return NULL;
758 
759 	if (dpp2_construct(dpp, ctx, inst,
760 			&tf_regs[inst], &tf_shift, &tf_mask))
761 		return &dpp->base;
762 
763 	BREAK_TO_DEBUGGER();
764 	kfree(dpp);
765 	return NULL;
766 }
767 
768 struct input_pixel_processor *dcn20_ipp_create(
769 	struct dc_context *ctx, uint32_t inst)
770 {
771 	struct dcn10_ipp *ipp =
772 		kzalloc(sizeof(struct dcn10_ipp), GFP_ATOMIC);
773 
774 	if (!ipp) {
775 		BREAK_TO_DEBUGGER();
776 		return NULL;
777 	}
778 
779 	dcn20_ipp_construct(ipp, ctx, inst,
780 			&ipp_regs[inst], &ipp_shift, &ipp_mask);
781 	return &ipp->base;
782 }
783 
784 
785 struct output_pixel_processor *dcn20_opp_create(
786 	struct dc_context *ctx, uint32_t inst)
787 {
788 	struct dcn20_opp *opp =
789 		kzalloc(sizeof(struct dcn20_opp), GFP_ATOMIC);
790 
791 	if (!opp) {
792 		BREAK_TO_DEBUGGER();
793 		return NULL;
794 	}
795 
796 	dcn20_opp_construct(opp, ctx, inst,
797 			&opp_regs[inst], &opp_shift, &opp_mask);
798 	return &opp->base;
799 }
800 
801 struct dce_aux *dcn20_aux_engine_create(
802 	struct dc_context *ctx,
803 	uint32_t inst)
804 {
805 	struct aux_engine_dce110 *aux_engine =
806 		kzalloc(sizeof(struct aux_engine_dce110), GFP_ATOMIC);
807 
808 	if (!aux_engine)
809 		return NULL;
810 
811 	dce110_aux_engine_construct(aux_engine, ctx, inst,
812 				    SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD,
813 				    &aux_engine_regs[inst],
814 					&aux_mask,
815 					&aux_shift,
816 					ctx->dc->caps.extended_aux_timeout_support);
817 
818 	return &aux_engine->base;
819 }
820 #define i2c_inst_regs(id) { I2C_HW_ENGINE_COMMON_REG_LIST(id) }
821 
822 static const struct dce_i2c_registers i2c_hw_regs[] = {
823 		i2c_inst_regs(1),
824 		i2c_inst_regs(2),
825 		i2c_inst_regs(3),
826 		i2c_inst_regs(4),
827 		i2c_inst_regs(5),
828 		i2c_inst_regs(6),
829 };
830 
831 static const struct dce_i2c_shift i2c_shifts = {
832 		I2C_COMMON_MASK_SH_LIST_DCN2(__SHIFT)
833 };
834 
835 static const struct dce_i2c_mask i2c_masks = {
836 		I2C_COMMON_MASK_SH_LIST_DCN2(_MASK)
837 };
838 
839 struct dce_i2c_hw *dcn20_i2c_hw_create(
840 	struct dc_context *ctx,
841 	uint32_t inst)
842 {
843 	struct dce_i2c_hw *dce_i2c_hw =
844 		kzalloc(sizeof(struct dce_i2c_hw), GFP_ATOMIC);
845 
846 	if (!dce_i2c_hw)
847 		return NULL;
848 
849 	dcn2_i2c_hw_construct(dce_i2c_hw, ctx, inst,
850 				    &i2c_hw_regs[inst], &i2c_shifts, &i2c_masks);
851 
852 	return dce_i2c_hw;
853 }
854 struct mpc *dcn20_mpc_create(struct dc_context *ctx)
855 {
856 	struct dcn20_mpc *mpc20 = kzalloc(sizeof(struct dcn20_mpc),
857 					  GFP_ATOMIC);
858 
859 	if (!mpc20)
860 		return NULL;
861 
862 	dcn20_mpc_construct(mpc20, ctx,
863 			&mpc_regs,
864 			&mpc_shift,
865 			&mpc_mask,
866 			6);
867 
868 	return &mpc20->base;
869 }
870 
871 struct hubbub *dcn20_hubbub_create(struct dc_context *ctx)
872 {
873 	int i;
874 	struct dcn20_hubbub *hubbub = kzalloc(sizeof(struct dcn20_hubbub),
875 					  GFP_ATOMIC);
876 
877 	if (!hubbub)
878 		return NULL;
879 
880 	hubbub2_construct(hubbub, ctx,
881 			&hubbub_reg,
882 			&hubbub_shift,
883 			&hubbub_mask);
884 
885 	for (i = 0; i < res_cap_nv10.num_vmid; i++) {
886 		struct dcn20_vmid *vmid = &hubbub->vmid[i];
887 
888 		vmid->ctx = ctx;
889 
890 		vmid->regs = &vmid_regs[i];
891 		vmid->shifts = &vmid_shifts;
892 		vmid->masks = &vmid_masks;
893 	}
894 
895 	return &hubbub->base;
896 }
897 
898 struct timing_generator *dcn20_timing_generator_create(
899 		struct dc_context *ctx,
900 		uint32_t instance)
901 {
902 	struct optc *tgn10 =
903 		kzalloc(sizeof(struct optc), GFP_ATOMIC);
904 
905 	if (!tgn10)
906 		return NULL;
907 
908 	tgn10->base.inst = instance;
909 	tgn10->base.ctx = ctx;
910 
911 	tgn10->tg_regs = &tg_regs[instance];
912 	tgn10->tg_shift = &tg_shift;
913 	tgn10->tg_mask = &tg_mask;
914 
915 	dcn20_timing_generator_init(tgn10);
916 
917 	return &tgn10->base;
918 }
919 
920 static const struct encoder_feature_support link_enc_feature = {
921 		.max_hdmi_deep_color = COLOR_DEPTH_121212,
922 		.max_hdmi_pixel_clock = 600000,
923 		.hdmi_ycbcr420_supported = true,
924 		.dp_ycbcr420_supported = true,
925 		.fec_supported = true,
926 		.flags.bits.IS_HBR2_CAPABLE = true,
927 		.flags.bits.IS_HBR3_CAPABLE = true,
928 		.flags.bits.IS_TPS3_CAPABLE = true,
929 		.flags.bits.IS_TPS4_CAPABLE = true
930 };
931 
932 struct link_encoder *dcn20_link_encoder_create(
933 	struct dc_context *ctx,
934 	const struct encoder_init_data *enc_init_data)
935 {
936 	struct dcn20_link_encoder *enc20 =
937 		kzalloc(sizeof(struct dcn20_link_encoder), GFP_KERNEL);
938 	int link_regs_id;
939 
940 	if (!enc20)
941 		return NULL;
942 
943 	link_regs_id =
944 		map_transmitter_id_to_phy_instance(enc_init_data->transmitter);
945 
946 	dcn20_link_encoder_construct(enc20,
947 				      enc_init_data,
948 				      &link_enc_feature,
949 				      &link_enc_regs[link_regs_id],
950 				      &link_enc_aux_regs[enc_init_data->channel - 1],
951 				      &link_enc_hpd_regs[enc_init_data->hpd_source],
952 				      &le_shift,
953 				      &le_mask);
954 
955 	return &enc20->enc10.base;
956 }
957 
958 static struct panel_cntl *dcn20_panel_cntl_create(const struct panel_cntl_init_data *init_data)
959 {
960 	struct dce_panel_cntl *panel_cntl =
961 		kzalloc(sizeof(struct dce_panel_cntl), GFP_KERNEL);
962 
963 	if (!panel_cntl)
964 		return NULL;
965 
966 	dce_panel_cntl_construct(panel_cntl,
967 			init_data,
968 			&panel_cntl_regs[init_data->inst],
969 			&panel_cntl_shift,
970 			&panel_cntl_mask);
971 
972 	return &panel_cntl->base;
973 }
974 
975 static struct clock_source *dcn20_clock_source_create(
976 	struct dc_context *ctx,
977 	struct dc_bios *bios,
978 	enum clock_source_id id,
979 	const struct dce110_clk_src_regs *regs,
980 	bool dp_clk_src)
981 {
982 	struct dce110_clk_src *clk_src =
983 		kzalloc(sizeof(struct dce110_clk_src), GFP_ATOMIC);
984 
985 	if (!clk_src)
986 		return NULL;
987 
988 	if (dcn20_clk_src_construct(clk_src, ctx, bios, id,
989 			regs, &cs_shift, &cs_mask)) {
990 		clk_src->base.dp_clk_src = dp_clk_src;
991 		return &clk_src->base;
992 	}
993 
994 	kfree(clk_src);
995 	BREAK_TO_DEBUGGER();
996 	return NULL;
997 }
998 
999 static void read_dce_straps(
1000 	struct dc_context *ctx,
1001 	struct resource_straps *straps)
1002 {
1003 	generic_reg_get(ctx, mmDC_PINSTRAPS + BASE(mmDC_PINSTRAPS_BASE_IDX),
1004 		FN(DC_PINSTRAPS, DC_PINSTRAPS_AUDIO), &straps->dc_pinstraps_audio);
1005 }
1006 
1007 static struct audio *dcn20_create_audio(
1008 		struct dc_context *ctx, unsigned int inst)
1009 {
1010 	return dce_audio_create(ctx, inst,
1011 			&audio_regs[inst], &audio_shift, &audio_mask);
1012 }
1013 
1014 struct stream_encoder *dcn20_stream_encoder_create(
1015 	enum engine_id eng_id,
1016 	struct dc_context *ctx)
1017 {
1018 	struct dcn10_stream_encoder *enc1 =
1019 		kzalloc(sizeof(struct dcn10_stream_encoder), GFP_KERNEL);
1020 
1021 	if (!enc1)
1022 		return NULL;
1023 
1024 	if (ASICREV_IS_NAVI14_M(ctx->asic_id.hw_internal_rev)) {
1025 		if (eng_id >= ENGINE_ID_DIGD)
1026 			eng_id++;
1027 	}
1028 
1029 	dcn20_stream_encoder_construct(enc1, ctx, ctx->dc_bios, eng_id,
1030 					&stream_enc_regs[eng_id],
1031 					&se_shift, &se_mask);
1032 
1033 	return &enc1->base;
1034 }
1035 
1036 static const struct dce_hwseq_registers hwseq_reg = {
1037 		HWSEQ_DCN2_REG_LIST()
1038 };
1039 
1040 static const struct dce_hwseq_shift hwseq_shift = {
1041 		HWSEQ_DCN2_MASK_SH_LIST(__SHIFT)
1042 };
1043 
1044 static const struct dce_hwseq_mask hwseq_mask = {
1045 		HWSEQ_DCN2_MASK_SH_LIST(_MASK)
1046 };
1047 
1048 struct dce_hwseq *dcn20_hwseq_create(
1049 	struct dc_context *ctx)
1050 {
1051 	struct dce_hwseq *hws = kzalloc(sizeof(struct dce_hwseq), GFP_KERNEL);
1052 
1053 	if (hws) {
1054 		hws->ctx = ctx;
1055 		hws->regs = &hwseq_reg;
1056 		hws->shifts = &hwseq_shift;
1057 		hws->masks = &hwseq_mask;
1058 	}
1059 	return hws;
1060 }
1061 
1062 static const struct resource_create_funcs res_create_funcs = {
1063 	.read_dce_straps = read_dce_straps,
1064 	.create_audio = dcn20_create_audio,
1065 	.create_stream_encoder = dcn20_stream_encoder_create,
1066 	.create_hwseq = dcn20_hwseq_create,
1067 };
1068 
1069 static const struct resource_create_funcs res_create_maximus_funcs = {
1070 	.read_dce_straps = NULL,
1071 	.create_audio = NULL,
1072 	.create_stream_encoder = NULL,
1073 	.create_hwseq = dcn20_hwseq_create,
1074 };
1075 
1076 static void dcn20_pp_smu_destroy(struct pp_smu_funcs **pp_smu);
1077 
1078 void dcn20_clock_source_destroy(struct clock_source **clk_src)
1079 {
1080 	kfree(TO_DCE110_CLK_SRC(*clk_src));
1081 	*clk_src = NULL;
1082 }
1083 
1084 
1085 struct display_stream_compressor *dcn20_dsc_create(
1086 	struct dc_context *ctx, uint32_t inst)
1087 {
1088 	struct dcn20_dsc *dsc =
1089 		kzalloc(sizeof(struct dcn20_dsc), GFP_ATOMIC);
1090 
1091 	if (!dsc) {
1092 		BREAK_TO_DEBUGGER();
1093 		return NULL;
1094 	}
1095 
1096 	dsc2_construct(dsc, ctx, inst, &dsc_regs[inst], &dsc_shift, &dsc_mask);
1097 	return &dsc->base;
1098 }
1099 
1100 void dcn20_dsc_destroy(struct display_stream_compressor **dsc)
1101 {
1102 	kfree(container_of(*dsc, struct dcn20_dsc, base));
1103 	*dsc = NULL;
1104 }
1105 
1106 
1107 static void dcn20_resource_destruct(struct dcn20_resource_pool *pool)
1108 {
1109 	unsigned int i;
1110 
1111 	for (i = 0; i < pool->base.stream_enc_count; i++) {
1112 		if (pool->base.stream_enc[i] != NULL) {
1113 			kfree(DCN10STRENC_FROM_STRENC(pool->base.stream_enc[i]));
1114 			pool->base.stream_enc[i] = NULL;
1115 		}
1116 	}
1117 
1118 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
1119 		if (pool->base.dscs[i] != NULL)
1120 			dcn20_dsc_destroy(&pool->base.dscs[i]);
1121 	}
1122 
1123 	if (pool->base.mpc != NULL) {
1124 		kfree(TO_DCN20_MPC(pool->base.mpc));
1125 		pool->base.mpc = NULL;
1126 	}
1127 	if (pool->base.hubbub != NULL) {
1128 		kfree(pool->base.hubbub);
1129 		pool->base.hubbub = NULL;
1130 	}
1131 	for (i = 0; i < pool->base.pipe_count; i++) {
1132 		if (pool->base.dpps[i] != NULL)
1133 			dcn20_dpp_destroy(&pool->base.dpps[i]);
1134 
1135 		if (pool->base.ipps[i] != NULL)
1136 			pool->base.ipps[i]->funcs->ipp_destroy(&pool->base.ipps[i]);
1137 
1138 		if (pool->base.hubps[i] != NULL) {
1139 			kfree(TO_DCN20_HUBP(pool->base.hubps[i]));
1140 			pool->base.hubps[i] = NULL;
1141 		}
1142 
1143 		if (pool->base.irqs != NULL) {
1144 			dal_irq_service_destroy(&pool->base.irqs);
1145 		}
1146 	}
1147 
1148 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1149 		if (pool->base.engines[i] != NULL)
1150 			dce110_engine_destroy(&pool->base.engines[i]);
1151 		if (pool->base.hw_i2cs[i] != NULL) {
1152 			kfree(pool->base.hw_i2cs[i]);
1153 			pool->base.hw_i2cs[i] = NULL;
1154 		}
1155 		if (pool->base.sw_i2cs[i] != NULL) {
1156 			kfree(pool->base.sw_i2cs[i]);
1157 			pool->base.sw_i2cs[i] = NULL;
1158 		}
1159 	}
1160 
1161 	for (i = 0; i < pool->base.res_cap->num_opp; i++) {
1162 		if (pool->base.opps[i] != NULL)
1163 			pool->base.opps[i]->funcs->opp_destroy(&pool->base.opps[i]);
1164 	}
1165 
1166 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1167 		if (pool->base.timing_generators[i] != NULL)	{
1168 			kfree(DCN10TG_FROM_TG(pool->base.timing_generators[i]));
1169 			pool->base.timing_generators[i] = NULL;
1170 		}
1171 	}
1172 
1173 	for (i = 0; i < pool->base.res_cap->num_dwb; i++) {
1174 		if (pool->base.dwbc[i] != NULL) {
1175 			kfree(TO_DCN20_DWBC(pool->base.dwbc[i]));
1176 			pool->base.dwbc[i] = NULL;
1177 		}
1178 		if (pool->base.mcif_wb[i] != NULL) {
1179 			kfree(TO_DCN20_MMHUBBUB(pool->base.mcif_wb[i]));
1180 			pool->base.mcif_wb[i] = NULL;
1181 		}
1182 	}
1183 
1184 	for (i = 0; i < pool->base.audio_count; i++) {
1185 		if (pool->base.audios[i])
1186 			dce_aud_destroy(&pool->base.audios[i]);
1187 	}
1188 
1189 	for (i = 0; i < pool->base.clk_src_count; i++) {
1190 		if (pool->base.clock_sources[i] != NULL) {
1191 			dcn20_clock_source_destroy(&pool->base.clock_sources[i]);
1192 			pool->base.clock_sources[i] = NULL;
1193 		}
1194 	}
1195 
1196 	if (pool->base.dp_clock_source != NULL) {
1197 		dcn20_clock_source_destroy(&pool->base.dp_clock_source);
1198 		pool->base.dp_clock_source = NULL;
1199 	}
1200 
1201 
1202 	if (pool->base.abm != NULL)
1203 		dce_abm_destroy(&pool->base.abm);
1204 
1205 	if (pool->base.dmcu != NULL)
1206 		dce_dmcu_destroy(&pool->base.dmcu);
1207 
1208 	if (pool->base.dccg != NULL)
1209 		dcn_dccg_destroy(&pool->base.dccg);
1210 
1211 	if (pool->base.pp_smu != NULL)
1212 		dcn20_pp_smu_destroy(&pool->base.pp_smu);
1213 
1214 	if (pool->base.oem_device != NULL)
1215 		link_destroy_ddc_service(&pool->base.oem_device);
1216 }
1217 
1218 struct hubp *dcn20_hubp_create(
1219 	struct dc_context *ctx,
1220 	uint32_t inst)
1221 {
1222 	struct dcn20_hubp *hubp2 =
1223 		kzalloc(sizeof(struct dcn20_hubp), GFP_ATOMIC);
1224 
1225 	if (!hubp2)
1226 		return NULL;
1227 
1228 	if (hubp2_construct(hubp2, ctx, inst,
1229 			&hubp_regs[inst], &hubp_shift, &hubp_mask))
1230 		return &hubp2->base;
1231 
1232 	BREAK_TO_DEBUGGER();
1233 	kfree(hubp2);
1234 	return NULL;
1235 }
1236 
1237 static void get_pixel_clock_parameters(
1238 	struct pipe_ctx *pipe_ctx,
1239 	struct pixel_clk_params *pixel_clk_params)
1240 {
1241 	const struct dc_stream_state *stream = pipe_ctx->stream;
1242 	struct pipe_ctx *odm_pipe;
1243 	int opp_cnt = 1;
1244 	struct dc_link *link = stream->link;
1245 	struct link_encoder *link_enc = NULL;
1246 	struct dc *dc = pipe_ctx->stream->ctx->dc;
1247 	struct dce_hwseq *hws = dc->hwseq;
1248 
1249 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
1250 		opp_cnt++;
1251 
1252 	pixel_clk_params->requested_pix_clk_100hz = stream->timing.pix_clk_100hz;
1253 
1254 	link_enc = link_enc_cfg_get_link_enc(link);
1255 	if (link_enc)
1256 		pixel_clk_params->encoder_object_id = link_enc->id;
1257 
1258 	pixel_clk_params->signal_type = pipe_ctx->stream->signal;
1259 	pixel_clk_params->controller_id = pipe_ctx->stream_res.tg->inst + 1;
1260 	/* TODO: un-hardcode*/
1261 	/* TODO - DP2.0 HW: calculate requested_sym_clk for UHBR rates */
1262 	pixel_clk_params->requested_sym_clk = LINK_RATE_LOW *
1263 		LINK_RATE_REF_FREQ_IN_KHZ;
1264 	pixel_clk_params->flags.ENABLE_SS = 0;
1265 	pixel_clk_params->color_depth =
1266 		stream->timing.display_color_depth;
1267 	pixel_clk_params->flags.DISPLAY_BLANKED = 1;
1268 	pixel_clk_params->pixel_encoding = stream->timing.pixel_encoding;
1269 
1270 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
1271 		pixel_clk_params->color_depth = COLOR_DEPTH_888;
1272 
1273 	if (opp_cnt == 4)
1274 		pixel_clk_params->requested_pix_clk_100hz /= 4;
1275 	else if (optc2_is_two_pixels_per_containter(&stream->timing) || opp_cnt == 2)
1276 		pixel_clk_params->requested_pix_clk_100hz /= 2;
1277 	else if (hws->funcs.is_dp_dig_pixel_rate_div_policy) {
1278 		if (hws->funcs.is_dp_dig_pixel_rate_div_policy(pipe_ctx))
1279 			pixel_clk_params->requested_pix_clk_100hz /= 2;
1280 	}
1281 
1282 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
1283 		pixel_clk_params->requested_pix_clk_100hz *= 2;
1284 
1285 }
1286 
1287 static void build_clamping_params(struct dc_stream_state *stream)
1288 {
1289 	stream->clamping.clamping_level = CLAMPING_FULL_RANGE;
1290 	stream->clamping.c_depth = stream->timing.display_color_depth;
1291 	stream->clamping.pixel_encoding = stream->timing.pixel_encoding;
1292 }
1293 
1294 static enum dc_status build_pipe_hw_param(struct pipe_ctx *pipe_ctx)
1295 {
1296 
1297 	get_pixel_clock_parameters(pipe_ctx, &pipe_ctx->stream_res.pix_clk_params);
1298 
1299 	pipe_ctx->clock_source->funcs->get_pix_clk_dividers(
1300 		pipe_ctx->clock_source,
1301 		&pipe_ctx->stream_res.pix_clk_params,
1302 		&pipe_ctx->pll_settings);
1303 
1304 	pipe_ctx->stream->clamping.pixel_encoding = pipe_ctx->stream->timing.pixel_encoding;
1305 
1306 	resource_build_bit_depth_reduction_params(pipe_ctx->stream,
1307 					&pipe_ctx->stream->bit_depth_params);
1308 	build_clamping_params(pipe_ctx->stream);
1309 
1310 	return DC_OK;
1311 }
1312 
1313 enum dc_status dcn20_build_mapped_resource(const struct dc *dc, struct dc_state *context, struct dc_stream_state *stream)
1314 {
1315 	enum dc_status status = DC_OK;
1316 	struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
1317 
1318 	if (!pipe_ctx)
1319 		return DC_ERROR_UNEXPECTED;
1320 
1321 
1322 	status = build_pipe_hw_param(pipe_ctx);
1323 
1324 	return status;
1325 }
1326 
1327 
1328 void dcn20_acquire_dsc(const struct dc *dc,
1329 			struct resource_context *res_ctx,
1330 			struct display_stream_compressor **dsc,
1331 			int pipe_idx)
1332 {
1333 	int i;
1334 	const struct resource_pool *pool = dc->res_pool;
1335 	struct display_stream_compressor *dsc_old = dc->current_state->res_ctx.pipe_ctx[pipe_idx].stream_res.dsc;
1336 
1337 	ASSERT(*dsc == NULL); /* If this ASSERT fails, dsc was not released properly */
1338 	*dsc = NULL;
1339 
1340 	/* Always do 1-to-1 mapping when number of DSCs is same as number of pipes */
1341 	if (pool->res_cap->num_dsc == pool->res_cap->num_opp) {
1342 		*dsc = pool->dscs[pipe_idx];
1343 		res_ctx->is_dsc_acquired[pipe_idx] = true;
1344 		return;
1345 	}
1346 
1347 	/* Return old DSC to avoid the need for re-programming */
1348 	if (dsc_old && !res_ctx->is_dsc_acquired[dsc_old->inst]) {
1349 		*dsc = dsc_old;
1350 		res_ctx->is_dsc_acquired[dsc_old->inst] = true;
1351 		return ;
1352 	}
1353 
1354 	/* Find first free DSC */
1355 	for (i = 0; i < pool->res_cap->num_dsc; i++)
1356 		if (!res_ctx->is_dsc_acquired[i]) {
1357 			*dsc = pool->dscs[i];
1358 			res_ctx->is_dsc_acquired[i] = true;
1359 			break;
1360 		}
1361 }
1362 
1363 void dcn20_release_dsc(struct resource_context *res_ctx,
1364 			const struct resource_pool *pool,
1365 			struct display_stream_compressor **dsc)
1366 {
1367 	int i;
1368 
1369 	for (i = 0; i < pool->res_cap->num_dsc; i++)
1370 		if (pool->dscs[i] == *dsc) {
1371 			res_ctx->is_dsc_acquired[i] = false;
1372 			*dsc = NULL;
1373 			break;
1374 		}
1375 }
1376 
1377 
1378 
1379 enum dc_status dcn20_add_dsc_to_stream_resource(struct dc *dc,
1380 		struct dc_state *dc_ctx,
1381 		struct dc_stream_state *dc_stream)
1382 {
1383 	enum dc_status result = DC_OK;
1384 	int i;
1385 
1386 	/* Get a DSC if required and available */
1387 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1388 		struct pipe_ctx *pipe_ctx = &dc_ctx->res_ctx.pipe_ctx[i];
1389 
1390 		if (pipe_ctx->top_pipe)
1391 			continue;
1392 
1393 		if (pipe_ctx->stream != dc_stream)
1394 			continue;
1395 
1396 		if (pipe_ctx->stream_res.dsc)
1397 			continue;
1398 
1399 		dcn20_acquire_dsc(dc, &dc_ctx->res_ctx, &pipe_ctx->stream_res.dsc, i);
1400 
1401 		/* The number of DSCs can be less than the number of pipes */
1402 		if (!pipe_ctx->stream_res.dsc) {
1403 			result = DC_NO_DSC_RESOURCE;
1404 		}
1405 
1406 		break;
1407 	}
1408 
1409 	return result;
1410 }
1411 
1412 
1413 static enum dc_status remove_dsc_from_stream_resource(struct dc *dc,
1414 		struct dc_state *new_ctx,
1415 		struct dc_stream_state *dc_stream)
1416 {
1417 	struct pipe_ctx *pipe_ctx = NULL;
1418 	int i;
1419 
1420 	for (i = 0; i < MAX_PIPES; i++) {
1421 		if (new_ctx->res_ctx.pipe_ctx[i].stream == dc_stream && !new_ctx->res_ctx.pipe_ctx[i].top_pipe) {
1422 			pipe_ctx = &new_ctx->res_ctx.pipe_ctx[i];
1423 
1424 			if (pipe_ctx->stream_res.dsc)
1425 				dcn20_release_dsc(&new_ctx->res_ctx, dc->res_pool, &pipe_ctx->stream_res.dsc);
1426 		}
1427 	}
1428 
1429 	if (!pipe_ctx)
1430 		return DC_ERROR_UNEXPECTED;
1431 	else
1432 		return DC_OK;
1433 }
1434 
1435 
1436 enum dc_status dcn20_add_stream_to_ctx(struct dc *dc, struct dc_state *new_ctx, struct dc_stream_state *dc_stream)
1437 {
1438 	enum dc_status result = DC_ERROR_UNEXPECTED;
1439 
1440 	result = resource_map_pool_resources(dc, new_ctx, dc_stream);
1441 
1442 	if (result == DC_OK)
1443 		result = resource_map_phy_clock_resources(dc, new_ctx, dc_stream);
1444 
1445 	/* Get a DSC if required and available */
1446 	if (result == DC_OK && dc_stream->timing.flags.DSC)
1447 		result = dcn20_add_dsc_to_stream_resource(dc, new_ctx, dc_stream);
1448 
1449 	if (result == DC_OK)
1450 		result = dcn20_build_mapped_resource(dc, new_ctx, dc_stream);
1451 
1452 	return result;
1453 }
1454 
1455 
1456 enum dc_status dcn20_remove_stream_from_ctx(struct dc *dc, struct dc_state *new_ctx, struct dc_stream_state *dc_stream)
1457 {
1458 	enum dc_status result = DC_OK;
1459 
1460 	result = remove_dsc_from_stream_resource(dc, new_ctx, dc_stream);
1461 
1462 	return result;
1463 }
1464 
1465 /**
1466  * dcn20_split_stream_for_odm - Check if stream can be splited for ODM
1467  *
1468  * @dc: DC object with resource pool info required for pipe split
1469  * @res_ctx: Persistent state of resources
1470  * @prev_odm_pipe: Reference to the previous ODM pipe
1471  * @next_odm_pipe: Reference to the next ODM pipe
1472  *
1473  * This function takes a logically active pipe and a logically free pipe and
1474  * halves all the scaling parameters that need to be halved while populating
1475  * the free pipe with the required resources and configuring the next/previous
1476  * ODM pipe pointers.
1477  *
1478  * Return:
1479  * Return true if split stream for ODM is possible, otherwise, return false.
1480  */
1481 bool dcn20_split_stream_for_odm(
1482 		const struct dc *dc,
1483 		struct resource_context *res_ctx,
1484 		struct pipe_ctx *prev_odm_pipe,
1485 		struct pipe_ctx *next_odm_pipe)
1486 {
1487 	int pipe_idx = next_odm_pipe->pipe_idx;
1488 	const struct resource_pool *pool = dc->res_pool;
1489 
1490 	*next_odm_pipe = *prev_odm_pipe;
1491 
1492 	next_odm_pipe->pipe_idx = pipe_idx;
1493 	next_odm_pipe->plane_res.mi = pool->mis[next_odm_pipe->pipe_idx];
1494 	next_odm_pipe->plane_res.hubp = pool->hubps[next_odm_pipe->pipe_idx];
1495 	next_odm_pipe->plane_res.ipp = pool->ipps[next_odm_pipe->pipe_idx];
1496 	next_odm_pipe->plane_res.xfm = pool->transforms[next_odm_pipe->pipe_idx];
1497 	next_odm_pipe->plane_res.dpp = pool->dpps[next_odm_pipe->pipe_idx];
1498 	next_odm_pipe->plane_res.mpcc_inst = pool->dpps[next_odm_pipe->pipe_idx]->inst;
1499 	next_odm_pipe->stream_res.dsc = NULL;
1500 	if (prev_odm_pipe->next_odm_pipe && prev_odm_pipe->next_odm_pipe != next_odm_pipe) {
1501 		next_odm_pipe->next_odm_pipe = prev_odm_pipe->next_odm_pipe;
1502 		next_odm_pipe->next_odm_pipe->prev_odm_pipe = next_odm_pipe;
1503 	}
1504 	if (prev_odm_pipe->top_pipe && prev_odm_pipe->top_pipe->next_odm_pipe) {
1505 		prev_odm_pipe->top_pipe->next_odm_pipe->bottom_pipe = next_odm_pipe;
1506 		next_odm_pipe->top_pipe = prev_odm_pipe->top_pipe->next_odm_pipe;
1507 	}
1508 	if (prev_odm_pipe->bottom_pipe && prev_odm_pipe->bottom_pipe->next_odm_pipe) {
1509 		prev_odm_pipe->bottom_pipe->next_odm_pipe->top_pipe = next_odm_pipe;
1510 		next_odm_pipe->bottom_pipe = prev_odm_pipe->bottom_pipe->next_odm_pipe;
1511 	}
1512 	prev_odm_pipe->next_odm_pipe = next_odm_pipe;
1513 	next_odm_pipe->prev_odm_pipe = prev_odm_pipe;
1514 
1515 	if (prev_odm_pipe->plane_state) {
1516 		struct scaler_data *sd = &prev_odm_pipe->plane_res.scl_data;
1517 		int new_width;
1518 
1519 		/* HACTIVE halved for odm combine */
1520 		sd->h_active /= 2;
1521 		/* Calculate new vp and recout for left pipe */
1522 		/* Need at least 16 pixels width per side */
1523 		if (sd->recout.x + 16 >= sd->h_active)
1524 			return false;
1525 		new_width = sd->h_active - sd->recout.x;
1526 		sd->viewport.width -= dc_fixpt_floor(dc_fixpt_mul_int(
1527 				sd->ratios.horz, sd->recout.width - new_width));
1528 		sd->viewport_c.width -= dc_fixpt_floor(dc_fixpt_mul_int(
1529 				sd->ratios.horz_c, sd->recout.width - new_width));
1530 		sd->recout.width = new_width;
1531 
1532 		/* Calculate new vp and recout for right pipe */
1533 		sd = &next_odm_pipe->plane_res.scl_data;
1534 		/* HACTIVE halved for odm combine */
1535 		sd->h_active /= 2;
1536 		/* Need at least 16 pixels width per side */
1537 		if (new_width <= 16)
1538 			return false;
1539 		new_width = sd->recout.width + sd->recout.x - sd->h_active;
1540 		sd->viewport.width -= dc_fixpt_floor(dc_fixpt_mul_int(
1541 				sd->ratios.horz, sd->recout.width - new_width));
1542 		sd->viewport_c.width -= dc_fixpt_floor(dc_fixpt_mul_int(
1543 				sd->ratios.horz_c, sd->recout.width - new_width));
1544 		sd->recout.width = new_width;
1545 		sd->viewport.x += dc_fixpt_floor(dc_fixpt_mul_int(
1546 				sd->ratios.horz, sd->h_active - sd->recout.x));
1547 		sd->viewport_c.x += dc_fixpt_floor(dc_fixpt_mul_int(
1548 				sd->ratios.horz_c, sd->h_active - sd->recout.x));
1549 		sd->recout.x = 0;
1550 	}
1551 	if (!next_odm_pipe->top_pipe)
1552 		next_odm_pipe->stream_res.opp = pool->opps[next_odm_pipe->pipe_idx];
1553 	else
1554 		next_odm_pipe->stream_res.opp = next_odm_pipe->top_pipe->stream_res.opp;
1555 	if (next_odm_pipe->stream->timing.flags.DSC == 1 && !next_odm_pipe->top_pipe) {
1556 		dcn20_acquire_dsc(dc, res_ctx, &next_odm_pipe->stream_res.dsc, next_odm_pipe->pipe_idx);
1557 		ASSERT(next_odm_pipe->stream_res.dsc);
1558 		if (next_odm_pipe->stream_res.dsc == NULL)
1559 			return false;
1560 	}
1561 
1562 	return true;
1563 }
1564 
1565 void dcn20_split_stream_for_mpc(
1566 		struct resource_context *res_ctx,
1567 		const struct resource_pool *pool,
1568 		struct pipe_ctx *primary_pipe,
1569 		struct pipe_ctx *secondary_pipe)
1570 {
1571 	int pipe_idx = secondary_pipe->pipe_idx;
1572 	struct pipe_ctx *sec_bot_pipe = secondary_pipe->bottom_pipe;
1573 
1574 	*secondary_pipe = *primary_pipe;
1575 	secondary_pipe->bottom_pipe = sec_bot_pipe;
1576 
1577 	secondary_pipe->pipe_idx = pipe_idx;
1578 	secondary_pipe->plane_res.mi = pool->mis[secondary_pipe->pipe_idx];
1579 	secondary_pipe->plane_res.hubp = pool->hubps[secondary_pipe->pipe_idx];
1580 	secondary_pipe->plane_res.ipp = pool->ipps[secondary_pipe->pipe_idx];
1581 	secondary_pipe->plane_res.xfm = pool->transforms[secondary_pipe->pipe_idx];
1582 	secondary_pipe->plane_res.dpp = pool->dpps[secondary_pipe->pipe_idx];
1583 	secondary_pipe->plane_res.mpcc_inst = pool->dpps[secondary_pipe->pipe_idx]->inst;
1584 	secondary_pipe->stream_res.dsc = NULL;
1585 	if (primary_pipe->bottom_pipe && primary_pipe->bottom_pipe != secondary_pipe) {
1586 		ASSERT(!secondary_pipe->bottom_pipe);
1587 		secondary_pipe->bottom_pipe = primary_pipe->bottom_pipe;
1588 		secondary_pipe->bottom_pipe->top_pipe = secondary_pipe;
1589 	}
1590 	primary_pipe->bottom_pipe = secondary_pipe;
1591 	secondary_pipe->top_pipe = primary_pipe;
1592 
1593 	ASSERT(primary_pipe->plane_state);
1594 }
1595 
1596 unsigned int dcn20_calc_max_scaled_time(
1597 		unsigned int time_per_pixel,
1598 		enum mmhubbub_wbif_mode mode,
1599 		unsigned int urgent_watermark)
1600 {
1601 	unsigned int time_per_byte = 0;
1602 	unsigned int total_y_free_entry = 0x200; /* two memory piece for luma */
1603 	unsigned int total_c_free_entry = 0x140; /* two memory piece for chroma */
1604 	unsigned int small_free_entry, max_free_entry;
1605 	unsigned int buf_lh_capability;
1606 	unsigned int max_scaled_time;
1607 
1608 	if (mode == PACKED_444) /* packed mode */
1609 		time_per_byte = time_per_pixel/4;
1610 	else if (mode == PLANAR_420_8BPC)
1611 		time_per_byte  = time_per_pixel;
1612 	else if (mode == PLANAR_420_10BPC) /* p010 */
1613 		time_per_byte  = time_per_pixel * 819/1024;
1614 
1615 	if (time_per_byte == 0)
1616 		time_per_byte = 1;
1617 
1618 	small_free_entry  = (total_y_free_entry > total_c_free_entry) ? total_c_free_entry : total_y_free_entry;
1619 	max_free_entry    = (mode == PACKED_444) ? total_y_free_entry + total_c_free_entry : small_free_entry;
1620 	buf_lh_capability = max_free_entry*time_per_byte*32/16; /* there is 4bit fraction */
1621 	max_scaled_time   = buf_lh_capability - urgent_watermark;
1622 	return max_scaled_time;
1623 }
1624 
1625 void dcn20_set_mcif_arb_params(
1626 		struct dc *dc,
1627 		struct dc_state *context,
1628 		display_e2e_pipe_params_st *pipes,
1629 		int pipe_cnt)
1630 {
1631 	enum mmhubbub_wbif_mode wbif_mode;
1632 	struct mcif_arb_params *wb_arb_params;
1633 	int i, j, dwb_pipe;
1634 
1635 	/* Writeback MCIF_WB arbitration parameters */
1636 	dwb_pipe = 0;
1637 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1638 
1639 		if (!context->res_ctx.pipe_ctx[i].stream)
1640 			continue;
1641 
1642 		for (j = 0; j < MAX_DWB_PIPES; j++) {
1643 			if (context->res_ctx.pipe_ctx[i].stream->writeback_info[j].wb_enabled == false)
1644 				continue;
1645 
1646 			//wb_arb_params = &context->res_ctx.pipe_ctx[i].stream->writeback_info[j].mcif_arb_params;
1647 			wb_arb_params = &context->bw_ctx.bw.dcn.bw_writeback.mcif_wb_arb[dwb_pipe];
1648 
1649 			if (context->res_ctx.pipe_ctx[i].stream->writeback_info[j].dwb_params.out_format == dwb_scaler_mode_yuv420) {
1650 				if (context->res_ctx.pipe_ctx[i].stream->writeback_info[j].dwb_params.output_depth == DWB_OUTPUT_PIXEL_DEPTH_8BPC)
1651 					wbif_mode = PLANAR_420_8BPC;
1652 				else
1653 					wbif_mode = PLANAR_420_10BPC;
1654 			} else
1655 				wbif_mode = PACKED_444;
1656 
1657 			DC_FP_START();
1658 			dcn20_fpu_set_wb_arb_params(wb_arb_params, context, pipes, pipe_cnt, i);
1659 			DC_FP_END();
1660 
1661 			wb_arb_params->slice_lines = 32;
1662 			wb_arb_params->arbitration_slice = 2;
1663 			wb_arb_params->max_scaled_time = dcn20_calc_max_scaled_time(wb_arb_params->time_per_pixel,
1664 				wbif_mode,
1665 				wb_arb_params->cli_watermark[0]); /* assume 4 watermark sets have the same value */
1666 
1667 			dwb_pipe++;
1668 
1669 			if (dwb_pipe >= MAX_DWB_PIPES)
1670 				return;
1671 		}
1672 		if (dwb_pipe >= MAX_DWB_PIPES)
1673 			return;
1674 	}
1675 }
1676 
1677 bool dcn20_validate_dsc(struct dc *dc, struct dc_state *new_ctx)
1678 {
1679 	int i;
1680 
1681 	/* Validate DSC config, dsc count validation is already done */
1682 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1683 		struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[i];
1684 		struct dc_stream_state *stream = pipe_ctx->stream;
1685 		struct dsc_config dsc_cfg;
1686 		struct pipe_ctx *odm_pipe;
1687 		int opp_cnt = 1;
1688 
1689 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
1690 			opp_cnt++;
1691 
1692 		/* Only need to validate top pipe */
1693 		if (pipe_ctx->top_pipe || pipe_ctx->prev_odm_pipe || !stream || !stream->timing.flags.DSC)
1694 			continue;
1695 
1696 		dsc_cfg.pic_width = (stream->timing.h_addressable + stream->timing.h_border_left
1697 				+ stream->timing.h_border_right) / opp_cnt;
1698 		dsc_cfg.pic_height = stream->timing.v_addressable + stream->timing.v_border_top
1699 				+ stream->timing.v_border_bottom;
1700 		dsc_cfg.pixel_encoding = stream->timing.pixel_encoding;
1701 		dsc_cfg.color_depth = stream->timing.display_color_depth;
1702 		dsc_cfg.is_odm = pipe_ctx->next_odm_pipe ? true : false;
1703 		dsc_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
1704 		dsc_cfg.dc_dsc_cfg.num_slices_h /= opp_cnt;
1705 
1706 		if (!pipe_ctx->stream_res.dsc->funcs->dsc_validate_stream(pipe_ctx->stream_res.dsc, &dsc_cfg))
1707 			return false;
1708 	}
1709 	return true;
1710 }
1711 
1712 struct pipe_ctx *dcn20_find_secondary_pipe(struct dc *dc,
1713 		struct resource_context *res_ctx,
1714 		const struct resource_pool *pool,
1715 		const struct pipe_ctx *primary_pipe)
1716 {
1717 	struct pipe_ctx *secondary_pipe = NULL;
1718 
1719 	if (dc && primary_pipe) {
1720 		int j;
1721 		int preferred_pipe_idx = 0;
1722 
1723 		/* first check the prev dc state:
1724 		 * if this primary pipe has a bottom pipe in prev. state
1725 		 * and if the bottom pipe is still available (which it should be),
1726 		 * pick that pipe as secondary
1727 		 * Same logic applies for ODM pipes
1728 		 */
1729 		if (dc->current_state->res_ctx.pipe_ctx[primary_pipe->pipe_idx].next_odm_pipe) {
1730 			preferred_pipe_idx = dc->current_state->res_ctx.pipe_ctx[primary_pipe->pipe_idx].next_odm_pipe->pipe_idx;
1731 			if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
1732 				secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
1733 				secondary_pipe->pipe_idx = preferred_pipe_idx;
1734 			}
1735 		}
1736 		if (secondary_pipe == NULL &&
1737 				dc->current_state->res_ctx.pipe_ctx[primary_pipe->pipe_idx].bottom_pipe) {
1738 			preferred_pipe_idx = dc->current_state->res_ctx.pipe_ctx[primary_pipe->pipe_idx].bottom_pipe->pipe_idx;
1739 			if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
1740 				secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
1741 				secondary_pipe->pipe_idx = preferred_pipe_idx;
1742 			}
1743 		}
1744 
1745 		/*
1746 		 * if this primary pipe does not have a bottom pipe in prev. state
1747 		 * start backward and find a pipe that did not used to be a bottom pipe in
1748 		 * prev. dc state. This way we make sure we keep the same assignment as
1749 		 * last state and will not have to reprogram every pipe
1750 		 */
1751 		if (secondary_pipe == NULL) {
1752 			for (j = dc->res_pool->pipe_count - 1; j >= 0; j--) {
1753 				if (dc->current_state->res_ctx.pipe_ctx[j].top_pipe == NULL
1754 						&& dc->current_state->res_ctx.pipe_ctx[j].prev_odm_pipe == NULL) {
1755 					preferred_pipe_idx = j;
1756 
1757 					if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
1758 						secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
1759 						secondary_pipe->pipe_idx = preferred_pipe_idx;
1760 						break;
1761 					}
1762 				}
1763 			}
1764 		}
1765 		/*
1766 		 * We should never hit this assert unless assignments are shuffled around
1767 		 * if this happens we will prob. hit a vsync tdr
1768 		 */
1769 		ASSERT(secondary_pipe);
1770 		/*
1771 		 * search backwards for the second pipe to keep pipe
1772 		 * assignment more consistent
1773 		 */
1774 		if (secondary_pipe == NULL) {
1775 			for (j = dc->res_pool->pipe_count - 1; j >= 0; j--) {
1776 				preferred_pipe_idx = j;
1777 
1778 				if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
1779 					secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
1780 					secondary_pipe->pipe_idx = preferred_pipe_idx;
1781 					break;
1782 				}
1783 			}
1784 		}
1785 	}
1786 
1787 	return secondary_pipe;
1788 }
1789 
1790 void dcn20_merge_pipes_for_validate(
1791 		struct dc *dc,
1792 		struct dc_state *context)
1793 {
1794 	int i;
1795 
1796 	/* merge previously split odm pipes since mode support needs to make the decision */
1797 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1798 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1799 		struct pipe_ctx *odm_pipe = pipe->next_odm_pipe;
1800 
1801 		if (pipe->prev_odm_pipe)
1802 			continue;
1803 
1804 		pipe->next_odm_pipe = NULL;
1805 		while (odm_pipe) {
1806 			struct pipe_ctx *next_odm_pipe = odm_pipe->next_odm_pipe;
1807 
1808 			odm_pipe->plane_state = NULL;
1809 			odm_pipe->stream = NULL;
1810 			odm_pipe->top_pipe = NULL;
1811 			odm_pipe->bottom_pipe = NULL;
1812 			odm_pipe->prev_odm_pipe = NULL;
1813 			odm_pipe->next_odm_pipe = NULL;
1814 			if (odm_pipe->stream_res.dsc)
1815 				dcn20_release_dsc(&context->res_ctx, dc->res_pool, &odm_pipe->stream_res.dsc);
1816 			/* Clear plane_res and stream_res */
1817 			memset(&odm_pipe->plane_res, 0, sizeof(odm_pipe->plane_res));
1818 			memset(&odm_pipe->stream_res, 0, sizeof(odm_pipe->stream_res));
1819 			odm_pipe = next_odm_pipe;
1820 		}
1821 		if (pipe->plane_state)
1822 			resource_build_scaling_params(pipe);
1823 	}
1824 
1825 	/* merge previously mpc split pipes since mode support needs to make the decision */
1826 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1827 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1828 		struct pipe_ctx *hsplit_pipe = pipe->bottom_pipe;
1829 
1830 		if (!hsplit_pipe || hsplit_pipe->plane_state != pipe->plane_state)
1831 			continue;
1832 
1833 		pipe->bottom_pipe = hsplit_pipe->bottom_pipe;
1834 		if (hsplit_pipe->bottom_pipe)
1835 			hsplit_pipe->bottom_pipe->top_pipe = pipe;
1836 		hsplit_pipe->plane_state = NULL;
1837 		hsplit_pipe->stream = NULL;
1838 		hsplit_pipe->top_pipe = NULL;
1839 		hsplit_pipe->bottom_pipe = NULL;
1840 
1841 		/* Clear plane_res and stream_res */
1842 		memset(&hsplit_pipe->plane_res, 0, sizeof(hsplit_pipe->plane_res));
1843 		memset(&hsplit_pipe->stream_res, 0, sizeof(hsplit_pipe->stream_res));
1844 		if (pipe->plane_state)
1845 			resource_build_scaling_params(pipe);
1846 	}
1847 }
1848 
1849 int dcn20_validate_apply_pipe_split_flags(
1850 		struct dc *dc,
1851 		struct dc_state *context,
1852 		int vlevel,
1853 		int *split,
1854 		bool *merge)
1855 {
1856 	int i, pipe_idx, vlevel_split;
1857 	int plane_count = 0;
1858 	bool force_split = false;
1859 	bool avoid_split = dc->debug.pipe_split_policy == MPC_SPLIT_AVOID;
1860 	struct vba_vars_st *v = &context->bw_ctx.dml.vba;
1861 	int max_mpc_comb = v->maxMpcComb;
1862 
1863 	if (context->stream_count > 1) {
1864 		if (dc->debug.pipe_split_policy == MPC_SPLIT_AVOID_MULT_DISP)
1865 			avoid_split = true;
1866 	} else if (dc->debug.force_single_disp_pipe_split)
1867 			force_split = true;
1868 
1869 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1870 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1871 
1872 		/**
1873 		 * Workaround for avoiding pipe-split in cases where we'd split
1874 		 * planes that are too small, resulting in splits that aren't
1875 		 * valid for the scaler.
1876 		 */
1877 		if (pipe->plane_state &&
1878 		    (pipe->plane_state->dst_rect.width <= 16 ||
1879 		     pipe->plane_state->dst_rect.height <= 16 ||
1880 		     pipe->plane_state->src_rect.width <= 16 ||
1881 		     pipe->plane_state->src_rect.height <= 16))
1882 			avoid_split = true;
1883 
1884 		/* TODO: fix dc bugs and remove this split threshold thing */
1885 		if (pipe->stream && !pipe->prev_odm_pipe &&
1886 				(!pipe->top_pipe || pipe->top_pipe->plane_state != pipe->plane_state))
1887 			++plane_count;
1888 	}
1889 	if (plane_count > dc->res_pool->pipe_count / 2)
1890 		avoid_split = true;
1891 
1892 	/* W/A: Mode timing with borders may not work well with pipe split, avoid for this corner case */
1893 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1894 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1895 		struct dc_crtc_timing timing;
1896 
1897 		if (!pipe->stream)
1898 			continue;
1899 		else {
1900 			timing = pipe->stream->timing;
1901 			if (timing.h_border_left + timing.h_border_right
1902 					+ timing.v_border_top + timing.v_border_bottom > 0) {
1903 				avoid_split = true;
1904 				break;
1905 			}
1906 		}
1907 	}
1908 
1909 	/* Avoid split loop looks for lowest voltage level that allows most unsplit pipes possible */
1910 	if (avoid_split) {
1911 		for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1912 			if (!context->res_ctx.pipe_ctx[i].stream)
1913 				continue;
1914 
1915 			for (vlevel_split = vlevel; vlevel <= context->bw_ctx.dml.soc.num_states; vlevel++)
1916 				if (v->NoOfDPP[vlevel][0][pipe_idx] == 1 &&
1917 						v->ModeSupport[vlevel][0])
1918 					break;
1919 			/* Impossible to not split this pipe */
1920 			if (vlevel > context->bw_ctx.dml.soc.num_states)
1921 				vlevel = vlevel_split;
1922 			else
1923 				max_mpc_comb = 0;
1924 			pipe_idx++;
1925 		}
1926 		v->maxMpcComb = max_mpc_comb;
1927 	}
1928 
1929 	/* Split loop sets which pipe should be split based on dml outputs and dc flags */
1930 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
1931 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1932 		int pipe_plane = v->pipe_plane[pipe_idx];
1933 		bool split4mpc = context->stream_count == 1 && plane_count == 1
1934 				&& dc->config.enable_4to1MPC && dc->res_pool->pipe_count >= 4;
1935 
1936 		if (!context->res_ctx.pipe_ctx[i].stream)
1937 			continue;
1938 
1939 		if (split4mpc || v->NoOfDPP[vlevel][max_mpc_comb][pipe_plane] == 4)
1940 			split[i] = 4;
1941 		else if (force_split || v->NoOfDPP[vlevel][max_mpc_comb][pipe_plane] == 2)
1942 				split[i] = 2;
1943 
1944 		if ((pipe->stream->view_format ==
1945 				VIEW_3D_FORMAT_SIDE_BY_SIDE ||
1946 				pipe->stream->view_format ==
1947 				VIEW_3D_FORMAT_TOP_AND_BOTTOM) &&
1948 				(pipe->stream->timing.timing_3d_format ==
1949 				TIMING_3D_FORMAT_TOP_AND_BOTTOM ||
1950 				 pipe->stream->timing.timing_3d_format ==
1951 				TIMING_3D_FORMAT_SIDE_BY_SIDE))
1952 			split[i] = 2;
1953 		if (dc->debug.force_odm_combine & (1 << pipe->stream_res.tg->inst)) {
1954 			split[i] = 2;
1955 			v->ODMCombineEnablePerState[vlevel][pipe_plane] = dm_odm_combine_mode_2to1;
1956 		}
1957 		if (dc->debug.force_odm_combine_4to1 & (1 << pipe->stream_res.tg->inst)) {
1958 			split[i] = 4;
1959 			v->ODMCombineEnablePerState[vlevel][pipe_plane] = dm_odm_combine_mode_4to1;
1960 		}
1961 		/*420 format workaround*/
1962 		if (pipe->stream->timing.h_addressable > 7680 &&
1963 				pipe->stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420) {
1964 			split[i] = 4;
1965 		}
1966 		v->ODMCombineEnabled[pipe_plane] =
1967 			v->ODMCombineEnablePerState[vlevel][pipe_plane];
1968 
1969 		if (v->ODMCombineEnabled[pipe_plane] == dm_odm_combine_mode_disabled) {
1970 			if (get_num_mpc_splits(pipe) == 1) {
1971 				/*If need split for mpc but 2 way split already*/
1972 				if (split[i] == 4)
1973 					split[i] = 2; /* 2 -> 4 MPC */
1974 				else if (split[i] == 2)
1975 					split[i] = 0; /* 2 -> 2 MPC */
1976 				else if (pipe->top_pipe && pipe->top_pipe->plane_state == pipe->plane_state)
1977 					merge[i] = true; /* 2 -> 1 MPC */
1978 			} else if (get_num_mpc_splits(pipe) == 3) {
1979 				/*If need split for mpc but 4 way split already*/
1980 				if (split[i] == 2 && ((pipe->top_pipe && !pipe->top_pipe->top_pipe)
1981 						|| !pipe->bottom_pipe)) {
1982 					merge[i] = true; /* 4 -> 2 MPC */
1983 				} else if (split[i] == 0 && pipe->top_pipe &&
1984 						pipe->top_pipe->plane_state == pipe->plane_state)
1985 					merge[i] = true; /* 4 -> 1 MPC */
1986 				split[i] = 0;
1987 			} else if (get_num_odm_splits(pipe)) {
1988 				/* ODM -> MPC transition */
1989 				if (pipe->prev_odm_pipe) {
1990 					split[i] = 0;
1991 					merge[i] = true;
1992 				}
1993 			}
1994 		} else {
1995 			if (get_num_odm_splits(pipe) == 1) {
1996 				/*If need split for odm but 2 way split already*/
1997 				if (split[i] == 4)
1998 					split[i] = 2; /* 2 -> 4 ODM */
1999 				else if (split[i] == 2)
2000 					split[i] = 0; /* 2 -> 2 ODM */
2001 				else if (pipe->prev_odm_pipe) {
2002 					ASSERT(0); /* NOT expected yet */
2003 					merge[i] = true; /* exit ODM */
2004 				}
2005 			} else if (get_num_odm_splits(pipe) == 3) {
2006 				/*If need split for odm but 4 way split already*/
2007 				if (split[i] == 2 && ((pipe->prev_odm_pipe && !pipe->prev_odm_pipe->prev_odm_pipe)
2008 						|| !pipe->next_odm_pipe)) {
2009 					merge[i] = true; /* 4 -> 2 ODM */
2010 				} else if (split[i] == 0 && pipe->prev_odm_pipe) {
2011 					ASSERT(0); /* NOT expected yet */
2012 					merge[i] = true; /* exit ODM */
2013 				}
2014 				split[i] = 0;
2015 			} else if (get_num_mpc_splits(pipe)) {
2016 				/* MPC -> ODM transition */
2017 				ASSERT(0); /* NOT expected yet */
2018 				if (pipe->top_pipe && pipe->top_pipe->plane_state == pipe->plane_state) {
2019 					split[i] = 0;
2020 					merge[i] = true;
2021 				}
2022 			}
2023 		}
2024 
2025 		/* Adjust dppclk when split is forced, do not bother with dispclk */
2026 		if (split[i] != 0 && v->NoOfDPP[vlevel][max_mpc_comb][pipe_idx] == 1) {
2027 			DC_FP_START();
2028 			dcn20_fpu_adjust_dppclk(v, vlevel, max_mpc_comb, pipe_idx, false);
2029 			DC_FP_END();
2030 		}
2031 		pipe_idx++;
2032 	}
2033 
2034 	return vlevel;
2035 }
2036 
2037 bool dcn20_fast_validate_bw(
2038 		struct dc *dc,
2039 		struct dc_state *context,
2040 		display_e2e_pipe_params_st *pipes,
2041 		int *pipe_cnt_out,
2042 		int *pipe_split_from,
2043 		int *vlevel_out,
2044 		bool fast_validate)
2045 {
2046 	bool out = false;
2047 	int split[MAX_PIPES] = { 0 };
2048 	int pipe_cnt, i, pipe_idx, vlevel;
2049 
2050 	ASSERT(pipes);
2051 	if (!pipes)
2052 		return false;
2053 
2054 	dcn20_merge_pipes_for_validate(dc, context);
2055 
2056 	DC_FP_START();
2057 	pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes, fast_validate);
2058 	DC_FP_END();
2059 
2060 	*pipe_cnt_out = pipe_cnt;
2061 
2062 	if (!pipe_cnt) {
2063 		out = true;
2064 		goto validate_out;
2065 	}
2066 
2067 	vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
2068 
2069 	if (vlevel > context->bw_ctx.dml.soc.num_states)
2070 		goto validate_fail;
2071 
2072 	vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, vlevel, split, NULL);
2073 
2074 	/*initialize pipe_just_split_from to invalid idx*/
2075 	for (i = 0; i < MAX_PIPES; i++)
2076 		pipe_split_from[i] = -1;
2077 
2078 	for (i = 0, pipe_idx = -1; i < dc->res_pool->pipe_count; i++) {
2079 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2080 		struct pipe_ctx *hsplit_pipe = pipe->bottom_pipe;
2081 
2082 		if (!pipe->stream || pipe_split_from[i] >= 0)
2083 			continue;
2084 
2085 		pipe_idx++;
2086 
2087 		if (!pipe->top_pipe && !pipe->plane_state && context->bw_ctx.dml.vba.ODMCombineEnabled[pipe_idx]) {
2088 			hsplit_pipe = dcn20_find_secondary_pipe(dc, &context->res_ctx, dc->res_pool, pipe);
2089 			ASSERT(hsplit_pipe);
2090 			if (!dcn20_split_stream_for_odm(
2091 					dc, &context->res_ctx,
2092 					pipe, hsplit_pipe))
2093 				goto validate_fail;
2094 			pipe_split_from[hsplit_pipe->pipe_idx] = pipe_idx;
2095 			dcn20_build_mapped_resource(dc, context, pipe->stream);
2096 		}
2097 
2098 		if (!pipe->plane_state)
2099 			continue;
2100 		/* Skip 2nd half of already split pipe */
2101 		if (pipe->top_pipe && pipe->plane_state == pipe->top_pipe->plane_state)
2102 			continue;
2103 
2104 		/* We do not support mpo + odm at the moment */
2105 		if (hsplit_pipe && hsplit_pipe->plane_state != pipe->plane_state
2106 				&& context->bw_ctx.dml.vba.ODMCombineEnabled[pipe_idx])
2107 			goto validate_fail;
2108 
2109 		if (split[i] == 2) {
2110 			if (!hsplit_pipe || hsplit_pipe->plane_state != pipe->plane_state) {
2111 				/* pipe not split previously needs split */
2112 				hsplit_pipe = dcn20_find_secondary_pipe(dc, &context->res_ctx, dc->res_pool, pipe);
2113 				ASSERT(hsplit_pipe);
2114 				if (!hsplit_pipe) {
2115 					DC_FP_START();
2116 					dcn20_fpu_adjust_dppclk(&context->bw_ctx.dml.vba, vlevel, context->bw_ctx.dml.vba.maxMpcComb, pipe_idx, true);
2117 					DC_FP_END();
2118 					continue;
2119 				}
2120 				if (context->bw_ctx.dml.vba.ODMCombineEnabled[pipe_idx]) {
2121 					if (!dcn20_split_stream_for_odm(
2122 							dc, &context->res_ctx,
2123 							pipe, hsplit_pipe))
2124 						goto validate_fail;
2125 					dcn20_build_mapped_resource(dc, context, pipe->stream);
2126 				} else {
2127 					dcn20_split_stream_for_mpc(
2128 							&context->res_ctx, dc->res_pool,
2129 							pipe, hsplit_pipe);
2130 					resource_build_scaling_params(pipe);
2131 					resource_build_scaling_params(hsplit_pipe);
2132 				}
2133 				pipe_split_from[hsplit_pipe->pipe_idx] = pipe_idx;
2134 			}
2135 		} else if (hsplit_pipe && hsplit_pipe->plane_state == pipe->plane_state) {
2136 			/* merge should already have been done */
2137 			ASSERT(0);
2138 		}
2139 	}
2140 	/* Actual dsc count per stream dsc validation*/
2141 	if (!dcn20_validate_dsc(dc, context)) {
2142 		context->bw_ctx.dml.vba.ValidationStatus[context->bw_ctx.dml.vba.soc.num_states] =
2143 				DML_FAIL_DSC_VALIDATION_FAILURE;
2144 		goto validate_fail;
2145 	}
2146 
2147 	*vlevel_out = vlevel;
2148 
2149 	out = true;
2150 	goto validate_out;
2151 
2152 validate_fail:
2153 	out = false;
2154 
2155 validate_out:
2156 	return out;
2157 }
2158 
2159 bool dcn20_validate_bandwidth(struct dc *dc, struct dc_state *context,
2160 		bool fast_validate)
2161 {
2162 	bool voltage_supported;
2163 	DC_FP_START();
2164 	voltage_supported = dcn20_validate_bandwidth_fp(dc, context, fast_validate);
2165 	DC_FP_END();
2166 	return voltage_supported;
2167 }
2168 
2169 struct pipe_ctx *dcn20_acquire_idle_pipe_for_layer(
2170 		struct dc_state *state,
2171 		const struct resource_pool *pool,
2172 		struct dc_stream_state *stream)
2173 {
2174 	struct resource_context *res_ctx = &state->res_ctx;
2175 	struct pipe_ctx *head_pipe = resource_get_head_pipe_for_stream(res_ctx, stream);
2176 	struct pipe_ctx *idle_pipe = find_idle_secondary_pipe(res_ctx, pool, head_pipe);
2177 
2178 	if (!head_pipe)
2179 		ASSERT(0);
2180 
2181 	if (!idle_pipe)
2182 		return NULL;
2183 
2184 	idle_pipe->stream = head_pipe->stream;
2185 	idle_pipe->stream_res.tg = head_pipe->stream_res.tg;
2186 	idle_pipe->stream_res.opp = head_pipe->stream_res.opp;
2187 
2188 	idle_pipe->plane_res.hubp = pool->hubps[idle_pipe->pipe_idx];
2189 	idle_pipe->plane_res.ipp = pool->ipps[idle_pipe->pipe_idx];
2190 	idle_pipe->plane_res.dpp = pool->dpps[idle_pipe->pipe_idx];
2191 	idle_pipe->plane_res.mpcc_inst = pool->dpps[idle_pipe->pipe_idx]->inst;
2192 
2193 	return idle_pipe;
2194 }
2195 
2196 bool dcn20_get_dcc_compression_cap(const struct dc *dc,
2197 		const struct dc_dcc_surface_param *input,
2198 		struct dc_surface_dcc_cap *output)
2199 {
2200 	return dc->res_pool->hubbub->funcs->get_dcc_compression_cap(
2201 			dc->res_pool->hubbub,
2202 			input,
2203 			output);
2204 }
2205 
2206 static void dcn20_destroy_resource_pool(struct resource_pool **pool)
2207 {
2208 	struct dcn20_resource_pool *dcn20_pool = TO_DCN20_RES_POOL(*pool);
2209 
2210 	dcn20_resource_destruct(dcn20_pool);
2211 	kfree(dcn20_pool);
2212 	*pool = NULL;
2213 }
2214 
2215 
2216 static struct dc_cap_funcs cap_funcs = {
2217 	.get_dcc_compression_cap = dcn20_get_dcc_compression_cap
2218 };
2219 
2220 
2221 enum dc_status dcn20_patch_unknown_plane_state(struct dc_plane_state *plane_state)
2222 {
2223 	enum surface_pixel_format surf_pix_format = plane_state->format;
2224 	unsigned int bpp = resource_pixel_format_to_bpp(surf_pix_format);
2225 
2226 	plane_state->tiling_info.gfx9.swizzle = DC_SW_64KB_S;
2227 	if (bpp == 64)
2228 		plane_state->tiling_info.gfx9.swizzle = DC_SW_64KB_D;
2229 
2230 	return DC_OK;
2231 }
2232 
2233 static const struct resource_funcs dcn20_res_pool_funcs = {
2234 	.destroy = dcn20_destroy_resource_pool,
2235 	.link_enc_create = dcn20_link_encoder_create,
2236 	.panel_cntl_create = dcn20_panel_cntl_create,
2237 	.validate_bandwidth = dcn20_validate_bandwidth,
2238 	.acquire_idle_pipe_for_layer = dcn20_acquire_idle_pipe_for_layer,
2239 	.add_stream_to_ctx = dcn20_add_stream_to_ctx,
2240 	.add_dsc_to_stream_resource = dcn20_add_dsc_to_stream_resource,
2241 	.remove_stream_from_ctx = dcn20_remove_stream_from_ctx,
2242 	.populate_dml_writeback_from_context = dcn20_populate_dml_writeback_from_context,
2243 	.patch_unknown_plane_state = dcn20_patch_unknown_plane_state,
2244 	.set_mcif_arb_params = dcn20_set_mcif_arb_params,
2245 	.populate_dml_pipes = dcn20_populate_dml_pipes_from_context,
2246 	.find_first_free_match_stream_enc_for_link = dcn10_find_first_free_match_stream_enc_for_link
2247 };
2248 
2249 bool dcn20_dwbc_create(struct dc_context *ctx, struct resource_pool *pool)
2250 {
2251 	int i;
2252 	uint32_t pipe_count = pool->res_cap->num_dwb;
2253 
2254 	for (i = 0; i < pipe_count; i++) {
2255 		struct dcn20_dwbc *dwbc20 = kzalloc(sizeof(struct dcn20_dwbc),
2256 						    GFP_KERNEL);
2257 
2258 		if (!dwbc20) {
2259 			dm_error("DC: failed to create dwbc20!\n");
2260 			return false;
2261 		}
2262 		dcn20_dwbc_construct(dwbc20, ctx,
2263 				&dwbc20_regs[i],
2264 				&dwbc20_shift,
2265 				&dwbc20_mask,
2266 				i);
2267 		pool->dwbc[i] = &dwbc20->base;
2268 	}
2269 	return true;
2270 }
2271 
2272 bool dcn20_mmhubbub_create(struct dc_context *ctx, struct resource_pool *pool)
2273 {
2274 	int i;
2275 	uint32_t pipe_count = pool->res_cap->num_dwb;
2276 
2277 	ASSERT(pipe_count > 0);
2278 
2279 	for (i = 0; i < pipe_count; i++) {
2280 		struct dcn20_mmhubbub *mcif_wb20 = kzalloc(sizeof(struct dcn20_mmhubbub),
2281 						    GFP_KERNEL);
2282 
2283 		if (!mcif_wb20) {
2284 			dm_error("DC: failed to create mcif_wb20!\n");
2285 			return false;
2286 		}
2287 
2288 		dcn20_mmhubbub_construct(mcif_wb20, ctx,
2289 				&mcif_wb20_regs[i],
2290 				&mcif_wb20_shift,
2291 				&mcif_wb20_mask,
2292 				i);
2293 
2294 		pool->mcif_wb[i] = &mcif_wb20->base;
2295 	}
2296 	return true;
2297 }
2298 
2299 static struct pp_smu_funcs *dcn20_pp_smu_create(struct dc_context *ctx)
2300 {
2301 	struct pp_smu_funcs *pp_smu = kzalloc(sizeof(*pp_smu), GFP_ATOMIC);
2302 
2303 	if (!pp_smu)
2304 		return pp_smu;
2305 
2306 	dm_pp_get_funcs(ctx, pp_smu);
2307 
2308 	if (pp_smu->ctx.ver != PP_SMU_VER_NV)
2309 		pp_smu = memset(pp_smu, 0, sizeof(struct pp_smu_funcs));
2310 
2311 	return pp_smu;
2312 }
2313 
2314 static void dcn20_pp_smu_destroy(struct pp_smu_funcs **pp_smu)
2315 {
2316 	if (pp_smu && *pp_smu) {
2317 		kfree(*pp_smu);
2318 		*pp_smu = NULL;
2319 	}
2320 }
2321 
2322 static struct _vcs_dpi_soc_bounding_box_st *get_asic_rev_soc_bb(
2323 	uint32_t hw_internal_rev)
2324 {
2325 	if (ASICREV_IS_NAVI14_M(hw_internal_rev))
2326 		return &dcn2_0_nv14_soc;
2327 
2328 	if (ASICREV_IS_NAVI12_P(hw_internal_rev))
2329 		return &dcn2_0_nv12_soc;
2330 
2331 	return &dcn2_0_soc;
2332 }
2333 
2334 static struct _vcs_dpi_ip_params_st *get_asic_rev_ip_params(
2335 	uint32_t hw_internal_rev)
2336 {
2337 	/* NV14 */
2338 	if (ASICREV_IS_NAVI14_M(hw_internal_rev))
2339 		return &dcn2_0_nv14_ip;
2340 
2341 	/* NV12 and NV10 */
2342 	return &dcn2_0_ip;
2343 }
2344 
2345 static enum dml_project get_dml_project_version(uint32_t hw_internal_rev)
2346 {
2347 	return DML_PROJECT_NAVI10v2;
2348 }
2349 
2350 static bool init_soc_bounding_box(struct dc *dc,
2351 				  struct dcn20_resource_pool *pool)
2352 {
2353 	struct _vcs_dpi_soc_bounding_box_st *loaded_bb =
2354 			get_asic_rev_soc_bb(dc->ctx->asic_id.hw_internal_rev);
2355 	struct _vcs_dpi_ip_params_st *loaded_ip =
2356 			get_asic_rev_ip_params(dc->ctx->asic_id.hw_internal_rev);
2357 
2358 	DC_LOGGER_INIT(dc->ctx->logger);
2359 
2360 	if (pool->base.pp_smu) {
2361 		struct pp_smu_nv_clock_table max_clocks = {0};
2362 		unsigned int uclk_states[8] = {0};
2363 		unsigned int num_states = 0;
2364 		enum pp_smu_status status;
2365 		bool clock_limits_available = false;
2366 		bool uclk_states_available = false;
2367 
2368 		if (pool->base.pp_smu->nv_funcs.get_uclk_dpm_states) {
2369 			status = (pool->base.pp_smu->nv_funcs.get_uclk_dpm_states)
2370 				(&pool->base.pp_smu->nv_funcs.pp_smu, uclk_states, &num_states);
2371 
2372 			uclk_states_available = (status == PP_SMU_RESULT_OK);
2373 		}
2374 
2375 		if (pool->base.pp_smu->nv_funcs.get_maximum_sustainable_clocks) {
2376 			status = (*pool->base.pp_smu->nv_funcs.get_maximum_sustainable_clocks)
2377 					(&pool->base.pp_smu->nv_funcs.pp_smu, &max_clocks);
2378 			/* SMU cannot set DCF clock to anything equal to or higher than SOC clock
2379 			 */
2380 			if (max_clocks.dcfClockInKhz >= max_clocks.socClockInKhz)
2381 				max_clocks.dcfClockInKhz = max_clocks.socClockInKhz - 1000;
2382 			clock_limits_available = (status == PP_SMU_RESULT_OK);
2383 		}
2384 
2385 		if (clock_limits_available && uclk_states_available && num_states) {
2386 			DC_FP_START();
2387 			dcn20_update_bounding_box(dc, loaded_bb, &max_clocks, uclk_states, num_states);
2388 			DC_FP_END();
2389 		} else if (clock_limits_available) {
2390 			DC_FP_START();
2391 			dcn20_cap_soc_clocks(loaded_bb, max_clocks);
2392 			DC_FP_END();
2393 		}
2394 	}
2395 
2396 	loaded_ip->max_num_otg = pool->base.res_cap->num_timing_generator;
2397 	loaded_ip->max_num_dpp = pool->base.pipe_count;
2398 	DC_FP_START();
2399 	dcn20_patch_bounding_box(dc, loaded_bb);
2400 	DC_FP_END();
2401 	return true;
2402 }
2403 
2404 static bool dcn20_resource_construct(
2405 	uint8_t num_virtual_links,
2406 	struct dc *dc,
2407 	struct dcn20_resource_pool *pool)
2408 {
2409 	int i;
2410 	struct dc_context *ctx = dc->ctx;
2411 	struct irq_service_init_data init_data;
2412 	struct ddc_service_init_data ddc_init_data = {0};
2413 	struct _vcs_dpi_soc_bounding_box_st *loaded_bb =
2414 			get_asic_rev_soc_bb(ctx->asic_id.hw_internal_rev);
2415 	struct _vcs_dpi_ip_params_st *loaded_ip =
2416 			get_asic_rev_ip_params(ctx->asic_id.hw_internal_rev);
2417 	enum dml_project dml_project_version =
2418 			get_dml_project_version(ctx->asic_id.hw_internal_rev);
2419 
2420 	ctx->dc_bios->regs = &bios_regs;
2421 	pool->base.funcs = &dcn20_res_pool_funcs;
2422 
2423 	if (ASICREV_IS_NAVI14_M(ctx->asic_id.hw_internal_rev)) {
2424 		pool->base.res_cap = &res_cap_nv14;
2425 		pool->base.pipe_count = 5;
2426 		pool->base.mpcc_count = 5;
2427 	} else {
2428 		pool->base.res_cap = &res_cap_nv10;
2429 		pool->base.pipe_count = 6;
2430 		pool->base.mpcc_count = 6;
2431 	}
2432 	/*************************************************
2433 	 *  Resource + asic cap harcoding                *
2434 	 *************************************************/
2435 	pool->base.underlay_pipe_index = NO_UNDERLAY_PIPE;
2436 
2437 	dc->caps.max_downscale_ratio = 200;
2438 	dc->caps.i2c_speed_in_khz = 100;
2439 	dc->caps.i2c_speed_in_khz_hdcp = 100; /*1.4 w/a not applied by default*/
2440 	dc->caps.max_cursor_size = 256;
2441 	dc->caps.min_horizontal_blanking_period = 80;
2442 	dc->caps.dmdata_alloc_size = 2048;
2443 
2444 	dc->caps.max_slave_planes = 1;
2445 	dc->caps.max_slave_yuv_planes = 1;
2446 	dc->caps.max_slave_rgb_planes = 1;
2447 	dc->caps.post_blend_color_processing = true;
2448 	dc->caps.force_dp_tps4_for_cp2520 = true;
2449 	dc->caps.extended_aux_timeout_support = true;
2450 
2451 	/* Color pipeline capabilities */
2452 	dc->caps.color.dpp.dcn_arch = 1;
2453 	dc->caps.color.dpp.input_lut_shared = 0;
2454 	dc->caps.color.dpp.icsc = 1;
2455 	dc->caps.color.dpp.dgam_ram = 1;
2456 	dc->caps.color.dpp.dgam_rom_caps.srgb = 1;
2457 	dc->caps.color.dpp.dgam_rom_caps.bt2020 = 1;
2458 	dc->caps.color.dpp.dgam_rom_caps.gamma2_2 = 0;
2459 	dc->caps.color.dpp.dgam_rom_caps.pq = 0;
2460 	dc->caps.color.dpp.dgam_rom_caps.hlg = 0;
2461 	dc->caps.color.dpp.post_csc = 0;
2462 	dc->caps.color.dpp.gamma_corr = 0;
2463 	dc->caps.color.dpp.dgam_rom_for_yuv = 1;
2464 
2465 	dc->caps.color.dpp.hw_3d_lut = 1;
2466 	dc->caps.color.dpp.ogam_ram = 1;
2467 	// no OGAM ROM on DCN2, only MPC ROM
2468 	dc->caps.color.dpp.ogam_rom_caps.srgb = 0;
2469 	dc->caps.color.dpp.ogam_rom_caps.bt2020 = 0;
2470 	dc->caps.color.dpp.ogam_rom_caps.gamma2_2 = 0;
2471 	dc->caps.color.dpp.ogam_rom_caps.pq = 0;
2472 	dc->caps.color.dpp.ogam_rom_caps.hlg = 0;
2473 	dc->caps.color.dpp.ocsc = 0;
2474 
2475 	dc->caps.color.mpc.gamut_remap = 0;
2476 	dc->caps.color.mpc.num_3dluts = 0;
2477 	dc->caps.color.mpc.shared_3d_lut = 0;
2478 	dc->caps.color.mpc.ogam_ram = 1;
2479 	dc->caps.color.mpc.ogam_rom_caps.srgb = 0;
2480 	dc->caps.color.mpc.ogam_rom_caps.bt2020 = 0;
2481 	dc->caps.color.mpc.ogam_rom_caps.gamma2_2 = 0;
2482 	dc->caps.color.mpc.ogam_rom_caps.pq = 0;
2483 	dc->caps.color.mpc.ogam_rom_caps.hlg = 0;
2484 	dc->caps.color.mpc.ocsc = 1;
2485 
2486 	dc->caps.dp_hdmi21_pcon_support = true;
2487 
2488 	if (dc->ctx->dce_environment == DCE_ENV_PRODUCTION_DRV) {
2489 		dc->debug = debug_defaults_drv;
2490 	} else if (dc->ctx->dce_environment == DCE_ENV_FPGA_MAXIMUS) {
2491 		pool->base.pipe_count = 4;
2492 		pool->base.mpcc_count = pool->base.pipe_count;
2493 		dc->debug = debug_defaults_diags;
2494 	} else {
2495 		dc->debug = debug_defaults_diags;
2496 	}
2497 	//dcn2.0x
2498 	dc->work_arounds.dedcn20_305_wa = true;
2499 
2500 	// Init the vm_helper
2501 	if (dc->vm_helper)
2502 		vm_helper_init(dc->vm_helper, 16);
2503 
2504 	/*************************************************
2505 	 *  Create resources                             *
2506 	 *************************************************/
2507 
2508 	pool->base.clock_sources[DCN20_CLK_SRC_PLL0] =
2509 			dcn20_clock_source_create(ctx, ctx->dc_bios,
2510 				CLOCK_SOURCE_COMBO_PHY_PLL0,
2511 				&clk_src_regs[0], false);
2512 	pool->base.clock_sources[DCN20_CLK_SRC_PLL1] =
2513 			dcn20_clock_source_create(ctx, ctx->dc_bios,
2514 				CLOCK_SOURCE_COMBO_PHY_PLL1,
2515 				&clk_src_regs[1], false);
2516 	pool->base.clock_sources[DCN20_CLK_SRC_PLL2] =
2517 			dcn20_clock_source_create(ctx, ctx->dc_bios,
2518 				CLOCK_SOURCE_COMBO_PHY_PLL2,
2519 				&clk_src_regs[2], false);
2520 	pool->base.clock_sources[DCN20_CLK_SRC_PLL3] =
2521 			dcn20_clock_source_create(ctx, ctx->dc_bios,
2522 				CLOCK_SOURCE_COMBO_PHY_PLL3,
2523 				&clk_src_regs[3], false);
2524 	pool->base.clock_sources[DCN20_CLK_SRC_PLL4] =
2525 			dcn20_clock_source_create(ctx, ctx->dc_bios,
2526 				CLOCK_SOURCE_COMBO_PHY_PLL4,
2527 				&clk_src_regs[4], false);
2528 	pool->base.clock_sources[DCN20_CLK_SRC_PLL5] =
2529 			dcn20_clock_source_create(ctx, ctx->dc_bios,
2530 				CLOCK_SOURCE_COMBO_PHY_PLL5,
2531 				&clk_src_regs[5], false);
2532 	pool->base.clk_src_count = DCN20_CLK_SRC_TOTAL;
2533 	/* todo: not reuse phy_pll registers */
2534 	pool->base.dp_clock_source =
2535 			dcn20_clock_source_create(ctx, ctx->dc_bios,
2536 				CLOCK_SOURCE_ID_DP_DTO,
2537 				&clk_src_regs[0], true);
2538 
2539 	for (i = 0; i < pool->base.clk_src_count; i++) {
2540 		if (pool->base.clock_sources[i] == NULL) {
2541 			dm_error("DC: failed to create clock sources!\n");
2542 			BREAK_TO_DEBUGGER();
2543 			goto create_fail;
2544 		}
2545 	}
2546 
2547 	pool->base.dccg = dccg2_create(ctx, &dccg_regs, &dccg_shift, &dccg_mask);
2548 	if (pool->base.dccg == NULL) {
2549 		dm_error("DC: failed to create dccg!\n");
2550 		BREAK_TO_DEBUGGER();
2551 		goto create_fail;
2552 	}
2553 
2554 	pool->base.dmcu = dcn20_dmcu_create(ctx,
2555 			&dmcu_regs,
2556 			&dmcu_shift,
2557 			&dmcu_mask);
2558 	if (pool->base.dmcu == NULL) {
2559 		dm_error("DC: failed to create dmcu!\n");
2560 		BREAK_TO_DEBUGGER();
2561 		goto create_fail;
2562 	}
2563 
2564 	pool->base.abm = dce_abm_create(ctx,
2565 			&abm_regs,
2566 			&abm_shift,
2567 			&abm_mask);
2568 	if (pool->base.abm == NULL) {
2569 		dm_error("DC: failed to create abm!\n");
2570 		BREAK_TO_DEBUGGER();
2571 		goto create_fail;
2572 	}
2573 
2574 	pool->base.pp_smu = dcn20_pp_smu_create(ctx);
2575 
2576 
2577 	if (!init_soc_bounding_box(dc, pool)) {
2578 		dm_error("DC: failed to initialize soc bounding box!\n");
2579 		BREAK_TO_DEBUGGER();
2580 		goto create_fail;
2581 	}
2582 
2583 	dml_init_instance(&dc->dml, loaded_bb, loaded_ip, dml_project_version);
2584 
2585 	if (!dc->debug.disable_pplib_wm_range) {
2586 		struct pp_smu_wm_range_sets ranges = {0};
2587 		int i = 0;
2588 
2589 		ranges.num_reader_wm_sets = 0;
2590 
2591 		if (loaded_bb->num_states == 1) {
2592 			ranges.reader_wm_sets[0].wm_inst = i;
2593 			ranges.reader_wm_sets[0].min_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
2594 			ranges.reader_wm_sets[0].max_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
2595 			ranges.reader_wm_sets[0].min_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
2596 			ranges.reader_wm_sets[0].max_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
2597 
2598 			ranges.num_reader_wm_sets = 1;
2599 		} else if (loaded_bb->num_states > 1) {
2600 			for (i = 0; i < 4 && i < loaded_bb->num_states; i++) {
2601 				ranges.reader_wm_sets[i].wm_inst = i;
2602 				ranges.reader_wm_sets[i].min_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
2603 				ranges.reader_wm_sets[i].max_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
2604 				DC_FP_START();
2605 				dcn20_fpu_set_wm_ranges(i, &ranges, loaded_bb);
2606 				DC_FP_END();
2607 
2608 				ranges.num_reader_wm_sets = i + 1;
2609 			}
2610 
2611 			ranges.reader_wm_sets[0].min_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
2612 			ranges.reader_wm_sets[ranges.num_reader_wm_sets - 1].max_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
2613 		}
2614 
2615 		ranges.num_writer_wm_sets = 1;
2616 
2617 		ranges.writer_wm_sets[0].wm_inst = 0;
2618 		ranges.writer_wm_sets[0].min_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
2619 		ranges.writer_wm_sets[0].max_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
2620 		ranges.writer_wm_sets[0].min_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
2621 		ranges.writer_wm_sets[0].max_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
2622 
2623 		/* Notify PP Lib/SMU which Watermarks to use for which clock ranges */
2624 		if (pool->base.pp_smu->nv_funcs.set_wm_ranges)
2625 			pool->base.pp_smu->nv_funcs.set_wm_ranges(&pool->base.pp_smu->nv_funcs.pp_smu, &ranges);
2626 	}
2627 
2628 	init_data.ctx = dc->ctx;
2629 	pool->base.irqs = dal_irq_service_dcn20_create(&init_data);
2630 	if (!pool->base.irqs)
2631 		goto create_fail;
2632 
2633 	/* mem input -> ipp -> dpp -> opp -> TG */
2634 	for (i = 0; i < pool->base.pipe_count; i++) {
2635 		pool->base.hubps[i] = dcn20_hubp_create(ctx, i);
2636 		if (pool->base.hubps[i] == NULL) {
2637 			BREAK_TO_DEBUGGER();
2638 			dm_error(
2639 				"DC: failed to create memory input!\n");
2640 			goto create_fail;
2641 		}
2642 
2643 		pool->base.ipps[i] = dcn20_ipp_create(ctx, i);
2644 		if (pool->base.ipps[i] == NULL) {
2645 			BREAK_TO_DEBUGGER();
2646 			dm_error(
2647 				"DC: failed to create input pixel processor!\n");
2648 			goto create_fail;
2649 		}
2650 
2651 		pool->base.dpps[i] = dcn20_dpp_create(ctx, i);
2652 		if (pool->base.dpps[i] == NULL) {
2653 			BREAK_TO_DEBUGGER();
2654 			dm_error(
2655 				"DC: failed to create dpps!\n");
2656 			goto create_fail;
2657 		}
2658 	}
2659 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
2660 		pool->base.engines[i] = dcn20_aux_engine_create(ctx, i);
2661 		if (pool->base.engines[i] == NULL) {
2662 			BREAK_TO_DEBUGGER();
2663 			dm_error(
2664 				"DC:failed to create aux engine!!\n");
2665 			goto create_fail;
2666 		}
2667 		pool->base.hw_i2cs[i] = dcn20_i2c_hw_create(ctx, i);
2668 		if (pool->base.hw_i2cs[i] == NULL) {
2669 			BREAK_TO_DEBUGGER();
2670 			dm_error(
2671 				"DC:failed to create hw i2c!!\n");
2672 			goto create_fail;
2673 		}
2674 		pool->base.sw_i2cs[i] = NULL;
2675 	}
2676 
2677 	for (i = 0; i < pool->base.res_cap->num_opp; i++) {
2678 		pool->base.opps[i] = dcn20_opp_create(ctx, i);
2679 		if (pool->base.opps[i] == NULL) {
2680 			BREAK_TO_DEBUGGER();
2681 			dm_error(
2682 				"DC: failed to create output pixel processor!\n");
2683 			goto create_fail;
2684 		}
2685 	}
2686 
2687 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
2688 		pool->base.timing_generators[i] = dcn20_timing_generator_create(
2689 				ctx, i);
2690 		if (pool->base.timing_generators[i] == NULL) {
2691 			BREAK_TO_DEBUGGER();
2692 			dm_error("DC: failed to create tg!\n");
2693 			goto create_fail;
2694 		}
2695 	}
2696 
2697 	pool->base.timing_generator_count = i;
2698 
2699 	pool->base.mpc = dcn20_mpc_create(ctx);
2700 	if (pool->base.mpc == NULL) {
2701 		BREAK_TO_DEBUGGER();
2702 		dm_error("DC: failed to create mpc!\n");
2703 		goto create_fail;
2704 	}
2705 
2706 	pool->base.hubbub = dcn20_hubbub_create(ctx);
2707 	if (pool->base.hubbub == NULL) {
2708 		BREAK_TO_DEBUGGER();
2709 		dm_error("DC: failed to create hubbub!\n");
2710 		goto create_fail;
2711 	}
2712 
2713 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
2714 		pool->base.dscs[i] = dcn20_dsc_create(ctx, i);
2715 		if (pool->base.dscs[i] == NULL) {
2716 			BREAK_TO_DEBUGGER();
2717 			dm_error("DC: failed to create display stream compressor %d!\n", i);
2718 			goto create_fail;
2719 		}
2720 	}
2721 
2722 	if (!dcn20_dwbc_create(ctx, &pool->base)) {
2723 		BREAK_TO_DEBUGGER();
2724 		dm_error("DC: failed to create dwbc!\n");
2725 		goto create_fail;
2726 	}
2727 	if (!dcn20_mmhubbub_create(ctx, &pool->base)) {
2728 		BREAK_TO_DEBUGGER();
2729 		dm_error("DC: failed to create mcif_wb!\n");
2730 		goto create_fail;
2731 	}
2732 
2733 	if (!resource_construct(num_virtual_links, dc, &pool->base,
2734 			(!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment) ?
2735 			&res_create_funcs : &res_create_maximus_funcs)))
2736 			goto create_fail;
2737 
2738 	dcn20_hw_sequencer_construct(dc);
2739 
2740 	// IF NV12, set PG function pointer to NULL. It's not that
2741 	// PG isn't supported for NV12, it's that we don't want to
2742 	// program the registers because that will cause more power
2743 	// to be consumed. We could have created dcn20_init_hw to get
2744 	// the same effect by checking ASIC rev, but there was a
2745 	// request at some point to not check ASIC rev on hw sequencer.
2746 	if (ASICREV_IS_NAVI12_P(dc->ctx->asic_id.hw_internal_rev)) {
2747 		dc->hwseq->funcs.enable_power_gating_plane = NULL;
2748 		dc->debug.disable_dpp_power_gate = true;
2749 		dc->debug.disable_hubp_power_gate = true;
2750 	}
2751 
2752 
2753 	dc->caps.max_planes =  pool->base.pipe_count;
2754 
2755 	for (i = 0; i < dc->caps.max_planes; ++i)
2756 		dc->caps.planes[i] = plane_cap;
2757 
2758 	dc->cap_funcs = cap_funcs;
2759 
2760 	if (dc->ctx->dc_bios->fw_info.oem_i2c_present) {
2761 		ddc_init_data.ctx = dc->ctx;
2762 		ddc_init_data.link = NULL;
2763 		ddc_init_data.id.id = dc->ctx->dc_bios->fw_info.oem_i2c_obj_id;
2764 		ddc_init_data.id.enum_id = 0;
2765 		ddc_init_data.id.type = OBJECT_TYPE_GENERIC;
2766 		pool->base.oem_device = link_create_ddc_service(&ddc_init_data);
2767 	} else {
2768 		pool->base.oem_device = NULL;
2769 	}
2770 
2771 	return true;
2772 
2773 create_fail:
2774 
2775 	dcn20_resource_destruct(pool);
2776 
2777 	return false;
2778 }
2779 
2780 struct resource_pool *dcn20_create_resource_pool(
2781 		const struct dc_init_data *init_data,
2782 		struct dc *dc)
2783 {
2784 	struct dcn20_resource_pool *pool =
2785 		kzalloc(sizeof(struct dcn20_resource_pool), GFP_ATOMIC);
2786 
2787 	if (!pool)
2788 		return NULL;
2789 
2790 	if (dcn20_resource_construct(init_data->num_virtual_links, dc, pool))
2791 		return &pool->base;
2792 
2793 	BREAK_TO_DEBUGGER();
2794 	kfree(pool);
2795 	return NULL;
2796 }
2797