xref: /openbmc/linux/drivers/gpu/drm/amd/display/dc/dcn20/dcn20_resource.c (revision 1da37801a8b0fffb024fea594c7f1d7867ed8aa0)
1 /*
2 * Copyright 2016 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 #include <linux/slab.h>
27 
28 #include "dm_services.h"
29 #include "dc.h"
30 
31 #include "resource.h"
32 #include "include/irq_service_interface.h"
33 #include "dcn20/dcn20_resource.h"
34 
35 #include "dcn10/dcn10_hubp.h"
36 #include "dcn10/dcn10_ipp.h"
37 #include "dcn20_hubbub.h"
38 #include "dcn20_mpc.h"
39 #include "dcn20_hubp.h"
40 #include "irq/dcn20/irq_service_dcn20.h"
41 #include "dcn20_dpp.h"
42 #include "dcn20_optc.h"
43 #include "dcn20_hwseq.h"
44 #include "dce110/dce110_hw_sequencer.h"
45 #include "dcn10/dcn10_resource.h"
46 #include "dcn20_opp.h"
47 
48 #include "dcn20_dsc.h"
49 
50 #include "dcn20_link_encoder.h"
51 #include "dcn20_stream_encoder.h"
52 #include "dce/dce_clock_source.h"
53 #include "dce/dce_audio.h"
54 #include "dce/dce_hwseq.h"
55 #include "virtual/virtual_stream_encoder.h"
56 #include "dce110/dce110_resource.h"
57 #include "dml/display_mode_vba.h"
58 #include "dcn20_dccg.h"
59 #include "dcn20_vmid.h"
60 #include "dc_link_ddc.h"
61 
62 #include "navi10_ip_offset.h"
63 
64 #include "dcn/dcn_2_0_0_offset.h"
65 #include "dcn/dcn_2_0_0_sh_mask.h"
66 
67 #include "nbio/nbio_2_3_offset.h"
68 
69 #include "dcn20/dcn20_dwb.h"
70 #include "dcn20/dcn20_mmhubbub.h"
71 
72 #include "mmhub/mmhub_2_0_0_offset.h"
73 #include "mmhub/mmhub_2_0_0_sh_mask.h"
74 
75 #include "reg_helper.h"
76 #include "dce/dce_abm.h"
77 #include "dce/dce_dmcu.h"
78 #include "dce/dce_aux.h"
79 #include "dce/dce_i2c.h"
80 #include "vm_helper.h"
81 
82 #include "amdgpu_socbb.h"
83 
84 /* NV12 SOC BB is currently in FW, mark SW bounding box invalid. */
85 #define SOC_BOUNDING_BOX_VALID false
86 #define DC_LOGGER_INIT(logger)
87 
88 struct _vcs_dpi_ip_params_st dcn2_0_ip = {
89 	.odm_capable = 1,
90 	.gpuvm_enable = 0,
91 	.hostvm_enable = 0,
92 	.gpuvm_max_page_table_levels = 4,
93 	.hostvm_max_page_table_levels = 4,
94 	.hostvm_cached_page_table_levels = 0,
95 	.pte_group_size_bytes = 2048,
96 	.num_dsc = 6,
97 	.rob_buffer_size_kbytes = 168,
98 	.det_buffer_size_kbytes = 164,
99 	.dpte_buffer_size_in_pte_reqs_luma = 84,
100 	.pde_proc_buffer_size_64k_reqs = 48,
101 	.dpp_output_buffer_pixels = 2560,
102 	.opp_output_buffer_lines = 1,
103 	.pixel_chunk_size_kbytes = 8,
104 	.pte_chunk_size_kbytes = 2,
105 	.meta_chunk_size_kbytes = 2,
106 	.writeback_chunk_size_kbytes = 2,
107 	.line_buffer_size_bits = 789504,
108 	.is_line_buffer_bpp_fixed = 0,
109 	.line_buffer_fixed_bpp = 0,
110 	.dcc_supported = true,
111 	.max_line_buffer_lines = 12,
112 	.writeback_luma_buffer_size_kbytes = 12,
113 	.writeback_chroma_buffer_size_kbytes = 8,
114 	.writeback_chroma_line_buffer_width_pixels = 4,
115 	.writeback_max_hscl_ratio = 1,
116 	.writeback_max_vscl_ratio = 1,
117 	.writeback_min_hscl_ratio = 1,
118 	.writeback_min_vscl_ratio = 1,
119 	.writeback_max_hscl_taps = 12,
120 	.writeback_max_vscl_taps = 12,
121 	.writeback_line_buffer_luma_buffer_size = 0,
122 	.writeback_line_buffer_chroma_buffer_size = 14643,
123 	.cursor_buffer_size = 8,
124 	.cursor_chunk_size = 2,
125 	.max_num_otg = 6,
126 	.max_num_dpp = 6,
127 	.max_num_wb = 1,
128 	.max_dchub_pscl_bw_pix_per_clk = 4,
129 	.max_pscl_lb_bw_pix_per_clk = 2,
130 	.max_lb_vscl_bw_pix_per_clk = 4,
131 	.max_vscl_hscl_bw_pix_per_clk = 4,
132 	.max_hscl_ratio = 8,
133 	.max_vscl_ratio = 8,
134 	.hscl_mults = 4,
135 	.vscl_mults = 4,
136 	.max_hscl_taps = 8,
137 	.max_vscl_taps = 8,
138 	.dispclk_ramp_margin_percent = 1,
139 	.underscan_factor = 1.10,
140 	.min_vblank_lines = 32, //
141 	.dppclk_delay_subtotal = 77, //
142 	.dppclk_delay_scl_lb_only = 16,
143 	.dppclk_delay_scl = 50,
144 	.dppclk_delay_cnvc_formatter = 8,
145 	.dppclk_delay_cnvc_cursor = 6,
146 	.dispclk_delay_subtotal = 87, //
147 	.dcfclk_cstate_latency = 10, // SRExitTime
148 	.max_inter_dcn_tile_repeaters = 8,
149 
150 	.xfc_supported = true,
151 	.xfc_fill_bw_overhead_percent = 10.0,
152 	.xfc_fill_constant_bytes = 0,
153 };
154 
155 struct _vcs_dpi_soc_bounding_box_st dcn2_0_soc = {
156 	/* Defaults that get patched on driver load from firmware. */
157 	.clock_limits = {
158 			{
159 				.state = 0,
160 				.dcfclk_mhz = 560.0,
161 				.fabricclk_mhz = 560.0,
162 				.dispclk_mhz = 513.0,
163 				.dppclk_mhz = 513.0,
164 				.phyclk_mhz = 540.0,
165 				.socclk_mhz = 560.0,
166 				.dscclk_mhz = 171.0,
167 				.dram_speed_mts = 8960.0,
168 			},
169 			{
170 				.state = 1,
171 				.dcfclk_mhz = 694.0,
172 				.fabricclk_mhz = 694.0,
173 				.dispclk_mhz = 642.0,
174 				.dppclk_mhz = 642.0,
175 				.phyclk_mhz = 600.0,
176 				.socclk_mhz = 694.0,
177 				.dscclk_mhz = 214.0,
178 				.dram_speed_mts = 11104.0,
179 			},
180 			{
181 				.state = 2,
182 				.dcfclk_mhz = 875.0,
183 				.fabricclk_mhz = 875.0,
184 				.dispclk_mhz = 734.0,
185 				.dppclk_mhz = 734.0,
186 				.phyclk_mhz = 810.0,
187 				.socclk_mhz = 875.0,
188 				.dscclk_mhz = 245.0,
189 				.dram_speed_mts = 14000.0,
190 			},
191 			{
192 				.state = 3,
193 				.dcfclk_mhz = 1000.0,
194 				.fabricclk_mhz = 1000.0,
195 				.dispclk_mhz = 1100.0,
196 				.dppclk_mhz = 1100.0,
197 				.phyclk_mhz = 810.0,
198 				.socclk_mhz = 1000.0,
199 				.dscclk_mhz = 367.0,
200 				.dram_speed_mts = 16000.0,
201 			},
202 			{
203 				.state = 4,
204 				.dcfclk_mhz = 1200.0,
205 				.fabricclk_mhz = 1200.0,
206 				.dispclk_mhz = 1284.0,
207 				.dppclk_mhz = 1284.0,
208 				.phyclk_mhz = 810.0,
209 				.socclk_mhz = 1200.0,
210 				.dscclk_mhz = 428.0,
211 				.dram_speed_mts = 16000.0,
212 			},
213 			/*Extra state, no dispclk ramping*/
214 			{
215 				.state = 5,
216 				.dcfclk_mhz = 1200.0,
217 				.fabricclk_mhz = 1200.0,
218 				.dispclk_mhz = 1284.0,
219 				.dppclk_mhz = 1284.0,
220 				.phyclk_mhz = 810.0,
221 				.socclk_mhz = 1200.0,
222 				.dscclk_mhz = 428.0,
223 				.dram_speed_mts = 16000.0,
224 			},
225 		},
226 	.num_states = 5,
227 	.sr_exit_time_us = 8.6,
228 	.sr_enter_plus_exit_time_us = 10.9,
229 	.urgent_latency_us = 4.0,
230 	.urgent_latency_pixel_data_only_us = 4.0,
231 	.urgent_latency_pixel_mixed_with_vm_data_us = 4.0,
232 	.urgent_latency_vm_data_only_us = 4.0,
233 	.urgent_out_of_order_return_per_channel_pixel_only_bytes = 4096,
234 	.urgent_out_of_order_return_per_channel_pixel_and_vm_bytes = 4096,
235 	.urgent_out_of_order_return_per_channel_vm_only_bytes = 4096,
236 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_only = 40.0,
237 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_and_vm = 40.0,
238 	.pct_ideal_dram_sdp_bw_after_urgent_vm_only = 40.0,
239 	.max_avg_sdp_bw_use_normal_percent = 40.0,
240 	.max_avg_dram_bw_use_normal_percent = 40.0,
241 	.writeback_latency_us = 12.0,
242 	.ideal_dram_bw_after_urgent_percent = 40.0,
243 	.max_request_size_bytes = 256,
244 	.dram_channel_width_bytes = 2,
245 	.fabric_datapath_to_dcn_data_return_bytes = 64,
246 	.dcn_downspread_percent = 0.5,
247 	.downspread_percent = 0.38,
248 	.dram_page_open_time_ns = 50.0,
249 	.dram_rw_turnaround_time_ns = 17.5,
250 	.dram_return_buffer_per_channel_bytes = 8192,
251 	.round_trip_ping_latency_dcfclk_cycles = 131,
252 	.urgent_out_of_order_return_per_channel_bytes = 256,
253 	.channel_interleave_bytes = 256,
254 	.num_banks = 8,
255 	.num_chans = 16,
256 	.vmm_page_size_bytes = 4096,
257 	.dram_clock_change_latency_us = 404.0,
258 	.dummy_pstate_latency_us = 5.0,
259 	.writeback_dram_clock_change_latency_us = 23.0,
260 	.return_bus_width_bytes = 64,
261 	.dispclk_dppclk_vco_speed_mhz = 3850,
262 	.xfc_bus_transport_time_us = 20,
263 	.xfc_xbuf_latency_tolerance_us = 4,
264 	.use_urgent_burst_bw = 0
265 };
266 
267 struct _vcs_dpi_soc_bounding_box_st dcn2_0_nv12_soc = { 0 };
268 
269 #ifndef mmDP0_DP_DPHY_INTERNAL_CTRL
270 	#define mmDP0_DP_DPHY_INTERNAL_CTRL		0x210f
271 	#define mmDP0_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
272 	#define mmDP1_DP_DPHY_INTERNAL_CTRL		0x220f
273 	#define mmDP1_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
274 	#define mmDP2_DP_DPHY_INTERNAL_CTRL		0x230f
275 	#define mmDP2_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
276 	#define mmDP3_DP_DPHY_INTERNAL_CTRL		0x240f
277 	#define mmDP3_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
278 	#define mmDP4_DP_DPHY_INTERNAL_CTRL		0x250f
279 	#define mmDP4_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
280 	#define mmDP5_DP_DPHY_INTERNAL_CTRL		0x260f
281 	#define mmDP5_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
282 	#define mmDP6_DP_DPHY_INTERNAL_CTRL		0x270f
283 	#define mmDP6_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
284 #endif
285 
286 
287 enum dcn20_clk_src_array_id {
288 	DCN20_CLK_SRC_PLL0,
289 	DCN20_CLK_SRC_PLL1,
290 	DCN20_CLK_SRC_PLL2,
291 	DCN20_CLK_SRC_PLL3,
292 	DCN20_CLK_SRC_PLL4,
293 	DCN20_CLK_SRC_PLL5,
294 	DCN20_CLK_SRC_TOTAL
295 };
296 
297 /* begin *********************
298  * macros to expend register list macro defined in HW object header file */
299 
300 /* DCN */
301 /* TODO awful hack. fixup dcn20_dwb.h */
302 #undef BASE_INNER
303 #define BASE_INNER(seg) DCN_BASE__INST0_SEG ## seg
304 
305 #define BASE(seg) BASE_INNER(seg)
306 
307 #define SR(reg_name)\
308 		.reg_name = BASE(mm ## reg_name ## _BASE_IDX) +  \
309 					mm ## reg_name
310 
311 #define SRI(reg_name, block, id)\
312 	.reg_name = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
313 					mm ## block ## id ## _ ## reg_name
314 
315 #define SRIR(var_name, reg_name, block, id)\
316 	.var_name = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
317 					mm ## block ## id ## _ ## reg_name
318 
319 #define SRII(reg_name, block, id)\
320 	.reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
321 					mm ## block ## id ## _ ## reg_name
322 
323 #define DCCG_SRII(reg_name, block, id)\
324 	.block ## _ ## reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
325 					mm ## block ## id ## _ ## reg_name
326 
327 /* NBIO */
328 #define NBIO_BASE_INNER(seg) \
329 	NBIO_BASE__INST0_SEG ## seg
330 
331 #define NBIO_BASE(seg) \
332 	NBIO_BASE_INNER(seg)
333 
334 #define NBIO_SR(reg_name)\
335 		.reg_name = NBIO_BASE(mm ## reg_name ## _BASE_IDX) + \
336 					mm ## reg_name
337 
338 /* MMHUB */
339 #define MMHUB_BASE_INNER(seg) \
340 	MMHUB_BASE__INST0_SEG ## seg
341 
342 #define MMHUB_BASE(seg) \
343 	MMHUB_BASE_INNER(seg)
344 
345 #define MMHUB_SR(reg_name)\
346 		.reg_name = MMHUB_BASE(mmMM ## reg_name ## _BASE_IDX) + \
347 					mmMM ## reg_name
348 
349 static const struct bios_registers bios_regs = {
350 		NBIO_SR(BIOS_SCRATCH_3),
351 		NBIO_SR(BIOS_SCRATCH_6)
352 };
353 
354 #define clk_src_regs(index, pllid)\
355 [index] = {\
356 	CS_COMMON_REG_LIST_DCN2_0(index, pllid),\
357 }
358 
359 static const struct dce110_clk_src_regs clk_src_regs[] = {
360 	clk_src_regs(0, A),
361 	clk_src_regs(1, B),
362 	clk_src_regs(2, C),
363 	clk_src_regs(3, D),
364 	clk_src_regs(4, E),
365 	clk_src_regs(5, F)
366 };
367 
368 static const struct dce110_clk_src_shift cs_shift = {
369 		CS_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
370 };
371 
372 static const struct dce110_clk_src_mask cs_mask = {
373 		CS_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
374 };
375 
376 static const struct dce_dmcu_registers dmcu_regs = {
377 		DMCU_DCN10_REG_LIST()
378 };
379 
380 static const struct dce_dmcu_shift dmcu_shift = {
381 		DMCU_MASK_SH_LIST_DCN10(__SHIFT)
382 };
383 
384 static const struct dce_dmcu_mask dmcu_mask = {
385 		DMCU_MASK_SH_LIST_DCN10(_MASK)
386 };
387 
388 static const struct dce_abm_registers abm_regs = {
389 		ABM_DCN20_REG_LIST()
390 };
391 
392 static const struct dce_abm_shift abm_shift = {
393 		ABM_MASK_SH_LIST_DCN20(__SHIFT)
394 };
395 
396 static const struct dce_abm_mask abm_mask = {
397 		ABM_MASK_SH_LIST_DCN20(_MASK)
398 };
399 
400 #define audio_regs(id)\
401 [id] = {\
402 		AUD_COMMON_REG_LIST(id)\
403 }
404 
405 static const struct dce_audio_registers audio_regs[] = {
406 	audio_regs(0),
407 	audio_regs(1),
408 	audio_regs(2),
409 	audio_regs(3),
410 	audio_regs(4),
411 	audio_regs(5),
412 	audio_regs(6),
413 };
414 
415 #define DCE120_AUD_COMMON_MASK_SH_LIST(mask_sh)\
416 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_INDEX, AZALIA_ENDPOINT_REG_INDEX, mask_sh),\
417 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_DATA, AZALIA_ENDPOINT_REG_DATA, mask_sh),\
418 		AUD_COMMON_MASK_SH_LIST_BASE(mask_sh)
419 
420 static const struct dce_audio_shift audio_shift = {
421 		DCE120_AUD_COMMON_MASK_SH_LIST(__SHIFT)
422 };
423 
424 static const struct dce_audio_mask audio_mask = {
425 		DCE120_AUD_COMMON_MASK_SH_LIST(_MASK)
426 };
427 
428 #define stream_enc_regs(id)\
429 [id] = {\
430 	SE_DCN2_REG_LIST(id)\
431 }
432 
433 static const struct dcn10_stream_enc_registers stream_enc_regs[] = {
434 	stream_enc_regs(0),
435 	stream_enc_regs(1),
436 	stream_enc_regs(2),
437 	stream_enc_regs(3),
438 	stream_enc_regs(4),
439 	stream_enc_regs(5),
440 };
441 
442 static const struct dcn10_stream_encoder_shift se_shift = {
443 		SE_COMMON_MASK_SH_LIST_DCN20(__SHIFT)
444 };
445 
446 static const struct dcn10_stream_encoder_mask se_mask = {
447 		SE_COMMON_MASK_SH_LIST_DCN20(_MASK)
448 };
449 
450 
451 #define aux_regs(id)\
452 [id] = {\
453 	DCN2_AUX_REG_LIST(id)\
454 }
455 
456 static const struct dcn10_link_enc_aux_registers link_enc_aux_regs[] = {
457 		aux_regs(0),
458 		aux_regs(1),
459 		aux_regs(2),
460 		aux_regs(3),
461 		aux_regs(4),
462 		aux_regs(5)
463 };
464 
465 #define hpd_regs(id)\
466 [id] = {\
467 	HPD_REG_LIST(id)\
468 }
469 
470 static const struct dcn10_link_enc_hpd_registers link_enc_hpd_regs[] = {
471 		hpd_regs(0),
472 		hpd_regs(1),
473 		hpd_regs(2),
474 		hpd_regs(3),
475 		hpd_regs(4),
476 		hpd_regs(5)
477 };
478 
479 #define link_regs(id, phyid)\
480 [id] = {\
481 	LE_DCN10_REG_LIST(id), \
482 	UNIPHY_DCN2_REG_LIST(phyid), \
483 	SRI(DP_DPHY_INTERNAL_CTRL, DP, id) \
484 }
485 
486 static const struct dcn10_link_enc_registers link_enc_regs[] = {
487 	link_regs(0, A),
488 	link_regs(1, B),
489 	link_regs(2, C),
490 	link_regs(3, D),
491 	link_regs(4, E),
492 	link_regs(5, F)
493 };
494 
495 static const struct dcn10_link_enc_shift le_shift = {
496 	LINK_ENCODER_MASK_SH_LIST_DCN20(__SHIFT)
497 };
498 
499 static const struct dcn10_link_enc_mask le_mask = {
500 	LINK_ENCODER_MASK_SH_LIST_DCN20(_MASK)
501 };
502 
503 #define ipp_regs(id)\
504 [id] = {\
505 	IPP_REG_LIST_DCN20(id),\
506 }
507 
508 static const struct dcn10_ipp_registers ipp_regs[] = {
509 	ipp_regs(0),
510 	ipp_regs(1),
511 	ipp_regs(2),
512 	ipp_regs(3),
513 	ipp_regs(4),
514 	ipp_regs(5),
515 };
516 
517 static const struct dcn10_ipp_shift ipp_shift = {
518 		IPP_MASK_SH_LIST_DCN20(__SHIFT)
519 };
520 
521 static const struct dcn10_ipp_mask ipp_mask = {
522 		IPP_MASK_SH_LIST_DCN20(_MASK),
523 };
524 
525 #define opp_regs(id)\
526 [id] = {\
527 	OPP_REG_LIST_DCN20(id),\
528 }
529 
530 static const struct dcn20_opp_registers opp_regs[] = {
531 	opp_regs(0),
532 	opp_regs(1),
533 	opp_regs(2),
534 	opp_regs(3),
535 	opp_regs(4),
536 	opp_regs(5),
537 };
538 
539 static const struct dcn20_opp_shift opp_shift = {
540 		OPP_MASK_SH_LIST_DCN20(__SHIFT)
541 };
542 
543 static const struct dcn20_opp_mask opp_mask = {
544 		OPP_MASK_SH_LIST_DCN20(_MASK)
545 };
546 
547 #define aux_engine_regs(id)\
548 [id] = {\
549 	AUX_COMMON_REG_LIST0(id), \
550 	.AUXN_IMPCAL = 0, \
551 	.AUXP_IMPCAL = 0, \
552 	.AUX_RESET_MASK = DP_AUX0_AUX_CONTROL__AUX_RESET_MASK, \
553 }
554 
555 static const struct dce110_aux_registers aux_engine_regs[] = {
556 		aux_engine_regs(0),
557 		aux_engine_regs(1),
558 		aux_engine_regs(2),
559 		aux_engine_regs(3),
560 		aux_engine_regs(4),
561 		aux_engine_regs(5)
562 };
563 
564 #define tf_regs(id)\
565 [id] = {\
566 	TF_REG_LIST_DCN20(id),\
567 }
568 
569 static const struct dcn2_dpp_registers tf_regs[] = {
570 	tf_regs(0),
571 	tf_regs(1),
572 	tf_regs(2),
573 	tf_regs(3),
574 	tf_regs(4),
575 	tf_regs(5),
576 };
577 
578 static const struct dcn2_dpp_shift tf_shift = {
579 		TF_REG_LIST_SH_MASK_DCN20(__SHIFT),
580 		TF_DEBUG_REG_LIST_SH_DCN10
581 };
582 
583 static const struct dcn2_dpp_mask tf_mask = {
584 		TF_REG_LIST_SH_MASK_DCN20(_MASK),
585 		TF_DEBUG_REG_LIST_MASK_DCN10
586 };
587 
588 #define dwbc_regs_dcn2(id)\
589 [id] = {\
590 	DWBC_COMMON_REG_LIST_DCN2_0(id),\
591 		}
592 
593 static const struct dcn20_dwbc_registers dwbc20_regs[] = {
594 	dwbc_regs_dcn2(0),
595 };
596 
597 static const struct dcn20_dwbc_shift dwbc20_shift = {
598 	DWBC_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
599 };
600 
601 static const struct dcn20_dwbc_mask dwbc20_mask = {
602 	DWBC_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
603 };
604 
605 #define mcif_wb_regs_dcn2(id)\
606 [id] = {\
607 	MCIF_WB_COMMON_REG_LIST_DCN2_0(id),\
608 		}
609 
610 static const struct dcn20_mmhubbub_registers mcif_wb20_regs[] = {
611 	mcif_wb_regs_dcn2(0),
612 };
613 
614 static const struct dcn20_mmhubbub_shift mcif_wb20_shift = {
615 	MCIF_WB_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
616 };
617 
618 static const struct dcn20_mmhubbub_mask mcif_wb20_mask = {
619 	MCIF_WB_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
620 };
621 
622 static const struct dcn20_mpc_registers mpc_regs = {
623 		MPC_REG_LIST_DCN2_0(0),
624 		MPC_REG_LIST_DCN2_0(1),
625 		MPC_REG_LIST_DCN2_0(2),
626 		MPC_REG_LIST_DCN2_0(3),
627 		MPC_REG_LIST_DCN2_0(4),
628 		MPC_REG_LIST_DCN2_0(5),
629 		MPC_OUT_MUX_REG_LIST_DCN2_0(0),
630 		MPC_OUT_MUX_REG_LIST_DCN2_0(1),
631 		MPC_OUT_MUX_REG_LIST_DCN2_0(2),
632 		MPC_OUT_MUX_REG_LIST_DCN2_0(3),
633 		MPC_OUT_MUX_REG_LIST_DCN2_0(4),
634 		MPC_OUT_MUX_REG_LIST_DCN2_0(5),
635 };
636 
637 static const struct dcn20_mpc_shift mpc_shift = {
638 	MPC_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
639 };
640 
641 static const struct dcn20_mpc_mask mpc_mask = {
642 	MPC_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
643 };
644 
645 #define tg_regs(id)\
646 [id] = {TG_COMMON_REG_LIST_DCN2_0(id)}
647 
648 
649 static const struct dcn_optc_registers tg_regs[] = {
650 	tg_regs(0),
651 	tg_regs(1),
652 	tg_regs(2),
653 	tg_regs(3),
654 	tg_regs(4),
655 	tg_regs(5)
656 };
657 
658 static const struct dcn_optc_shift tg_shift = {
659 	TG_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
660 };
661 
662 static const struct dcn_optc_mask tg_mask = {
663 	TG_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
664 };
665 
666 #define hubp_regs(id)\
667 [id] = {\
668 	HUBP_REG_LIST_DCN20(id)\
669 }
670 
671 static const struct dcn_hubp2_registers hubp_regs[] = {
672 		hubp_regs(0),
673 		hubp_regs(1),
674 		hubp_regs(2),
675 		hubp_regs(3),
676 		hubp_regs(4),
677 		hubp_regs(5)
678 };
679 
680 static const struct dcn_hubp2_shift hubp_shift = {
681 		HUBP_MASK_SH_LIST_DCN20(__SHIFT)
682 };
683 
684 static const struct dcn_hubp2_mask hubp_mask = {
685 		HUBP_MASK_SH_LIST_DCN20(_MASK)
686 };
687 
688 static const struct dcn_hubbub_registers hubbub_reg = {
689 		HUBBUB_REG_LIST_DCN20(0)
690 };
691 
692 static const struct dcn_hubbub_shift hubbub_shift = {
693 		HUBBUB_MASK_SH_LIST_DCN20(__SHIFT)
694 };
695 
696 static const struct dcn_hubbub_mask hubbub_mask = {
697 		HUBBUB_MASK_SH_LIST_DCN20(_MASK)
698 };
699 
700 #define vmid_regs(id)\
701 [id] = {\
702 		DCN20_VMID_REG_LIST(id)\
703 }
704 
705 static const struct dcn_vmid_registers vmid_regs[] = {
706 	vmid_regs(0),
707 	vmid_regs(1),
708 	vmid_regs(2),
709 	vmid_regs(3),
710 	vmid_regs(4),
711 	vmid_regs(5),
712 	vmid_regs(6),
713 	vmid_regs(7),
714 	vmid_regs(8),
715 	vmid_regs(9),
716 	vmid_regs(10),
717 	vmid_regs(11),
718 	vmid_regs(12),
719 	vmid_regs(13),
720 	vmid_regs(14),
721 	vmid_regs(15)
722 };
723 
724 static const struct dcn20_vmid_shift vmid_shifts = {
725 		DCN20_VMID_MASK_SH_LIST(__SHIFT)
726 };
727 
728 static const struct dcn20_vmid_mask vmid_masks = {
729 		DCN20_VMID_MASK_SH_LIST(_MASK)
730 };
731 
732 static const struct dce110_aux_registers_shift aux_shift = {
733 		DCN_AUX_MASK_SH_LIST(__SHIFT)
734 };
735 
736 static const struct dce110_aux_registers_mask aux_mask = {
737 		DCN_AUX_MASK_SH_LIST(_MASK)
738 };
739 
740 static int map_transmitter_id_to_phy_instance(
741 	enum transmitter transmitter)
742 {
743 	switch (transmitter) {
744 	case TRANSMITTER_UNIPHY_A:
745 		return 0;
746 	break;
747 	case TRANSMITTER_UNIPHY_B:
748 		return 1;
749 	break;
750 	case TRANSMITTER_UNIPHY_C:
751 		return 2;
752 	break;
753 	case TRANSMITTER_UNIPHY_D:
754 		return 3;
755 	break;
756 	case TRANSMITTER_UNIPHY_E:
757 		return 4;
758 	break;
759 	case TRANSMITTER_UNIPHY_F:
760 		return 5;
761 	break;
762 	default:
763 		ASSERT(0);
764 		return 0;
765 	}
766 }
767 
768 #define dsc_regsDCN20(id)\
769 [id] = {\
770 	DSC_REG_LIST_DCN20(id)\
771 }
772 
773 static const struct dcn20_dsc_registers dsc_regs[] = {
774 	dsc_regsDCN20(0),
775 	dsc_regsDCN20(1),
776 	dsc_regsDCN20(2),
777 	dsc_regsDCN20(3),
778 	dsc_regsDCN20(4),
779 	dsc_regsDCN20(5)
780 };
781 
782 static const struct dcn20_dsc_shift dsc_shift = {
783 	DSC_REG_LIST_SH_MASK_DCN20(__SHIFT)
784 };
785 
786 static const struct dcn20_dsc_mask dsc_mask = {
787 	DSC_REG_LIST_SH_MASK_DCN20(_MASK)
788 };
789 
790 static const struct dccg_registers dccg_regs = {
791 		DCCG_REG_LIST_DCN2()
792 };
793 
794 static const struct dccg_shift dccg_shift = {
795 		DCCG_MASK_SH_LIST_DCN2(__SHIFT)
796 };
797 
798 static const struct dccg_mask dccg_mask = {
799 		DCCG_MASK_SH_LIST_DCN2(_MASK)
800 };
801 
802 static const struct resource_caps res_cap_nv10 = {
803 		.num_timing_generator = 6,
804 		.num_opp = 6,
805 		.num_video_plane = 6,
806 		.num_audio = 7,
807 		.num_stream_encoder = 6,
808 		.num_pll = 6,
809 		.num_dwb = 1,
810 		.num_ddc = 6,
811 		.num_vmid = 16,
812 		.num_dsc = 6,
813 };
814 
815 static const struct dc_plane_cap plane_cap = {
816 	.type = DC_PLANE_TYPE_DCN_UNIVERSAL,
817 	.blends_with_above = true,
818 	.blends_with_below = true,
819 	.per_pixel_alpha = true,
820 
821 	.pixel_format_support = {
822 			.argb8888 = true,
823 			.nv12 = true,
824 			.fp16 = true
825 	},
826 
827 	.max_upscale_factor = {
828 			.argb8888 = 16000,
829 			.nv12 = 16000,
830 			.fp16 = 1
831 	},
832 
833 	.max_downscale_factor = {
834 			.argb8888 = 250,
835 			.nv12 = 250,
836 			.fp16 = 1
837 	}
838 };
839 static const struct resource_caps res_cap_nv14 = {
840 		.num_timing_generator = 5,
841 		.num_opp = 5,
842 		.num_video_plane = 5,
843 		.num_audio = 6,
844 		.num_stream_encoder = 5,
845 		.num_pll = 5,
846 		.num_dwb = 1,
847 		.num_ddc = 5,
848 };
849 
850 static const struct dc_debug_options debug_defaults_drv = {
851 		.disable_dmcu = true,
852 		.force_abm_enable = false,
853 		.timing_trace = false,
854 		.clock_trace = true,
855 		.disable_pplib_clock_request = true,
856 		.pipe_split_policy = MPC_SPLIT_DYNAMIC,
857 		.force_single_disp_pipe_split = false,
858 		.disable_dcc = DCC_ENABLE,
859 		.vsr_support = true,
860 		.performance_trace = false,
861 		.max_downscale_src_width = 5120,/*upto 5K*/
862 		.disable_pplib_wm_range = false,
863 		.scl_reset_length10 = true,
864 		.sanity_checks = false,
865 		.disable_tri_buf = true,
866 		.underflow_assert_delay_us = 0xFFFFFFFF,
867 };
868 
869 static const struct dc_debug_options debug_defaults_diags = {
870 		.disable_dmcu = true,
871 		.force_abm_enable = false,
872 		.timing_trace = true,
873 		.clock_trace = true,
874 		.disable_dpp_power_gate = true,
875 		.disable_hubp_power_gate = true,
876 		.disable_clock_gate = true,
877 		.disable_pplib_clock_request = true,
878 		.disable_pplib_wm_range = true,
879 		.disable_stutter = true,
880 		.scl_reset_length10 = true,
881 		.underflow_assert_delay_us = 0xFFFFFFFF,
882 };
883 
884 void dcn20_dpp_destroy(struct dpp **dpp)
885 {
886 	kfree(TO_DCN20_DPP(*dpp));
887 	*dpp = NULL;
888 }
889 
890 struct dpp *dcn20_dpp_create(
891 	struct dc_context *ctx,
892 	uint32_t inst)
893 {
894 	struct dcn20_dpp *dpp =
895 		kzalloc(sizeof(struct dcn20_dpp), GFP_KERNEL);
896 
897 	if (!dpp)
898 		return NULL;
899 
900 	if (dpp2_construct(dpp, ctx, inst,
901 			&tf_regs[inst], &tf_shift, &tf_mask))
902 		return &dpp->base;
903 
904 	BREAK_TO_DEBUGGER();
905 	kfree(dpp);
906 	return NULL;
907 }
908 
909 struct input_pixel_processor *dcn20_ipp_create(
910 	struct dc_context *ctx, uint32_t inst)
911 {
912 	struct dcn10_ipp *ipp =
913 		kzalloc(sizeof(struct dcn10_ipp), GFP_KERNEL);
914 
915 	if (!ipp) {
916 		BREAK_TO_DEBUGGER();
917 		return NULL;
918 	}
919 
920 	dcn20_ipp_construct(ipp, ctx, inst,
921 			&ipp_regs[inst], &ipp_shift, &ipp_mask);
922 	return &ipp->base;
923 }
924 
925 
926 struct output_pixel_processor *dcn20_opp_create(
927 	struct dc_context *ctx, uint32_t inst)
928 {
929 	struct dcn20_opp *opp =
930 		kzalloc(sizeof(struct dcn20_opp), GFP_KERNEL);
931 
932 	if (!opp) {
933 		BREAK_TO_DEBUGGER();
934 		return NULL;
935 	}
936 
937 	dcn20_opp_construct(opp, ctx, inst,
938 			&opp_regs[inst], &opp_shift, &opp_mask);
939 	return &opp->base;
940 }
941 
942 struct dce_aux *dcn20_aux_engine_create(
943 	struct dc_context *ctx,
944 	uint32_t inst)
945 {
946 	struct aux_engine_dce110 *aux_engine =
947 		kzalloc(sizeof(struct aux_engine_dce110), GFP_KERNEL);
948 
949 	if (!aux_engine)
950 		return NULL;
951 
952 	dce110_aux_engine_construct(aux_engine, ctx, inst,
953 				    SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD,
954 				    &aux_engine_regs[inst],
955 					&aux_mask,
956 					&aux_shift,
957 					ctx->dc->caps.extended_aux_timeout_support);
958 
959 	return &aux_engine->base;
960 }
961 #define i2c_inst_regs(id) { I2C_HW_ENGINE_COMMON_REG_LIST(id) }
962 
963 static const struct dce_i2c_registers i2c_hw_regs[] = {
964 		i2c_inst_regs(1),
965 		i2c_inst_regs(2),
966 		i2c_inst_regs(3),
967 		i2c_inst_regs(4),
968 		i2c_inst_regs(5),
969 		i2c_inst_regs(6),
970 };
971 
972 static const struct dce_i2c_shift i2c_shifts = {
973 		I2C_COMMON_MASK_SH_LIST_DCN2(__SHIFT)
974 };
975 
976 static const struct dce_i2c_mask i2c_masks = {
977 		I2C_COMMON_MASK_SH_LIST_DCN2(_MASK)
978 };
979 
980 struct dce_i2c_hw *dcn20_i2c_hw_create(
981 	struct dc_context *ctx,
982 	uint32_t inst)
983 {
984 	struct dce_i2c_hw *dce_i2c_hw =
985 		kzalloc(sizeof(struct dce_i2c_hw), GFP_KERNEL);
986 
987 	if (!dce_i2c_hw)
988 		return NULL;
989 
990 	dcn2_i2c_hw_construct(dce_i2c_hw, ctx, inst,
991 				    &i2c_hw_regs[inst], &i2c_shifts, &i2c_masks);
992 
993 	return dce_i2c_hw;
994 }
995 struct mpc *dcn20_mpc_create(struct dc_context *ctx)
996 {
997 	struct dcn20_mpc *mpc20 = kzalloc(sizeof(struct dcn20_mpc),
998 					  GFP_KERNEL);
999 
1000 	if (!mpc20)
1001 		return NULL;
1002 
1003 	dcn20_mpc_construct(mpc20, ctx,
1004 			&mpc_regs,
1005 			&mpc_shift,
1006 			&mpc_mask,
1007 			6);
1008 
1009 	return &mpc20->base;
1010 }
1011 
1012 struct hubbub *dcn20_hubbub_create(struct dc_context *ctx)
1013 {
1014 	int i;
1015 	struct dcn20_hubbub *hubbub = kzalloc(sizeof(struct dcn20_hubbub),
1016 					  GFP_KERNEL);
1017 
1018 	if (!hubbub)
1019 		return NULL;
1020 
1021 	hubbub2_construct(hubbub, ctx,
1022 			&hubbub_reg,
1023 			&hubbub_shift,
1024 			&hubbub_mask);
1025 
1026 	for (i = 0; i < res_cap_nv10.num_vmid; i++) {
1027 		struct dcn20_vmid *vmid = &hubbub->vmid[i];
1028 
1029 		vmid->ctx = ctx;
1030 
1031 		vmid->regs = &vmid_regs[i];
1032 		vmid->shifts = &vmid_shifts;
1033 		vmid->masks = &vmid_masks;
1034 	}
1035 
1036 	return &hubbub->base;
1037 }
1038 
1039 struct timing_generator *dcn20_timing_generator_create(
1040 		struct dc_context *ctx,
1041 		uint32_t instance)
1042 {
1043 	struct optc *tgn10 =
1044 		kzalloc(sizeof(struct optc), GFP_KERNEL);
1045 
1046 	if (!tgn10)
1047 		return NULL;
1048 
1049 	tgn10->base.inst = instance;
1050 	tgn10->base.ctx = ctx;
1051 
1052 	tgn10->tg_regs = &tg_regs[instance];
1053 	tgn10->tg_shift = &tg_shift;
1054 	tgn10->tg_mask = &tg_mask;
1055 
1056 	dcn20_timing_generator_init(tgn10);
1057 
1058 	return &tgn10->base;
1059 }
1060 
1061 static const struct encoder_feature_support link_enc_feature = {
1062 		.max_hdmi_deep_color = COLOR_DEPTH_121212,
1063 		.max_hdmi_pixel_clock = 600000,
1064 		.hdmi_ycbcr420_supported = true,
1065 		.dp_ycbcr420_supported = true,
1066 		.flags.bits.IS_HBR2_CAPABLE = true,
1067 		.flags.bits.IS_HBR3_CAPABLE = true,
1068 		.flags.bits.IS_TPS3_CAPABLE = true,
1069 		.flags.bits.IS_TPS4_CAPABLE = true
1070 };
1071 
1072 struct link_encoder *dcn20_link_encoder_create(
1073 	const struct encoder_init_data *enc_init_data)
1074 {
1075 	struct dcn20_link_encoder *enc20 =
1076 		kzalloc(sizeof(struct dcn20_link_encoder), GFP_KERNEL);
1077 	int link_regs_id;
1078 
1079 	if (!enc20)
1080 		return NULL;
1081 
1082 	link_regs_id =
1083 		map_transmitter_id_to_phy_instance(enc_init_data->transmitter);
1084 
1085 	dcn20_link_encoder_construct(enc20,
1086 				      enc_init_data,
1087 				      &link_enc_feature,
1088 				      &link_enc_regs[link_regs_id],
1089 				      &link_enc_aux_regs[enc_init_data->channel - 1],
1090 				      &link_enc_hpd_regs[enc_init_data->hpd_source],
1091 				      &le_shift,
1092 				      &le_mask);
1093 
1094 	return &enc20->enc10.base;
1095 }
1096 
1097 struct clock_source *dcn20_clock_source_create(
1098 	struct dc_context *ctx,
1099 	struct dc_bios *bios,
1100 	enum clock_source_id id,
1101 	const struct dce110_clk_src_regs *regs,
1102 	bool dp_clk_src)
1103 {
1104 	struct dce110_clk_src *clk_src =
1105 		kzalloc(sizeof(struct dce110_clk_src), GFP_KERNEL);
1106 
1107 	if (!clk_src)
1108 		return NULL;
1109 
1110 	if (dcn20_clk_src_construct(clk_src, ctx, bios, id,
1111 			regs, &cs_shift, &cs_mask)) {
1112 		clk_src->base.dp_clk_src = dp_clk_src;
1113 		return &clk_src->base;
1114 	}
1115 
1116 	kfree(clk_src);
1117 	BREAK_TO_DEBUGGER();
1118 	return NULL;
1119 }
1120 
1121 static void read_dce_straps(
1122 	struct dc_context *ctx,
1123 	struct resource_straps *straps)
1124 {
1125 	generic_reg_get(ctx, mmDC_PINSTRAPS + BASE(mmDC_PINSTRAPS_BASE_IDX),
1126 		FN(DC_PINSTRAPS, DC_PINSTRAPS_AUDIO), &straps->dc_pinstraps_audio);
1127 }
1128 
1129 static struct audio *dcn20_create_audio(
1130 		struct dc_context *ctx, unsigned int inst)
1131 {
1132 	return dce_audio_create(ctx, inst,
1133 			&audio_regs[inst], &audio_shift, &audio_mask);
1134 }
1135 
1136 struct stream_encoder *dcn20_stream_encoder_create(
1137 	enum engine_id eng_id,
1138 	struct dc_context *ctx)
1139 {
1140 	struct dcn10_stream_encoder *enc1 =
1141 		kzalloc(sizeof(struct dcn10_stream_encoder), GFP_KERNEL);
1142 
1143 	if (!enc1)
1144 		return NULL;
1145 
1146 	if (ASICREV_IS_NAVI14_M(ctx->asic_id.hw_internal_rev)) {
1147 		if (eng_id >= ENGINE_ID_DIGD)
1148 			eng_id++;
1149 	}
1150 
1151 	dcn20_stream_encoder_construct(enc1, ctx, ctx->dc_bios, eng_id,
1152 					&stream_enc_regs[eng_id],
1153 					&se_shift, &se_mask);
1154 
1155 	return &enc1->base;
1156 }
1157 
1158 static const struct dce_hwseq_registers hwseq_reg = {
1159 		HWSEQ_DCN2_REG_LIST()
1160 };
1161 
1162 static const struct dce_hwseq_shift hwseq_shift = {
1163 		HWSEQ_DCN2_MASK_SH_LIST(__SHIFT)
1164 };
1165 
1166 static const struct dce_hwseq_mask hwseq_mask = {
1167 		HWSEQ_DCN2_MASK_SH_LIST(_MASK)
1168 };
1169 
1170 struct dce_hwseq *dcn20_hwseq_create(
1171 	struct dc_context *ctx)
1172 {
1173 	struct dce_hwseq *hws = kzalloc(sizeof(struct dce_hwseq), GFP_KERNEL);
1174 
1175 	if (hws) {
1176 		hws->ctx = ctx;
1177 		hws->regs = &hwseq_reg;
1178 		hws->shifts = &hwseq_shift;
1179 		hws->masks = &hwseq_mask;
1180 	}
1181 	return hws;
1182 }
1183 
1184 static const struct resource_create_funcs res_create_funcs = {
1185 	.read_dce_straps = read_dce_straps,
1186 	.create_audio = dcn20_create_audio,
1187 	.create_stream_encoder = dcn20_stream_encoder_create,
1188 	.create_hwseq = dcn20_hwseq_create,
1189 };
1190 
1191 static const struct resource_create_funcs res_create_maximus_funcs = {
1192 	.read_dce_straps = NULL,
1193 	.create_audio = NULL,
1194 	.create_stream_encoder = NULL,
1195 	.create_hwseq = dcn20_hwseq_create,
1196 };
1197 
1198 static void dcn20_pp_smu_destroy(struct pp_smu_funcs **pp_smu);
1199 
1200 void dcn20_clock_source_destroy(struct clock_source **clk_src)
1201 {
1202 	kfree(TO_DCE110_CLK_SRC(*clk_src));
1203 	*clk_src = NULL;
1204 }
1205 
1206 
1207 struct display_stream_compressor *dcn20_dsc_create(
1208 	struct dc_context *ctx, uint32_t inst)
1209 {
1210 	struct dcn20_dsc *dsc =
1211 		kzalloc(sizeof(struct dcn20_dsc), GFP_KERNEL);
1212 
1213 	if (!dsc) {
1214 		BREAK_TO_DEBUGGER();
1215 		return NULL;
1216 	}
1217 
1218 	dsc2_construct(dsc, ctx, inst, &dsc_regs[inst], &dsc_shift, &dsc_mask);
1219 	return &dsc->base;
1220 }
1221 
1222 void dcn20_dsc_destroy(struct display_stream_compressor **dsc)
1223 {
1224 	kfree(container_of(*dsc, struct dcn20_dsc, base));
1225 	*dsc = NULL;
1226 }
1227 
1228 
1229 static void destruct(struct dcn20_resource_pool *pool)
1230 {
1231 	unsigned int i;
1232 
1233 	for (i = 0; i < pool->base.stream_enc_count; i++) {
1234 		if (pool->base.stream_enc[i] != NULL) {
1235 			kfree(DCN10STRENC_FROM_STRENC(pool->base.stream_enc[i]));
1236 			pool->base.stream_enc[i] = NULL;
1237 		}
1238 	}
1239 
1240 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
1241 		if (pool->base.dscs[i] != NULL)
1242 			dcn20_dsc_destroy(&pool->base.dscs[i]);
1243 	}
1244 
1245 	if (pool->base.mpc != NULL) {
1246 		kfree(TO_DCN20_MPC(pool->base.mpc));
1247 		pool->base.mpc = NULL;
1248 	}
1249 	if (pool->base.hubbub != NULL) {
1250 		kfree(pool->base.hubbub);
1251 		pool->base.hubbub = NULL;
1252 	}
1253 	for (i = 0; i < pool->base.pipe_count; i++) {
1254 		if (pool->base.dpps[i] != NULL)
1255 			dcn20_dpp_destroy(&pool->base.dpps[i]);
1256 
1257 		if (pool->base.ipps[i] != NULL)
1258 			pool->base.ipps[i]->funcs->ipp_destroy(&pool->base.ipps[i]);
1259 
1260 		if (pool->base.hubps[i] != NULL) {
1261 			kfree(TO_DCN20_HUBP(pool->base.hubps[i]));
1262 			pool->base.hubps[i] = NULL;
1263 		}
1264 
1265 		if (pool->base.irqs != NULL) {
1266 			dal_irq_service_destroy(&pool->base.irqs);
1267 		}
1268 	}
1269 
1270 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1271 		if (pool->base.engines[i] != NULL)
1272 			dce110_engine_destroy(&pool->base.engines[i]);
1273 		if (pool->base.hw_i2cs[i] != NULL) {
1274 			kfree(pool->base.hw_i2cs[i]);
1275 			pool->base.hw_i2cs[i] = NULL;
1276 		}
1277 		if (pool->base.sw_i2cs[i] != NULL) {
1278 			kfree(pool->base.sw_i2cs[i]);
1279 			pool->base.sw_i2cs[i] = NULL;
1280 		}
1281 	}
1282 
1283 	for (i = 0; i < pool->base.res_cap->num_opp; i++) {
1284 		if (pool->base.opps[i] != NULL)
1285 			pool->base.opps[i]->funcs->opp_destroy(&pool->base.opps[i]);
1286 	}
1287 
1288 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1289 		if (pool->base.timing_generators[i] != NULL)	{
1290 			kfree(DCN10TG_FROM_TG(pool->base.timing_generators[i]));
1291 			pool->base.timing_generators[i] = NULL;
1292 		}
1293 	}
1294 
1295 	for (i = 0; i < pool->base.res_cap->num_dwb; i++) {
1296 		if (pool->base.dwbc[i] != NULL) {
1297 			kfree(TO_DCN20_DWBC(pool->base.dwbc[i]));
1298 			pool->base.dwbc[i] = NULL;
1299 		}
1300 		if (pool->base.mcif_wb[i] != NULL) {
1301 			kfree(TO_DCN20_MMHUBBUB(pool->base.mcif_wb[i]));
1302 			pool->base.mcif_wb[i] = NULL;
1303 		}
1304 	}
1305 
1306 	for (i = 0; i < pool->base.audio_count; i++) {
1307 		if (pool->base.audios[i])
1308 			dce_aud_destroy(&pool->base.audios[i]);
1309 	}
1310 
1311 	for (i = 0; i < pool->base.clk_src_count; i++) {
1312 		if (pool->base.clock_sources[i] != NULL) {
1313 			dcn20_clock_source_destroy(&pool->base.clock_sources[i]);
1314 			pool->base.clock_sources[i] = NULL;
1315 		}
1316 	}
1317 
1318 	if (pool->base.dp_clock_source != NULL) {
1319 		dcn20_clock_source_destroy(&pool->base.dp_clock_source);
1320 		pool->base.dp_clock_source = NULL;
1321 	}
1322 
1323 
1324 	if (pool->base.abm != NULL)
1325 		dce_abm_destroy(&pool->base.abm);
1326 
1327 	if (pool->base.dmcu != NULL)
1328 		dce_dmcu_destroy(&pool->base.dmcu);
1329 
1330 	if (pool->base.dccg != NULL)
1331 		dcn_dccg_destroy(&pool->base.dccg);
1332 
1333 	if (pool->base.pp_smu != NULL)
1334 		dcn20_pp_smu_destroy(&pool->base.pp_smu);
1335 
1336 	if (pool->base.oem_device != NULL)
1337 		dal_ddc_service_destroy(&pool->base.oem_device);
1338 }
1339 
1340 struct hubp *dcn20_hubp_create(
1341 	struct dc_context *ctx,
1342 	uint32_t inst)
1343 {
1344 	struct dcn20_hubp *hubp2 =
1345 		kzalloc(sizeof(struct dcn20_hubp), GFP_KERNEL);
1346 
1347 	if (!hubp2)
1348 		return NULL;
1349 
1350 	if (hubp2_construct(hubp2, ctx, inst,
1351 			&hubp_regs[inst], &hubp_shift, &hubp_mask))
1352 		return &hubp2->base;
1353 
1354 	BREAK_TO_DEBUGGER();
1355 	kfree(hubp2);
1356 	return NULL;
1357 }
1358 
1359 static void get_pixel_clock_parameters(
1360 	struct pipe_ctx *pipe_ctx,
1361 	struct pixel_clk_params *pixel_clk_params)
1362 {
1363 	const struct dc_stream_state *stream = pipe_ctx->stream;
1364 	struct pipe_ctx *odm_pipe;
1365 	int opp_cnt = 1;
1366 
1367 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
1368 		opp_cnt++;
1369 
1370 	pixel_clk_params->requested_pix_clk_100hz = stream->timing.pix_clk_100hz;
1371 	pixel_clk_params->encoder_object_id = stream->link->link_enc->id;
1372 	pixel_clk_params->signal_type = pipe_ctx->stream->signal;
1373 	pixel_clk_params->controller_id = pipe_ctx->stream_res.tg->inst + 1;
1374 	/* TODO: un-hardcode*/
1375 	pixel_clk_params->requested_sym_clk = LINK_RATE_LOW *
1376 		LINK_RATE_REF_FREQ_IN_KHZ;
1377 	pixel_clk_params->flags.ENABLE_SS = 0;
1378 	pixel_clk_params->color_depth =
1379 		stream->timing.display_color_depth;
1380 	pixel_clk_params->flags.DISPLAY_BLANKED = 1;
1381 	pixel_clk_params->pixel_encoding = stream->timing.pixel_encoding;
1382 
1383 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
1384 		pixel_clk_params->color_depth = COLOR_DEPTH_888;
1385 
1386 	if (opp_cnt == 4)
1387 		pixel_clk_params->requested_pix_clk_100hz /= 4;
1388 	else if (optc1_is_two_pixels_per_containter(&stream->timing) || opp_cnt == 2)
1389 		pixel_clk_params->requested_pix_clk_100hz /= 2;
1390 
1391 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
1392 		pixel_clk_params->requested_pix_clk_100hz *= 2;
1393 
1394 }
1395 
1396 static void build_clamping_params(struct dc_stream_state *stream)
1397 {
1398 	stream->clamping.clamping_level = CLAMPING_FULL_RANGE;
1399 	stream->clamping.c_depth = stream->timing.display_color_depth;
1400 	stream->clamping.pixel_encoding = stream->timing.pixel_encoding;
1401 }
1402 
1403 static enum dc_status build_pipe_hw_param(struct pipe_ctx *pipe_ctx)
1404 {
1405 
1406 	get_pixel_clock_parameters(pipe_ctx, &pipe_ctx->stream_res.pix_clk_params);
1407 
1408 	pipe_ctx->clock_source->funcs->get_pix_clk_dividers(
1409 		pipe_ctx->clock_source,
1410 		&pipe_ctx->stream_res.pix_clk_params,
1411 		&pipe_ctx->pll_settings);
1412 
1413 	pipe_ctx->stream->clamping.pixel_encoding = pipe_ctx->stream->timing.pixel_encoding;
1414 
1415 	resource_build_bit_depth_reduction_params(pipe_ctx->stream,
1416 					&pipe_ctx->stream->bit_depth_params);
1417 	build_clamping_params(pipe_ctx->stream);
1418 
1419 	return DC_OK;
1420 }
1421 
1422 enum dc_status dcn20_build_mapped_resource(const struct dc *dc, struct dc_state *context, struct dc_stream_state *stream)
1423 {
1424 	enum dc_status status = DC_OK;
1425 	struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
1426 
1427 	/*TODO Seems unneeded anymore */
1428 	/*	if (old_context && resource_is_stream_unchanged(old_context, stream)) {
1429 			if (stream != NULL && old_context->streams[i] != NULL) {
1430 				 todo: shouldn't have to copy missing parameter here
1431 				resource_build_bit_depth_reduction_params(stream,
1432 						&stream->bit_depth_params);
1433 				stream->clamping.pixel_encoding =
1434 						stream->timing.pixel_encoding;
1435 
1436 				resource_build_bit_depth_reduction_params(stream,
1437 								&stream->bit_depth_params);
1438 				build_clamping_params(stream);
1439 
1440 				continue;
1441 			}
1442 		}
1443 	*/
1444 
1445 	if (!pipe_ctx)
1446 		return DC_ERROR_UNEXPECTED;
1447 
1448 
1449 	status = build_pipe_hw_param(pipe_ctx);
1450 
1451 	return status;
1452 }
1453 
1454 
1455 static void acquire_dsc(struct resource_context *res_ctx,
1456 			const struct resource_pool *pool,
1457 			struct display_stream_compressor **dsc)
1458 {
1459 	int i;
1460 
1461 	ASSERT(*dsc == NULL);
1462 	*dsc = NULL;
1463 
1464 	/* Find first free DSC */
1465 	for (i = 0; i < pool->res_cap->num_dsc; i++)
1466 		if (!res_ctx->is_dsc_acquired[i]) {
1467 			*dsc = pool->dscs[i];
1468 			res_ctx->is_dsc_acquired[i] = true;
1469 			break;
1470 		}
1471 }
1472 
1473 static void release_dsc(struct resource_context *res_ctx,
1474 			const struct resource_pool *pool,
1475 			struct display_stream_compressor **dsc)
1476 {
1477 	int i;
1478 
1479 	for (i = 0; i < pool->res_cap->num_dsc; i++)
1480 		if (pool->dscs[i] == *dsc) {
1481 			res_ctx->is_dsc_acquired[i] = false;
1482 			*dsc = NULL;
1483 			break;
1484 		}
1485 }
1486 
1487 
1488 
1489 static enum dc_status add_dsc_to_stream_resource(struct dc *dc,
1490 		struct dc_state *dc_ctx,
1491 		struct dc_stream_state *dc_stream)
1492 {
1493 	enum dc_status result = DC_OK;
1494 	int i;
1495 	const struct resource_pool *pool = dc->res_pool;
1496 
1497 	/* Get a DSC if required and available */
1498 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1499 		struct pipe_ctx *pipe_ctx = &dc_ctx->res_ctx.pipe_ctx[i];
1500 
1501 		if (pipe_ctx->stream != dc_stream)
1502 			continue;
1503 
1504 		acquire_dsc(&dc_ctx->res_ctx, pool, &pipe_ctx->stream_res.dsc);
1505 
1506 		/* The number of DSCs can be less than the number of pipes */
1507 		if (!pipe_ctx->stream_res.dsc) {
1508 			dm_output_to_console("No DSCs available\n");
1509 			result = DC_NO_DSC_RESOURCE;
1510 		}
1511 
1512 		break;
1513 	}
1514 
1515 	return result;
1516 }
1517 
1518 
1519 static enum dc_status remove_dsc_from_stream_resource(struct dc *dc,
1520 		struct dc_state *new_ctx,
1521 		struct dc_stream_state *dc_stream)
1522 {
1523 	struct pipe_ctx *pipe_ctx = NULL;
1524 	int i;
1525 
1526 	for (i = 0; i < MAX_PIPES; i++) {
1527 		if (new_ctx->res_ctx.pipe_ctx[i].stream == dc_stream && !new_ctx->res_ctx.pipe_ctx[i].top_pipe) {
1528 			pipe_ctx = &new_ctx->res_ctx.pipe_ctx[i];
1529 
1530 			if (pipe_ctx->stream_res.dsc)
1531 				release_dsc(&new_ctx->res_ctx, dc->res_pool, &pipe_ctx->stream_res.dsc);
1532 		}
1533 	}
1534 
1535 	if (!pipe_ctx)
1536 		return DC_ERROR_UNEXPECTED;
1537 	else
1538 		return DC_OK;
1539 }
1540 
1541 
1542 enum dc_status dcn20_add_stream_to_ctx(struct dc *dc, struct dc_state *new_ctx, struct dc_stream_state *dc_stream)
1543 {
1544 	enum dc_status result = DC_ERROR_UNEXPECTED;
1545 
1546 	result = resource_map_pool_resources(dc, new_ctx, dc_stream);
1547 
1548 	if (result == DC_OK)
1549 		result = resource_map_phy_clock_resources(dc, new_ctx, dc_stream);
1550 
1551 	/* Get a DSC if required and available */
1552 	if (result == DC_OK && dc_stream->timing.flags.DSC)
1553 		result = add_dsc_to_stream_resource(dc, new_ctx, dc_stream);
1554 
1555 	if (result == DC_OK)
1556 		result = dcn20_build_mapped_resource(dc, new_ctx, dc_stream);
1557 
1558 	return result;
1559 }
1560 
1561 
1562 enum dc_status dcn20_remove_stream_from_ctx(struct dc *dc, struct dc_state *new_ctx, struct dc_stream_state *dc_stream)
1563 {
1564 	enum dc_status result = DC_OK;
1565 
1566 	result = remove_dsc_from_stream_resource(dc, new_ctx, dc_stream);
1567 
1568 	return result;
1569 }
1570 
1571 
1572 static void swizzle_to_dml_params(
1573 		enum swizzle_mode_values swizzle,
1574 		unsigned int *sw_mode)
1575 {
1576 	switch (swizzle) {
1577 	case DC_SW_LINEAR:
1578 		*sw_mode = dm_sw_linear;
1579 		break;
1580 	case DC_SW_4KB_S:
1581 		*sw_mode = dm_sw_4kb_s;
1582 		break;
1583 	case DC_SW_4KB_S_X:
1584 		*sw_mode = dm_sw_4kb_s_x;
1585 		break;
1586 	case DC_SW_4KB_D:
1587 		*sw_mode = dm_sw_4kb_d;
1588 		break;
1589 	case DC_SW_4KB_D_X:
1590 		*sw_mode = dm_sw_4kb_d_x;
1591 		break;
1592 	case DC_SW_64KB_S:
1593 		*sw_mode = dm_sw_64kb_s;
1594 		break;
1595 	case DC_SW_64KB_S_X:
1596 		*sw_mode = dm_sw_64kb_s_x;
1597 		break;
1598 	case DC_SW_64KB_S_T:
1599 		*sw_mode = dm_sw_64kb_s_t;
1600 		break;
1601 	case DC_SW_64KB_D:
1602 		*sw_mode = dm_sw_64kb_d;
1603 		break;
1604 	case DC_SW_64KB_D_X:
1605 		*sw_mode = dm_sw_64kb_d_x;
1606 		break;
1607 	case DC_SW_64KB_D_T:
1608 		*sw_mode = dm_sw_64kb_d_t;
1609 		break;
1610 	case DC_SW_64KB_R_X:
1611 		*sw_mode = dm_sw_64kb_r_x;
1612 		break;
1613 	case DC_SW_VAR_S:
1614 		*sw_mode = dm_sw_var_s;
1615 		break;
1616 	case DC_SW_VAR_S_X:
1617 		*sw_mode = dm_sw_var_s_x;
1618 		break;
1619 	case DC_SW_VAR_D:
1620 		*sw_mode = dm_sw_var_d;
1621 		break;
1622 	case DC_SW_VAR_D_X:
1623 		*sw_mode = dm_sw_var_d_x;
1624 		break;
1625 
1626 	default:
1627 		ASSERT(0); /* Not supported */
1628 		break;
1629 	}
1630 }
1631 
1632 bool dcn20_split_stream_for_odm(
1633 		struct resource_context *res_ctx,
1634 		const struct resource_pool *pool,
1635 		struct pipe_ctx *prev_odm_pipe,
1636 		struct pipe_ctx *next_odm_pipe)
1637 {
1638 	int pipe_idx = next_odm_pipe->pipe_idx;
1639 
1640 	*next_odm_pipe = *prev_odm_pipe;
1641 
1642 	next_odm_pipe->pipe_idx = pipe_idx;
1643 	next_odm_pipe->plane_res.mi = pool->mis[next_odm_pipe->pipe_idx];
1644 	next_odm_pipe->plane_res.hubp = pool->hubps[next_odm_pipe->pipe_idx];
1645 	next_odm_pipe->plane_res.ipp = pool->ipps[next_odm_pipe->pipe_idx];
1646 	next_odm_pipe->plane_res.xfm = pool->transforms[next_odm_pipe->pipe_idx];
1647 	next_odm_pipe->plane_res.dpp = pool->dpps[next_odm_pipe->pipe_idx];
1648 	next_odm_pipe->plane_res.mpcc_inst = pool->dpps[next_odm_pipe->pipe_idx]->inst;
1649 	next_odm_pipe->stream_res.dsc = NULL;
1650 	if (prev_odm_pipe->next_odm_pipe && prev_odm_pipe->next_odm_pipe != next_odm_pipe) {
1651 		next_odm_pipe->next_odm_pipe = prev_odm_pipe->next_odm_pipe;
1652 		next_odm_pipe->next_odm_pipe->prev_odm_pipe = next_odm_pipe;
1653 	}
1654 	prev_odm_pipe->next_odm_pipe = next_odm_pipe;
1655 	next_odm_pipe->prev_odm_pipe = prev_odm_pipe;
1656 	ASSERT(next_odm_pipe->top_pipe == NULL);
1657 
1658 	if (prev_odm_pipe->plane_state) {
1659 		struct scaler_data *sd = &prev_odm_pipe->plane_res.scl_data;
1660 		int new_width;
1661 
1662 		/* HACTIVE halved for odm combine */
1663 		sd->h_active /= 2;
1664 		/* Calculate new vp and recout for left pipe */
1665 		/* Need at least 16 pixels width per side */
1666 		if (sd->recout.x + 16 >= sd->h_active)
1667 			return false;
1668 		new_width = sd->h_active - sd->recout.x;
1669 		sd->viewport.width -= dc_fixpt_floor(dc_fixpt_mul_int(
1670 				sd->ratios.horz, sd->recout.width - new_width));
1671 		sd->viewport_c.width -= dc_fixpt_floor(dc_fixpt_mul_int(
1672 				sd->ratios.horz_c, sd->recout.width - new_width));
1673 		sd->recout.width = new_width;
1674 
1675 		/* Calculate new vp and recout for right pipe */
1676 		sd = &next_odm_pipe->plane_res.scl_data;
1677 		/* HACTIVE halved for odm combine */
1678 		sd->h_active /= 2;
1679 		/* Need at least 16 pixels width per side */
1680 		if (new_width <= 16)
1681 			return false;
1682 		new_width = sd->recout.width + sd->recout.x - sd->h_active;
1683 		sd->viewport.width -= dc_fixpt_floor(dc_fixpt_mul_int(
1684 				sd->ratios.horz, sd->recout.width - new_width));
1685 		sd->viewport_c.width -= dc_fixpt_floor(dc_fixpt_mul_int(
1686 				sd->ratios.horz_c, sd->recout.width - new_width));
1687 		sd->recout.width = new_width;
1688 		sd->viewport.x += dc_fixpt_floor(dc_fixpt_mul_int(
1689 				sd->ratios.horz, sd->h_active - sd->recout.x));
1690 		sd->viewport_c.x += dc_fixpt_floor(dc_fixpt_mul_int(
1691 				sd->ratios.horz_c, sd->h_active - sd->recout.x));
1692 		sd->recout.x = 0;
1693 	}
1694 	next_odm_pipe->stream_res.opp = pool->opps[next_odm_pipe->pipe_idx];
1695 	if (next_odm_pipe->stream->timing.flags.DSC == 1) {
1696 		acquire_dsc(res_ctx, pool, &next_odm_pipe->stream_res.dsc);
1697 		ASSERT(next_odm_pipe->stream_res.dsc);
1698 		if (next_odm_pipe->stream_res.dsc == NULL)
1699 			return false;
1700 	}
1701 
1702 	return true;
1703 }
1704 
1705 void dcn20_split_stream_for_mpc(
1706 		struct resource_context *res_ctx,
1707 		const struct resource_pool *pool,
1708 		struct pipe_ctx *primary_pipe,
1709 		struct pipe_ctx *secondary_pipe)
1710 {
1711 	int pipe_idx = secondary_pipe->pipe_idx;
1712 	struct pipe_ctx *sec_bot_pipe = secondary_pipe->bottom_pipe;
1713 
1714 	*secondary_pipe = *primary_pipe;
1715 	secondary_pipe->bottom_pipe = sec_bot_pipe;
1716 
1717 	secondary_pipe->pipe_idx = pipe_idx;
1718 	secondary_pipe->plane_res.mi = pool->mis[secondary_pipe->pipe_idx];
1719 	secondary_pipe->plane_res.hubp = pool->hubps[secondary_pipe->pipe_idx];
1720 	secondary_pipe->plane_res.ipp = pool->ipps[secondary_pipe->pipe_idx];
1721 	secondary_pipe->plane_res.xfm = pool->transforms[secondary_pipe->pipe_idx];
1722 	secondary_pipe->plane_res.dpp = pool->dpps[secondary_pipe->pipe_idx];
1723 	secondary_pipe->plane_res.mpcc_inst = pool->dpps[secondary_pipe->pipe_idx]->inst;
1724 	secondary_pipe->stream_res.dsc = NULL;
1725 	if (primary_pipe->bottom_pipe && primary_pipe->bottom_pipe != secondary_pipe) {
1726 		ASSERT(!secondary_pipe->bottom_pipe);
1727 		secondary_pipe->bottom_pipe = primary_pipe->bottom_pipe;
1728 		secondary_pipe->bottom_pipe->top_pipe = secondary_pipe;
1729 	}
1730 	primary_pipe->bottom_pipe = secondary_pipe;
1731 	secondary_pipe->top_pipe = primary_pipe;
1732 
1733 	ASSERT(primary_pipe->plane_state);
1734 	resource_build_scaling_params(primary_pipe);
1735 	resource_build_scaling_params(secondary_pipe);
1736 }
1737 
1738 void dcn20_populate_dml_writeback_from_context(
1739 		struct dc *dc, struct resource_context *res_ctx, display_e2e_pipe_params_st *pipes)
1740 {
1741 	int pipe_cnt, i;
1742 
1743 	for (i = 0, pipe_cnt = 0; i < dc->res_pool->pipe_count; i++) {
1744 		struct dc_writeback_info *wb_info = &res_ctx->pipe_ctx[i].stream->writeback_info[0];
1745 
1746 		if (!res_ctx->pipe_ctx[i].stream)
1747 			continue;
1748 
1749 		/* Set writeback information */
1750 		pipes[pipe_cnt].dout.wb_enable = (wb_info->wb_enabled == true) ? 1 : 0;
1751 		pipes[pipe_cnt].dout.num_active_wb++;
1752 		pipes[pipe_cnt].dout.wb.wb_src_height = wb_info->dwb_params.cnv_params.crop_height;
1753 		pipes[pipe_cnt].dout.wb.wb_src_width = wb_info->dwb_params.cnv_params.crop_width;
1754 		pipes[pipe_cnt].dout.wb.wb_dst_width = wb_info->dwb_params.dest_width;
1755 		pipes[pipe_cnt].dout.wb.wb_dst_height = wb_info->dwb_params.dest_height;
1756 		pipes[pipe_cnt].dout.wb.wb_htaps_luma = 1;
1757 		pipes[pipe_cnt].dout.wb.wb_vtaps_luma = 1;
1758 		pipes[pipe_cnt].dout.wb.wb_htaps_chroma = wb_info->dwb_params.scaler_taps.h_taps_c;
1759 		pipes[pipe_cnt].dout.wb.wb_vtaps_chroma = wb_info->dwb_params.scaler_taps.v_taps_c;
1760 		pipes[pipe_cnt].dout.wb.wb_hratio = 1.0;
1761 		pipes[pipe_cnt].dout.wb.wb_vratio = 1.0;
1762 		if (wb_info->dwb_params.out_format == dwb_scaler_mode_yuv420) {
1763 			if (wb_info->dwb_params.output_depth == DWB_OUTPUT_PIXEL_DEPTH_8BPC)
1764 				pipes[pipe_cnt].dout.wb.wb_pixel_format = dm_420_8;
1765 			else
1766 				pipes[pipe_cnt].dout.wb.wb_pixel_format = dm_420_10;
1767 		} else
1768 			pipes[pipe_cnt].dout.wb.wb_pixel_format = dm_444_32;
1769 
1770 		pipe_cnt++;
1771 	}
1772 
1773 }
1774 
1775 int dcn20_populate_dml_pipes_from_context(
1776 		struct dc *dc, struct resource_context *res_ctx, display_e2e_pipe_params_st *pipes)
1777 {
1778 	int pipe_cnt, i;
1779 	bool synchronized_vblank = true;
1780 
1781 	for (i = 0, pipe_cnt = -1; i < dc->res_pool->pipe_count; i++) {
1782 		if (!res_ctx->pipe_ctx[i].stream)
1783 			continue;
1784 
1785 		if (pipe_cnt < 0) {
1786 			pipe_cnt = i;
1787 			continue;
1788 		}
1789 		if (dc->debug.disable_timing_sync || !resource_are_streams_timing_synchronizable(
1790 				res_ctx->pipe_ctx[pipe_cnt].stream,
1791 				res_ctx->pipe_ctx[i].stream)) {
1792 			synchronized_vblank = false;
1793 			break;
1794 		}
1795 	}
1796 
1797 	for (i = 0, pipe_cnt = 0; i < dc->res_pool->pipe_count; i++) {
1798 		struct dc_crtc_timing *timing = &res_ctx->pipe_ctx[i].stream->timing;
1799 		int output_bpc;
1800 
1801 		if (!res_ctx->pipe_ctx[i].stream)
1802 			continue;
1803 		/* todo:
1804 		pipes[pipe_cnt].pipe.src.dynamic_metadata_enable = 0;
1805 		pipes[pipe_cnt].pipe.src.dcc = 0;
1806 		pipes[pipe_cnt].pipe.src.vm = 0;*/
1807 
1808 		pipes[pipe_cnt].dout.dsc_enable = res_ctx->pipe_ctx[i].stream->timing.flags.DSC;
1809 		/* todo: rotation?*/
1810 		pipes[pipe_cnt].dout.dsc_slices = res_ctx->pipe_ctx[i].stream->timing.dsc_cfg.num_slices_h;
1811 		if (res_ctx->pipe_ctx[i].stream->use_dynamic_meta) {
1812 			pipes[pipe_cnt].pipe.src.dynamic_metadata_enable = true;
1813 			/* 1/2 vblank */
1814 			pipes[pipe_cnt].pipe.src.dynamic_metadata_lines_before_active =
1815 				(timing->v_total - timing->v_addressable
1816 					- timing->v_border_top - timing->v_border_bottom) / 2;
1817 			/* 36 bytes dp, 32 hdmi */
1818 			pipes[pipe_cnt].pipe.src.dynamic_metadata_xmit_bytes =
1819 				dc_is_dp_signal(res_ctx->pipe_ctx[i].stream->signal) ? 36 : 32;
1820 		}
1821 		pipes[pipe_cnt].pipe.src.dcc = false;
1822 		pipes[pipe_cnt].pipe.src.dcc_rate = 1;
1823 		pipes[pipe_cnt].pipe.dest.synchronized_vblank_all_planes = synchronized_vblank;
1824 		pipes[pipe_cnt].pipe.dest.hblank_start = timing->h_total - timing->h_front_porch;
1825 		pipes[pipe_cnt].pipe.dest.hblank_end = pipes[pipe_cnt].pipe.dest.hblank_start
1826 				- timing->h_addressable
1827 				- timing->h_border_left
1828 				- timing->h_border_right;
1829 		pipes[pipe_cnt].pipe.dest.vblank_start = timing->v_total - timing->v_front_porch;
1830 		pipes[pipe_cnt].pipe.dest.vblank_end = pipes[pipe_cnt].pipe.dest.vblank_start
1831 				- timing->v_addressable
1832 				- timing->v_border_top
1833 				- timing->v_border_bottom;
1834 		pipes[pipe_cnt].pipe.dest.htotal = timing->h_total;
1835 		pipes[pipe_cnt].pipe.dest.vtotal = timing->v_total;
1836 		pipes[pipe_cnt].pipe.dest.hactive = timing->h_addressable;
1837 		pipes[pipe_cnt].pipe.dest.vactive = timing->v_addressable;
1838 		pipes[pipe_cnt].pipe.dest.interlaced = timing->flags.INTERLACE;
1839 		pipes[pipe_cnt].pipe.dest.pixel_rate_mhz = timing->pix_clk_100hz/10000.0;
1840 		if (timing->timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
1841 			pipes[pipe_cnt].pipe.dest.pixel_rate_mhz *= 2;
1842 		pipes[pipe_cnt].pipe.dest.otg_inst = res_ctx->pipe_ctx[i].stream_res.tg->inst;
1843 		pipes[pipe_cnt].dout.dp_lanes = 4;
1844 		pipes[pipe_cnt].pipe.dest.vtotal_min = res_ctx->pipe_ctx[i].stream->adjust.v_total_min;
1845 		pipes[pipe_cnt].pipe.dest.vtotal_max = res_ctx->pipe_ctx[i].stream->adjust.v_total_max;
1846 		pipes[pipe_cnt].pipe.dest.odm_combine = res_ctx->pipe_ctx[i].prev_odm_pipe
1847 							|| res_ctx->pipe_ctx[i].next_odm_pipe;
1848 		pipes[pipe_cnt].pipe.src.hsplit_grp = res_ctx->pipe_ctx[i].pipe_idx;
1849 		if (res_ctx->pipe_ctx[i].top_pipe && res_ctx->pipe_ctx[i].top_pipe->plane_state
1850 				== res_ctx->pipe_ctx[i].plane_state)
1851 			pipes[pipe_cnt].pipe.src.hsplit_grp = res_ctx->pipe_ctx[i].top_pipe->pipe_idx;
1852 		else if (res_ctx->pipe_ctx[i].prev_odm_pipe) {
1853 			struct pipe_ctx *first_pipe = res_ctx->pipe_ctx[i].prev_odm_pipe;
1854 
1855 			while (first_pipe->prev_odm_pipe)
1856 				first_pipe = first_pipe->prev_odm_pipe;
1857 			pipes[pipe_cnt].pipe.src.hsplit_grp = first_pipe->pipe_idx;
1858 		}
1859 
1860 		switch (res_ctx->pipe_ctx[i].stream->signal) {
1861 		case SIGNAL_TYPE_DISPLAY_PORT_MST:
1862 		case SIGNAL_TYPE_DISPLAY_PORT:
1863 			pipes[pipe_cnt].dout.output_type = dm_dp;
1864 			break;
1865 		case SIGNAL_TYPE_EDP:
1866 			pipes[pipe_cnt].dout.output_type = dm_edp;
1867 			break;
1868 		case SIGNAL_TYPE_HDMI_TYPE_A:
1869 		case SIGNAL_TYPE_DVI_SINGLE_LINK:
1870 		case SIGNAL_TYPE_DVI_DUAL_LINK:
1871 			pipes[pipe_cnt].dout.output_type = dm_hdmi;
1872 			break;
1873 		default:
1874 			/* In case there is no signal, set dp with 4 lanes to allow max config */
1875 			pipes[pipe_cnt].dout.output_type = dm_dp;
1876 			pipes[pipe_cnt].dout.dp_lanes = 4;
1877 		}
1878 
1879 		switch (res_ctx->pipe_ctx[i].stream->timing.display_color_depth) {
1880 		case COLOR_DEPTH_666:
1881 			output_bpc = 6;
1882 			break;
1883 		case COLOR_DEPTH_888:
1884 			output_bpc = 8;
1885 			break;
1886 		case COLOR_DEPTH_101010:
1887 			output_bpc = 10;
1888 			break;
1889 		case COLOR_DEPTH_121212:
1890 			output_bpc = 12;
1891 			break;
1892 		case COLOR_DEPTH_141414:
1893 			output_bpc = 14;
1894 			break;
1895 		case COLOR_DEPTH_161616:
1896 			output_bpc = 16;
1897 			break;
1898 		case COLOR_DEPTH_999:
1899 			output_bpc = 9;
1900 			break;
1901 		case COLOR_DEPTH_111111:
1902 			output_bpc = 11;
1903 			break;
1904 		default:
1905 			output_bpc = 8;
1906 			break;
1907 		}
1908 
1909 		switch (res_ctx->pipe_ctx[i].stream->timing.pixel_encoding) {
1910 		case PIXEL_ENCODING_RGB:
1911 		case PIXEL_ENCODING_YCBCR444:
1912 			pipes[pipe_cnt].dout.output_format = dm_444;
1913 			pipes[pipe_cnt].dout.output_bpp = output_bpc * 3;
1914 			break;
1915 		case PIXEL_ENCODING_YCBCR420:
1916 			pipes[pipe_cnt].dout.output_format = dm_420;
1917 			pipes[pipe_cnt].dout.output_bpp = (output_bpc * 3.0) / 2;
1918 			break;
1919 		case PIXEL_ENCODING_YCBCR422:
1920 			if (true) /* todo */
1921 				pipes[pipe_cnt].dout.output_format = dm_s422;
1922 			else
1923 				pipes[pipe_cnt].dout.output_format = dm_n422;
1924 			pipes[pipe_cnt].dout.output_bpp = output_bpc * 2;
1925 			break;
1926 		default:
1927 			pipes[pipe_cnt].dout.output_format = dm_444;
1928 			pipes[pipe_cnt].dout.output_bpp = output_bpc * 3;
1929 		}
1930 
1931 		if (res_ctx->pipe_ctx[i].stream->timing.flags.DSC)
1932 			pipes[pipe_cnt].dout.output_bpp = res_ctx->pipe_ctx[i].stream->timing.dsc_cfg.bits_per_pixel / 16.0;
1933 
1934 		/* todo: default max for now, until there is logic reflecting this in dc*/
1935 		pipes[pipe_cnt].dout.output_bpc = 12;
1936 		/*
1937 		 * Use max cursor settings for calculations to minimize
1938 		 * bw calculations due to cursor on/off
1939 		 */
1940 		pipes[pipe_cnt].pipe.src.num_cursors = 2;
1941 		pipes[pipe_cnt].pipe.src.cur0_src_width = 256;
1942 		pipes[pipe_cnt].pipe.src.cur0_bpp = dm_cur_32bit;
1943 		pipes[pipe_cnt].pipe.src.cur1_src_width = 256;
1944 		pipes[pipe_cnt].pipe.src.cur1_bpp = dm_cur_32bit;
1945 
1946 		if (!res_ctx->pipe_ctx[i].plane_state) {
1947 			pipes[pipe_cnt].pipe.src.source_scan = dm_horz;
1948 			pipes[pipe_cnt].pipe.src.sw_mode = dm_sw_linear;
1949 			pipes[pipe_cnt].pipe.src.macro_tile_size = dm_64k_tile;
1950 			pipes[pipe_cnt].pipe.src.viewport_width = timing->h_addressable;
1951 			if (pipes[pipe_cnt].pipe.src.viewport_width > 1920)
1952 				pipes[pipe_cnt].pipe.src.viewport_width = 1920;
1953 			pipes[pipe_cnt].pipe.src.viewport_height = timing->v_addressable;
1954 			if (pipes[pipe_cnt].pipe.src.viewport_height > 1080)
1955 				pipes[pipe_cnt].pipe.src.viewport_height = 1080;
1956 			pipes[pipe_cnt].pipe.src.data_pitch = ((pipes[pipe_cnt].pipe.src.viewport_width + 63) / 64) * 64; /* linear sw only */
1957 			pipes[pipe_cnt].pipe.src.source_format = dm_444_32;
1958 			pipes[pipe_cnt].pipe.dest.recout_width = pipes[pipe_cnt].pipe.src.viewport_width; /*vp_width/hratio*/
1959 			pipes[pipe_cnt].pipe.dest.recout_height = pipes[pipe_cnt].pipe.src.viewport_height; /*vp_height/vratio*/
1960 			pipes[pipe_cnt].pipe.dest.full_recout_width = pipes[pipe_cnt].pipe.dest.recout_width;  /*when is_hsplit != 1*/
1961 			pipes[pipe_cnt].pipe.dest.full_recout_height = pipes[pipe_cnt].pipe.dest.recout_height; /*when is_hsplit != 1*/
1962 			pipes[pipe_cnt].pipe.scale_ratio_depth.lb_depth = dm_lb_16;
1963 			pipes[pipe_cnt].pipe.scale_ratio_depth.hscl_ratio = 1.0;
1964 			pipes[pipe_cnt].pipe.scale_ratio_depth.vscl_ratio = 1.0;
1965 			pipes[pipe_cnt].pipe.scale_ratio_depth.scl_enable = 0; /*Lb only or Full scl*/
1966 			pipes[pipe_cnt].pipe.scale_taps.htaps = 1;
1967 			pipes[pipe_cnt].pipe.scale_taps.vtaps = 1;
1968 			pipes[pipe_cnt].pipe.src.is_hsplit = 0;
1969 			pipes[pipe_cnt].pipe.dest.odm_combine = 0;
1970 			pipes[pipe_cnt].pipe.dest.vtotal_min = timing->v_total;
1971 			pipes[pipe_cnt].pipe.dest.vtotal_max = timing->v_total;
1972 		} else {
1973 			struct dc_plane_state *pln = res_ctx->pipe_ctx[i].plane_state;
1974 			struct scaler_data *scl = &res_ctx->pipe_ctx[i].plane_res.scl_data;
1975 
1976 			pipes[pipe_cnt].pipe.src.immediate_flip = pln->flip_immediate;
1977 			pipes[pipe_cnt].pipe.src.is_hsplit = (res_ctx->pipe_ctx[i].bottom_pipe
1978 					&& res_ctx->pipe_ctx[i].bottom_pipe->plane_state == pln)
1979 					|| (res_ctx->pipe_ctx[i].top_pipe
1980 					&& res_ctx->pipe_ctx[i].top_pipe->plane_state == pln);
1981 			pipes[pipe_cnt].pipe.src.source_scan = pln->rotation == ROTATION_ANGLE_90
1982 					|| pln->rotation == ROTATION_ANGLE_270 ? dm_vert : dm_horz;
1983 			pipes[pipe_cnt].pipe.src.viewport_y_y = scl->viewport.y;
1984 			pipes[pipe_cnt].pipe.src.viewport_y_c = scl->viewport_c.y;
1985 			pipes[pipe_cnt].pipe.src.viewport_width = scl->viewport.width;
1986 			pipes[pipe_cnt].pipe.src.viewport_width_c = scl->viewport_c.width;
1987 			pipes[pipe_cnt].pipe.src.viewport_height = scl->viewport.height;
1988 			pipes[pipe_cnt].pipe.src.viewport_height_c = scl->viewport_c.height;
1989 			if (pln->format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
1990 				pipes[pipe_cnt].pipe.src.data_pitch = pln->plane_size.surface_pitch;
1991 				pipes[pipe_cnt].pipe.src.data_pitch_c = pln->plane_size.chroma_pitch;
1992 				pipes[pipe_cnt].pipe.src.meta_pitch = pln->dcc.meta_pitch;
1993 				pipes[pipe_cnt].pipe.src.meta_pitch_c = pln->dcc.meta_pitch_c;
1994 			} else {
1995 				pipes[pipe_cnt].pipe.src.data_pitch = pln->plane_size.surface_pitch;
1996 				pipes[pipe_cnt].pipe.src.meta_pitch = pln->dcc.meta_pitch;
1997 			}
1998 			pipes[pipe_cnt].pipe.src.dcc = pln->dcc.enable;
1999 			pipes[pipe_cnt].pipe.dest.recout_width = scl->recout.width;
2000 			pipes[pipe_cnt].pipe.dest.recout_height = scl->recout.height;
2001 			pipes[pipe_cnt].pipe.dest.full_recout_width = scl->recout.width;
2002 			pipes[pipe_cnt].pipe.dest.full_recout_height = scl->recout.height;
2003 			if (res_ctx->pipe_ctx[i].bottom_pipe && res_ctx->pipe_ctx[i].bottom_pipe->plane_state == pln) {
2004 				pipes[pipe_cnt].pipe.dest.full_recout_width +=
2005 						res_ctx->pipe_ctx[i].bottom_pipe->plane_res.scl_data.recout.width;
2006 				pipes[pipe_cnt].pipe.dest.full_recout_height +=
2007 						res_ctx->pipe_ctx[i].bottom_pipe->plane_res.scl_data.recout.height;
2008 			} else if (res_ctx->pipe_ctx[i].top_pipe && res_ctx->pipe_ctx[i].top_pipe->plane_state == pln) {
2009 				pipes[pipe_cnt].pipe.dest.full_recout_width +=
2010 						res_ctx->pipe_ctx[i].top_pipe->plane_res.scl_data.recout.width;
2011 				pipes[pipe_cnt].pipe.dest.full_recout_height +=
2012 						res_ctx->pipe_ctx[i].top_pipe->plane_res.scl_data.recout.height;
2013 			}
2014 
2015 			pipes[pipe_cnt].pipe.scale_ratio_depth.lb_depth = dm_lb_16;
2016 			pipes[pipe_cnt].pipe.scale_ratio_depth.hscl_ratio = (double) scl->ratios.horz.value / (1ULL<<32);
2017 			pipes[pipe_cnt].pipe.scale_ratio_depth.hscl_ratio_c = (double) scl->ratios.horz_c.value / (1ULL<<32);
2018 			pipes[pipe_cnt].pipe.scale_ratio_depth.vscl_ratio = (double) scl->ratios.vert.value / (1ULL<<32);
2019 			pipes[pipe_cnt].pipe.scale_ratio_depth.vscl_ratio_c = (double) scl->ratios.vert_c.value / (1ULL<<32);
2020 			pipes[pipe_cnt].pipe.scale_ratio_depth.scl_enable =
2021 					scl->ratios.vert.value != dc_fixpt_one.value
2022 					|| scl->ratios.horz.value != dc_fixpt_one.value
2023 					|| scl->ratios.vert_c.value != dc_fixpt_one.value
2024 					|| scl->ratios.horz_c.value != dc_fixpt_one.value /*Lb only or Full scl*/
2025 					|| dc->debug.always_scale; /*support always scale*/
2026 			pipes[pipe_cnt].pipe.scale_taps.htaps = scl->taps.h_taps;
2027 			pipes[pipe_cnt].pipe.scale_taps.htaps_c = scl->taps.h_taps_c;
2028 			pipes[pipe_cnt].pipe.scale_taps.vtaps = scl->taps.v_taps;
2029 			pipes[pipe_cnt].pipe.scale_taps.vtaps_c = scl->taps.v_taps_c;
2030 
2031 			pipes[pipe_cnt].pipe.src.macro_tile_size =
2032 					swizzle_mode_to_macro_tile_size(pln->tiling_info.gfx9.swizzle);
2033 			swizzle_to_dml_params(pln->tiling_info.gfx9.swizzle,
2034 					&pipes[pipe_cnt].pipe.src.sw_mode);
2035 
2036 			switch (pln->format) {
2037 			case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
2038 			case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
2039 				pipes[pipe_cnt].pipe.src.source_format = dm_420_8;
2040 				break;
2041 			case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
2042 			case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
2043 				pipes[pipe_cnt].pipe.src.source_format = dm_420_10;
2044 				break;
2045 			case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
2046 			case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
2047 			case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
2048 				pipes[pipe_cnt].pipe.src.source_format = dm_444_64;
2049 				break;
2050 			case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
2051 			case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
2052 				pipes[pipe_cnt].pipe.src.source_format = dm_444_16;
2053 				break;
2054 			case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
2055 				pipes[pipe_cnt].pipe.src.source_format = dm_444_8;
2056 				break;
2057 			default:
2058 				pipes[pipe_cnt].pipe.src.source_format = dm_444_32;
2059 				break;
2060 			}
2061 		}
2062 
2063 		pipe_cnt++;
2064 	}
2065 
2066 	/* populate writeback information */
2067 	dc->res_pool->funcs->populate_dml_writeback_from_context(dc, res_ctx, pipes);
2068 
2069 	return pipe_cnt;
2070 }
2071 
2072 unsigned int dcn20_calc_max_scaled_time(
2073 		unsigned int time_per_pixel,
2074 		enum mmhubbub_wbif_mode mode,
2075 		unsigned int urgent_watermark)
2076 {
2077 	unsigned int time_per_byte = 0;
2078 	unsigned int total_y_free_entry = 0x200; /* two memory piece for luma */
2079 	unsigned int total_c_free_entry = 0x140; /* two memory piece for chroma */
2080 	unsigned int small_free_entry, max_free_entry;
2081 	unsigned int buf_lh_capability;
2082 	unsigned int max_scaled_time;
2083 
2084 	if (mode == PACKED_444) /* packed mode */
2085 		time_per_byte = time_per_pixel/4;
2086 	else if (mode == PLANAR_420_8BPC)
2087 		time_per_byte  = time_per_pixel;
2088 	else if (mode == PLANAR_420_10BPC) /* p010 */
2089 		time_per_byte  = time_per_pixel * 819/1024;
2090 
2091 	if (time_per_byte == 0)
2092 		time_per_byte = 1;
2093 
2094 	small_free_entry  = (total_y_free_entry > total_c_free_entry) ? total_c_free_entry : total_y_free_entry;
2095 	max_free_entry    = (mode == PACKED_444) ? total_y_free_entry + total_c_free_entry : small_free_entry;
2096 	buf_lh_capability = max_free_entry*time_per_byte*32/16; /* there is 4bit fraction */
2097 	max_scaled_time   = buf_lh_capability - urgent_watermark;
2098 	return max_scaled_time;
2099 }
2100 
2101 void dcn20_set_mcif_arb_params(
2102 		struct dc *dc,
2103 		struct dc_state *context,
2104 		display_e2e_pipe_params_st *pipes,
2105 		int pipe_cnt)
2106 {
2107 	enum mmhubbub_wbif_mode wbif_mode;
2108 	struct mcif_arb_params *wb_arb_params;
2109 	int i, j, k, dwb_pipe;
2110 
2111 	/* Writeback MCIF_WB arbitration parameters */
2112 	dwb_pipe = 0;
2113 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2114 
2115 		if (!context->res_ctx.pipe_ctx[i].stream)
2116 			continue;
2117 
2118 		for (j = 0; j < MAX_DWB_PIPES; j++) {
2119 			if (context->res_ctx.pipe_ctx[i].stream->writeback_info[j].wb_enabled == false)
2120 				continue;
2121 
2122 			//wb_arb_params = &context->res_ctx.pipe_ctx[i].stream->writeback_info[j].mcif_arb_params;
2123 			wb_arb_params = &context->bw_ctx.bw.dcn.bw_writeback.mcif_wb_arb[dwb_pipe];
2124 
2125 			if (context->res_ctx.pipe_ctx[i].stream->writeback_info[j].dwb_params.out_format == dwb_scaler_mode_yuv420) {
2126 				if (context->res_ctx.pipe_ctx[i].stream->writeback_info[j].dwb_params.output_depth == DWB_OUTPUT_PIXEL_DEPTH_8BPC)
2127 					wbif_mode = PLANAR_420_8BPC;
2128 				else
2129 					wbif_mode = PLANAR_420_10BPC;
2130 			} else
2131 				wbif_mode = PACKED_444;
2132 
2133 			for (k = 0; k < sizeof(wb_arb_params->cli_watermark)/sizeof(wb_arb_params->cli_watermark[0]); k++) {
2134 				wb_arb_params->cli_watermark[k] = get_wm_writeback_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2135 				wb_arb_params->pstate_watermark[k] = get_wm_writeback_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2136 			}
2137 			wb_arb_params->time_per_pixel = 16.0 / context->res_ctx.pipe_ctx[i].stream->phy_pix_clk; /* 4 bit fraction, ms */
2138 			wb_arb_params->slice_lines = 32;
2139 			wb_arb_params->arbitration_slice = 2;
2140 			wb_arb_params->max_scaled_time = dcn20_calc_max_scaled_time(wb_arb_params->time_per_pixel,
2141 				wbif_mode,
2142 				wb_arb_params->cli_watermark[0]); /* assume 4 watermark sets have the same value */
2143 
2144 			dwb_pipe++;
2145 
2146 			if (dwb_pipe >= MAX_DWB_PIPES)
2147 				return;
2148 		}
2149 		if (dwb_pipe >= MAX_DWB_PIPES)
2150 			return;
2151 	}
2152 }
2153 
2154 bool dcn20_validate_dsc(struct dc *dc, struct dc_state *new_ctx)
2155 {
2156 	int i;
2157 
2158 	/* Validate DSC config, dsc count validation is already done */
2159 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2160 		struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[i];
2161 		struct dc_stream_state *stream = pipe_ctx->stream;
2162 		struct dsc_config dsc_cfg;
2163 		struct pipe_ctx *odm_pipe;
2164 		int opp_cnt = 1;
2165 
2166 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
2167 			opp_cnt++;
2168 
2169 		/* Only need to validate top pipe */
2170 		if (pipe_ctx->top_pipe || pipe_ctx->prev_odm_pipe || !stream || !stream->timing.flags.DSC)
2171 			continue;
2172 
2173 		dsc_cfg.pic_width = (stream->timing.h_addressable + stream->timing.h_border_left
2174 				+ stream->timing.h_border_right) / opp_cnt;
2175 		dsc_cfg.pic_height = stream->timing.v_addressable + stream->timing.v_border_top
2176 				+ stream->timing.v_border_bottom;
2177 		dsc_cfg.pixel_encoding = stream->timing.pixel_encoding;
2178 		dsc_cfg.color_depth = stream->timing.display_color_depth;
2179 		dsc_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
2180 		dsc_cfg.dc_dsc_cfg.num_slices_h /= opp_cnt;
2181 
2182 		if (!pipe_ctx->stream_res.dsc->funcs->dsc_validate_stream(pipe_ctx->stream_res.dsc, &dsc_cfg))
2183 			return false;
2184 	}
2185 	return true;
2186 }
2187 
2188 struct pipe_ctx *dcn20_find_secondary_pipe(struct dc *dc,
2189 		struct resource_context *res_ctx,
2190 		const struct resource_pool *pool,
2191 		const struct pipe_ctx *primary_pipe)
2192 {
2193 	struct pipe_ctx *secondary_pipe = NULL;
2194 
2195 	if (dc && primary_pipe) {
2196 		int j;
2197 		int preferred_pipe_idx = 0;
2198 
2199 		/* first check the prev dc state:
2200 		 * if this primary pipe has a bottom pipe in prev. state
2201 		 * and if the bottom pipe is still available (which it should be),
2202 		 * pick that pipe as secondary
2203 		 * Same logic applies for ODM pipes. Since mpo is not allowed with odm
2204 		 * check in else case.
2205 		 */
2206 		if (dc->current_state->res_ctx.pipe_ctx[primary_pipe->pipe_idx].bottom_pipe) {
2207 			preferred_pipe_idx = dc->current_state->res_ctx.pipe_ctx[primary_pipe->pipe_idx].bottom_pipe->pipe_idx;
2208 			if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
2209 				secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
2210 				secondary_pipe->pipe_idx = preferred_pipe_idx;
2211 			}
2212 		} else if (dc->current_state->res_ctx.pipe_ctx[primary_pipe->pipe_idx].next_odm_pipe) {
2213 			preferred_pipe_idx = dc->current_state->res_ctx.pipe_ctx[primary_pipe->pipe_idx].next_odm_pipe->pipe_idx;
2214 			if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
2215 				secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
2216 				secondary_pipe->pipe_idx = preferred_pipe_idx;
2217 			}
2218 		}
2219 
2220 		/*
2221 		 * if this primary pipe does not have a bottom pipe in prev. state
2222 		 * start backward and find a pipe that did not used to be a bottom pipe in
2223 		 * prev. dc state. This way we make sure we keep the same assignment as
2224 		 * last state and will not have to reprogram every pipe
2225 		 */
2226 		if (secondary_pipe == NULL) {
2227 			for (j = dc->res_pool->pipe_count - 1; j >= 0; j--) {
2228 				if (dc->current_state->res_ctx.pipe_ctx[j].top_pipe == NULL
2229 						&& dc->current_state->res_ctx.pipe_ctx[j].prev_odm_pipe == NULL) {
2230 					preferred_pipe_idx = j;
2231 
2232 					if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
2233 						secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
2234 						secondary_pipe->pipe_idx = preferred_pipe_idx;
2235 						break;
2236 					}
2237 				}
2238 			}
2239 		}
2240 		/*
2241 		 * We should never hit this assert unless assignments are shuffled around
2242 		 * if this happens we will prob. hit a vsync tdr
2243 		 */
2244 		ASSERT(secondary_pipe);
2245 		/*
2246 		 * search backwards for the second pipe to keep pipe
2247 		 * assignment more consistent
2248 		 */
2249 		if (secondary_pipe == NULL) {
2250 			for (j = dc->res_pool->pipe_count - 1; j >= 0; j--) {
2251 				preferred_pipe_idx = j;
2252 
2253 				if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
2254 					secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
2255 					secondary_pipe->pipe_idx = preferred_pipe_idx;
2256 					break;
2257 				}
2258 			}
2259 		}
2260 	}
2261 
2262 	return secondary_pipe;
2263 }
2264 
2265 void dcn20_merge_pipes_for_validate(
2266 		struct dc *dc,
2267 		struct dc_state *context)
2268 {
2269 	int i;
2270 
2271 	/* merge previously split odm pipes since mode support needs to make the decision */
2272 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2273 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2274 		struct pipe_ctx *odm_pipe = pipe->next_odm_pipe;
2275 
2276 		if (pipe->prev_odm_pipe)
2277 			continue;
2278 
2279 		pipe->next_odm_pipe = NULL;
2280 		while (odm_pipe) {
2281 			struct pipe_ctx *next_odm_pipe = odm_pipe->next_odm_pipe;
2282 
2283 			odm_pipe->plane_state = NULL;
2284 			odm_pipe->stream = NULL;
2285 			odm_pipe->top_pipe = NULL;
2286 			odm_pipe->bottom_pipe = NULL;
2287 			odm_pipe->prev_odm_pipe = NULL;
2288 			odm_pipe->next_odm_pipe = NULL;
2289 			if (odm_pipe->stream_res.dsc)
2290 				release_dsc(&context->res_ctx, dc->res_pool, &odm_pipe->stream_res.dsc);
2291 			/* Clear plane_res and stream_res */
2292 			memset(&odm_pipe->plane_res, 0, sizeof(odm_pipe->plane_res));
2293 			memset(&odm_pipe->stream_res, 0, sizeof(odm_pipe->stream_res));
2294 			odm_pipe = next_odm_pipe;
2295 		}
2296 		if (pipe->plane_state)
2297 			resource_build_scaling_params(pipe);
2298 	}
2299 
2300 	/* merge previously mpc split pipes since mode support needs to make the decision */
2301 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2302 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2303 		struct pipe_ctx *hsplit_pipe = pipe->bottom_pipe;
2304 
2305 		if (!hsplit_pipe || hsplit_pipe->plane_state != pipe->plane_state)
2306 			continue;
2307 
2308 		pipe->bottom_pipe = hsplit_pipe->bottom_pipe;
2309 		if (hsplit_pipe->bottom_pipe)
2310 			hsplit_pipe->bottom_pipe->top_pipe = pipe;
2311 		hsplit_pipe->plane_state = NULL;
2312 		hsplit_pipe->stream = NULL;
2313 		hsplit_pipe->top_pipe = NULL;
2314 		hsplit_pipe->bottom_pipe = NULL;
2315 
2316 		/* Clear plane_res and stream_res */
2317 		memset(&hsplit_pipe->plane_res, 0, sizeof(hsplit_pipe->plane_res));
2318 		memset(&hsplit_pipe->stream_res, 0, sizeof(hsplit_pipe->stream_res));
2319 		if (pipe->plane_state)
2320 			resource_build_scaling_params(pipe);
2321 	}
2322 }
2323 
2324 int dcn20_validate_apply_pipe_split_flags(
2325 		struct dc *dc,
2326 		struct dc_state *context,
2327 		int vlevel,
2328 		bool *split)
2329 {
2330 	int i, pipe_idx, vlevel_split;
2331 	bool force_split = false;
2332 	bool avoid_split = dc->debug.pipe_split_policy != MPC_SPLIT_DYNAMIC;
2333 
2334 	/* Single display loop, exits if there is more than one display */
2335 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2336 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2337 		bool exit_loop = false;
2338 
2339 		if (!pipe->stream || pipe->top_pipe)
2340 			continue;
2341 
2342 		if (dc->debug.force_single_disp_pipe_split) {
2343 			if (!force_split)
2344 				force_split = true;
2345 			else {
2346 				force_split = false;
2347 				exit_loop = true;
2348 			}
2349 		}
2350 		if (dc->debug.pipe_split_policy == MPC_SPLIT_AVOID_MULT_DISP) {
2351 			if (avoid_split)
2352 				avoid_split = false;
2353 			else {
2354 				avoid_split = true;
2355 				exit_loop = true;
2356 			}
2357 		}
2358 		if (exit_loop)
2359 			break;
2360 	}
2361 	/* TODO: fix dc bugs and remove this split threshold thing */
2362 	if (context->stream_count > dc->res_pool->pipe_count / 2)
2363 		avoid_split = true;
2364 
2365 	/* Avoid split loop looks for lowest voltage level that allows most unsplit pipes possible */
2366 	if (avoid_split) {
2367 		for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2368 			if (!context->res_ctx.pipe_ctx[i].stream)
2369 				continue;
2370 
2371 			for (vlevel_split = vlevel; vlevel <= context->bw_ctx.dml.soc.num_states; vlevel++)
2372 				if (context->bw_ctx.dml.vba.NoOfDPP[vlevel][0][pipe_idx] == 1)
2373 					break;
2374 			/* Impossible to not split this pipe */
2375 			if (vlevel > context->bw_ctx.dml.soc.num_states)
2376 				vlevel = vlevel_split;
2377 			pipe_idx++;
2378 		}
2379 		context->bw_ctx.dml.vba.maxMpcComb = 0;
2380 	}
2381 
2382 	/* Split loop sets which pipe should be split based on dml outputs and dc flags */
2383 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2384 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2385 
2386 		if (!context->res_ctx.pipe_ctx[i].stream)
2387 			continue;
2388 
2389 		if (force_split || context->bw_ctx.dml.vba.NoOfDPP[vlevel][context->bw_ctx.dml.vba.maxMpcComb][pipe_idx] > 1)
2390 			split[i] = true;
2391 		if ((pipe->stream->view_format ==
2392 				VIEW_3D_FORMAT_SIDE_BY_SIDE ||
2393 				pipe->stream->view_format ==
2394 				VIEW_3D_FORMAT_TOP_AND_BOTTOM) &&
2395 				(pipe->stream->timing.timing_3d_format ==
2396 				TIMING_3D_FORMAT_TOP_AND_BOTTOM ||
2397 				 pipe->stream->timing.timing_3d_format ==
2398 				TIMING_3D_FORMAT_SIDE_BY_SIDE))
2399 			split[i] = true;
2400 		if (dc->debug.force_odm_combine & (1 << pipe->stream_res.tg->inst)) {
2401 			split[i] = true;
2402 			context->bw_ctx.dml.vba.ODMCombineEnablePerState[vlevel][pipe_idx] = true;
2403 		}
2404 		context->bw_ctx.dml.vba.ODMCombineEnabled[pipe_idx] =
2405 			context->bw_ctx.dml.vba.ODMCombineEnablePerState[vlevel][pipe_idx];
2406 		/* Adjust dppclk when split is forced, do not bother with dispclk */
2407 		if (split[i] && context->bw_ctx.dml.vba.NoOfDPP[vlevel][context->bw_ctx.dml.vba.maxMpcComb][pipe_idx] == 1)
2408 			context->bw_ctx.dml.vba.RequiredDPPCLK[vlevel][context->bw_ctx.dml.vba.maxMpcComb][pipe_idx] /= 2;
2409 		pipe_idx++;
2410 	}
2411 
2412 	return vlevel;
2413 }
2414 
2415 bool dcn20_fast_validate_bw(
2416 		struct dc *dc,
2417 		struct dc_state *context,
2418 		display_e2e_pipe_params_st *pipes,
2419 		int *pipe_cnt_out,
2420 		int *pipe_split_from,
2421 		int *vlevel_out)
2422 {
2423 	bool out = false;
2424 	bool split[MAX_PIPES] = { false };
2425 	int pipe_cnt, i, pipe_idx, vlevel;
2426 
2427 	ASSERT(pipes);
2428 	if (!pipes)
2429 		return false;
2430 
2431 	dcn20_merge_pipes_for_validate(dc, context);
2432 
2433 	pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, &context->res_ctx, pipes);
2434 
2435 	*pipe_cnt_out = pipe_cnt;
2436 
2437 	if (!pipe_cnt) {
2438 		out = true;
2439 		goto validate_out;
2440 	}
2441 
2442 	vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
2443 
2444 	if (vlevel > context->bw_ctx.dml.soc.num_states)
2445 		goto validate_fail;
2446 
2447 	vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, vlevel, split);
2448 
2449 	/*initialize pipe_just_split_from to invalid idx*/
2450 	for (i = 0; i < MAX_PIPES; i++)
2451 		pipe_split_from[i] = -1;
2452 
2453 	for (i = 0, pipe_idx = -1; i < dc->res_pool->pipe_count; i++) {
2454 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2455 		struct pipe_ctx *hsplit_pipe = pipe->bottom_pipe;
2456 
2457 		if (!pipe->stream || pipe_split_from[i] >= 0)
2458 			continue;
2459 
2460 		pipe_idx++;
2461 
2462 		if (!pipe->top_pipe && !pipe->plane_state && context->bw_ctx.dml.vba.ODMCombineEnabled[pipe_idx]) {
2463 			hsplit_pipe = dcn20_find_secondary_pipe(dc, &context->res_ctx, dc->res_pool, pipe);
2464 			ASSERT(hsplit_pipe);
2465 			if (!dcn20_split_stream_for_odm(
2466 					&context->res_ctx, dc->res_pool,
2467 					pipe, hsplit_pipe))
2468 				goto validate_fail;
2469 			pipe_split_from[hsplit_pipe->pipe_idx] = pipe_idx;
2470 			dcn20_build_mapped_resource(dc, context, pipe->stream);
2471 		}
2472 
2473 		if (!pipe->plane_state)
2474 			continue;
2475 		/* Skip 2nd half of already split pipe */
2476 		if (pipe->top_pipe && pipe->plane_state == pipe->top_pipe->plane_state)
2477 			continue;
2478 
2479 		/* We do not support mpo + odm at the moment */
2480 		if (hsplit_pipe && hsplit_pipe->plane_state != pipe->plane_state
2481 				&& context->bw_ctx.dml.vba.ODMCombineEnabled[pipe_idx])
2482 			goto validate_fail;
2483 
2484 		if (split[i]) {
2485 			if (!hsplit_pipe || hsplit_pipe->plane_state != pipe->plane_state) {
2486 				/* pipe not split previously needs split */
2487 				hsplit_pipe = dcn20_find_secondary_pipe(dc, &context->res_ctx, dc->res_pool, pipe);
2488 				ASSERT(hsplit_pipe);
2489 				if (!hsplit_pipe) {
2490 					context->bw_ctx.dml.vba.RequiredDPPCLK[vlevel][context->bw_ctx.dml.vba.maxMpcComb][pipe_idx] *= 2;
2491 					continue;
2492 				}
2493 				if (context->bw_ctx.dml.vba.ODMCombineEnabled[pipe_idx]) {
2494 					if (!dcn20_split_stream_for_odm(
2495 							&context->res_ctx, dc->res_pool,
2496 							pipe, hsplit_pipe))
2497 						goto validate_fail;
2498 					dcn20_build_mapped_resource(dc, context, pipe->stream);
2499 				} else
2500 					dcn20_split_stream_for_mpc(
2501 						&context->res_ctx, dc->res_pool,
2502 						pipe, hsplit_pipe);
2503 				pipe_split_from[hsplit_pipe->pipe_idx] = pipe_idx;
2504 			}
2505 		} else if (hsplit_pipe && hsplit_pipe->plane_state == pipe->plane_state) {
2506 			/* merge should already have been done */
2507 			ASSERT(0);
2508 		}
2509 	}
2510 	/* Actual dsc count per stream dsc validation*/
2511 	if (!dcn20_validate_dsc(dc, context)) {
2512 		context->bw_ctx.dml.vba.ValidationStatus[context->bw_ctx.dml.vba.soc.num_states] =
2513 				DML_FAIL_DSC_VALIDATION_FAILURE;
2514 		goto validate_fail;
2515 	}
2516 
2517 	*vlevel_out = vlevel;
2518 
2519 	out = true;
2520 	goto validate_out;
2521 
2522 validate_fail:
2523 	out = false;
2524 
2525 validate_out:
2526 	return out;
2527 }
2528 
2529 static void dcn20_calculate_wm(
2530 		struct dc *dc, struct dc_state *context,
2531 		display_e2e_pipe_params_st *pipes,
2532 		int *out_pipe_cnt,
2533 		int *pipe_split_from,
2534 		int vlevel)
2535 {
2536 	int pipe_cnt, i, pipe_idx;
2537 
2538 	for (i = 0, pipe_idx = 0, pipe_cnt = 0; i < dc->res_pool->pipe_count; i++) {
2539 		if (!context->res_ctx.pipe_ctx[i].stream)
2540 			continue;
2541 
2542 		pipes[pipe_cnt].clks_cfg.refclk_mhz = dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000.0;
2543 		pipes[pipe_cnt].clks_cfg.dispclk_mhz = context->bw_ctx.dml.vba.RequiredDISPCLK[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
2544 
2545 		if (pipe_split_from[i] < 0) {
2546 			pipes[pipe_cnt].clks_cfg.dppclk_mhz =
2547 					context->bw_ctx.dml.vba.RequiredDPPCLK[vlevel][context->bw_ctx.dml.vba.maxMpcComb][pipe_idx];
2548 			if (context->bw_ctx.dml.vba.BlendingAndTiming[pipe_idx] == pipe_idx)
2549 				pipes[pipe_cnt].pipe.dest.odm_combine =
2550 						context->bw_ctx.dml.vba.ODMCombineEnabled[pipe_idx];
2551 			else
2552 				pipes[pipe_cnt].pipe.dest.odm_combine = 0;
2553 			pipe_idx++;
2554 		} else {
2555 			pipes[pipe_cnt].clks_cfg.dppclk_mhz =
2556 					context->bw_ctx.dml.vba.RequiredDPPCLK[vlevel][context->bw_ctx.dml.vba.maxMpcComb][pipe_split_from[i]];
2557 			if (context->bw_ctx.dml.vba.BlendingAndTiming[pipe_split_from[i]] == pipe_split_from[i])
2558 				pipes[pipe_cnt].pipe.dest.odm_combine =
2559 						context->bw_ctx.dml.vba.ODMCombineEnabled[pipe_split_from[i]];
2560 			else
2561 				pipes[pipe_cnt].pipe.dest.odm_combine = 0;
2562 		}
2563 
2564 		if (dc->config.forced_clocks) {
2565 			pipes[pipe_cnt].clks_cfg.dispclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dispclk_mhz;
2566 			pipes[pipe_cnt].clks_cfg.dppclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dppclk_mhz;
2567 		}
2568 		if (dc->debug.min_disp_clk_khz > pipes[pipe_cnt].clks_cfg.dispclk_mhz * 1000)
2569 			pipes[pipe_cnt].clks_cfg.dispclk_mhz = dc->debug.min_disp_clk_khz / 1000.0;
2570 		if (dc->debug.min_dpp_clk_khz > pipes[pipe_cnt].clks_cfg.dppclk_mhz * 1000)
2571 			pipes[pipe_cnt].clks_cfg.dppclk_mhz = dc->debug.min_dpp_clk_khz / 1000.0;
2572 
2573 		pipe_cnt++;
2574 	}
2575 
2576 	if (pipe_cnt != pipe_idx) {
2577 		if (dc->res_pool->funcs->populate_dml_pipes)
2578 			pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc,
2579 				&context->res_ctx, pipes);
2580 		else
2581 			pipe_cnt = dcn20_populate_dml_pipes_from_context(dc,
2582 				&context->res_ctx, pipes);
2583 	}
2584 
2585 	*out_pipe_cnt = pipe_cnt;
2586 
2587 	pipes[0].clks_cfg.voltage = vlevel;
2588 	pipes[0].clks_cfg.dcfclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel].dcfclk_mhz;
2589 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel].socclk_mhz;
2590 
2591 	/* only pipe 0 is read for voltage and dcf/soc clocks */
2592 	if (vlevel < 1) {
2593 		pipes[0].clks_cfg.voltage = 1;
2594 		pipes[0].clks_cfg.dcfclk_mhz = context->bw_ctx.dml.soc.clock_limits[1].dcfclk_mhz;
2595 		pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[1].socclk_mhz;
2596 	}
2597 	context->bw_ctx.bw.dcn.watermarks.b.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2598 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.cstate_enter_plus_exit_ns = get_wm_stutter_enter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2599 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2600 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.pstate_change_ns = get_wm_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2601 	context->bw_ctx.bw.dcn.watermarks.b.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2602 #if defined(CONFIG_DRM_AMD_DC_DCN2_1)
2603 	context->bw_ctx.bw.dcn.watermarks.b.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2604 	context->bw_ctx.bw.dcn.watermarks.b.frac_urg_bw_flip = get_fraction_of_urgent_bandwidth_imm_flip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2605 	context->bw_ctx.bw.dcn.watermarks.b.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2606 #endif
2607 
2608 	if (vlevel < 2) {
2609 		pipes[0].clks_cfg.voltage = 2;
2610 		pipes[0].clks_cfg.dcfclk_mhz = context->bw_ctx.dml.soc.clock_limits[2].dcfclk_mhz;
2611 		pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[2].socclk_mhz;
2612 	}
2613 	context->bw_ctx.bw.dcn.watermarks.c.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2614 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.cstate_enter_plus_exit_ns = get_wm_stutter_enter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2615 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2616 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.pstate_change_ns = get_wm_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2617 	context->bw_ctx.bw.dcn.watermarks.c.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2618 #if defined(CONFIG_DRM_AMD_DC_DCN2_1)
2619 	context->bw_ctx.bw.dcn.watermarks.c.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2620 	context->bw_ctx.bw.dcn.watermarks.c.frac_urg_bw_flip = get_fraction_of_urgent_bandwidth_imm_flip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2621 #endif
2622 
2623 	if (vlevel < 3) {
2624 		pipes[0].clks_cfg.voltage = 3;
2625 		pipes[0].clks_cfg.dcfclk_mhz = context->bw_ctx.dml.soc.clock_limits[2].dcfclk_mhz;
2626 		pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[2].socclk_mhz;
2627 	}
2628 	context->bw_ctx.bw.dcn.watermarks.d.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2629 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.cstate_enter_plus_exit_ns = get_wm_stutter_enter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2630 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2631 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.pstate_change_ns = get_wm_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2632 	context->bw_ctx.bw.dcn.watermarks.d.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2633 #if defined(CONFIG_DRM_AMD_DC_DCN2_1)
2634 	context->bw_ctx.bw.dcn.watermarks.d.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2635 	context->bw_ctx.bw.dcn.watermarks.d.frac_urg_bw_flip = get_fraction_of_urgent_bandwidth_imm_flip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2636 #endif
2637 
2638 	pipes[0].clks_cfg.voltage = vlevel;
2639 	pipes[0].clks_cfg.dcfclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel].dcfclk_mhz;
2640 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel].socclk_mhz;
2641 	context->bw_ctx.bw.dcn.watermarks.a.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2642 	context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.cstate_enter_plus_exit_ns = get_wm_stutter_enter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2643 	context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2644 	context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns = get_wm_dram_clock_change(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2645 	context->bw_ctx.bw.dcn.watermarks.a.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2646 #if defined(CONFIG_DRM_AMD_DC_DCN2_1)
2647 	context->bw_ctx.bw.dcn.watermarks.a.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2648 	context->bw_ctx.bw.dcn.watermarks.a.frac_urg_bw_flip = get_fraction_of_urgent_bandwidth_imm_flip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2649 #endif
2650 }
2651 
2652 void dcn20_calculate_dlg_params(
2653 		struct dc *dc, struct dc_state *context,
2654 		display_e2e_pipe_params_st *pipes,
2655 		int pipe_cnt,
2656 		int vlevel)
2657 {
2658 	int i, j, pipe_idx, pipe_idx_unsplit;
2659 	bool visited[MAX_PIPES] = { 0 };
2660 
2661 	/* Writeback MCIF_WB arbitration parameters */
2662 	dc->res_pool->funcs->set_mcif_arb_params(dc, context, pipes, pipe_cnt);
2663 
2664 	context->bw_ctx.bw.dcn.clk.dispclk_khz = context->bw_ctx.dml.vba.DISPCLK * 1000;
2665 	context->bw_ctx.bw.dcn.clk.dcfclk_khz = context->bw_ctx.dml.vba.DCFCLK * 1000;
2666 	context->bw_ctx.bw.dcn.clk.socclk_khz = context->bw_ctx.dml.vba.SOCCLK * 1000;
2667 	context->bw_ctx.bw.dcn.clk.dramclk_khz = context->bw_ctx.dml.vba.DRAMSpeed * 1000 / 16;
2668 	context->bw_ctx.bw.dcn.clk.dcfclk_deep_sleep_khz = context->bw_ctx.dml.vba.DCFCLKDeepSleep * 1000;
2669 	context->bw_ctx.bw.dcn.clk.fclk_khz = context->bw_ctx.dml.vba.FabricClock * 1000;
2670 	context->bw_ctx.bw.dcn.clk.p_state_change_support =
2671 		context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb]
2672 							!= dm_dram_clock_change_unsupported;
2673 	context->bw_ctx.bw.dcn.clk.dppclk_khz = 0;
2674 
2675 	/*
2676 	 * An artifact of dml pipe split/odm is that pipes get merged back together for
2677 	 * calculation. Therefore we need to only extract for first pipe in ascending index order
2678 	 * and copy into the other split half.
2679 	 */
2680 	for (i = 0, pipe_idx = 0, pipe_idx_unsplit = 0; i < dc->res_pool->pipe_count; i++) {
2681 		if (!context->res_ctx.pipe_ctx[i].stream)
2682 			continue;
2683 
2684 		if (!visited[pipe_idx]) {
2685 			display_pipe_source_params_st *src = &pipes[pipe_idx].pipe.src;
2686 			display_pipe_dest_params_st *dst = &pipes[pipe_idx].pipe.dest;
2687 
2688 			dst->vstartup_start = context->bw_ctx.dml.vba.VStartup[pipe_idx_unsplit];
2689 			dst->vupdate_offset = context->bw_ctx.dml.vba.VUpdateOffsetPix[pipe_idx_unsplit];
2690 			dst->vupdate_width = context->bw_ctx.dml.vba.VUpdateWidthPix[pipe_idx_unsplit];
2691 			dst->vready_offset = context->bw_ctx.dml.vba.VReadyOffsetPix[pipe_idx_unsplit];
2692 			/*
2693 			 * j iterates inside pipes array, unlike i which iterates inside
2694 			 * pipe_ctx array
2695 			 */
2696 			if (src->is_hsplit)
2697 				for (j = pipe_idx + 1; j < pipe_cnt; j++) {
2698 					display_pipe_source_params_st *src_j = &pipes[j].pipe.src;
2699 					display_pipe_dest_params_st *dst_j = &pipes[j].pipe.dest;
2700 
2701 					if (src_j->is_hsplit && !visited[j]
2702 							&& src->hsplit_grp == src_j->hsplit_grp) {
2703 						dst_j->vstartup_start = context->bw_ctx.dml.vba.VStartup[pipe_idx_unsplit];
2704 						dst_j->vupdate_offset = context->bw_ctx.dml.vba.VUpdateOffsetPix[pipe_idx_unsplit];
2705 						dst_j->vupdate_width = context->bw_ctx.dml.vba.VUpdateWidthPix[pipe_idx_unsplit];
2706 						dst_j->vready_offset = context->bw_ctx.dml.vba.VReadyOffsetPix[pipe_idx_unsplit];
2707 						visited[j] = true;
2708 					}
2709 				}
2710 			visited[pipe_idx] = true;
2711 			pipe_idx_unsplit++;
2712 		}
2713 		pipe_idx++;
2714 	}
2715 
2716 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2717 		if (!context->res_ctx.pipe_ctx[i].stream)
2718 			continue;
2719 		if (context->bw_ctx.bw.dcn.clk.dppclk_khz < pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000)
2720 			context->bw_ctx.bw.dcn.clk.dppclk_khz = pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000;
2721 		context->res_ctx.pipe_ctx[i].plane_res.bw.dppclk_khz =
2722 						pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000;
2723 		ASSERT(visited[pipe_idx]);
2724 		context->res_ctx.pipe_ctx[i].pipe_dlg_param = pipes[pipe_idx].pipe.dest;
2725 		pipe_idx++;
2726 	}
2727 	/*save a original dppclock copy*/
2728 	context->bw_ctx.bw.dcn.clk.bw_dppclk_khz = context->bw_ctx.bw.dcn.clk.dppclk_khz;
2729 	context->bw_ctx.bw.dcn.clk.bw_dispclk_khz = context->bw_ctx.bw.dcn.clk.dispclk_khz;
2730 	context->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz = context->bw_ctx.dml.soc.clock_limits[vlevel].dppclk_mhz * 1000;
2731 	context->bw_ctx.bw.dcn.clk.max_supported_dispclk_khz = context->bw_ctx.dml.soc.clock_limits[vlevel].dispclk_mhz * 1000;
2732 
2733 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2734 		bool cstate_en = context->bw_ctx.dml.vba.PrefetchMode[vlevel][context->bw_ctx.dml.vba.maxMpcComb] != 2;
2735 
2736 		if (!context->res_ctx.pipe_ctx[i].stream)
2737 			continue;
2738 
2739 		context->bw_ctx.dml.funcs.rq_dlg_get_dlg_reg(&context->bw_ctx.dml,
2740 				&context->res_ctx.pipe_ctx[i].dlg_regs,
2741 				&context->res_ctx.pipe_ctx[i].ttu_regs,
2742 				pipes,
2743 				pipe_cnt,
2744 				pipe_idx,
2745 				cstate_en,
2746 				context->bw_ctx.bw.dcn.clk.p_state_change_support,
2747 				false, false, false);
2748 
2749 		context->bw_ctx.dml.funcs.rq_dlg_get_rq_reg(&context->bw_ctx.dml,
2750 				&context->res_ctx.pipe_ctx[i].rq_regs,
2751 				pipes[pipe_idx].pipe);
2752 		pipe_idx++;
2753 	}
2754 }
2755 
2756 static bool dcn20_validate_bandwidth_internal(struct dc *dc, struct dc_state *context,
2757 		bool fast_validate)
2758 {
2759 	bool out = false;
2760 
2761 	BW_VAL_TRACE_SETUP();
2762 
2763 	int vlevel = 0;
2764 	int pipe_split_from[MAX_PIPES];
2765 	int pipe_cnt = 0;
2766 	display_e2e_pipe_params_st *pipes = kzalloc(dc->res_pool->pipe_count * sizeof(display_e2e_pipe_params_st), GFP_KERNEL);
2767 	DC_LOGGER_INIT(dc->ctx->logger);
2768 
2769 	BW_VAL_TRACE_COUNT();
2770 
2771 	out = dcn20_fast_validate_bw(dc, context, pipes, &pipe_cnt, pipe_split_from, &vlevel);
2772 
2773 	if (pipe_cnt == 0)
2774 		goto validate_out;
2775 
2776 	if (!out)
2777 		goto validate_fail;
2778 
2779 	BW_VAL_TRACE_END_VOLTAGE_LEVEL();
2780 
2781 	if (fast_validate) {
2782 		BW_VAL_TRACE_SKIP(fast);
2783 		goto validate_out;
2784 	}
2785 
2786 	dcn20_calculate_wm(dc, context, pipes, &pipe_cnt, pipe_split_from, vlevel);
2787 	dcn20_calculate_dlg_params(dc, context, pipes, pipe_cnt, vlevel);
2788 
2789 	BW_VAL_TRACE_END_WATERMARKS();
2790 
2791 	goto validate_out;
2792 
2793 validate_fail:
2794 	DC_LOG_WARNING("Mode Validation Warning: %s failed validation.\n",
2795 		dml_get_status_message(context->bw_ctx.dml.vba.ValidationStatus[context->bw_ctx.dml.vba.soc.num_states]));
2796 
2797 	BW_VAL_TRACE_SKIP(fail);
2798 	out = false;
2799 
2800 validate_out:
2801 	kfree(pipes);
2802 
2803 	BW_VAL_TRACE_FINISH();
2804 
2805 	return out;
2806 }
2807 
2808 
2809 bool dcn20_validate_bandwidth(struct dc *dc, struct dc_state *context,
2810 		bool fast_validate)
2811 {
2812 	bool voltage_supported = false;
2813 	bool full_pstate_supported = false;
2814 	bool dummy_pstate_supported = false;
2815 	double p_state_latency_us = context->bw_ctx.dml.soc.dram_clock_change_latency_us;
2816 	context->bw_ctx.dml.soc.disable_dram_clock_change_vactive_support = dc->debug.disable_dram_clock_change_vactive_support;
2817 
2818 	if (fast_validate)
2819 		return dcn20_validate_bandwidth_internal(dc, context, true);
2820 
2821 
2822 	// Best case, we support full UCLK switch latency
2823 	voltage_supported = dcn20_validate_bandwidth_internal(dc, context, false);
2824 	full_pstate_supported = context->bw_ctx.bw.dcn.clk.p_state_change_support;
2825 
2826 	if (context->bw_ctx.dml.soc.dummy_pstate_latency_us == 0 ||
2827 		(voltage_supported && full_pstate_supported)) {
2828 		context->bw_ctx.bw.dcn.clk.p_state_change_support = true;
2829 		goto restore_dml_state;
2830 	}
2831 
2832 	// Fallback: Try to only support G6 temperature read latency
2833 	context->bw_ctx.dml.soc.dram_clock_change_latency_us = context->bw_ctx.dml.soc.dummy_pstate_latency_us;
2834 
2835 	voltage_supported = dcn20_validate_bandwidth_internal(dc, context, false);
2836 	dummy_pstate_supported = context->bw_ctx.bw.dcn.clk.p_state_change_support;
2837 
2838 	if (voltage_supported && dummy_pstate_supported) {
2839 		context->bw_ctx.bw.dcn.clk.p_state_change_support = false;
2840 		goto restore_dml_state;
2841 	}
2842 
2843 	// ERROR: fallback is supposed to always work.
2844 	ASSERT(false);
2845 
2846 restore_dml_state:
2847 	context->bw_ctx.dml.soc.dram_clock_change_latency_us = p_state_latency_us;
2848 
2849 	return voltage_supported;
2850 }
2851 
2852 struct pipe_ctx *dcn20_acquire_idle_pipe_for_layer(
2853 		struct dc_state *state,
2854 		const struct resource_pool *pool,
2855 		struct dc_stream_state *stream)
2856 {
2857 	struct resource_context *res_ctx = &state->res_ctx;
2858 	struct pipe_ctx *head_pipe = resource_get_head_pipe_for_stream(res_ctx, stream);
2859 	struct pipe_ctx *idle_pipe = find_idle_secondary_pipe(res_ctx, pool, head_pipe);
2860 
2861 	if (!head_pipe)
2862 		ASSERT(0);
2863 
2864 	if (!idle_pipe)
2865 		return NULL;
2866 
2867 	idle_pipe->stream = head_pipe->stream;
2868 	idle_pipe->stream_res.tg = head_pipe->stream_res.tg;
2869 	idle_pipe->stream_res.opp = head_pipe->stream_res.opp;
2870 
2871 	idle_pipe->plane_res.hubp = pool->hubps[idle_pipe->pipe_idx];
2872 	idle_pipe->plane_res.ipp = pool->ipps[idle_pipe->pipe_idx];
2873 	idle_pipe->plane_res.dpp = pool->dpps[idle_pipe->pipe_idx];
2874 	idle_pipe->plane_res.mpcc_inst = pool->dpps[idle_pipe->pipe_idx]->inst;
2875 
2876 	return idle_pipe;
2877 }
2878 
2879 bool dcn20_get_dcc_compression_cap(const struct dc *dc,
2880 		const struct dc_dcc_surface_param *input,
2881 		struct dc_surface_dcc_cap *output)
2882 {
2883 	return dc->res_pool->hubbub->funcs->get_dcc_compression_cap(
2884 			dc->res_pool->hubbub,
2885 			input,
2886 			output);
2887 }
2888 
2889 static void dcn20_destroy_resource_pool(struct resource_pool **pool)
2890 {
2891 	struct dcn20_resource_pool *dcn20_pool = TO_DCN20_RES_POOL(*pool);
2892 
2893 	destruct(dcn20_pool);
2894 	kfree(dcn20_pool);
2895 	*pool = NULL;
2896 }
2897 
2898 
2899 static struct dc_cap_funcs cap_funcs = {
2900 	.get_dcc_compression_cap = dcn20_get_dcc_compression_cap
2901 };
2902 
2903 
2904 enum dc_status dcn20_get_default_swizzle_mode(struct dc_plane_state *plane_state)
2905 {
2906 	enum dc_status result = DC_OK;
2907 
2908 	enum surface_pixel_format surf_pix_format = plane_state->format;
2909 	unsigned int bpp = resource_pixel_format_to_bpp(surf_pix_format);
2910 
2911 	enum swizzle_mode_values swizzle = DC_SW_LINEAR;
2912 
2913 	if (bpp == 64)
2914 		swizzle = DC_SW_64KB_D;
2915 	else
2916 		swizzle = DC_SW_64KB_S;
2917 
2918 	plane_state->tiling_info.gfx9.swizzle = swizzle;
2919 	return result;
2920 }
2921 
2922 static struct resource_funcs dcn20_res_pool_funcs = {
2923 	.destroy = dcn20_destroy_resource_pool,
2924 	.link_enc_create = dcn20_link_encoder_create,
2925 	.validate_bandwidth = dcn20_validate_bandwidth,
2926 	.acquire_idle_pipe_for_layer = dcn20_acquire_idle_pipe_for_layer,
2927 	.add_stream_to_ctx = dcn20_add_stream_to_ctx,
2928 	.remove_stream_from_ctx = dcn20_remove_stream_from_ctx,
2929 	.populate_dml_writeback_from_context = dcn20_populate_dml_writeback_from_context,
2930 	.get_default_swizzle_mode = dcn20_get_default_swizzle_mode,
2931 	.set_mcif_arb_params = dcn20_set_mcif_arb_params,
2932 	.populate_dml_pipes = dcn20_populate_dml_pipes_from_context,
2933 	.find_first_free_match_stream_enc_for_link = dcn10_find_first_free_match_stream_enc_for_link
2934 };
2935 
2936 bool dcn20_dwbc_create(struct dc_context *ctx, struct resource_pool *pool)
2937 {
2938 	int i;
2939 	uint32_t pipe_count = pool->res_cap->num_dwb;
2940 
2941 	for (i = 0; i < pipe_count; i++) {
2942 		struct dcn20_dwbc *dwbc20 = kzalloc(sizeof(struct dcn20_dwbc),
2943 						    GFP_KERNEL);
2944 
2945 		if (!dwbc20) {
2946 			dm_error("DC: failed to create dwbc20!\n");
2947 			return false;
2948 		}
2949 		dcn20_dwbc_construct(dwbc20, ctx,
2950 				&dwbc20_regs[i],
2951 				&dwbc20_shift,
2952 				&dwbc20_mask,
2953 				i);
2954 		pool->dwbc[i] = &dwbc20->base;
2955 	}
2956 	return true;
2957 }
2958 
2959 bool dcn20_mmhubbub_create(struct dc_context *ctx, struct resource_pool *pool)
2960 {
2961 	int i;
2962 	uint32_t pipe_count = pool->res_cap->num_dwb;
2963 
2964 	ASSERT(pipe_count > 0);
2965 
2966 	for (i = 0; i < pipe_count; i++) {
2967 		struct dcn20_mmhubbub *mcif_wb20 = kzalloc(sizeof(struct dcn20_mmhubbub),
2968 						    GFP_KERNEL);
2969 
2970 		if (!mcif_wb20) {
2971 			dm_error("DC: failed to create mcif_wb20!\n");
2972 			return false;
2973 		}
2974 
2975 		dcn20_mmhubbub_construct(mcif_wb20, ctx,
2976 				&mcif_wb20_regs[i],
2977 				&mcif_wb20_shift,
2978 				&mcif_wb20_mask,
2979 				i);
2980 
2981 		pool->mcif_wb[i] = &mcif_wb20->base;
2982 	}
2983 	return true;
2984 }
2985 
2986 static struct pp_smu_funcs *dcn20_pp_smu_create(struct dc_context *ctx)
2987 {
2988 	struct pp_smu_funcs *pp_smu = kzalloc(sizeof(*pp_smu), GFP_KERNEL);
2989 
2990 	if (!pp_smu)
2991 		return pp_smu;
2992 
2993 	dm_pp_get_funcs(ctx, pp_smu);
2994 
2995 	if (pp_smu->ctx.ver != PP_SMU_VER_NV)
2996 		pp_smu = memset(pp_smu, 0, sizeof(struct pp_smu_funcs));
2997 
2998 	return pp_smu;
2999 }
3000 
3001 static void dcn20_pp_smu_destroy(struct pp_smu_funcs **pp_smu)
3002 {
3003 	if (pp_smu && *pp_smu) {
3004 		kfree(*pp_smu);
3005 		*pp_smu = NULL;
3006 	}
3007 }
3008 
3009 void dcn20_cap_soc_clocks(
3010 		struct _vcs_dpi_soc_bounding_box_st *bb,
3011 		struct pp_smu_nv_clock_table max_clocks)
3012 {
3013 	int i;
3014 
3015 	// First pass - cap all clocks higher than the reported max
3016 	for (i = 0; i < bb->num_states; i++) {
3017 		if ((bb->clock_limits[i].dcfclk_mhz > (max_clocks.dcfClockInKhz / 1000))
3018 				&& max_clocks.dcfClockInKhz != 0)
3019 			bb->clock_limits[i].dcfclk_mhz = (max_clocks.dcfClockInKhz / 1000);
3020 
3021 		if ((bb->clock_limits[i].dram_speed_mts > (max_clocks.uClockInKhz / 1000) * 16)
3022 						&& max_clocks.uClockInKhz != 0)
3023 			bb->clock_limits[i].dram_speed_mts = (max_clocks.uClockInKhz / 1000) * 16;
3024 
3025 		if ((bb->clock_limits[i].fabricclk_mhz > (max_clocks.fabricClockInKhz / 1000))
3026 						&& max_clocks.fabricClockInKhz != 0)
3027 			bb->clock_limits[i].fabricclk_mhz = (max_clocks.fabricClockInKhz / 1000);
3028 
3029 		if ((bb->clock_limits[i].dispclk_mhz > (max_clocks.displayClockInKhz / 1000))
3030 						&& max_clocks.displayClockInKhz != 0)
3031 			bb->clock_limits[i].dispclk_mhz = (max_clocks.displayClockInKhz / 1000);
3032 
3033 		if ((bb->clock_limits[i].dppclk_mhz > (max_clocks.dppClockInKhz / 1000))
3034 						&& max_clocks.dppClockInKhz != 0)
3035 			bb->clock_limits[i].dppclk_mhz = (max_clocks.dppClockInKhz / 1000);
3036 
3037 		if ((bb->clock_limits[i].phyclk_mhz > (max_clocks.phyClockInKhz / 1000))
3038 						&& max_clocks.phyClockInKhz != 0)
3039 			bb->clock_limits[i].phyclk_mhz = (max_clocks.phyClockInKhz / 1000);
3040 
3041 		if ((bb->clock_limits[i].socclk_mhz > (max_clocks.socClockInKhz / 1000))
3042 						&& max_clocks.socClockInKhz != 0)
3043 			bb->clock_limits[i].socclk_mhz = (max_clocks.socClockInKhz / 1000);
3044 
3045 		if ((bb->clock_limits[i].dscclk_mhz > (max_clocks.dscClockInKhz / 1000))
3046 						&& max_clocks.dscClockInKhz != 0)
3047 			bb->clock_limits[i].dscclk_mhz = (max_clocks.dscClockInKhz / 1000);
3048 	}
3049 
3050 	// Second pass - remove all duplicate clock states
3051 	for (i = bb->num_states - 1; i > 1; i--) {
3052 		bool duplicate = true;
3053 
3054 		if (bb->clock_limits[i-1].dcfclk_mhz != bb->clock_limits[i].dcfclk_mhz)
3055 			duplicate = false;
3056 		if (bb->clock_limits[i-1].dispclk_mhz != bb->clock_limits[i].dispclk_mhz)
3057 			duplicate = false;
3058 		if (bb->clock_limits[i-1].dppclk_mhz != bb->clock_limits[i].dppclk_mhz)
3059 			duplicate = false;
3060 		if (bb->clock_limits[i-1].dram_speed_mts != bb->clock_limits[i].dram_speed_mts)
3061 			duplicate = false;
3062 		if (bb->clock_limits[i-1].dscclk_mhz != bb->clock_limits[i].dscclk_mhz)
3063 			duplicate = false;
3064 		if (bb->clock_limits[i-1].fabricclk_mhz != bb->clock_limits[i].fabricclk_mhz)
3065 			duplicate = false;
3066 		if (bb->clock_limits[i-1].phyclk_mhz != bb->clock_limits[i].phyclk_mhz)
3067 			duplicate = false;
3068 		if (bb->clock_limits[i-1].socclk_mhz != bb->clock_limits[i].socclk_mhz)
3069 			duplicate = false;
3070 
3071 		if (duplicate)
3072 			bb->num_states--;
3073 	}
3074 }
3075 
3076 void dcn20_update_bounding_box(struct dc *dc, struct _vcs_dpi_soc_bounding_box_st *bb,
3077 		struct pp_smu_nv_clock_table *max_clocks, unsigned int *uclk_states, unsigned int num_states)
3078 {
3079 	struct _vcs_dpi_voltage_scaling_st calculated_states[MAX_CLOCK_LIMIT_STATES];
3080 	int i;
3081 	int num_calculated_states = 0;
3082 	int min_dcfclk = 0;
3083 
3084 	if (num_states == 0)
3085 		return;
3086 
3087 	memset(calculated_states, 0, sizeof(calculated_states));
3088 
3089 	if (dc->bb_overrides.min_dcfclk_mhz > 0)
3090 		min_dcfclk = dc->bb_overrides.min_dcfclk_mhz;
3091 	else {
3092 		if (ASICREV_IS_NAVI12_P(dc->ctx->asic_id.hw_internal_rev))
3093 			min_dcfclk = 310;
3094 		else
3095 			// Accounting for SOC/DCF relationship, we can go as high as
3096 			// 506Mhz in Vmin.
3097 			min_dcfclk = 506;
3098 	}
3099 
3100 	for (i = 0; i < num_states; i++) {
3101 		int min_fclk_required_by_uclk;
3102 		calculated_states[i].state = i;
3103 		calculated_states[i].dram_speed_mts = uclk_states[i] * 16 / 1000;
3104 
3105 		// FCLK:UCLK ratio is 1.08
3106 		min_fclk_required_by_uclk = mul_u64_u32_shr(BIT_ULL(32) * 1080 / 1000000, uclk_states[i], 32);
3107 
3108 		calculated_states[i].fabricclk_mhz = (min_fclk_required_by_uclk < min_dcfclk) ?
3109 				min_dcfclk : min_fclk_required_by_uclk;
3110 
3111 		calculated_states[i].socclk_mhz = (calculated_states[i].fabricclk_mhz > max_clocks->socClockInKhz / 1000) ?
3112 				max_clocks->socClockInKhz / 1000 : calculated_states[i].fabricclk_mhz;
3113 
3114 		calculated_states[i].dcfclk_mhz = (calculated_states[i].fabricclk_mhz > max_clocks->dcfClockInKhz / 1000) ?
3115 				max_clocks->dcfClockInKhz / 1000 : calculated_states[i].fabricclk_mhz;
3116 
3117 		calculated_states[i].dispclk_mhz = max_clocks->displayClockInKhz / 1000;
3118 		calculated_states[i].dppclk_mhz = max_clocks->displayClockInKhz / 1000;
3119 		calculated_states[i].dscclk_mhz = max_clocks->displayClockInKhz / (1000 * 3);
3120 
3121 		calculated_states[i].phyclk_mhz = max_clocks->phyClockInKhz / 1000;
3122 
3123 		num_calculated_states++;
3124 	}
3125 
3126 	calculated_states[num_calculated_states - 1].socclk_mhz = max_clocks->socClockInKhz / 1000;
3127 	calculated_states[num_calculated_states - 1].fabricclk_mhz = max_clocks->socClockInKhz / 1000;
3128 	calculated_states[num_calculated_states - 1].dcfclk_mhz = max_clocks->dcfClockInKhz / 1000;
3129 
3130 	memcpy(bb->clock_limits, calculated_states, sizeof(bb->clock_limits));
3131 	bb->num_states = num_calculated_states;
3132 
3133 	// Duplicate the last state, DML always an extra state identical to max state to work
3134 	memcpy(&bb->clock_limits[num_calculated_states], &bb->clock_limits[num_calculated_states - 1], sizeof(struct _vcs_dpi_voltage_scaling_st));
3135 	bb->clock_limits[num_calculated_states].state = bb->num_states;
3136 }
3137 
3138 void dcn20_patch_bounding_box(struct dc *dc, struct _vcs_dpi_soc_bounding_box_st *bb)
3139 {
3140 	kernel_fpu_begin();
3141 	if ((int)(bb->sr_exit_time_us * 1000) != dc->bb_overrides.sr_exit_time_ns
3142 			&& dc->bb_overrides.sr_exit_time_ns) {
3143 		bb->sr_exit_time_us = dc->bb_overrides.sr_exit_time_ns / 1000.0;
3144 	}
3145 
3146 	if ((int)(bb->sr_enter_plus_exit_time_us * 1000)
3147 				!= dc->bb_overrides.sr_enter_plus_exit_time_ns
3148 			&& dc->bb_overrides.sr_enter_plus_exit_time_ns) {
3149 		bb->sr_enter_plus_exit_time_us =
3150 				dc->bb_overrides.sr_enter_plus_exit_time_ns / 1000.0;
3151 	}
3152 
3153 	if ((int)(bb->urgent_latency_us * 1000) != dc->bb_overrides.urgent_latency_ns
3154 			&& dc->bb_overrides.urgent_latency_ns) {
3155 		bb->urgent_latency_us = dc->bb_overrides.urgent_latency_ns / 1000.0;
3156 	}
3157 
3158 	if ((int)(bb->dram_clock_change_latency_us * 1000)
3159 				!= dc->bb_overrides.dram_clock_change_latency_ns
3160 			&& dc->bb_overrides.dram_clock_change_latency_ns) {
3161 		bb->dram_clock_change_latency_us =
3162 				dc->bb_overrides.dram_clock_change_latency_ns / 1000.0;
3163 	}
3164 	kernel_fpu_end();
3165 }
3166 
3167 static struct _vcs_dpi_soc_bounding_box_st *get_asic_rev_soc_bb(
3168 	uint32_t hw_internal_rev)
3169 {
3170 	if (ASICREV_IS_NAVI12_P(hw_internal_rev))
3171 		return &dcn2_0_nv12_soc;
3172 
3173 	return &dcn2_0_soc;
3174 }
3175 
3176 static struct _vcs_dpi_ip_params_st *get_asic_rev_ip_params(
3177 	uint32_t hw_internal_rev)
3178 {
3179 	/* NV12 and NV10 */
3180 	return &dcn2_0_ip;
3181 }
3182 
3183 static enum dml_project get_dml_project_version(uint32_t hw_internal_rev)
3184 {
3185 	return DML_PROJECT_NAVI10v2;
3186 }
3187 
3188 #define fixed16_to_double(x) (((double) x) / ((double) (1 << 16)))
3189 #define fixed16_to_double_to_cpu(x) fixed16_to_double(le32_to_cpu(x))
3190 
3191 static bool init_soc_bounding_box(struct dc *dc,
3192 				  struct dcn20_resource_pool *pool)
3193 {
3194 	const struct gpu_info_soc_bounding_box_v1_0 *bb = dc->soc_bounding_box;
3195 	struct _vcs_dpi_soc_bounding_box_st *loaded_bb =
3196 			get_asic_rev_soc_bb(dc->ctx->asic_id.hw_internal_rev);
3197 	struct _vcs_dpi_ip_params_st *loaded_ip =
3198 			get_asic_rev_ip_params(dc->ctx->asic_id.hw_internal_rev);
3199 
3200 	DC_LOGGER_INIT(dc->ctx->logger);
3201 
3202 	if (!bb && !SOC_BOUNDING_BOX_VALID) {
3203 		DC_LOG_ERROR("%s: not valid soc bounding box/n", __func__);
3204 		return false;
3205 	}
3206 
3207 	if (bb && !SOC_BOUNDING_BOX_VALID) {
3208 		int i;
3209 
3210 		dcn2_0_nv12_soc.sr_exit_time_us =
3211 				fixed16_to_double_to_cpu(bb->sr_exit_time_us);
3212 		dcn2_0_nv12_soc.sr_enter_plus_exit_time_us =
3213 				fixed16_to_double_to_cpu(bb->sr_enter_plus_exit_time_us);
3214 		dcn2_0_nv12_soc.urgent_latency_us =
3215 				fixed16_to_double_to_cpu(bb->urgent_latency_us);
3216 		dcn2_0_nv12_soc.urgent_latency_pixel_data_only_us =
3217 				fixed16_to_double_to_cpu(bb->urgent_latency_pixel_data_only_us);
3218 		dcn2_0_nv12_soc.urgent_latency_pixel_mixed_with_vm_data_us =
3219 				fixed16_to_double_to_cpu(bb->urgent_latency_pixel_mixed_with_vm_data_us);
3220 		dcn2_0_nv12_soc.urgent_latency_vm_data_only_us =
3221 				fixed16_to_double_to_cpu(bb->urgent_latency_vm_data_only_us);
3222 		dcn2_0_nv12_soc.urgent_out_of_order_return_per_channel_pixel_only_bytes =
3223 				le32_to_cpu(bb->urgent_out_of_order_return_per_channel_pixel_only_bytes);
3224 		dcn2_0_nv12_soc.urgent_out_of_order_return_per_channel_pixel_and_vm_bytes =
3225 				le32_to_cpu(bb->urgent_out_of_order_return_per_channel_pixel_and_vm_bytes);
3226 		dcn2_0_nv12_soc.urgent_out_of_order_return_per_channel_vm_only_bytes =
3227 				le32_to_cpu(bb->urgent_out_of_order_return_per_channel_vm_only_bytes);
3228 		dcn2_0_nv12_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only =
3229 				fixed16_to_double_to_cpu(bb->pct_ideal_dram_sdp_bw_after_urgent_pixel_only);
3230 		dcn2_0_nv12_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_and_vm =
3231 				fixed16_to_double_to_cpu(bb->pct_ideal_dram_sdp_bw_after_urgent_pixel_and_vm);
3232 		dcn2_0_nv12_soc.pct_ideal_dram_sdp_bw_after_urgent_vm_only =
3233 				fixed16_to_double_to_cpu(bb->pct_ideal_dram_sdp_bw_after_urgent_vm_only);
3234 		dcn2_0_nv12_soc.max_avg_sdp_bw_use_normal_percent =
3235 				fixed16_to_double_to_cpu(bb->max_avg_sdp_bw_use_normal_percent);
3236 		dcn2_0_nv12_soc.max_avg_dram_bw_use_normal_percent =
3237 				fixed16_to_double_to_cpu(bb->max_avg_dram_bw_use_normal_percent);
3238 		dcn2_0_nv12_soc.writeback_latency_us =
3239 				fixed16_to_double_to_cpu(bb->writeback_latency_us);
3240 		dcn2_0_nv12_soc.ideal_dram_bw_after_urgent_percent =
3241 				fixed16_to_double_to_cpu(bb->ideal_dram_bw_after_urgent_percent);
3242 		dcn2_0_nv12_soc.max_request_size_bytes =
3243 				le32_to_cpu(bb->max_request_size_bytes);
3244 		dcn2_0_nv12_soc.dram_channel_width_bytes =
3245 				le32_to_cpu(bb->dram_channel_width_bytes);
3246 		dcn2_0_nv12_soc.fabric_datapath_to_dcn_data_return_bytes =
3247 				le32_to_cpu(bb->fabric_datapath_to_dcn_data_return_bytes);
3248 		dcn2_0_nv12_soc.dcn_downspread_percent =
3249 				fixed16_to_double_to_cpu(bb->dcn_downspread_percent);
3250 		dcn2_0_nv12_soc.downspread_percent =
3251 				fixed16_to_double_to_cpu(bb->downspread_percent);
3252 		dcn2_0_nv12_soc.dram_page_open_time_ns =
3253 				fixed16_to_double_to_cpu(bb->dram_page_open_time_ns);
3254 		dcn2_0_nv12_soc.dram_rw_turnaround_time_ns =
3255 				fixed16_to_double_to_cpu(bb->dram_rw_turnaround_time_ns);
3256 		dcn2_0_nv12_soc.dram_return_buffer_per_channel_bytes =
3257 				le32_to_cpu(bb->dram_return_buffer_per_channel_bytes);
3258 		dcn2_0_nv12_soc.round_trip_ping_latency_dcfclk_cycles =
3259 				le32_to_cpu(bb->round_trip_ping_latency_dcfclk_cycles);
3260 		dcn2_0_nv12_soc.urgent_out_of_order_return_per_channel_bytes =
3261 				le32_to_cpu(bb->urgent_out_of_order_return_per_channel_bytes);
3262 		dcn2_0_nv12_soc.channel_interleave_bytes =
3263 				le32_to_cpu(bb->channel_interleave_bytes);
3264 		dcn2_0_nv12_soc.num_banks =
3265 				le32_to_cpu(bb->num_banks);
3266 		dcn2_0_nv12_soc.num_chans =
3267 				le32_to_cpu(bb->num_chans);
3268 		dcn2_0_nv12_soc.vmm_page_size_bytes =
3269 				le32_to_cpu(bb->vmm_page_size_bytes);
3270 		dcn2_0_nv12_soc.dram_clock_change_latency_us =
3271 				fixed16_to_double_to_cpu(bb->dram_clock_change_latency_us);
3272 		// HACK!! Lower uclock latency switch time so we don't switch
3273 		dcn2_0_nv12_soc.dram_clock_change_latency_us = 10;
3274 		dcn2_0_nv12_soc.writeback_dram_clock_change_latency_us =
3275 				fixed16_to_double_to_cpu(bb->writeback_dram_clock_change_latency_us);
3276 		dcn2_0_nv12_soc.return_bus_width_bytes =
3277 				le32_to_cpu(bb->return_bus_width_bytes);
3278 		dcn2_0_nv12_soc.dispclk_dppclk_vco_speed_mhz =
3279 				le32_to_cpu(bb->dispclk_dppclk_vco_speed_mhz);
3280 		dcn2_0_nv12_soc.xfc_bus_transport_time_us =
3281 				le32_to_cpu(bb->xfc_bus_transport_time_us);
3282 		dcn2_0_nv12_soc.xfc_xbuf_latency_tolerance_us =
3283 				le32_to_cpu(bb->xfc_xbuf_latency_tolerance_us);
3284 		dcn2_0_nv12_soc.use_urgent_burst_bw =
3285 				le32_to_cpu(bb->use_urgent_burst_bw);
3286 		dcn2_0_nv12_soc.num_states =
3287 				le32_to_cpu(bb->num_states);
3288 
3289 		for (i = 0; i < dcn2_0_nv12_soc.num_states; i++) {
3290 			dcn2_0_nv12_soc.clock_limits[i].state =
3291 					le32_to_cpu(bb->clock_limits[i].state);
3292 			dcn2_0_nv12_soc.clock_limits[i].dcfclk_mhz =
3293 					fixed16_to_double_to_cpu(bb->clock_limits[i].dcfclk_mhz);
3294 			dcn2_0_nv12_soc.clock_limits[i].fabricclk_mhz =
3295 					fixed16_to_double_to_cpu(bb->clock_limits[i].fabricclk_mhz);
3296 			dcn2_0_nv12_soc.clock_limits[i].dispclk_mhz =
3297 					fixed16_to_double_to_cpu(bb->clock_limits[i].dispclk_mhz);
3298 			dcn2_0_nv12_soc.clock_limits[i].dppclk_mhz =
3299 					fixed16_to_double_to_cpu(bb->clock_limits[i].dppclk_mhz);
3300 			dcn2_0_nv12_soc.clock_limits[i].phyclk_mhz =
3301 					fixed16_to_double_to_cpu(bb->clock_limits[i].phyclk_mhz);
3302 			dcn2_0_nv12_soc.clock_limits[i].socclk_mhz =
3303 					fixed16_to_double_to_cpu(bb->clock_limits[i].socclk_mhz);
3304 			dcn2_0_nv12_soc.clock_limits[i].dscclk_mhz =
3305 					fixed16_to_double_to_cpu(bb->clock_limits[i].dscclk_mhz);
3306 			dcn2_0_nv12_soc.clock_limits[i].dram_speed_mts =
3307 					fixed16_to_double_to_cpu(bb->clock_limits[i].dram_speed_mts);
3308 		}
3309 	}
3310 
3311 	if (pool->base.pp_smu) {
3312 		struct pp_smu_nv_clock_table max_clocks = {0};
3313 		unsigned int uclk_states[8] = {0};
3314 		unsigned int num_states = 0;
3315 		enum pp_smu_status status;
3316 		bool clock_limits_available = false;
3317 		bool uclk_states_available = false;
3318 
3319 		if (pool->base.pp_smu->nv_funcs.get_uclk_dpm_states) {
3320 			status = (pool->base.pp_smu->nv_funcs.get_uclk_dpm_states)
3321 				(&pool->base.pp_smu->nv_funcs.pp_smu, uclk_states, &num_states);
3322 
3323 			uclk_states_available = (status == PP_SMU_RESULT_OK);
3324 		}
3325 
3326 		if (pool->base.pp_smu->nv_funcs.get_maximum_sustainable_clocks) {
3327 			status = (*pool->base.pp_smu->nv_funcs.get_maximum_sustainable_clocks)
3328 					(&pool->base.pp_smu->nv_funcs.pp_smu, &max_clocks);
3329 			/* SMU cannot set DCF clock to anything equal to or higher than SOC clock
3330 			 */
3331 			if (max_clocks.dcfClockInKhz >= max_clocks.socClockInKhz)
3332 				max_clocks.dcfClockInKhz = max_clocks.socClockInKhz - 1000;
3333 			clock_limits_available = (status == PP_SMU_RESULT_OK);
3334 		}
3335 
3336 		if (clock_limits_available && uclk_states_available && num_states)
3337 			dcn20_update_bounding_box(dc, loaded_bb, &max_clocks, uclk_states, num_states);
3338 		else if (clock_limits_available)
3339 			dcn20_cap_soc_clocks(loaded_bb, max_clocks);
3340 	}
3341 
3342 	loaded_ip->max_num_otg = pool->base.res_cap->num_timing_generator;
3343 	loaded_ip->max_num_dpp = pool->base.pipe_count;
3344 	dcn20_patch_bounding_box(dc, loaded_bb);
3345 
3346 	return true;
3347 }
3348 
3349 static bool construct(
3350 	uint8_t num_virtual_links,
3351 	struct dc *dc,
3352 	struct dcn20_resource_pool *pool)
3353 {
3354 	int i;
3355 	struct dc_context *ctx = dc->ctx;
3356 	struct irq_service_init_data init_data;
3357 	struct ddc_service_init_data ddc_init_data;
3358 	struct _vcs_dpi_soc_bounding_box_st *loaded_bb =
3359 			get_asic_rev_soc_bb(ctx->asic_id.hw_internal_rev);
3360 	struct _vcs_dpi_ip_params_st *loaded_ip =
3361 			get_asic_rev_ip_params(ctx->asic_id.hw_internal_rev);
3362 	enum dml_project dml_project_version =
3363 			get_dml_project_version(ctx->asic_id.hw_internal_rev);
3364 
3365 	ctx->dc_bios->regs = &bios_regs;
3366 	pool->base.funcs = &dcn20_res_pool_funcs;
3367 
3368 	if (ASICREV_IS_NAVI14_M(ctx->asic_id.hw_internal_rev)) {
3369 		pool->base.res_cap = &res_cap_nv14;
3370 		pool->base.pipe_count = 5;
3371 		pool->base.mpcc_count = 5;
3372 	} else {
3373 		pool->base.res_cap = &res_cap_nv10;
3374 		pool->base.pipe_count = 6;
3375 		pool->base.mpcc_count = 6;
3376 	}
3377 	/*************************************************
3378 	 *  Resource + asic cap harcoding                *
3379 	 *************************************************/
3380 	pool->base.underlay_pipe_index = NO_UNDERLAY_PIPE;
3381 
3382 	dc->caps.max_downscale_ratio = 200;
3383 	dc->caps.i2c_speed_in_khz = 100;
3384 	dc->caps.max_cursor_size = 256;
3385 	dc->caps.dmdata_alloc_size = 2048;
3386 
3387 	dc->caps.max_slave_planes = 1;
3388 	dc->caps.post_blend_color_processing = true;
3389 	dc->caps.force_dp_tps4_for_cp2520 = true;
3390 	dc->caps.hw_3d_lut = true;
3391 	dc->caps.extended_aux_timeout_support = true;
3392 
3393 	if (dc->ctx->dce_environment == DCE_ENV_PRODUCTION_DRV) {
3394 		dc->debug = debug_defaults_drv;
3395 	} else if (dc->ctx->dce_environment == DCE_ENV_FPGA_MAXIMUS) {
3396 		pool->base.pipe_count = 4;
3397 		pool->base.mpcc_count = pool->base.pipe_count;
3398 		dc->debug = debug_defaults_diags;
3399 	} else {
3400 		dc->debug = debug_defaults_diags;
3401 	}
3402 	//dcn2.0x
3403 	dc->work_arounds.dedcn20_305_wa = true;
3404 
3405 	// Init the vm_helper
3406 	if (dc->vm_helper)
3407 		vm_helper_init(dc->vm_helper, 16);
3408 
3409 	/*************************************************
3410 	 *  Create resources                             *
3411 	 *************************************************/
3412 
3413 	pool->base.clock_sources[DCN20_CLK_SRC_PLL0] =
3414 			dcn20_clock_source_create(ctx, ctx->dc_bios,
3415 				CLOCK_SOURCE_COMBO_PHY_PLL0,
3416 				&clk_src_regs[0], false);
3417 	pool->base.clock_sources[DCN20_CLK_SRC_PLL1] =
3418 			dcn20_clock_source_create(ctx, ctx->dc_bios,
3419 				CLOCK_SOURCE_COMBO_PHY_PLL1,
3420 				&clk_src_regs[1], false);
3421 	pool->base.clock_sources[DCN20_CLK_SRC_PLL2] =
3422 			dcn20_clock_source_create(ctx, ctx->dc_bios,
3423 				CLOCK_SOURCE_COMBO_PHY_PLL2,
3424 				&clk_src_regs[2], false);
3425 	pool->base.clock_sources[DCN20_CLK_SRC_PLL3] =
3426 			dcn20_clock_source_create(ctx, ctx->dc_bios,
3427 				CLOCK_SOURCE_COMBO_PHY_PLL3,
3428 				&clk_src_regs[3], false);
3429 	pool->base.clock_sources[DCN20_CLK_SRC_PLL4] =
3430 			dcn20_clock_source_create(ctx, ctx->dc_bios,
3431 				CLOCK_SOURCE_COMBO_PHY_PLL4,
3432 				&clk_src_regs[4], false);
3433 	pool->base.clock_sources[DCN20_CLK_SRC_PLL5] =
3434 			dcn20_clock_source_create(ctx, ctx->dc_bios,
3435 				CLOCK_SOURCE_COMBO_PHY_PLL5,
3436 				&clk_src_regs[5], false);
3437 	pool->base.clk_src_count = DCN20_CLK_SRC_TOTAL;
3438 	/* todo: not reuse phy_pll registers */
3439 	pool->base.dp_clock_source =
3440 			dcn20_clock_source_create(ctx, ctx->dc_bios,
3441 				CLOCK_SOURCE_ID_DP_DTO,
3442 				&clk_src_regs[0], true);
3443 
3444 	for (i = 0; i < pool->base.clk_src_count; i++) {
3445 		if (pool->base.clock_sources[i] == NULL) {
3446 			dm_error("DC: failed to create clock sources!\n");
3447 			BREAK_TO_DEBUGGER();
3448 			goto create_fail;
3449 		}
3450 	}
3451 
3452 	pool->base.dccg = dccg2_create(ctx, &dccg_regs, &dccg_shift, &dccg_mask);
3453 	if (pool->base.dccg == NULL) {
3454 		dm_error("DC: failed to create dccg!\n");
3455 		BREAK_TO_DEBUGGER();
3456 		goto create_fail;
3457 	}
3458 
3459 	pool->base.dmcu = dcn20_dmcu_create(ctx,
3460 			&dmcu_regs,
3461 			&dmcu_shift,
3462 			&dmcu_mask);
3463 	if (pool->base.dmcu == NULL) {
3464 		dm_error("DC: failed to create dmcu!\n");
3465 		BREAK_TO_DEBUGGER();
3466 		goto create_fail;
3467 	}
3468 
3469 	pool->base.abm = dce_abm_create(ctx,
3470 			&abm_regs,
3471 			&abm_shift,
3472 			&abm_mask);
3473 	if (pool->base.abm == NULL) {
3474 		dm_error("DC: failed to create abm!\n");
3475 		BREAK_TO_DEBUGGER();
3476 		goto create_fail;
3477 	}
3478 
3479 	pool->base.pp_smu = dcn20_pp_smu_create(ctx);
3480 
3481 
3482 	if (!init_soc_bounding_box(dc, pool)) {
3483 		dm_error("DC: failed to initialize soc bounding box!\n");
3484 		BREAK_TO_DEBUGGER();
3485 		goto create_fail;
3486 	}
3487 
3488 	dml_init_instance(&dc->dml, loaded_bb, loaded_ip, dml_project_version);
3489 
3490 	if (!dc->debug.disable_pplib_wm_range) {
3491 		struct pp_smu_wm_range_sets ranges = {0};
3492 		int i = 0;
3493 
3494 		ranges.num_reader_wm_sets = 0;
3495 
3496 		if (loaded_bb->num_states == 1) {
3497 			ranges.reader_wm_sets[0].wm_inst = i;
3498 			ranges.reader_wm_sets[0].min_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
3499 			ranges.reader_wm_sets[0].max_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
3500 			ranges.reader_wm_sets[0].min_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
3501 			ranges.reader_wm_sets[0].max_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
3502 
3503 			ranges.num_reader_wm_sets = 1;
3504 		} else if (loaded_bb->num_states > 1) {
3505 			for (i = 0; i < 4 && i < loaded_bb->num_states; i++) {
3506 				ranges.reader_wm_sets[i].wm_inst = i;
3507 				ranges.reader_wm_sets[i].min_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
3508 				ranges.reader_wm_sets[i].max_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
3509 				ranges.reader_wm_sets[i].min_fill_clk_mhz = (i > 0) ? (loaded_bb->clock_limits[i - 1].dram_speed_mts / 16) + 1 : 0;
3510 				ranges.reader_wm_sets[i].max_fill_clk_mhz = loaded_bb->clock_limits[i].dram_speed_mts / 16;
3511 
3512 				ranges.num_reader_wm_sets = i + 1;
3513 			}
3514 
3515 			ranges.reader_wm_sets[0].min_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
3516 			ranges.reader_wm_sets[ranges.num_reader_wm_sets - 1].max_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
3517 		}
3518 
3519 		ranges.num_writer_wm_sets = 1;
3520 
3521 		ranges.writer_wm_sets[0].wm_inst = 0;
3522 		ranges.writer_wm_sets[0].min_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
3523 		ranges.writer_wm_sets[0].max_fill_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
3524 		ranges.writer_wm_sets[0].min_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MIN;
3525 		ranges.writer_wm_sets[0].max_drain_clk_mhz = PP_SMU_WM_SET_RANGE_CLK_UNCONSTRAINED_MAX;
3526 
3527 		/* Notify PP Lib/SMU which Watermarks to use for which clock ranges */
3528 		if (pool->base.pp_smu->nv_funcs.set_wm_ranges)
3529 			pool->base.pp_smu->nv_funcs.set_wm_ranges(&pool->base.pp_smu->nv_funcs.pp_smu, &ranges);
3530 	}
3531 
3532 	init_data.ctx = dc->ctx;
3533 	pool->base.irqs = dal_irq_service_dcn20_create(&init_data);
3534 	if (!pool->base.irqs)
3535 		goto create_fail;
3536 
3537 	/* mem input -> ipp -> dpp -> opp -> TG */
3538 	for (i = 0; i < pool->base.pipe_count; i++) {
3539 		pool->base.hubps[i] = dcn20_hubp_create(ctx, i);
3540 		if (pool->base.hubps[i] == NULL) {
3541 			BREAK_TO_DEBUGGER();
3542 			dm_error(
3543 				"DC: failed to create memory input!\n");
3544 			goto create_fail;
3545 		}
3546 
3547 		pool->base.ipps[i] = dcn20_ipp_create(ctx, i);
3548 		if (pool->base.ipps[i] == NULL) {
3549 			BREAK_TO_DEBUGGER();
3550 			dm_error(
3551 				"DC: failed to create input pixel processor!\n");
3552 			goto create_fail;
3553 		}
3554 
3555 		pool->base.dpps[i] = dcn20_dpp_create(ctx, i);
3556 		if (pool->base.dpps[i] == NULL) {
3557 			BREAK_TO_DEBUGGER();
3558 			dm_error(
3559 				"DC: failed to create dpps!\n");
3560 			goto create_fail;
3561 		}
3562 	}
3563 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
3564 		pool->base.engines[i] = dcn20_aux_engine_create(ctx, i);
3565 		if (pool->base.engines[i] == NULL) {
3566 			BREAK_TO_DEBUGGER();
3567 			dm_error(
3568 				"DC:failed to create aux engine!!\n");
3569 			goto create_fail;
3570 		}
3571 		pool->base.hw_i2cs[i] = dcn20_i2c_hw_create(ctx, i);
3572 		if (pool->base.hw_i2cs[i] == NULL) {
3573 			BREAK_TO_DEBUGGER();
3574 			dm_error(
3575 				"DC:failed to create hw i2c!!\n");
3576 			goto create_fail;
3577 		}
3578 		pool->base.sw_i2cs[i] = NULL;
3579 	}
3580 
3581 	for (i = 0; i < pool->base.res_cap->num_opp; i++) {
3582 		pool->base.opps[i] = dcn20_opp_create(ctx, i);
3583 		if (pool->base.opps[i] == NULL) {
3584 			BREAK_TO_DEBUGGER();
3585 			dm_error(
3586 				"DC: failed to create output pixel processor!\n");
3587 			goto create_fail;
3588 		}
3589 	}
3590 
3591 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
3592 		pool->base.timing_generators[i] = dcn20_timing_generator_create(
3593 				ctx, i);
3594 		if (pool->base.timing_generators[i] == NULL) {
3595 			BREAK_TO_DEBUGGER();
3596 			dm_error("DC: failed to create tg!\n");
3597 			goto create_fail;
3598 		}
3599 	}
3600 
3601 	pool->base.timing_generator_count = i;
3602 
3603 	pool->base.mpc = dcn20_mpc_create(ctx);
3604 	if (pool->base.mpc == NULL) {
3605 		BREAK_TO_DEBUGGER();
3606 		dm_error("DC: failed to create mpc!\n");
3607 		goto create_fail;
3608 	}
3609 
3610 	pool->base.hubbub = dcn20_hubbub_create(ctx);
3611 	if (pool->base.hubbub == NULL) {
3612 		BREAK_TO_DEBUGGER();
3613 		dm_error("DC: failed to create hubbub!\n");
3614 		goto create_fail;
3615 	}
3616 
3617 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
3618 		pool->base.dscs[i] = dcn20_dsc_create(ctx, i);
3619 		if (pool->base.dscs[i] == NULL) {
3620 			BREAK_TO_DEBUGGER();
3621 			dm_error("DC: failed to create display stream compressor %d!\n", i);
3622 			goto create_fail;
3623 		}
3624 	}
3625 
3626 	if (!dcn20_dwbc_create(ctx, &pool->base)) {
3627 		BREAK_TO_DEBUGGER();
3628 		dm_error("DC: failed to create dwbc!\n");
3629 		goto create_fail;
3630 	}
3631 	if (!dcn20_mmhubbub_create(ctx, &pool->base)) {
3632 		BREAK_TO_DEBUGGER();
3633 		dm_error("DC: failed to create mcif_wb!\n");
3634 		goto create_fail;
3635 	}
3636 
3637 	if (!resource_construct(num_virtual_links, dc, &pool->base,
3638 			(!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment) ?
3639 			&res_create_funcs : &res_create_maximus_funcs)))
3640 			goto create_fail;
3641 
3642 	dcn20_hw_sequencer_construct(dc);
3643 
3644 	dc->caps.max_planes =  pool->base.pipe_count;
3645 
3646 	for (i = 0; i < dc->caps.max_planes; ++i)
3647 		dc->caps.planes[i] = plane_cap;
3648 
3649 	dc->cap_funcs = cap_funcs;
3650 
3651 	if (dc->ctx->dc_bios->fw_info.oem_i2c_present) {
3652 		ddc_init_data.ctx = dc->ctx;
3653 		ddc_init_data.link = NULL;
3654 		ddc_init_data.id.id = dc->ctx->dc_bios->fw_info.oem_i2c_obj_id;
3655 		ddc_init_data.id.enum_id = 0;
3656 		ddc_init_data.id.type = OBJECT_TYPE_GENERIC;
3657 		pool->base.oem_device = dal_ddc_service_create(&ddc_init_data);
3658 	} else {
3659 		pool->base.oem_device = NULL;
3660 	}
3661 
3662 	return true;
3663 
3664 create_fail:
3665 
3666 	destruct(pool);
3667 
3668 	return false;
3669 }
3670 
3671 struct resource_pool *dcn20_create_resource_pool(
3672 		const struct dc_init_data *init_data,
3673 		struct dc *dc)
3674 {
3675 	struct dcn20_resource_pool *pool =
3676 		kzalloc(sizeof(struct dcn20_resource_pool), GFP_KERNEL);
3677 
3678 	if (!pool)
3679 		return NULL;
3680 
3681 	if (construct(init_data->num_virtual_links, dc, pool))
3682 		return &pool->base;
3683 
3684 	BREAK_TO_DEBUGGER();
3685 	kfree(pool);
3686 	return NULL;
3687 }
3688