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