1 /*
2  * Copyright 2019-2021 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 
27 #include "dm_services.h"
28 #include "dc.h"
29 
30 #include "dcn301_init.h"
31 
32 #include "resource.h"
33 #include "include/irq_service_interface.h"
34 #include "dcn30/dcn30_resource.h"
35 #include "dcn301_resource.h"
36 
37 #include "dcn20/dcn20_resource.h"
38 
39 #include "dcn10/dcn10_ipp.h"
40 #include "dcn301/dcn301_hubbub.h"
41 #include "dcn30/dcn30_mpc.h"
42 #include "dcn30/dcn30_hubp.h"
43 #include "irq/dcn30/irq_service_dcn30.h"
44 #include "dcn30/dcn30_dpp.h"
45 #include "dcn30/dcn30_optc.h"
46 #include "dcn20/dcn20_hwseq.h"
47 #include "dcn30/dcn30_hwseq.h"
48 #include "dce110/dce110_hw_sequencer.h"
49 #include "dcn30/dcn30_opp.h"
50 #include "dcn20/dcn20_dsc.h"
51 #include "dcn30/dcn30_vpg.h"
52 #include "dcn30/dcn30_afmt.h"
53 #include "dce/dce_clock_source.h"
54 #include "dce/dce_audio.h"
55 #include "dce/dce_hwseq.h"
56 #include "clk_mgr.h"
57 #include "virtual/virtual_stream_encoder.h"
58 #include "dce110/dce110_resource.h"
59 #include "dml/display_mode_vba.h"
60 #include "dcn301/dcn301_dccg.h"
61 #include "dcn10/dcn10_resource.h"
62 #include "dcn30/dcn30_dio_stream_encoder.h"
63 #include "dcn301/dcn301_dio_link_encoder.h"
64 #include "dcn301_panel_cntl.h"
65 
66 #include "vangogh_ip_offset.h"
67 
68 #include "dcn30/dcn30_dwb.h"
69 #include "dcn30/dcn30_mmhubbub.h"
70 
71 #include "dcn/dcn_3_0_1_offset.h"
72 #include "dcn/dcn_3_0_1_sh_mask.h"
73 
74 #include "nbio/nbio_7_2_0_offset.h"
75 
76 #include "dpcs/dpcs_3_0_0_offset.h"
77 #include "dpcs/dpcs_3_0_0_sh_mask.h"
78 
79 #include "reg_helper.h"
80 #include "dce/dmub_abm.h"
81 #include "dce/dce_aux.h"
82 #include "dce/dce_i2c.h"
83 
84 #include "dml/dcn30/dcn30_fpu.h"
85 
86 #include "dml/dcn30/display_mode_vba_30.h"
87 #include "dml/dcn301/dcn301_fpu.h"
88 #include "vm_helper.h"
89 #include "dcn20/dcn20_vmid.h"
90 #include "amdgpu_socbb.h"
91 
92 #define TO_DCN301_RES_POOL(pool)\
93 	container_of(pool, struct dcn301_resource_pool, base)
94 
95 #define DC_LOGGER_INIT(logger)
96 
97 enum dcn301_clk_src_array_id {
98 	DCN301_CLK_SRC_PLL0,
99 	DCN301_CLK_SRC_PLL1,
100 	DCN301_CLK_SRC_PLL2,
101 	DCN301_CLK_SRC_PLL3,
102 	DCN301_CLK_SRC_TOTAL
103 };
104 
105 /* begin *********************
106  * macros to expend register list macro defined in HW object header file
107  */
108 
109 /* DCN */
110 #define BASE_INNER(seg) DCN_BASE__INST0_SEG ## seg
111 
112 #define BASE(seg) BASE_INNER(seg)
113 
114 #define SR(reg_name)\
115 		.reg_name = BASE(mm ## reg_name ## _BASE_IDX) +  \
116 					mm ## reg_name
117 
118 #define SRI(reg_name, block, id)\
119 	.reg_name = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
120 					mm ## block ## id ## _ ## reg_name
121 
122 #define SRI2(reg_name, block, id)\
123 	.reg_name = BASE(mm ## reg_name ## _BASE_IDX) + \
124 					mm ## reg_name
125 
126 #define SRIR(var_name, reg_name, block, id)\
127 	.var_name = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
128 					mm ## block ## id ## _ ## reg_name
129 
130 #define SRII(reg_name, block, id)\
131 	.reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
132 					mm ## block ## id ## _ ## reg_name
133 
134 #define SRII2(reg_name_pre, reg_name_post, id)\
135 	.reg_name_pre ## _ ##  reg_name_post[id] = BASE(mm ## reg_name_pre \
136 			## id ## _ ## reg_name_post ## _BASE_IDX) + \
137 			mm ## reg_name_pre ## id ## _ ## reg_name_post
138 
139 #define SRII_MPC_RMU(reg_name, block, id)\
140 	.RMU##_##reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
141 					mm ## block ## id ## _ ## reg_name
142 
143 #define SRII_DWB(reg_name, temp_name, block, id)\
144 	.reg_name[id] = BASE(mm ## block ## id ## _ ## temp_name ## _BASE_IDX) + \
145 					mm ## block ## id ## _ ## temp_name
146 
147 #define SF_DWB2(reg_name, block, id, field_name, post_fix)	\
148 	.field_name = reg_name ## __ ## field_name ## post_fix
149 
150 #define DCCG_SRII(reg_name, block, id)\
151 	.block ## _ ## reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
152 					mm ## block ## id ## _ ## reg_name
153 
154 #define VUPDATE_SRII(reg_name, block, id)\
155 	.reg_name[id] = BASE(mm ## reg_name ## _ ## block ## id ## _BASE_IDX) + \
156 					mm ## reg_name ## _ ## block ## id
157 
158 /* NBIO */
159 #define NBIO_BASE_INNER(seg) \
160 	NBIO_BASE__INST0_SEG ## seg
161 
162 #define NBIO_BASE(seg) \
163 	NBIO_BASE_INNER(seg)
164 
165 #define NBIO_SR(reg_name)\
166 		.reg_name = NBIO_BASE(regBIF_BX0_ ## reg_name ## _BASE_IDX) + \
167 					regBIF_BX0_ ## reg_name
168 
169 /* MMHUB */
170 #define MMHUB_BASE_INNER(seg) \
171 	MMHUB_BASE__INST0_SEG ## seg
172 
173 #define MMHUB_BASE(seg) \
174 	MMHUB_BASE_INNER(seg)
175 
176 #define MMHUB_SR(reg_name)\
177 		.reg_name = MMHUB_BASE(regMM ## reg_name ## _BASE_IDX) + \
178 					regMM ## reg_name
179 
180 /* CLOCK */
181 #define CLK_BASE_INNER(seg) \
182 	CLK_BASE__INST0_SEG ## seg
183 
184 #define CLK_BASE(seg) \
185 	CLK_BASE_INNER(seg)
186 
187 #define CLK_SRI(reg_name, block, inst)\
188 	.reg_name = CLK_BASE(mm ## block ## _ ## inst ## _ ## reg_name ## _BASE_IDX) + \
189 					mm ## block ## _ ## inst ## _ ## reg_name
190 
191 static const struct bios_registers bios_regs = {
192 		NBIO_SR(BIOS_SCRATCH_3),
193 		NBIO_SR(BIOS_SCRATCH_6)
194 };
195 
196 #define clk_src_regs(index, pllid)\
197 [index] = {\
198 	CS_COMMON_REG_LIST_DCN3_01(index, pllid),\
199 }
200 
201 static const struct dce110_clk_src_regs clk_src_regs[] = {
202 	clk_src_regs(0, A),
203 	clk_src_regs(1, B),
204 	clk_src_regs(2, C),
205 	clk_src_regs(3, D)
206 };
207 
208 static const struct dce110_clk_src_shift cs_shift = {
209 		CS_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
210 };
211 
212 static const struct dce110_clk_src_mask cs_mask = {
213 		CS_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
214 };
215 
216 #define abm_regs(id)\
217 [id] = {\
218 		ABM_DCN301_REG_LIST(id)\
219 }
220 
221 static const struct dce_abm_registers abm_regs[] = {
222 		abm_regs(0),
223 		abm_regs(1),
224 		abm_regs(2),
225 		abm_regs(3),
226 };
227 
228 static const struct dce_abm_shift abm_shift = {
229 		ABM_MASK_SH_LIST_DCN30(__SHIFT)
230 };
231 
232 static const struct dce_abm_mask abm_mask = {
233 		ABM_MASK_SH_LIST_DCN30(_MASK)
234 };
235 
236 #define audio_regs(id)\
237 [id] = {\
238 		AUD_COMMON_REG_LIST(id)\
239 }
240 
241 static const struct dce_audio_registers audio_regs[] = {
242 	audio_regs(0),
243 	audio_regs(1),
244 	audio_regs(2),
245 	audio_regs(3),
246 	audio_regs(4),
247 	audio_regs(5),
248 	audio_regs(6)
249 };
250 
251 #define DCE120_AUD_COMMON_MASK_SH_LIST(mask_sh)\
252 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_INDEX, AZALIA_ENDPOINT_REG_INDEX, mask_sh),\
253 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_DATA, AZALIA_ENDPOINT_REG_DATA, mask_sh),\
254 		AUD_COMMON_MASK_SH_LIST_BASE(mask_sh)
255 
256 static const struct dce_audio_shift audio_shift = {
257 		DCE120_AUD_COMMON_MASK_SH_LIST(__SHIFT)
258 };
259 
260 static const struct dce_audio_mask audio_mask = {
261 		DCE120_AUD_COMMON_MASK_SH_LIST(_MASK)
262 };
263 
264 #define vpg_regs(id)\
265 [id] = {\
266 	VPG_DCN3_REG_LIST(id)\
267 }
268 
269 static const struct dcn30_vpg_registers vpg_regs[] = {
270 	vpg_regs(0),
271 	vpg_regs(1),
272 	vpg_regs(2),
273 	vpg_regs(3),
274 };
275 
276 static const struct dcn30_vpg_shift vpg_shift = {
277 	DCN3_VPG_MASK_SH_LIST(__SHIFT)
278 };
279 
280 static const struct dcn30_vpg_mask vpg_mask = {
281 	DCN3_VPG_MASK_SH_LIST(_MASK)
282 };
283 
284 #define afmt_regs(id)\
285 [id] = {\
286 	AFMT_DCN3_REG_LIST(id)\
287 }
288 
289 static const struct dcn30_afmt_registers afmt_regs[] = {
290 	afmt_regs(0),
291 	afmt_regs(1),
292 	afmt_regs(2),
293 	afmt_regs(3),
294 };
295 
296 static const struct dcn30_afmt_shift afmt_shift = {
297 	DCN3_AFMT_MASK_SH_LIST(__SHIFT)
298 };
299 
300 static const struct dcn30_afmt_mask afmt_mask = {
301 	DCN3_AFMT_MASK_SH_LIST(_MASK)
302 };
303 
304 #define stream_enc_regs(id)\
305 [id] = {\
306 	SE_DCN3_REG_LIST(id)\
307 }
308 
309 static const struct dcn10_stream_enc_registers stream_enc_regs[] = {
310 	stream_enc_regs(0),
311 	stream_enc_regs(1),
312 	stream_enc_regs(2),
313 	stream_enc_regs(3),
314 };
315 
316 static const struct dcn10_stream_encoder_shift se_shift = {
317 		SE_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
318 };
319 
320 static const struct dcn10_stream_encoder_mask se_mask = {
321 		SE_COMMON_MASK_SH_LIST_DCN30(_MASK)
322 };
323 
324 
325 #define aux_regs(id)\
326 [id] = {\
327 	DCN2_AUX_REG_LIST(id)\
328 }
329 
330 static const struct dcn10_link_enc_aux_registers link_enc_aux_regs[] = {
331 		aux_regs(0),
332 		aux_regs(1),
333 		aux_regs(2),
334 		aux_regs(3),
335 };
336 
337 #define hpd_regs(id)\
338 [id] = {\
339 	HPD_REG_LIST(id)\
340 }
341 
342 static const struct dcn10_link_enc_hpd_registers link_enc_hpd_regs[] = {
343 		hpd_regs(0),
344 		hpd_regs(1),
345 		hpd_regs(2),
346 		hpd_regs(3),
347 };
348 
349 
350 #define link_regs(id, phyid)\
351 [id] = {\
352 	LE_DCN301_REG_LIST(id), \
353 	UNIPHY_DCN2_REG_LIST(phyid), \
354 	DPCS_DCN2_REG_LIST(id), \
355 	SRI(DP_DPHY_INTERNAL_CTRL, DP, id) \
356 }
357 
358 static const struct dce110_aux_registers_shift aux_shift = {
359 	DCN_AUX_MASK_SH_LIST(__SHIFT)
360 };
361 
362 static const struct dce110_aux_registers_mask aux_mask = {
363 	DCN_AUX_MASK_SH_LIST(_MASK)
364 };
365 
366 static const struct dcn10_link_enc_registers link_enc_regs[] = {
367 	link_regs(0, A),
368 	link_regs(1, B),
369 	link_regs(2, C),
370 	link_regs(3, D),
371 };
372 
373 static const struct dcn10_link_enc_shift le_shift = {
374 	LINK_ENCODER_MASK_SH_LIST_DCN301(__SHIFT),\
375 	DPCS_DCN2_MASK_SH_LIST(__SHIFT)
376 };
377 
378 static const struct dcn10_link_enc_mask le_mask = {
379 	LINK_ENCODER_MASK_SH_LIST_DCN301(_MASK),\
380 	DPCS_DCN2_MASK_SH_LIST(_MASK)
381 };
382 
383 #define panel_cntl_regs(id)\
384 [id] = {\
385 	DCN301_PANEL_CNTL_REG_LIST(id),\
386 }
387 
388 static const struct dce_panel_cntl_registers panel_cntl_regs[] = {
389 	panel_cntl_regs(0),
390 	panel_cntl_regs(1),
391 };
392 
393 static const struct dcn301_panel_cntl_shift panel_cntl_shift = {
394 	DCN301_PANEL_CNTL_MASK_SH_LIST(__SHIFT)
395 };
396 
397 static const struct dcn301_panel_cntl_mask panel_cntl_mask = {
398 	DCN301_PANEL_CNTL_MASK_SH_LIST(_MASK)
399 };
400 
401 #define dpp_regs(id)\
402 [id] = {\
403 	DPP_REG_LIST_DCN30(id),\
404 }
405 
406 static const struct dcn3_dpp_registers dpp_regs[] = {
407 	dpp_regs(0),
408 	dpp_regs(1),
409 	dpp_regs(2),
410 	dpp_regs(3),
411 };
412 
413 static const struct dcn3_dpp_shift tf_shift = {
414 		DPP_REG_LIST_SH_MASK_DCN30(__SHIFT)
415 };
416 
417 static const struct dcn3_dpp_mask tf_mask = {
418 		DPP_REG_LIST_SH_MASK_DCN30(_MASK)
419 };
420 
421 #define opp_regs(id)\
422 [id] = {\
423 	OPP_REG_LIST_DCN30(id),\
424 }
425 
426 static const struct dcn20_opp_registers opp_regs[] = {
427 	opp_regs(0),
428 	opp_regs(1),
429 	opp_regs(2),
430 	opp_regs(3),
431 };
432 
433 static const struct dcn20_opp_shift opp_shift = {
434 	OPP_MASK_SH_LIST_DCN20(__SHIFT)
435 };
436 
437 static const struct dcn20_opp_mask opp_mask = {
438 	OPP_MASK_SH_LIST_DCN20(_MASK)
439 };
440 
441 #define aux_engine_regs(id)\
442 [id] = {\
443 	AUX_COMMON_REG_LIST0(id), \
444 	.AUXN_IMPCAL = 0, \
445 	.AUXP_IMPCAL = 0, \
446 	.AUX_RESET_MASK = DP_AUX0_AUX_CONTROL__AUX_RESET_MASK, \
447 }
448 
449 static const struct dce110_aux_registers aux_engine_regs[] = {
450 		aux_engine_regs(0),
451 		aux_engine_regs(1),
452 		aux_engine_regs(2),
453 		aux_engine_regs(3),
454 };
455 
456 #define dwbc_regs_dcn3(id)\
457 [id] = {\
458 	DWBC_COMMON_REG_LIST_DCN30(id),\
459 }
460 
461 static const struct dcn30_dwbc_registers dwbc30_regs[] = {
462 	dwbc_regs_dcn3(0),
463 };
464 
465 static const struct dcn30_dwbc_shift dwbc30_shift = {
466 	DWBC_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
467 };
468 
469 static const struct dcn30_dwbc_mask dwbc30_mask = {
470 	DWBC_COMMON_MASK_SH_LIST_DCN30(_MASK)
471 };
472 
473 #define mcif_wb_regs_dcn3(id)\
474 [id] = {\
475 	MCIF_WB_COMMON_REG_LIST_DCN30(id),\
476 }
477 
478 static const struct dcn30_mmhubbub_registers mcif_wb30_regs[] = {
479 	mcif_wb_regs_dcn3(0)
480 };
481 
482 static const struct dcn30_mmhubbub_shift mcif_wb30_shift = {
483 	MCIF_WB_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
484 };
485 
486 static const struct dcn30_mmhubbub_mask mcif_wb30_mask = {
487 	MCIF_WB_COMMON_MASK_SH_LIST_DCN30(_MASK)
488 };
489 
490 #define dsc_regsDCN20(id)\
491 [id] = {\
492 	DSC_REG_LIST_DCN20(id)\
493 }
494 
495 static const struct dcn20_dsc_registers dsc_regs[] = {
496 	dsc_regsDCN20(0),
497 	dsc_regsDCN20(1),
498 	dsc_regsDCN20(2),
499 };
500 
501 static const struct dcn20_dsc_shift dsc_shift = {
502 	DSC_REG_LIST_SH_MASK_DCN20(__SHIFT)
503 };
504 
505 static const struct dcn20_dsc_mask dsc_mask = {
506 	DSC_REG_LIST_SH_MASK_DCN20(_MASK)
507 };
508 
509 static const struct dcn30_mpc_registers mpc_regs = {
510 		MPC_REG_LIST_DCN3_0(0),
511 		MPC_REG_LIST_DCN3_0(1),
512 		MPC_REG_LIST_DCN3_0(2),
513 		MPC_REG_LIST_DCN3_0(3),
514 		MPC_OUT_MUX_REG_LIST_DCN3_0(0),
515 		MPC_OUT_MUX_REG_LIST_DCN3_0(1),
516 		MPC_OUT_MUX_REG_LIST_DCN3_0(2),
517 		MPC_OUT_MUX_REG_LIST_DCN3_0(3),
518 		MPC_RMU_GLOBAL_REG_LIST_DCN3AG,
519 		MPC_RMU_REG_LIST_DCN3AG(0),
520 		MPC_RMU_REG_LIST_DCN3AG(1),
521 		MPC_DWB_MUX_REG_LIST_DCN3_0(0),
522 };
523 
524 static const struct dcn30_mpc_shift mpc_shift = {
525 	MPC_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
526 };
527 
528 static const struct dcn30_mpc_mask mpc_mask = {
529 	MPC_COMMON_MASK_SH_LIST_DCN30(_MASK)
530 };
531 
532 #define optc_regs(id)\
533 [id] = {OPTC_COMMON_REG_LIST_DCN3_0(id)}
534 
535 
536 static const struct dcn_optc_registers optc_regs[] = {
537 	optc_regs(0),
538 	optc_regs(1),
539 	optc_regs(2),
540 	optc_regs(3),
541 };
542 
543 static const struct dcn_optc_shift optc_shift = {
544 	OPTC_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
545 };
546 
547 static const struct dcn_optc_mask optc_mask = {
548 	OPTC_COMMON_MASK_SH_LIST_DCN30(_MASK)
549 };
550 
551 #define hubp_regs(id)\
552 [id] = {\
553 	HUBP_REG_LIST_DCN30(id)\
554 }
555 
556 static const struct dcn_hubp2_registers hubp_regs[] = {
557 		hubp_regs(0),
558 		hubp_regs(1),
559 		hubp_regs(2),
560 		hubp_regs(3),
561 };
562 
563 static const struct dcn_hubp2_shift hubp_shift = {
564 		HUBP_MASK_SH_LIST_DCN30(__SHIFT)
565 };
566 
567 static const struct dcn_hubp2_mask hubp_mask = {
568 		HUBP_MASK_SH_LIST_DCN30(_MASK)
569 };
570 
571 static const struct dcn_hubbub_registers hubbub_reg = {
572 		HUBBUB_REG_LIST_DCN301(0)
573 };
574 
575 static const struct dcn_hubbub_shift hubbub_shift = {
576 		HUBBUB_MASK_SH_LIST_DCN301(__SHIFT)
577 };
578 
579 static const struct dcn_hubbub_mask hubbub_mask = {
580 		HUBBUB_MASK_SH_LIST_DCN301(_MASK)
581 };
582 
583 static const struct dccg_registers dccg_regs = {
584 		DCCG_REG_LIST_DCN301()
585 };
586 
587 static const struct dccg_shift dccg_shift = {
588 		DCCG_MASK_SH_LIST_DCN301(__SHIFT)
589 };
590 
591 static const struct dccg_mask dccg_mask = {
592 		DCCG_MASK_SH_LIST_DCN301(_MASK)
593 };
594 
595 static const struct dce_hwseq_registers hwseq_reg = {
596 		HWSEQ_DCN301_REG_LIST()
597 };
598 
599 static const struct dce_hwseq_shift hwseq_shift = {
600 		HWSEQ_DCN301_MASK_SH_LIST(__SHIFT)
601 };
602 
603 static const struct dce_hwseq_mask hwseq_mask = {
604 		HWSEQ_DCN301_MASK_SH_LIST(_MASK)
605 };
606 #define vmid_regs(id)\
607 [id] = {\
608 		DCN20_VMID_REG_LIST(id)\
609 }
610 
611 static const struct dcn_vmid_registers vmid_regs[] = {
612 	vmid_regs(0),
613 	vmid_regs(1),
614 	vmid_regs(2),
615 	vmid_regs(3),
616 	vmid_regs(4),
617 	vmid_regs(5),
618 	vmid_regs(6),
619 	vmid_regs(7),
620 	vmid_regs(8),
621 	vmid_regs(9),
622 	vmid_regs(10),
623 	vmid_regs(11),
624 	vmid_regs(12),
625 	vmid_regs(13),
626 	vmid_regs(14),
627 	vmid_regs(15)
628 };
629 
630 static const struct dcn20_vmid_shift vmid_shifts = {
631 		DCN20_VMID_MASK_SH_LIST(__SHIFT)
632 };
633 
634 static const struct dcn20_vmid_mask vmid_masks = {
635 		DCN20_VMID_MASK_SH_LIST(_MASK)
636 };
637 
638 static struct resource_caps res_cap_dcn301 = {
639 	.num_timing_generator = 4,
640 	.num_opp = 4,
641 	.num_video_plane = 4,
642 	.num_audio = 4,
643 	.num_stream_encoder = 4,
644 	.num_pll = 4,
645 	.num_dwb = 1,
646 	.num_ddc = 4,
647 	.num_vmid = 16,
648 	.num_mpc_3dlut = 2,
649 	.num_dsc = 3,
650 };
651 
652 static const struct dc_plane_cap plane_cap = {
653 	.type = DC_PLANE_TYPE_DCN_UNIVERSAL,
654 	.blends_with_above = true,
655 	.blends_with_below = true,
656 	.per_pixel_alpha = true,
657 
658 	.pixel_format_support = {
659 			.argb8888 = true,
660 			.nv12 = true,
661 			.fp16 = true,
662 			.p010 = true,
663 			.ayuv = false,
664 	},
665 
666 	.max_upscale_factor = {
667 			.argb8888 = 16000,
668 			.nv12 = 16000,
669 			.fp16 = 16000
670 	},
671 
672 	/* 6:1 downscaling ratio: 1000/6 = 166.666 */
673 	.max_downscale_factor = {
674 			.argb8888 = 167,
675 			.nv12 = 167,
676 			.fp16 = 167
677 	},
678 	64,
679 	64
680 };
681 
682 static const struct dc_debug_options debug_defaults_drv = {
683 	.disable_dmcu = true,
684 	.force_abm_enable = false,
685 	.timing_trace = false,
686 	.clock_trace = true,
687 	.disable_dpp_power_gate = false,
688 	.disable_hubp_power_gate = false,
689 	.disable_clock_gate = true,
690 	.disable_pplib_clock_request = true,
691 	.disable_pplib_wm_range = true,
692 	.pipe_split_policy = MPC_SPLIT_AVOID,
693 	.force_single_disp_pipe_split = false,
694 	.disable_dcc = DCC_ENABLE,
695 	.vsr_support = true,
696 	.performance_trace = false,
697 	.max_downscale_src_width = 7680,/*upto 8K*/
698 	.scl_reset_length10 = true,
699 	.sanity_checks = false,
700 	.underflow_assert_delay_us = 0xFFFFFFFF,
701 	.dwb_fi_phase = -1, // -1 = disable
702 	.dmub_command_table = true,
703 	.use_max_lb = false,
704 	.exit_idle_opt_for_cursor_updates = true
705 };
706 
707 static const struct dc_debug_options debug_defaults_diags = {
708 	.disable_dmcu = true,
709 	.force_abm_enable = false,
710 	.timing_trace = true,
711 	.clock_trace = true,
712 	.disable_dpp_power_gate = false,
713 	.disable_hubp_power_gate = false,
714 	.disable_clock_gate = true,
715 	.disable_pplib_clock_request = true,
716 	.disable_pplib_wm_range = true,
717 	.disable_stutter = true,
718 	.scl_reset_length10 = true,
719 	.dwb_fi_phase = -1, // -1 = disable
720 	.dmub_command_table = true,
721 	.use_max_lb = false,
722 };
723 
724 static void dcn301_dpp_destroy(struct dpp **dpp)
725 {
726 	kfree(TO_DCN20_DPP(*dpp));
727 	*dpp = NULL;
728 }
729 
730 static struct dpp *dcn301_dpp_create(struct dc_context *ctx, uint32_t inst)
731 {
732 	struct dcn3_dpp *dpp =
733 		kzalloc(sizeof(struct dcn3_dpp), GFP_KERNEL);
734 
735 	if (!dpp)
736 		return NULL;
737 
738 	if (dpp3_construct(dpp, ctx, inst,
739 			&dpp_regs[inst], &tf_shift, &tf_mask))
740 		return &dpp->base;
741 
742 	BREAK_TO_DEBUGGER();
743 	kfree(dpp);
744 	return NULL;
745 }
746 static struct output_pixel_processor *dcn301_opp_create(struct dc_context *ctx,
747 							uint32_t inst)
748 {
749 	struct dcn20_opp *opp =
750 		kzalloc(sizeof(struct dcn20_opp), GFP_KERNEL);
751 
752 	if (!opp) {
753 		BREAK_TO_DEBUGGER();
754 		return NULL;
755 	}
756 
757 	dcn20_opp_construct(opp, ctx, inst,
758 			&opp_regs[inst], &opp_shift, &opp_mask);
759 	return &opp->base;
760 }
761 
762 static struct dce_aux *dcn301_aux_engine_create(struct dc_context *ctx, uint32_t inst)
763 {
764 	struct aux_engine_dce110 *aux_engine =
765 		kzalloc(sizeof(struct aux_engine_dce110), GFP_KERNEL);
766 
767 	if (!aux_engine)
768 		return NULL;
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(id) { I2C_HW_ENGINE_COMMON_REG_LIST(id) }
780 
781 static const struct dce_i2c_registers i2c_hw_regs[] = {
782 		i2c_inst_regs(1),
783 		i2c_inst_regs(2),
784 		i2c_inst_regs(3),
785 		i2c_inst_regs(4),
786 };
787 
788 static const struct dce_i2c_shift i2c_shifts = {
789 		I2C_COMMON_MASK_SH_LIST_DCN2(__SHIFT)
790 };
791 
792 static const struct dce_i2c_mask i2c_masks = {
793 		I2C_COMMON_MASK_SH_LIST_DCN2(_MASK)
794 };
795 
796 static struct dce_i2c_hw *dcn301_i2c_hw_create(struct dc_context *ctx, uint32_t inst)
797 {
798 	struct dce_i2c_hw *dce_i2c_hw =
799 		kzalloc(sizeof(struct dce_i2c_hw), GFP_KERNEL);
800 
801 	if (!dce_i2c_hw)
802 		return NULL;
803 
804 	dcn2_i2c_hw_construct(dce_i2c_hw, ctx, inst,
805 				    &i2c_hw_regs[inst], &i2c_shifts, &i2c_masks);
806 
807 	return dce_i2c_hw;
808 }
809 static struct mpc *dcn301_mpc_create(
810 		struct dc_context *ctx,
811 		int num_mpcc,
812 		int num_rmu)
813 {
814 	struct dcn30_mpc *mpc30 = kzalloc(sizeof(struct dcn30_mpc),
815 					  GFP_KERNEL);
816 
817 	if (!mpc30)
818 		return NULL;
819 
820 	dcn30_mpc_construct(mpc30, ctx,
821 			&mpc_regs,
822 			&mpc_shift,
823 			&mpc_mask,
824 			num_mpcc,
825 			num_rmu);
826 
827 	return &mpc30->base;
828 }
829 
830 static struct hubbub *dcn301_hubbub_create(struct dc_context *ctx)
831 {
832 	int i;
833 
834 	struct dcn20_hubbub *hubbub3 = kzalloc(sizeof(struct dcn20_hubbub),
835 					  GFP_KERNEL);
836 
837 	if (!hubbub3)
838 		return NULL;
839 
840 	hubbub301_construct(hubbub3, ctx,
841 			&hubbub_reg,
842 			&hubbub_shift,
843 			&hubbub_mask);
844 
845 
846 	for (i = 0; i < res_cap_dcn301.num_vmid; i++) {
847 		struct dcn20_vmid *vmid = &hubbub3->vmid[i];
848 
849 		vmid->ctx = ctx;
850 
851 		vmid->regs = &vmid_regs[i];
852 		vmid->shifts = &vmid_shifts;
853 		vmid->masks = &vmid_masks;
854 	}
855 
856 	hubbub3->num_vmid = res_cap_dcn301.num_vmid;
857 
858 	return &hubbub3->base;
859 }
860 
861 static struct timing_generator *dcn301_timing_generator_create(
862 	struct dc_context *ctx, uint32_t instance)
863 {
864 	struct optc *tgn10 =
865 		kzalloc(sizeof(struct optc), GFP_KERNEL);
866 
867 	if (!tgn10)
868 		return NULL;
869 
870 	tgn10->base.inst = instance;
871 	tgn10->base.ctx = ctx;
872 
873 	tgn10->tg_regs = &optc_regs[instance];
874 	tgn10->tg_shift = &optc_shift;
875 	tgn10->tg_mask = &optc_mask;
876 
877 	dcn30_timing_generator_init(tgn10);
878 
879 	return &tgn10->base;
880 }
881 
882 static const struct encoder_feature_support link_enc_feature = {
883 		.max_hdmi_deep_color = COLOR_DEPTH_121212,
884 		.max_hdmi_pixel_clock = 600000,
885 		.hdmi_ycbcr420_supported = true,
886 		.dp_ycbcr420_supported = true,
887 		.fec_supported = true,
888 		.flags.bits.IS_HBR2_CAPABLE = true,
889 		.flags.bits.IS_HBR3_CAPABLE = true,
890 		.flags.bits.IS_TPS3_CAPABLE = true,
891 		.flags.bits.IS_TPS4_CAPABLE = true
892 };
893 
894 static struct link_encoder *dcn301_link_encoder_create(
895 	struct dc_context *ctx,
896 	const struct encoder_init_data *enc_init_data)
897 {
898 	struct dcn20_link_encoder *enc20 =
899 		kzalloc(sizeof(struct dcn20_link_encoder), GFP_KERNEL);
900 
901 	if (!enc20)
902 		return NULL;
903 
904 	dcn301_link_encoder_construct(enc20,
905 			enc_init_data,
906 			&link_enc_feature,
907 			&link_enc_regs[enc_init_data->transmitter],
908 			&link_enc_aux_regs[enc_init_data->channel - 1],
909 			&link_enc_hpd_regs[enc_init_data->hpd_source],
910 			&le_shift,
911 			&le_mask);
912 
913 	return &enc20->enc10.base;
914 }
915 
916 static struct panel_cntl *dcn301_panel_cntl_create(const struct panel_cntl_init_data *init_data)
917 {
918 	struct dcn301_panel_cntl *panel_cntl =
919 		kzalloc(sizeof(struct dcn301_panel_cntl), GFP_KERNEL);
920 
921 	if (!panel_cntl)
922 		return NULL;
923 
924 	dcn301_panel_cntl_construct(panel_cntl,
925 			init_data,
926 			&panel_cntl_regs[init_data->inst],
927 			&panel_cntl_shift,
928 			&panel_cntl_mask);
929 
930 	return &panel_cntl->base;
931 }
932 
933 
934 #define CTX ctx
935 
936 #define REG(reg_name) \
937 	(DCN_BASE.instance[0].segment[mm ## reg_name ## _BASE_IDX] + mm ## reg_name)
938 
939 static uint32_t read_pipe_fuses(struct dc_context *ctx)
940 {
941 	uint32_t value = REG_READ(CC_DC_PIPE_DIS);
942 	/* RV1 support max 4 pipes */
943 	value = value & 0xf;
944 	return value;
945 }
946 
947 
948 static void read_dce_straps(
949 	struct dc_context *ctx,
950 	struct resource_straps *straps)
951 {
952 	generic_reg_get(ctx, mmDC_PINSTRAPS + BASE(mmDC_PINSTRAPS_BASE_IDX),
953 		FN(DC_PINSTRAPS, DC_PINSTRAPS_AUDIO), &straps->dc_pinstraps_audio);
954 
955 }
956 
957 static struct audio *dcn301_create_audio(
958 		struct dc_context *ctx, unsigned int inst)
959 {
960 	return dce_audio_create(ctx, inst,
961 			&audio_regs[inst], &audio_shift, &audio_mask);
962 }
963 
964 static struct vpg *dcn301_vpg_create(
965 	struct dc_context *ctx,
966 	uint32_t inst)
967 {
968 	struct dcn30_vpg *vpg3 = kzalloc(sizeof(struct dcn30_vpg), GFP_KERNEL);
969 
970 	if (!vpg3)
971 		return NULL;
972 
973 	vpg3_construct(vpg3, ctx, inst,
974 			&vpg_regs[inst],
975 			&vpg_shift,
976 			&vpg_mask);
977 
978 	return &vpg3->base;
979 }
980 
981 static struct afmt *dcn301_afmt_create(
982 	struct dc_context *ctx,
983 	uint32_t inst)
984 {
985 	struct dcn30_afmt *afmt3 = kzalloc(sizeof(struct dcn30_afmt), GFP_KERNEL);
986 
987 	if (!afmt3)
988 		return NULL;
989 
990 	afmt3_construct(afmt3, ctx, inst,
991 			&afmt_regs[inst],
992 			&afmt_shift,
993 			&afmt_mask);
994 
995 	return &afmt3->base;
996 }
997 
998 static struct stream_encoder *dcn301_stream_encoder_create(enum engine_id eng_id,
999 							   struct dc_context *ctx)
1000 {
1001 	struct dcn10_stream_encoder *enc1;
1002 	struct vpg *vpg;
1003 	struct afmt *afmt;
1004 	int vpg_inst;
1005 	int afmt_inst;
1006 
1007 	/* Mapping of VPG, AFMT, DME register blocks to DIO block instance */
1008 	if (eng_id <= ENGINE_ID_DIGF) {
1009 		vpg_inst = eng_id;
1010 		afmt_inst = eng_id;
1011 	} else
1012 		return NULL;
1013 
1014 	enc1 = kzalloc(sizeof(struct dcn10_stream_encoder), GFP_KERNEL);
1015 	vpg = dcn301_vpg_create(ctx, vpg_inst);
1016 	afmt = dcn301_afmt_create(ctx, afmt_inst);
1017 
1018 	if (!enc1 || !vpg || !afmt) {
1019 		kfree(enc1);
1020 		kfree(vpg);
1021 		kfree(afmt);
1022 		return NULL;
1023 	}
1024 
1025 	dcn30_dio_stream_encoder_construct(enc1, ctx, ctx->dc_bios,
1026 					eng_id, vpg, afmt,
1027 					&stream_enc_regs[eng_id],
1028 					&se_shift, &se_mask);
1029 
1030 	return &enc1->base;
1031 }
1032 
1033 static struct dce_hwseq *dcn301_hwseq_create(struct dc_context *ctx)
1034 {
1035 	struct dce_hwseq *hws = kzalloc(sizeof(struct dce_hwseq), GFP_KERNEL);
1036 
1037 	if (hws) {
1038 		hws->ctx = ctx;
1039 		hws->regs = &hwseq_reg;
1040 		hws->shifts = &hwseq_shift;
1041 		hws->masks = &hwseq_mask;
1042 	}
1043 	return hws;
1044 }
1045 static const struct resource_create_funcs res_create_funcs = {
1046 	.read_dce_straps = read_dce_straps,
1047 	.create_audio = dcn301_create_audio,
1048 	.create_stream_encoder = dcn301_stream_encoder_create,
1049 	.create_hwseq = dcn301_hwseq_create,
1050 };
1051 
1052 static const struct resource_create_funcs res_create_maximus_funcs = {
1053 	.read_dce_straps = NULL,
1054 	.create_audio = NULL,
1055 	.create_stream_encoder = NULL,
1056 	.create_hwseq = dcn301_hwseq_create,
1057 };
1058 
1059 static void dcn301_destruct(struct dcn301_resource_pool *pool)
1060 {
1061 	unsigned int i;
1062 
1063 	for (i = 0; i < pool->base.stream_enc_count; i++) {
1064 		if (pool->base.stream_enc[i] != NULL) {
1065 			if (pool->base.stream_enc[i]->vpg != NULL) {
1066 				kfree(DCN30_VPG_FROM_VPG(pool->base.stream_enc[i]->vpg));
1067 				pool->base.stream_enc[i]->vpg = NULL;
1068 			}
1069 			if (pool->base.stream_enc[i]->afmt != NULL) {
1070 				kfree(DCN30_AFMT_FROM_AFMT(pool->base.stream_enc[i]->afmt));
1071 				pool->base.stream_enc[i]->afmt = NULL;
1072 			}
1073 			kfree(DCN10STRENC_FROM_STRENC(pool->base.stream_enc[i]));
1074 			pool->base.stream_enc[i] = NULL;
1075 		}
1076 	}
1077 
1078 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
1079 		if (pool->base.dscs[i] != NULL)
1080 			dcn20_dsc_destroy(&pool->base.dscs[i]);
1081 	}
1082 
1083 	if (pool->base.mpc != NULL) {
1084 		kfree(TO_DCN20_MPC(pool->base.mpc));
1085 		pool->base.mpc = NULL;
1086 	}
1087 	if (pool->base.hubbub != NULL) {
1088 		kfree(pool->base.hubbub);
1089 		pool->base.hubbub = NULL;
1090 	}
1091 	for (i = 0; i < pool->base.pipe_count; i++) {
1092 		if (pool->base.dpps[i] != NULL)
1093 			dcn301_dpp_destroy(&pool->base.dpps[i]);
1094 
1095 		if (pool->base.ipps[i] != NULL)
1096 			pool->base.ipps[i]->funcs->ipp_destroy(&pool->base.ipps[i]);
1097 
1098 		if (pool->base.hubps[i] != NULL) {
1099 			kfree(TO_DCN20_HUBP(pool->base.hubps[i]));
1100 			pool->base.hubps[i] = NULL;
1101 		}
1102 
1103 		if (pool->base.irqs != NULL) {
1104 			dal_irq_service_destroy(&pool->base.irqs);
1105 		}
1106 	}
1107 
1108 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1109 		if (pool->base.engines[i] != NULL)
1110 			dce110_engine_destroy(&pool->base.engines[i]);
1111 		if (pool->base.hw_i2cs[i] != NULL) {
1112 			kfree(pool->base.hw_i2cs[i]);
1113 			pool->base.hw_i2cs[i] = NULL;
1114 		}
1115 		if (pool->base.sw_i2cs[i] != NULL) {
1116 			kfree(pool->base.sw_i2cs[i]);
1117 			pool->base.sw_i2cs[i] = NULL;
1118 		}
1119 	}
1120 
1121 	for (i = 0; i < pool->base.res_cap->num_opp; i++) {
1122 		if (pool->base.opps[i] != NULL)
1123 			pool->base.opps[i]->funcs->opp_destroy(&pool->base.opps[i]);
1124 	}
1125 
1126 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1127 		if (pool->base.timing_generators[i] != NULL)	{
1128 			kfree(DCN10TG_FROM_TG(pool->base.timing_generators[i]));
1129 			pool->base.timing_generators[i] = NULL;
1130 		}
1131 	}
1132 
1133 	for (i = 0; i < pool->base.res_cap->num_dwb; i++) {
1134 		if (pool->base.dwbc[i] != NULL) {
1135 			kfree(TO_DCN30_DWBC(pool->base.dwbc[i]));
1136 			pool->base.dwbc[i] = NULL;
1137 		}
1138 		if (pool->base.mcif_wb[i] != NULL) {
1139 			kfree(TO_DCN30_MMHUBBUB(pool->base.mcif_wb[i]));
1140 			pool->base.mcif_wb[i] = NULL;
1141 		}
1142 	}
1143 
1144 	for (i = 0; i < pool->base.audio_count; i++) {
1145 		if (pool->base.audios[i])
1146 			dce_aud_destroy(&pool->base.audios[i]);
1147 	}
1148 
1149 	for (i = 0; i < pool->base.clk_src_count; i++) {
1150 		if (pool->base.clock_sources[i] != NULL) {
1151 			dcn20_clock_source_destroy(&pool->base.clock_sources[i]);
1152 			pool->base.clock_sources[i] = NULL;
1153 		}
1154 	}
1155 
1156 	for (i = 0; i < pool->base.res_cap->num_mpc_3dlut; i++) {
1157 		if (pool->base.mpc_lut[i] != NULL) {
1158 			dc_3dlut_func_release(pool->base.mpc_lut[i]);
1159 			pool->base.mpc_lut[i] = NULL;
1160 		}
1161 		if (pool->base.mpc_shaper[i] != NULL) {
1162 			dc_transfer_func_release(pool->base.mpc_shaper[i]);
1163 			pool->base.mpc_shaper[i] = NULL;
1164 		}
1165 	}
1166 
1167 	if (pool->base.dp_clock_source != NULL) {
1168 		dcn20_clock_source_destroy(&pool->base.dp_clock_source);
1169 		pool->base.dp_clock_source = NULL;
1170 	}
1171 
1172 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1173 		if (pool->base.multiple_abms[i] != NULL)
1174 			dce_abm_destroy(&pool->base.multiple_abms[i]);
1175 	}
1176 
1177 	if (pool->base.dccg != NULL)
1178 		dcn_dccg_destroy(&pool->base.dccg);
1179 }
1180 
1181 static struct hubp *dcn301_hubp_create(struct dc_context *ctx, uint32_t inst)
1182 {
1183 	struct dcn20_hubp *hubp2 =
1184 		kzalloc(sizeof(struct dcn20_hubp), GFP_KERNEL);
1185 
1186 	if (!hubp2)
1187 		return NULL;
1188 
1189 	if (hubp3_construct(hubp2, ctx, inst,
1190 			&hubp_regs[inst], &hubp_shift, &hubp_mask))
1191 		return &hubp2->base;
1192 
1193 	BREAK_TO_DEBUGGER();
1194 	kfree(hubp2);
1195 	return NULL;
1196 }
1197 
1198 static bool dcn301_dwbc_create(struct dc_context *ctx, struct resource_pool *pool)
1199 {
1200 	int i;
1201 	uint32_t pipe_count = pool->res_cap->num_dwb;
1202 
1203 	for (i = 0; i < pipe_count; i++) {
1204 		struct dcn30_dwbc *dwbc30 = kzalloc(sizeof(struct dcn30_dwbc),
1205 						    GFP_KERNEL);
1206 
1207 		if (!dwbc30) {
1208 			dm_error("DC: failed to create dwbc30!\n");
1209 			return false;
1210 		}
1211 
1212 		dcn30_dwbc_construct(dwbc30, ctx,
1213 				&dwbc30_regs[i],
1214 				&dwbc30_shift,
1215 				&dwbc30_mask,
1216 				i);
1217 
1218 		pool->dwbc[i] = &dwbc30->base;
1219 	}
1220 	return true;
1221 }
1222 
1223 static bool dcn301_mmhubbub_create(struct dc_context *ctx, struct resource_pool *pool)
1224 {
1225 	int i;
1226 	uint32_t pipe_count = pool->res_cap->num_dwb;
1227 
1228 	for (i = 0; i < pipe_count; i++) {
1229 		struct dcn30_mmhubbub *mcif_wb30 = kzalloc(sizeof(struct dcn30_mmhubbub),
1230 						    GFP_KERNEL);
1231 
1232 		if (!mcif_wb30) {
1233 			dm_error("DC: failed to create mcif_wb30!\n");
1234 			return false;
1235 		}
1236 
1237 		dcn30_mmhubbub_construct(mcif_wb30, ctx,
1238 				&mcif_wb30_regs[i],
1239 				&mcif_wb30_shift,
1240 				&mcif_wb30_mask,
1241 				i);
1242 
1243 		pool->mcif_wb[i] = &mcif_wb30->base;
1244 	}
1245 	return true;
1246 }
1247 
1248 static struct display_stream_compressor *dcn301_dsc_create(
1249 	struct dc_context *ctx, uint32_t inst)
1250 {
1251 	struct dcn20_dsc *dsc =
1252 		kzalloc(sizeof(struct dcn20_dsc), GFP_KERNEL);
1253 
1254 	if (!dsc) {
1255 		BREAK_TO_DEBUGGER();
1256 		return NULL;
1257 	}
1258 
1259 	dsc2_construct(dsc, ctx, inst, &dsc_regs[inst], &dsc_shift, &dsc_mask);
1260 	return &dsc->base;
1261 }
1262 
1263 
1264 static void dcn301_destroy_resource_pool(struct resource_pool **pool)
1265 {
1266 	struct dcn301_resource_pool *dcn301_pool = TO_DCN301_RES_POOL(*pool);
1267 
1268 	dcn301_destruct(dcn301_pool);
1269 	kfree(dcn301_pool);
1270 	*pool = NULL;
1271 }
1272 
1273 static struct clock_source *dcn301_clock_source_create(
1274 		struct dc_context *ctx,
1275 		struct dc_bios *bios,
1276 		enum clock_source_id id,
1277 		const struct dce110_clk_src_regs *regs,
1278 		bool dp_clk_src)
1279 {
1280 	struct dce110_clk_src *clk_src =
1281 		kzalloc(sizeof(struct dce110_clk_src), GFP_KERNEL);
1282 
1283 	if (!clk_src)
1284 		return NULL;
1285 
1286 	if (dcn301_clk_src_construct(clk_src, ctx, bios, id,
1287 			regs, &cs_shift, &cs_mask)) {
1288 		clk_src->base.dp_clk_src = dp_clk_src;
1289 		return &clk_src->base;
1290 	}
1291 
1292 	kfree(clk_src);
1293 	BREAK_TO_DEBUGGER();
1294 	return NULL;
1295 }
1296 
1297 static struct dc_cap_funcs cap_funcs = {
1298 	.get_dcc_compression_cap = dcn20_get_dcc_compression_cap
1299 };
1300 
1301 
1302 static bool is_soc_bounding_box_valid(struct dc *dc)
1303 {
1304 	uint32_t hw_internal_rev = dc->ctx->asic_id.hw_internal_rev;
1305 
1306 	if (ASICREV_IS_VANGOGH(hw_internal_rev))
1307 		return true;
1308 
1309 	return false;
1310 }
1311 
1312 static bool init_soc_bounding_box(struct dc *dc,
1313 				  struct dcn301_resource_pool *pool)
1314 {
1315 	struct _vcs_dpi_soc_bounding_box_st *loaded_bb = &dcn3_01_soc;
1316 	struct _vcs_dpi_ip_params_st *loaded_ip = &dcn3_01_ip;
1317 
1318 	DC_LOGGER_INIT(dc->ctx->logger);
1319 
1320 	if (!is_soc_bounding_box_valid(dc)) {
1321 		DC_LOG_ERROR("%s: not valid soc bounding box\n", __func__);
1322 		return false;
1323 	}
1324 
1325 	loaded_ip->max_num_otg = pool->base.res_cap->num_timing_generator;
1326 	loaded_ip->max_num_dpp = pool->base.pipe_count;
1327 	DC_FP_START();
1328 	dcn20_patch_bounding_box(dc, loaded_bb);
1329 	DC_FP_END();
1330 
1331 	if (dc->ctx->dc_bios->funcs->get_soc_bb_info) {
1332 		struct bp_soc_bb_info bb_info = {0};
1333 
1334 		if (dc->ctx->dc_bios->funcs->get_soc_bb_info(dc->ctx->dc_bios, &bb_info) == BP_RESULT_OK) {
1335 			DC_FP_START();
1336 			dcn301_fpu_init_soc_bounding_box(bb_info);
1337 			DC_FP_END();
1338 		}
1339 	}
1340 
1341 	return true;
1342 }
1343 
1344 
1345 static void set_wm_ranges(
1346 		struct pp_smu_funcs *pp_smu,
1347 		struct _vcs_dpi_soc_bounding_box_st *loaded_bb)
1348 {
1349 	struct pp_smu_wm_range_sets ranges = {0};
1350 	int i;
1351 
1352 	ranges.num_reader_wm_sets = 0;
1353 
1354 	if (loaded_bb->num_states == 1) {
1355 		ranges.reader_wm_sets[0].wm_inst = 0;
1356 		ranges.reader_wm_sets[0].min_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
1357 		ranges.reader_wm_sets[0].max_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
1358 		ranges.reader_wm_sets[0].min_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
1359 		ranges.reader_wm_sets[0].max_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
1360 
1361 		ranges.num_reader_wm_sets = 1;
1362 	} else if (loaded_bb->num_states > 1) {
1363 		for (i = 0; i < 4 && i < loaded_bb->num_states; i++) {
1364 			ranges.reader_wm_sets[i].wm_inst = i;
1365 			ranges.reader_wm_sets[i].min_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
1366 			ranges.reader_wm_sets[i].max_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
1367 			DC_FP_START();
1368 			dcn301_fpu_set_wm_ranges(i, &ranges, loaded_bb);
1369 			DC_FP_END();
1370 			ranges.num_reader_wm_sets = i + 1;
1371 		}
1372 
1373 		ranges.reader_wm_sets[0].min_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
1374 		ranges.reader_wm_sets[ranges.num_reader_wm_sets - 1].max_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
1375 	}
1376 
1377 	ranges.num_writer_wm_sets = 1;
1378 
1379 	ranges.writer_wm_sets[0].wm_inst = 0;
1380 	ranges.writer_wm_sets[0].min_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
1381 	ranges.writer_wm_sets[0].max_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
1382 	ranges.writer_wm_sets[0].min_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
1383 	ranges.writer_wm_sets[0].max_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
1384 
1385 	/* Notify PP Lib/SMU which Watermarks to use for which clock ranges */
1386 	pp_smu->nv_funcs.set_wm_ranges(&pp_smu->nv_funcs.pp_smu, &ranges);
1387 }
1388 
1389 static void dcn301_calculate_wm_and_dlg(
1390 		struct dc *dc, struct dc_state *context,
1391 		display_e2e_pipe_params_st *pipes,
1392 		int pipe_cnt,
1393 		int vlevel)
1394 {
1395 	DC_FP_START();
1396 	dcn301_calculate_wm_and_dlg_fp(dc, context, pipes, pipe_cnt, vlevel);
1397 	DC_FP_END();
1398 }
1399 
1400 static struct resource_funcs dcn301_res_pool_funcs = {
1401 	.destroy = dcn301_destroy_resource_pool,
1402 	.link_enc_create = dcn301_link_encoder_create,
1403 	.panel_cntl_create = dcn301_panel_cntl_create,
1404 	.validate_bandwidth = dcn30_validate_bandwidth,
1405 	.calculate_wm_and_dlg = dcn301_calculate_wm_and_dlg,
1406 	.update_soc_for_wm_a = dcn30_update_soc_for_wm_a,
1407 	.populate_dml_pipes = dcn30_populate_dml_pipes_from_context,
1408 	.acquire_idle_pipe_for_layer = dcn20_acquire_idle_pipe_for_layer,
1409 	.add_stream_to_ctx = dcn30_add_stream_to_ctx,
1410 	.add_dsc_to_stream_resource = dcn20_add_dsc_to_stream_resource,
1411 	.remove_stream_from_ctx = dcn20_remove_stream_from_ctx,
1412 	.populate_dml_writeback_from_context = dcn30_populate_dml_writeback_from_context,
1413 	.set_mcif_arb_params = dcn30_set_mcif_arb_params,
1414 	.find_first_free_match_stream_enc_for_link = dcn10_find_first_free_match_stream_enc_for_link,
1415 	.acquire_post_bldn_3dlut = dcn30_acquire_post_bldn_3dlut,
1416 	.release_post_bldn_3dlut = dcn30_release_post_bldn_3dlut,
1417 	.update_bw_bounding_box = dcn301_update_bw_bounding_box
1418 };
1419 
1420 static bool dcn301_resource_construct(
1421 	uint8_t num_virtual_links,
1422 	struct dc *dc,
1423 	struct dcn301_resource_pool *pool)
1424 {
1425 	int i, j;
1426 	struct dc_context *ctx = dc->ctx;
1427 	struct irq_service_init_data init_data;
1428 	uint32_t pipe_fuses = read_pipe_fuses(ctx);
1429 	uint32_t num_pipes = 0;
1430 
1431 	DC_LOGGER_INIT(dc->ctx->logger);
1432 
1433 	ctx->dc_bios->regs = &bios_regs;
1434 
1435 	if (dc->ctx->asic_id.chip_id == DEVICE_ID_VGH_1435)
1436 		res_cap_dcn301.num_pll = 2;
1437 	pool->base.res_cap = &res_cap_dcn301;
1438 
1439 	pool->base.funcs = &dcn301_res_pool_funcs;
1440 
1441 	/*************************************************
1442 	 *  Resource + asic cap harcoding                *
1443 	 *************************************************/
1444 	pool->base.underlay_pipe_index = NO_UNDERLAY_PIPE;
1445 	pool->base.pipe_count = pool->base.res_cap->num_timing_generator;
1446 	pool->base.mpcc_count = pool->base.res_cap->num_timing_generator;
1447 	dc->caps.max_downscale_ratio = 600;
1448 	dc->caps.i2c_speed_in_khz = 100;
1449 	dc->caps.i2c_speed_in_khz_hdcp = 5; /*1.4 w/a enabled by default*/
1450 	dc->caps.max_cursor_size = 256;
1451 	dc->caps.min_horizontal_blanking_period = 80;
1452 	dc->caps.dmdata_alloc_size = 2048;
1453 	dc->caps.max_slave_planes = 1;
1454 	dc->caps.max_slave_yuv_planes = 1;
1455 	dc->caps.max_slave_rgb_planes = 1;
1456 	dc->caps.is_apu = true;
1457 	dc->caps.post_blend_color_processing = true;
1458 	dc->caps.force_dp_tps4_for_cp2520 = true;
1459 	dc->caps.extended_aux_timeout_support = true;
1460 	dc->caps.dmcub_support = true;
1461 
1462 	/* Color pipeline capabilities */
1463 	dc->caps.color.dpp.dcn_arch = 1;
1464 	dc->caps.color.dpp.input_lut_shared = 0;
1465 	dc->caps.color.dpp.icsc = 1;
1466 	dc->caps.color.dpp.dgam_ram = 0; // must use gamma_corr
1467 	dc->caps.color.dpp.dgam_rom_caps.srgb = 1;
1468 	dc->caps.color.dpp.dgam_rom_caps.bt2020 = 1;
1469 	dc->caps.color.dpp.dgam_rom_caps.gamma2_2 = 1;
1470 	dc->caps.color.dpp.dgam_rom_caps.pq = 1;
1471 	dc->caps.color.dpp.dgam_rom_caps.hlg = 1;
1472 	dc->caps.color.dpp.post_csc = 1;
1473 	dc->caps.color.dpp.gamma_corr = 1;
1474 	dc->caps.color.dpp.dgam_rom_for_yuv = 0;
1475 
1476 	dc->caps.color.dpp.hw_3d_lut = 1;
1477 	dc->caps.color.dpp.ogam_ram = 1;
1478 	// no OGAM ROM on DCN301
1479 	dc->caps.color.dpp.ogam_rom_caps.srgb = 0;
1480 	dc->caps.color.dpp.ogam_rom_caps.bt2020 = 0;
1481 	dc->caps.color.dpp.ogam_rom_caps.gamma2_2 = 0;
1482 	dc->caps.color.dpp.ogam_rom_caps.pq = 0;
1483 	dc->caps.color.dpp.ogam_rom_caps.hlg = 0;
1484 	dc->caps.color.dpp.ocsc = 0;
1485 
1486 	dc->caps.color.mpc.gamut_remap = 1;
1487 	dc->caps.color.mpc.num_3dluts = pool->base.res_cap->num_mpc_3dlut; //2
1488 	dc->caps.color.mpc.ogam_ram = 1;
1489 	dc->caps.color.mpc.ogam_rom_caps.srgb = 0;
1490 	dc->caps.color.mpc.ogam_rom_caps.bt2020 = 0;
1491 	dc->caps.color.mpc.ogam_rom_caps.gamma2_2 = 0;
1492 	dc->caps.color.mpc.ogam_rom_caps.pq = 0;
1493 	dc->caps.color.mpc.ogam_rom_caps.hlg = 0;
1494 	dc->caps.color.mpc.ocsc = 1;
1495 
1496 	dc->caps.dp_hdmi21_pcon_support = true;
1497 
1498 	/* read VBIOS LTTPR caps */
1499 	if (ctx->dc_bios->funcs->get_lttpr_caps) {
1500 		enum bp_result bp_query_result;
1501 		uint8_t is_vbios_lttpr_enable = 0;
1502 
1503 		bp_query_result = ctx->dc_bios->funcs->get_lttpr_caps(ctx->dc_bios, &is_vbios_lttpr_enable);
1504 		dc->caps.vbios_lttpr_enable = (bp_query_result == BP_RESULT_OK) && !!is_vbios_lttpr_enable;
1505 	}
1506 
1507 	if (ctx->dc_bios->funcs->get_lttpr_interop) {
1508 		enum bp_result bp_query_result;
1509 		uint8_t is_vbios_interop_enabled = 0;
1510 
1511 		bp_query_result = ctx->dc_bios->funcs->get_lttpr_interop(ctx->dc_bios, &is_vbios_interop_enabled);
1512 		dc->caps.vbios_lttpr_aware = (bp_query_result == BP_RESULT_OK) && !!is_vbios_interop_enabled;
1513 	}
1514 
1515 	if (dc->ctx->dce_environment == DCE_ENV_PRODUCTION_DRV)
1516 		dc->debug = debug_defaults_drv;
1517 	else if (dc->ctx->dce_environment == DCE_ENV_FPGA_MAXIMUS) {
1518 		dc->debug = debug_defaults_diags;
1519 	} else
1520 		dc->debug = debug_defaults_diags;
1521 	// Init the vm_helper
1522 	if (dc->vm_helper)
1523 		vm_helper_init(dc->vm_helper, 16);
1524 
1525 	/*************************************************
1526 	 *  Create resources                             *
1527 	 *************************************************/
1528 
1529 	/* Clock Sources for Pixel Clock*/
1530 	pool->base.clock_sources[DCN301_CLK_SRC_PLL0] =
1531 			dcn301_clock_source_create(ctx, ctx->dc_bios,
1532 				CLOCK_SOURCE_COMBO_PHY_PLL0,
1533 				&clk_src_regs[0], false);
1534 	pool->base.clock_sources[DCN301_CLK_SRC_PLL1] =
1535 			dcn301_clock_source_create(ctx, ctx->dc_bios,
1536 				CLOCK_SOURCE_COMBO_PHY_PLL1,
1537 				&clk_src_regs[1], false);
1538 	pool->base.clock_sources[DCN301_CLK_SRC_PLL2] =
1539 			dcn301_clock_source_create(ctx, ctx->dc_bios,
1540 				CLOCK_SOURCE_COMBO_PHY_PLL2,
1541 				&clk_src_regs[2], false);
1542 	pool->base.clock_sources[DCN301_CLK_SRC_PLL3] =
1543 			dcn301_clock_source_create(ctx, ctx->dc_bios,
1544 				CLOCK_SOURCE_COMBO_PHY_PLL3,
1545 				&clk_src_regs[3], false);
1546 
1547 	pool->base.clk_src_count = DCN301_CLK_SRC_TOTAL;
1548 
1549 	/* todo: not reuse phy_pll registers */
1550 	pool->base.dp_clock_source =
1551 			dcn301_clock_source_create(ctx, ctx->dc_bios,
1552 				CLOCK_SOURCE_ID_DP_DTO,
1553 				&clk_src_regs[0], true);
1554 
1555 	for (i = 0; i < pool->base.clk_src_count; i++) {
1556 		if (pool->base.clock_sources[i] == NULL) {
1557 			dm_error("DC: failed to create clock sources!\n");
1558 			BREAK_TO_DEBUGGER();
1559 			goto create_fail;
1560 		}
1561 	}
1562 
1563 	/* DCCG */
1564 	pool->base.dccg = dccg301_create(ctx, &dccg_regs, &dccg_shift, &dccg_mask);
1565 	if (pool->base.dccg == NULL) {
1566 		dm_error("DC: failed to create dccg!\n");
1567 		BREAK_TO_DEBUGGER();
1568 		goto create_fail;
1569 	}
1570 
1571 	init_soc_bounding_box(dc, pool);
1572 
1573 	if (!dc->debug.disable_pplib_wm_range && pool->base.pp_smu->nv_funcs.set_wm_ranges)
1574 		set_wm_ranges(pool->base.pp_smu, &dcn3_01_soc);
1575 
1576 	num_pipes = dcn3_01_ip.max_num_dpp;
1577 
1578 	for (i = 0; i < dcn3_01_ip.max_num_dpp; i++)
1579 		if (pipe_fuses & 1 << i)
1580 			num_pipes--;
1581 	dcn3_01_ip.max_num_dpp = num_pipes;
1582 	dcn3_01_ip.max_num_otg = num_pipes;
1583 
1584 
1585 	dml_init_instance(&dc->dml, &dcn3_01_soc, &dcn3_01_ip, DML_PROJECT_DCN30);
1586 
1587 	/* IRQ */
1588 	init_data.ctx = dc->ctx;
1589 	pool->base.irqs = dal_irq_service_dcn30_create(&init_data);
1590 	if (!pool->base.irqs)
1591 		goto create_fail;
1592 
1593 	/* HUBBUB */
1594 	pool->base.hubbub = dcn301_hubbub_create(ctx);
1595 	if (pool->base.hubbub == NULL) {
1596 		BREAK_TO_DEBUGGER();
1597 		dm_error("DC: failed to create hubbub!\n");
1598 		goto create_fail;
1599 	}
1600 
1601 	j = 0;
1602 	/* HUBPs, DPPs, OPPs and TGs */
1603 	for (i = 0; i < pool->base.pipe_count; i++) {
1604 
1605 		/* if pipe is disabled, skip instance of HW pipe,
1606 		 * i.e, skip ASIC register instance
1607 		 */
1608 		if ((pipe_fuses & (1 << i)) != 0) {
1609 			DC_LOG_DEBUG("%s: fusing pipe %d\n", __func__, i);
1610 			continue;
1611 		}
1612 
1613 		pool->base.hubps[j] = dcn301_hubp_create(ctx, i);
1614 		if (pool->base.hubps[j] == NULL) {
1615 			BREAK_TO_DEBUGGER();
1616 			dm_error(
1617 				"DC: failed to create hubps!\n");
1618 			goto create_fail;
1619 		}
1620 
1621 		pool->base.dpps[j] = dcn301_dpp_create(ctx, i);
1622 		if (pool->base.dpps[j] == NULL) {
1623 			BREAK_TO_DEBUGGER();
1624 			dm_error(
1625 				"DC: failed to create dpps!\n");
1626 			goto create_fail;
1627 		}
1628 
1629 		pool->base.opps[j] = dcn301_opp_create(ctx, i);
1630 		if (pool->base.opps[j] == NULL) {
1631 			BREAK_TO_DEBUGGER();
1632 			dm_error(
1633 				"DC: failed to create output pixel processor!\n");
1634 			goto create_fail;
1635 		}
1636 
1637 		pool->base.timing_generators[j] = dcn301_timing_generator_create(ctx, i);
1638 		if (pool->base.timing_generators[j] == NULL) {
1639 			BREAK_TO_DEBUGGER();
1640 			dm_error("DC: failed to create tg!\n");
1641 			goto create_fail;
1642 		}
1643 		j++;
1644 	}
1645 	pool->base.timing_generator_count = j;
1646 	pool->base.pipe_count = j;
1647 	pool->base.mpcc_count = j;
1648 
1649 	/* ABM (or ABMs for NV2x) */
1650 	/* TODO: */
1651 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1652 		pool->base.multiple_abms[i] = dmub_abm_create(ctx,
1653 				&abm_regs[i],
1654 				&abm_shift,
1655 				&abm_mask);
1656 		if (pool->base.multiple_abms[i] == NULL) {
1657 			dm_error("DC: failed to create abm for pipe %d!\n", i);
1658 			BREAK_TO_DEBUGGER();
1659 			goto create_fail;
1660 		}
1661 	}
1662 
1663 	/* MPC and DSC */
1664 	pool->base.mpc = dcn301_mpc_create(ctx, pool->base.mpcc_count, pool->base.res_cap->num_mpc_3dlut);
1665 	if (pool->base.mpc == NULL) {
1666 		BREAK_TO_DEBUGGER();
1667 		dm_error("DC: failed to create mpc!\n");
1668 		goto create_fail;
1669 	}
1670 
1671 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
1672 		pool->base.dscs[i] = dcn301_dsc_create(ctx, i);
1673 		if (pool->base.dscs[i] == NULL) {
1674 			BREAK_TO_DEBUGGER();
1675 			dm_error("DC: failed to create display stream compressor %d!\n", i);
1676 			goto create_fail;
1677 		}
1678 	}
1679 
1680 	/* DWB and MMHUBBUB */
1681 	if (!dcn301_dwbc_create(ctx, &pool->base)) {
1682 		BREAK_TO_DEBUGGER();
1683 		dm_error("DC: failed to create dwbc!\n");
1684 		goto create_fail;
1685 	}
1686 
1687 	if (!dcn301_mmhubbub_create(ctx, &pool->base)) {
1688 		BREAK_TO_DEBUGGER();
1689 		dm_error("DC: failed to create mcif_wb!\n");
1690 		goto create_fail;
1691 	}
1692 
1693 	/* AUX and I2C */
1694 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1695 		pool->base.engines[i] = dcn301_aux_engine_create(ctx, i);
1696 		if (pool->base.engines[i] == NULL) {
1697 			BREAK_TO_DEBUGGER();
1698 			dm_error(
1699 				"DC:failed to create aux engine!!\n");
1700 			goto create_fail;
1701 		}
1702 		pool->base.hw_i2cs[i] = dcn301_i2c_hw_create(ctx, i);
1703 		if (pool->base.hw_i2cs[i] == NULL) {
1704 			BREAK_TO_DEBUGGER();
1705 			dm_error(
1706 				"DC:failed to create hw i2c!!\n");
1707 			goto create_fail;
1708 		}
1709 		pool->base.sw_i2cs[i] = NULL;
1710 	}
1711 
1712 	/* Audio, Stream Encoders including HPO and virtual, MPC 3D LUTs */
1713 	if (!resource_construct(num_virtual_links, dc, &pool->base,
1714 			(!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment) ?
1715 			&res_create_funcs : &res_create_maximus_funcs)))
1716 			goto create_fail;
1717 
1718 	/* HW Sequencer and Plane caps */
1719 	dcn301_hw_sequencer_construct(dc);
1720 
1721 	dc->caps.max_planes =  pool->base.pipe_count;
1722 
1723 	for (i = 0; i < dc->caps.max_planes; ++i)
1724 		dc->caps.planes[i] = plane_cap;
1725 
1726 	dc->cap_funcs = cap_funcs;
1727 
1728 	return true;
1729 
1730 create_fail:
1731 
1732 	dcn301_destruct(pool);
1733 
1734 	return false;
1735 }
1736 
1737 struct resource_pool *dcn301_create_resource_pool(
1738 		const struct dc_init_data *init_data,
1739 		struct dc *dc)
1740 {
1741 	struct dcn301_resource_pool *pool =
1742 		kzalloc(sizeof(struct dcn301_resource_pool), GFP_KERNEL);
1743 
1744 	if (!pool)
1745 		return NULL;
1746 
1747 	if (dcn301_resource_construct(init_data->num_virtual_links, dc, pool))
1748 		return &pool->base;
1749 
1750 	BREAK_TO_DEBUGGER();
1751 	kfree(pool);
1752 	return NULL;
1753 }
1754