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