1 /*
2  * Copyright 2020 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 
27 #include "dm_services.h"
28 #include "dc.h"
29 
30 #include "dcn30_init.h"
31 
32 #include "resource.h"
33 #include "include/irq_service_interface.h"
34 #include "dcn20/dcn20_resource.h"
35 
36 #include "dcn30_resource.h"
37 
38 #include "dcn10/dcn10_ipp.h"
39 #include "dcn30/dcn30_hubbub.h"
40 #include "dcn30/dcn30_mpc.h"
41 #include "dcn30/dcn30_hubp.h"
42 #include "irq/dcn30/irq_service_dcn30.h"
43 #include "dcn30/dcn30_dpp.h"
44 #include "dcn30/dcn30_optc.h"
45 #include "dcn20/dcn20_hwseq.h"
46 #include "dcn30/dcn30_hwseq.h"
47 #include "dce110/dce110_hw_sequencer.h"
48 #include "dcn30/dcn30_opp.h"
49 #include "dcn20/dcn20_dsc.h"
50 #include "dcn30/dcn30_vpg.h"
51 #include "dcn30/dcn30_afmt.h"
52 #include "dcn30/dcn30_dio_stream_encoder.h"
53 #include "dcn30/dcn30_dio_link_encoder.h"
54 #include "dce/dce_clock_source.h"
55 #include "dce/dce_audio.h"
56 #include "dce/dce_hwseq.h"
57 #include "clk_mgr.h"
58 #include "virtual/virtual_stream_encoder.h"
59 #include "dce110/dce110_resource.h"
60 #include "dml/display_mode_vba.h"
61 #include "dcn30/dcn30_dccg.h"
62 #include "dcn10/dcn10_resource.h"
63 #include "dce/dce_panel_cntl.h"
64 
65 #include "dcn30/dcn30_dwb.h"
66 #include "dcn30/dcn30_mmhubbub.h"
67 
68 #include "sienna_cichlid_ip_offset.h"
69 #include "dcn/dcn_3_0_0_offset.h"
70 #include "dcn/dcn_3_0_0_sh_mask.h"
71 
72 #include "nbio/nbio_7_4_offset.h"
73 
74 #include "dcn/dpcs_3_0_0_offset.h"
75 #include "dcn/dpcs_3_0_0_sh_mask.h"
76 
77 #include "mmhub/mmhub_2_0_0_offset.h"
78 #include "mmhub/mmhub_2_0_0_sh_mask.h"
79 
80 #include "reg_helper.h"
81 #include "dce/dmub_abm.h"
82 #include "dce/dmub_psr.h"
83 #include "dce/dce_aux.h"
84 #include "dce/dce_i2c.h"
85 
86 #include "dml/dcn30/display_mode_vba_30.h"
87 #include "vm_helper.h"
88 #include "dcn20/dcn20_vmid.h"
89 #include "amdgpu_socbb.h"
90 
91 #define DC_LOGGER_INIT(logger)
92 
93 struct _vcs_dpi_ip_params_st dcn3_0_ip = {
94 	.use_min_dcfclk = 1,
95 	.clamp_min_dcfclk = 0,
96 	.odm_capable = 1,
97 	.gpuvm_enable = 0,
98 	.hostvm_enable = 0,
99 	.gpuvm_max_page_table_levels = 4,
100 	.hostvm_max_page_table_levels = 4,
101 	.hostvm_cached_page_table_levels = 0,
102 	.pte_group_size_bytes = 2048,
103 	.num_dsc = 6,
104 	.rob_buffer_size_kbytes = 184,
105 	.det_buffer_size_kbytes = 184,
106 	.dpte_buffer_size_in_pte_reqs_luma = 84,
107 	.pde_proc_buffer_size_64k_reqs = 48,
108 	.dpp_output_buffer_pixels = 2560,
109 	.opp_output_buffer_lines = 1,
110 	.pixel_chunk_size_kbytes = 8,
111 	.pte_enable = 1,
112 	.max_page_table_levels = 2,
113 	.pte_chunk_size_kbytes = 2,  // ?
114 	.meta_chunk_size_kbytes = 2,
115 	.writeback_chunk_size_kbytes = 8,
116 	.line_buffer_size_bits = 789504,
117 	.is_line_buffer_bpp_fixed = 0,  // ?
118 	.line_buffer_fixed_bpp = 0,     // ?
119 	.dcc_supported = true,
120 	.writeback_interface_buffer_size_kbytes = 90,
121 	.writeback_line_buffer_buffer_size = 0,
122 	.max_line_buffer_lines = 12,
123 	.writeback_luma_buffer_size_kbytes = 12,  // writeback_line_buffer_buffer_size = 656640
124 	.writeback_chroma_buffer_size_kbytes = 8,
125 	.writeback_chroma_line_buffer_width_pixels = 4,
126 	.writeback_max_hscl_ratio = 1,
127 	.writeback_max_vscl_ratio = 1,
128 	.writeback_min_hscl_ratio = 1,
129 	.writeback_min_vscl_ratio = 1,
130 	.writeback_max_hscl_taps = 1,
131 	.writeback_max_vscl_taps = 1,
132 	.writeback_line_buffer_luma_buffer_size = 0,
133 	.writeback_line_buffer_chroma_buffer_size = 14643,
134 	.cursor_buffer_size = 8,
135 	.cursor_chunk_size = 2,
136 	.max_num_otg = 6,
137 	.max_num_dpp = 6,
138 	.max_num_wb = 1,
139 	.max_dchub_pscl_bw_pix_per_clk = 4,
140 	.max_pscl_lb_bw_pix_per_clk = 2,
141 	.max_lb_vscl_bw_pix_per_clk = 4,
142 	.max_vscl_hscl_bw_pix_per_clk = 4,
143 	.max_hscl_ratio = 6,
144 	.max_vscl_ratio = 6,
145 	.hscl_mults = 4,
146 	.vscl_mults = 4,
147 	.max_hscl_taps = 8,
148 	.max_vscl_taps = 8,
149 	.dispclk_ramp_margin_percent = 1,
150 	.underscan_factor = 1.11,
151 	.min_vblank_lines = 32,
152 	.dppclk_delay_subtotal = 46,
153 	.dynamic_metadata_vm_enabled = true,
154 	.dppclk_delay_scl_lb_only = 16,
155 	.dppclk_delay_scl = 50,
156 	.dppclk_delay_cnvc_formatter = 27,
157 	.dppclk_delay_cnvc_cursor = 6,
158 	.dispclk_delay_subtotal = 119,
159 	.dcfclk_cstate_latency = 5.2, // SRExitTime
160 	.max_inter_dcn_tile_repeaters = 8,
161 	.odm_combine_4to1_supported = true,
162 
163 	.xfc_supported = false,
164 	.xfc_fill_bw_overhead_percent = 10.0,
165 	.xfc_fill_constant_bytes = 0,
166 	.gfx7_compat_tiling_supported = 0,
167 	.number_of_cursors = 1,
168 };
169 
170 struct _vcs_dpi_soc_bounding_box_st dcn3_0_soc = {
171 	.clock_limits = {
172 			{
173 				.state = 0,
174 				.dispclk_mhz = 562.0,
175 				.dppclk_mhz = 300.0,
176 				.phyclk_mhz = 300.0,
177 				.phyclk_d18_mhz = 667.0,
178 				.dscclk_mhz = 405.6,
179 			},
180 		},
181 	.min_dcfclk = 500.0, /* TODO: set this to actual min DCFCLK */
182 	.num_states = 1,
183 	.sr_exit_time_us = 12,
184 	.sr_enter_plus_exit_time_us = 20,
185 	.urgent_latency_us = 4.0,
186 	.urgent_latency_pixel_data_only_us = 4.0,
187 	.urgent_latency_pixel_mixed_with_vm_data_us = 4.0,
188 	.urgent_latency_vm_data_only_us = 4.0,
189 	.urgent_out_of_order_return_per_channel_pixel_only_bytes = 4096,
190 	.urgent_out_of_order_return_per_channel_pixel_and_vm_bytes = 4096,
191 	.urgent_out_of_order_return_per_channel_vm_only_bytes = 4096,
192 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_only = 80.0,
193 	.pct_ideal_dram_sdp_bw_after_urgent_pixel_and_vm = 60.0,
194 	.pct_ideal_dram_sdp_bw_after_urgent_vm_only = 40.0,
195 	.max_avg_sdp_bw_use_normal_percent = 60.0,
196 	.max_avg_dram_bw_use_normal_percent = 40.0,
197 	.writeback_latency_us = 12.0,
198 	.max_request_size_bytes = 256,
199 	.fabric_datapath_to_dcn_data_return_bytes = 64,
200 	.dcn_downspread_percent = 0.5,
201 	.downspread_percent = 0.38,
202 	.dram_page_open_time_ns = 50.0,
203 	.dram_rw_turnaround_time_ns = 17.5,
204 	.dram_return_buffer_per_channel_bytes = 8192,
205 	.round_trip_ping_latency_dcfclk_cycles = 191,
206 	.urgent_out_of_order_return_per_channel_bytes = 4096,
207 	.channel_interleave_bytes = 256,
208 	.num_banks = 8,
209 	.gpuvm_min_page_size_bytes = 4096,
210 	.hostvm_min_page_size_bytes = 4096,
211 	.dram_clock_change_latency_us = 404,
212 	.dummy_pstate_latency_us = 5,
213 	.writeback_dram_clock_change_latency_us = 23.0,
214 	.return_bus_width_bytes = 64,
215 	.dispclk_dppclk_vco_speed_mhz = 3650,
216 	.xfc_bus_transport_time_us = 20,      // ?
217 	.xfc_xbuf_latency_tolerance_us = 4,  // ?
218 	.use_urgent_burst_bw = 1,            // ?
219 	.do_urgent_latency_adjustment = true,
220 	.urgent_latency_adjustment_fabric_clock_component_us = 1.0,
221 	.urgent_latency_adjustment_fabric_clock_reference_mhz = 1000,
222 };
223 
224 enum dcn30_clk_src_array_id {
225 	DCN30_CLK_SRC_PLL0,
226 	DCN30_CLK_SRC_PLL1,
227 	DCN30_CLK_SRC_PLL2,
228 	DCN30_CLK_SRC_PLL3,
229 	DCN30_CLK_SRC_PLL4,
230 	DCN30_CLK_SRC_PLL5,
231 	DCN30_CLK_SRC_TOTAL
232 };
233 
234 /* begin *********************
235  * macros to expend register list macro defined in HW object header file
236  */
237 
238 /* DCN */
239 /* TODO awful hack. fixup dcn20_dwb.h */
240 #undef BASE_INNER
241 #define BASE_INNER(seg) DCN_BASE__INST0_SEG ## seg
242 
243 #define BASE(seg) BASE_INNER(seg)
244 
245 #define SR(reg_name)\
246 		.reg_name = BASE(mm ## reg_name ## _BASE_IDX) +  \
247 					mm ## reg_name
248 
249 #define SRI(reg_name, block, id)\
250 	.reg_name = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
251 					mm ## block ## id ## _ ## reg_name
252 
253 #define SRI2(reg_name, block, id)\
254 	.reg_name = BASE(mm ## reg_name ## _BASE_IDX) + \
255 					mm ## reg_name
256 
257 #define SRIR(var_name, reg_name, block, id)\
258 	.var_name = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
259 					mm ## block ## id ## _ ## reg_name
260 
261 #define SRII(reg_name, block, id)\
262 	.reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
263 					mm ## block ## id ## _ ## reg_name
264 
265 #define SRII_MPC_RMU(reg_name, block, id)\
266 	.RMU##_##reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
267 					mm ## block ## id ## _ ## reg_name
268 
269 #define SRII_DWB(reg_name, temp_name, block, id)\
270 	.reg_name[id] = BASE(mm ## block ## id ## _ ## temp_name ## _BASE_IDX) + \
271 					mm ## block ## id ## _ ## temp_name
272 
273 #define DCCG_SRII(reg_name, block, id)\
274 	.block ## _ ## reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
275 					mm ## block ## id ## _ ## reg_name
276 
277 #define VUPDATE_SRII(reg_name, block, id)\
278 	.reg_name[id] = BASE(mm ## reg_name ## _ ## block ## id ## _BASE_IDX) + \
279 					mm ## reg_name ## _ ## block ## id
280 
281 /* NBIO */
282 #define NBIO_BASE_INNER(seg) \
283 	NBIO_BASE__INST0_SEG ## seg
284 
285 #define NBIO_BASE(seg) \
286 	NBIO_BASE_INNER(seg)
287 
288 #define NBIO_SR(reg_name)\
289 		.reg_name = NBIO_BASE(mm ## reg_name ## _BASE_IDX) + \
290 					mm ## reg_name
291 
292 /* MMHUB */
293 #define MMHUB_BASE_INNER(seg) \
294 	MMHUB_BASE__INST0_SEG ## seg
295 
296 #define MMHUB_BASE(seg) \
297 	MMHUB_BASE_INNER(seg)
298 
299 #define MMHUB_SR(reg_name)\
300 		.reg_name = MMHUB_BASE(mmMM ## reg_name ## _BASE_IDX) + \
301 					mmMM ## reg_name
302 
303 /* CLOCK */
304 #define CLK_BASE_INNER(seg) \
305 	CLK_BASE__INST0_SEG ## seg
306 
307 #define CLK_BASE(seg) \
308 	CLK_BASE_INNER(seg)
309 
310 #define CLK_SRI(reg_name, block, inst)\
311 	.reg_name = CLK_BASE(mm ## block ## _ ## inst ## _ ## reg_name ## _BASE_IDX) + \
312 					mm ## block ## _ ## inst ## _ ## reg_name
313 
314 
315 static const struct bios_registers bios_regs = {
316 		NBIO_SR(BIOS_SCRATCH_3),
317 		NBIO_SR(BIOS_SCRATCH_6)
318 };
319 
320 #define clk_src_regs(index, pllid)\
321 [index] = {\
322 	CS_COMMON_REG_LIST_DCN2_0(index, pllid),\
323 }
324 
325 static const struct dce110_clk_src_regs clk_src_regs[] = {
326 	clk_src_regs(0, A),
327 	clk_src_regs(1, B),
328 	clk_src_regs(2, C),
329 	clk_src_regs(3, D),
330 	clk_src_regs(4, E),
331 	clk_src_regs(5, F)
332 };
333 
334 static const struct dce110_clk_src_shift cs_shift = {
335 		CS_COMMON_MASK_SH_LIST_DCN2_0(__SHIFT)
336 };
337 
338 static const struct dce110_clk_src_mask cs_mask = {
339 		CS_COMMON_MASK_SH_LIST_DCN2_0(_MASK)
340 };
341 
342 #define abm_regs(id)\
343 [id] = {\
344 		ABM_DCN30_REG_LIST(id)\
345 }
346 
347 static const struct dce_abm_registers abm_regs[] = {
348 		abm_regs(0),
349 		abm_regs(1),
350 		abm_regs(2),
351 		abm_regs(3),
352 		abm_regs(4),
353 		abm_regs(5),
354 };
355 
356 static const struct dce_abm_shift abm_shift = {
357 		ABM_MASK_SH_LIST_DCN301(__SHIFT)
358 };
359 
360 static const struct dce_abm_mask abm_mask = {
361 		ABM_MASK_SH_LIST_DCN301(_MASK)
362 };
363 
364 
365 
366 #define audio_regs(id)\
367 [id] = {\
368 		AUD_COMMON_REG_LIST(id)\
369 }
370 
371 static const struct dce_audio_registers audio_regs[] = {
372 	audio_regs(0),
373 	audio_regs(1),
374 	audio_regs(2),
375 	audio_regs(3),
376 	audio_regs(4),
377 	audio_regs(5),
378 	audio_regs(6)
379 };
380 
381 #define DCE120_AUD_COMMON_MASK_SH_LIST(mask_sh)\
382 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_INDEX, AZALIA_ENDPOINT_REG_INDEX, mask_sh),\
383 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_DATA, AZALIA_ENDPOINT_REG_DATA, mask_sh),\
384 		AUD_COMMON_MASK_SH_LIST_BASE(mask_sh)
385 
386 static const struct dce_audio_shift audio_shift = {
387 		DCE120_AUD_COMMON_MASK_SH_LIST(__SHIFT)
388 };
389 
390 static const struct dce_audio_mask audio_mask = {
391 		DCE120_AUD_COMMON_MASK_SH_LIST(_MASK)
392 };
393 
394 #define vpg_regs(id)\
395 [id] = {\
396 	VPG_DCN3_REG_LIST(id)\
397 }
398 
399 static const struct dcn30_vpg_registers vpg_regs[] = {
400 	vpg_regs(0),
401 	vpg_regs(1),
402 	vpg_regs(2),
403 	vpg_regs(3),
404 	vpg_regs(4),
405 	vpg_regs(5),
406 	vpg_regs(6),
407 };
408 
409 static const struct dcn30_vpg_shift vpg_shift = {
410 	DCN3_VPG_MASK_SH_LIST(__SHIFT)
411 };
412 
413 static const struct dcn30_vpg_mask vpg_mask = {
414 	DCN3_VPG_MASK_SH_LIST(_MASK)
415 };
416 
417 #define afmt_regs(id)\
418 [id] = {\
419 	AFMT_DCN3_REG_LIST(id)\
420 }
421 
422 static const struct dcn30_afmt_registers afmt_regs[] = {
423 	afmt_regs(0),
424 	afmt_regs(1),
425 	afmt_regs(2),
426 	afmt_regs(3),
427 	afmt_regs(4),
428 	afmt_regs(5),
429 	afmt_regs(6),
430 };
431 
432 static const struct dcn30_afmt_shift afmt_shift = {
433 	DCN3_AFMT_MASK_SH_LIST(__SHIFT)
434 };
435 
436 static const struct dcn30_afmt_mask afmt_mask = {
437 	DCN3_AFMT_MASK_SH_LIST(_MASK)
438 };
439 
440 #define stream_enc_regs(id)\
441 [id] = {\
442 	SE_DCN3_REG_LIST(id)\
443 }
444 
445 static const struct dcn10_stream_enc_registers stream_enc_regs[] = {
446 	stream_enc_regs(0),
447 	stream_enc_regs(1),
448 	stream_enc_regs(2),
449 	stream_enc_regs(3),
450 	stream_enc_regs(4),
451 	stream_enc_regs(5)
452 };
453 
454 static const struct dcn10_stream_encoder_shift se_shift = {
455 		SE_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
456 };
457 
458 static const struct dcn10_stream_encoder_mask se_mask = {
459 		SE_COMMON_MASK_SH_LIST_DCN30(_MASK)
460 };
461 
462 
463 #define aux_regs(id)\
464 [id] = {\
465 	DCN2_AUX_REG_LIST(id)\
466 }
467 
468 static const struct dcn10_link_enc_aux_registers link_enc_aux_regs[] = {
469 		aux_regs(0),
470 		aux_regs(1),
471 		aux_regs(2),
472 		aux_regs(3),
473 		aux_regs(4),
474 		aux_regs(5)
475 };
476 
477 #define hpd_regs(id)\
478 [id] = {\
479 	HPD_REG_LIST(id)\
480 }
481 
482 static const struct dcn10_link_enc_hpd_registers link_enc_hpd_regs[] = {
483 		hpd_regs(0),
484 		hpd_regs(1),
485 		hpd_regs(2),
486 		hpd_regs(3),
487 		hpd_regs(4),
488 		hpd_regs(5)
489 };
490 
491 #define link_regs(id, phyid)\
492 [id] = {\
493 	LE_DCN3_REG_LIST(id), \
494 	UNIPHY_DCN2_REG_LIST(phyid), \
495 	DPCS_DCN2_REG_LIST(id), \
496 	SRI(DP_DPHY_INTERNAL_CTRL, DP, id) \
497 }
498 
499 static const struct dce110_aux_registers_shift aux_shift = {
500 	DCN_AUX_MASK_SH_LIST(__SHIFT)
501 };
502 
503 static const struct dce110_aux_registers_mask aux_mask = {
504 	DCN_AUX_MASK_SH_LIST(_MASK)
505 };
506 
507 static const struct dcn10_link_enc_registers link_enc_regs[] = {
508 	link_regs(0, A),
509 	link_regs(1, B),
510 	link_regs(2, C),
511 	link_regs(3, D),
512 	link_regs(4, E),
513 	link_regs(5, F)
514 };
515 
516 static const struct dcn10_link_enc_shift le_shift = {
517 	LINK_ENCODER_MASK_SH_LIST_DCN30(__SHIFT),\
518 	DPCS_DCN2_MASK_SH_LIST(__SHIFT)
519 };
520 
521 static const struct dcn10_link_enc_mask le_mask = {
522 	LINK_ENCODER_MASK_SH_LIST_DCN30(_MASK),\
523 	DPCS_DCN2_MASK_SH_LIST(_MASK)
524 };
525 
526 
527 static const struct dce_panel_cntl_registers panel_cntl_regs[] = {
528 	{ DCN_PANEL_CNTL_REG_LIST() }
529 };
530 
531 static const struct dce_panel_cntl_shift panel_cntl_shift = {
532 	DCE_PANEL_CNTL_MASK_SH_LIST(__SHIFT)
533 };
534 
535 static const struct dce_panel_cntl_mask panel_cntl_mask = {
536 	DCE_PANEL_CNTL_MASK_SH_LIST(_MASK)
537 };
538 
539 #define dpp_regs(id)\
540 [id] = {\
541 	DPP_REG_LIST_DCN30(id),\
542 }
543 
544 static const struct dcn3_dpp_registers dpp_regs[] = {
545 	dpp_regs(0),
546 	dpp_regs(1),
547 	dpp_regs(2),
548 	dpp_regs(3),
549 	dpp_regs(4),
550 	dpp_regs(5),
551 };
552 
553 static const struct dcn3_dpp_shift tf_shift = {
554 		DPP_REG_LIST_SH_MASK_DCN30(__SHIFT)
555 };
556 
557 static const struct dcn3_dpp_mask tf_mask = {
558 		DPP_REG_LIST_SH_MASK_DCN30(_MASK)
559 };
560 
561 #define opp_regs(id)\
562 [id] = {\
563 	OPP_REG_LIST_DCN30(id),\
564 }
565 
566 static const struct dcn20_opp_registers opp_regs[] = {
567 	opp_regs(0),
568 	opp_regs(1),
569 	opp_regs(2),
570 	opp_regs(3),
571 	opp_regs(4),
572 	opp_regs(5)
573 };
574 
575 static const struct dcn20_opp_shift opp_shift = {
576 	OPP_MASK_SH_LIST_DCN20(__SHIFT)
577 };
578 
579 static const struct dcn20_opp_mask opp_mask = {
580 	OPP_MASK_SH_LIST_DCN20(_MASK)
581 };
582 
583 #define aux_engine_regs(id)\
584 [id] = {\
585 	AUX_COMMON_REG_LIST0(id), \
586 	.AUXN_IMPCAL = 0, \
587 	.AUXP_IMPCAL = 0, \
588 	.AUX_RESET_MASK = DP_AUX0_AUX_CONTROL__AUX_RESET_MASK, \
589 }
590 
591 static const struct dce110_aux_registers aux_engine_regs[] = {
592 		aux_engine_regs(0),
593 		aux_engine_regs(1),
594 		aux_engine_regs(2),
595 		aux_engine_regs(3),
596 		aux_engine_regs(4),
597 		aux_engine_regs(5)
598 };
599 
600 #define dwbc_regs_dcn3(id)\
601 [id] = {\
602 	DWBC_COMMON_REG_LIST_DCN30(id),\
603 }
604 
605 static const struct dcn30_dwbc_registers dwbc30_regs[] = {
606 	dwbc_regs_dcn3(0),
607 };
608 
609 static const struct dcn30_dwbc_shift dwbc30_shift = {
610 	DWBC_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
611 };
612 
613 static const struct dcn30_dwbc_mask dwbc30_mask = {
614 	DWBC_COMMON_MASK_SH_LIST_DCN30(_MASK)
615 };
616 
617 #define mcif_wb_regs_dcn3(id)\
618 [id] = {\
619 	MCIF_WB_COMMON_REG_LIST_DCN30(id),\
620 }
621 
622 static const struct dcn30_mmhubbub_registers mcif_wb30_regs[] = {
623 	mcif_wb_regs_dcn3(0)
624 };
625 
626 static const struct dcn30_mmhubbub_shift mcif_wb30_shift = {
627 	MCIF_WB_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
628 };
629 
630 static const struct dcn30_mmhubbub_mask mcif_wb30_mask = {
631 	MCIF_WB_COMMON_MASK_SH_LIST_DCN30(_MASK)
632 };
633 
634 #define dsc_regsDCN20(id)\
635 [id] = {\
636 	DSC_REG_LIST_DCN20(id)\
637 }
638 
639 static const struct dcn20_dsc_registers dsc_regs[] = {
640 	dsc_regsDCN20(0),
641 	dsc_regsDCN20(1),
642 	dsc_regsDCN20(2),
643 	dsc_regsDCN20(3),
644 	dsc_regsDCN20(4),
645 	dsc_regsDCN20(5)
646 };
647 
648 static const struct dcn20_dsc_shift dsc_shift = {
649 	DSC_REG_LIST_SH_MASK_DCN20(__SHIFT)
650 };
651 
652 static const struct dcn20_dsc_mask dsc_mask = {
653 	DSC_REG_LIST_SH_MASK_DCN20(_MASK)
654 };
655 
656 static const struct dcn30_mpc_registers mpc_regs = {
657 		MPC_REG_LIST_DCN3_0(0),
658 		MPC_REG_LIST_DCN3_0(1),
659 		MPC_REG_LIST_DCN3_0(2),
660 		MPC_REG_LIST_DCN3_0(3),
661 		MPC_REG_LIST_DCN3_0(4),
662 		MPC_REG_LIST_DCN3_0(5),
663 		MPC_OUT_MUX_REG_LIST_DCN3_0(0),
664 		MPC_OUT_MUX_REG_LIST_DCN3_0(1),
665 		MPC_OUT_MUX_REG_LIST_DCN3_0(2),
666 		MPC_OUT_MUX_REG_LIST_DCN3_0(3),
667 		MPC_OUT_MUX_REG_LIST_DCN3_0(4),
668 		MPC_OUT_MUX_REG_LIST_DCN3_0(5),
669 		MPC_RMU_GLOBAL_REG_LIST_DCN3AG,
670 		MPC_RMU_REG_LIST_DCN3AG(0),
671 		MPC_RMU_REG_LIST_DCN3AG(1),
672 		MPC_RMU_REG_LIST_DCN3AG(2),
673 		MPC_DWB_MUX_REG_LIST_DCN3_0(0),
674 };
675 
676 static const struct dcn30_mpc_shift mpc_shift = {
677 	MPC_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
678 };
679 
680 static const struct dcn30_mpc_mask mpc_mask = {
681 	MPC_COMMON_MASK_SH_LIST_DCN30(_MASK)
682 };
683 
684 #define optc_regs(id)\
685 [id] = {OPTC_COMMON_REG_LIST_DCN3_0(id)}
686 
687 
688 static const struct dcn_optc_registers optc_regs[] = {
689 	optc_regs(0),
690 	optc_regs(1),
691 	optc_regs(2),
692 	optc_regs(3),
693 	optc_regs(4),
694 	optc_regs(5)
695 };
696 
697 static const struct dcn_optc_shift optc_shift = {
698 	OPTC_COMMON_MASK_SH_LIST_DCN30(__SHIFT)
699 };
700 
701 static const struct dcn_optc_mask optc_mask = {
702 	OPTC_COMMON_MASK_SH_LIST_DCN30(_MASK)
703 };
704 
705 #define hubp_regs(id)\
706 [id] = {\
707 	HUBP_REG_LIST_DCN30(id)\
708 }
709 
710 static const struct dcn_hubp2_registers hubp_regs[] = {
711 		hubp_regs(0),
712 		hubp_regs(1),
713 		hubp_regs(2),
714 		hubp_regs(3),
715 		hubp_regs(4),
716 		hubp_regs(5)
717 };
718 
719 static const struct dcn_hubp2_shift hubp_shift = {
720 		HUBP_MASK_SH_LIST_DCN30(__SHIFT)
721 };
722 
723 static const struct dcn_hubp2_mask hubp_mask = {
724 		HUBP_MASK_SH_LIST_DCN30(_MASK)
725 };
726 
727 static const struct dcn_hubbub_registers hubbub_reg = {
728 		HUBBUB_REG_LIST_DCN30(0)
729 };
730 
731 static const struct dcn_hubbub_shift hubbub_shift = {
732 		HUBBUB_MASK_SH_LIST_DCN30(__SHIFT)
733 };
734 
735 static const struct dcn_hubbub_mask hubbub_mask = {
736 		HUBBUB_MASK_SH_LIST_DCN30(_MASK)
737 };
738 
739 static const struct dccg_registers dccg_regs = {
740 		DCCG_REG_LIST_DCN30()
741 };
742 
743 static const struct dccg_shift dccg_shift = {
744 		DCCG_MASK_SH_LIST_DCN3(__SHIFT)
745 };
746 
747 static const struct dccg_mask dccg_mask = {
748 		DCCG_MASK_SH_LIST_DCN3(_MASK)
749 };
750 
751 static const struct dce_hwseq_registers hwseq_reg = {
752 		HWSEQ_DCN30_REG_LIST()
753 };
754 
755 static const struct dce_hwseq_shift hwseq_shift = {
756 		HWSEQ_DCN30_MASK_SH_LIST(__SHIFT)
757 };
758 
759 static const struct dce_hwseq_mask hwseq_mask = {
760 		HWSEQ_DCN30_MASK_SH_LIST(_MASK)
761 };
762 #define vmid_regs(id)\
763 [id] = {\
764 		DCN20_VMID_REG_LIST(id)\
765 }
766 
767 static const struct dcn_vmid_registers vmid_regs[] = {
768 	vmid_regs(0),
769 	vmid_regs(1),
770 	vmid_regs(2),
771 	vmid_regs(3),
772 	vmid_regs(4),
773 	vmid_regs(5),
774 	vmid_regs(6),
775 	vmid_regs(7),
776 	vmid_regs(8),
777 	vmid_regs(9),
778 	vmid_regs(10),
779 	vmid_regs(11),
780 	vmid_regs(12),
781 	vmid_regs(13),
782 	vmid_regs(14),
783 	vmid_regs(15)
784 };
785 
786 static const struct dcn20_vmid_shift vmid_shifts = {
787 		DCN20_VMID_MASK_SH_LIST(__SHIFT)
788 };
789 
790 static const struct dcn20_vmid_mask vmid_masks = {
791 		DCN20_VMID_MASK_SH_LIST(_MASK)
792 };
793 
794 static const struct resource_caps res_cap_dcn3 = {
795 	.num_timing_generator = 6,
796 	.num_opp = 6,
797 	.num_video_plane = 6,
798 	.num_audio = 6,
799 	.num_stream_encoder = 6,
800 	.num_pll = 6,
801 	.num_dwb = 1,
802 	.num_ddc = 6,
803 	.num_vmid = 16,
804 	.num_mpc_3dlut = 3,
805 	.num_dsc = 6,
806 };
807 
808 static const struct dc_plane_cap plane_cap = {
809 	.type = DC_PLANE_TYPE_DCN_UNIVERSAL,
810 	.blends_with_above = true,
811 	.blends_with_below = true,
812 	.per_pixel_alpha = true,
813 
814 	.pixel_format_support = {
815 			.argb8888 = true,
816 			.nv12 = true,
817 			.fp16 = true,
818 			.p010 = false,
819 			.ayuv = false,
820 	},
821 
822 	.max_upscale_factor = {
823 			.argb8888 = 16000,
824 			.nv12 = 16000,
825 			.fp16 = 16000
826 	},
827 
828 	.max_downscale_factor = {
829 			.argb8888 = 600,
830 			.nv12 = 600,
831 			.fp16 = 600
832 	}
833 };
834 
835 static const struct dc_debug_options debug_defaults_drv = {
836 	.disable_dmcu = true, //No DMCU on DCN30
837 	.force_abm_enable = false,
838 	.timing_trace = false,
839 	.clock_trace = true,
840 	.disable_pplib_clock_request = true,
841 	.pipe_split_policy = MPC_SPLIT_DYNAMIC,
842 	.force_single_disp_pipe_split = false,
843 	.disable_dcc = DCC_ENABLE,
844 	.vsr_support = true,
845 	.performance_trace = false,
846 	.max_downscale_src_width = 7680,/*upto 8K*/
847 	.disable_pplib_wm_range = false,
848 	.scl_reset_length10 = true,
849 	.sanity_checks = false,
850 	.underflow_assert_delay_us = 0xFFFFFFFF,
851 	.dwb_fi_phase = -1, // -1 = disable,
852 	.dmub_command_table = true,
853 	.disable_psr = false,
854 };
855 
856 static const struct dc_debug_options debug_defaults_diags = {
857 	.disable_dmcu = true, //No dmcu on DCN30
858 	.force_abm_enable = false,
859 	.timing_trace = true,
860 	.clock_trace = true,
861 	.disable_dpp_power_gate = true,
862 	.disable_hubp_power_gate = true,
863 	.disable_clock_gate = true,
864 	.disable_pplib_clock_request = true,
865 	.disable_pplib_wm_range = true,
866 	.disable_stutter = false,
867 	.scl_reset_length10 = true,
868 	.dwb_fi_phase = -1, // -1 = disable
869 	.dmub_command_table = true,
870 	.disable_psr = true,
871 	.enable_tri_buf = true,
872 };
873 
874 void dcn30_dpp_destroy(struct dpp **dpp)
875 {
876 	kfree(TO_DCN20_DPP(*dpp));
877 	*dpp = NULL;
878 }
879 
880 static struct dpp *dcn30_dpp_create(
881 	struct dc_context *ctx,
882 	uint32_t inst)
883 {
884 	struct dcn3_dpp *dpp =
885 		kzalloc(sizeof(struct dcn3_dpp), GFP_KERNEL);
886 
887 	if (!dpp)
888 		return NULL;
889 
890 	if (dpp3_construct(dpp, ctx, inst,
891 			&dpp_regs[inst], &tf_shift, &tf_mask))
892 		return &dpp->base;
893 
894 	BREAK_TO_DEBUGGER();
895 	kfree(dpp);
896 	return NULL;
897 }
898 
899 static struct output_pixel_processor *dcn30_opp_create(
900 	struct dc_context *ctx, uint32_t inst)
901 {
902 	struct dcn20_opp *opp =
903 		kzalloc(sizeof(struct dcn20_opp), GFP_KERNEL);
904 
905 	if (!opp) {
906 		BREAK_TO_DEBUGGER();
907 		return NULL;
908 	}
909 
910 	dcn20_opp_construct(opp, ctx, inst,
911 			&opp_regs[inst], &opp_shift, &opp_mask);
912 	return &opp->base;
913 }
914 
915 static struct dce_aux *dcn30_aux_engine_create(
916 	struct dc_context *ctx,
917 	uint32_t inst)
918 {
919 	struct aux_engine_dce110 *aux_engine =
920 		kzalloc(sizeof(struct aux_engine_dce110), GFP_KERNEL);
921 
922 	if (!aux_engine)
923 		return NULL;
924 
925 	dce110_aux_engine_construct(aux_engine, ctx, inst,
926 				    SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD,
927 				    &aux_engine_regs[inst],
928 					&aux_mask,
929 					&aux_shift,
930 					ctx->dc->caps.extended_aux_timeout_support);
931 
932 	return &aux_engine->base;
933 }
934 
935 #define i2c_inst_regs(id) { I2C_HW_ENGINE_COMMON_REG_LIST(id) }
936 
937 static const struct dce_i2c_registers i2c_hw_regs[] = {
938 		i2c_inst_regs(1),
939 		i2c_inst_regs(2),
940 		i2c_inst_regs(3),
941 		i2c_inst_regs(4),
942 		i2c_inst_regs(5),
943 		i2c_inst_regs(6),
944 };
945 
946 static const struct dce_i2c_shift i2c_shifts = {
947 		I2C_COMMON_MASK_SH_LIST_DCN2(__SHIFT)
948 };
949 
950 static const struct dce_i2c_mask i2c_masks = {
951 		I2C_COMMON_MASK_SH_LIST_DCN2(_MASK)
952 };
953 
954 static struct dce_i2c_hw *dcn30_i2c_hw_create(
955 	struct dc_context *ctx,
956 	uint32_t inst)
957 {
958 	struct dce_i2c_hw *dce_i2c_hw =
959 		kzalloc(sizeof(struct dce_i2c_hw), GFP_KERNEL);
960 
961 	if (!dce_i2c_hw)
962 		return NULL;
963 
964 	dcn2_i2c_hw_construct(dce_i2c_hw, ctx, inst,
965 				    &i2c_hw_regs[inst], &i2c_shifts, &i2c_masks);
966 
967 	return dce_i2c_hw;
968 }
969 
970 static struct mpc *dcn30_mpc_create(
971 		struct dc_context *ctx,
972 		int num_mpcc,
973 		int num_rmu)
974 {
975 	struct dcn30_mpc *mpc30 = kzalloc(sizeof(struct dcn30_mpc),
976 					  GFP_KERNEL);
977 
978 	if (!mpc30)
979 		return NULL;
980 
981 	dcn30_mpc_construct(mpc30, ctx,
982 			&mpc_regs,
983 			&mpc_shift,
984 			&mpc_mask,
985 			num_mpcc,
986 			num_rmu);
987 
988 	return &mpc30->base;
989 }
990 
991 struct hubbub *dcn30_hubbub_create(struct dc_context *ctx)
992 {
993 	int i;
994 
995 	struct dcn20_hubbub *hubbub3 = kzalloc(sizeof(struct dcn20_hubbub),
996 					  GFP_KERNEL);
997 
998 	if (!hubbub3)
999 		return NULL;
1000 
1001 	hubbub3_construct(hubbub3, ctx,
1002 			&hubbub_reg,
1003 			&hubbub_shift,
1004 			&hubbub_mask);
1005 
1006 
1007 	for (i = 0; i < res_cap_dcn3.num_vmid; i++) {
1008 		struct dcn20_vmid *vmid = &hubbub3->vmid[i];
1009 
1010 		vmid->ctx = ctx;
1011 
1012 		vmid->regs = &vmid_regs[i];
1013 		vmid->shifts = &vmid_shifts;
1014 		vmid->masks = &vmid_masks;
1015 	}
1016 
1017 	return &hubbub3->base;
1018 }
1019 
1020 static struct timing_generator *dcn30_timing_generator_create(
1021 		struct dc_context *ctx,
1022 		uint32_t instance)
1023 {
1024 	struct optc *tgn10 =
1025 		kzalloc(sizeof(struct optc), GFP_KERNEL);
1026 
1027 	if (!tgn10)
1028 		return NULL;
1029 
1030 	tgn10->base.inst = instance;
1031 	tgn10->base.ctx = ctx;
1032 
1033 	tgn10->tg_regs = &optc_regs[instance];
1034 	tgn10->tg_shift = &optc_shift;
1035 	tgn10->tg_mask = &optc_mask;
1036 
1037 	dcn30_timing_generator_init(tgn10);
1038 
1039 	return &tgn10->base;
1040 }
1041 
1042 static const struct encoder_feature_support link_enc_feature = {
1043 		.max_hdmi_deep_color = COLOR_DEPTH_121212,
1044 		.max_hdmi_pixel_clock = 600000,
1045 		.hdmi_ycbcr420_supported = true,
1046 		.dp_ycbcr420_supported = true,
1047 		.fec_supported = true,
1048 		.flags.bits.IS_HBR2_CAPABLE = true,
1049 		.flags.bits.IS_HBR3_CAPABLE = true,
1050 		.flags.bits.IS_TPS3_CAPABLE = true,
1051 		.flags.bits.IS_TPS4_CAPABLE = true
1052 };
1053 
1054 static struct link_encoder *dcn30_link_encoder_create(
1055 	const struct encoder_init_data *enc_init_data)
1056 {
1057 	struct dcn20_link_encoder *enc20 =
1058 		kzalloc(sizeof(struct dcn20_link_encoder), GFP_KERNEL);
1059 
1060 	if (!enc20)
1061 		return NULL;
1062 
1063 	dcn30_link_encoder_construct(enc20,
1064 			enc_init_data,
1065 			&link_enc_feature,
1066 			&link_enc_regs[enc_init_data->transmitter],
1067 			&link_enc_aux_regs[enc_init_data->channel - 1],
1068 			&link_enc_hpd_regs[enc_init_data->hpd_source],
1069 			&le_shift,
1070 			&le_mask);
1071 
1072 	return &enc20->enc10.base;
1073 }
1074 
1075 static struct panel_cntl *dcn30_panel_cntl_create(const struct panel_cntl_init_data *init_data)
1076 {
1077 	struct dce_panel_cntl *panel_cntl =
1078 		kzalloc(sizeof(struct dce_panel_cntl), GFP_KERNEL);
1079 
1080 	if (!panel_cntl)
1081 		return NULL;
1082 
1083 	dce_panel_cntl_construct(panel_cntl,
1084 			init_data,
1085 			&panel_cntl_regs[init_data->inst],
1086 			&panel_cntl_shift,
1087 			&panel_cntl_mask);
1088 
1089 	return &panel_cntl->base;
1090 }
1091 
1092 static void read_dce_straps(
1093 	struct dc_context *ctx,
1094 	struct resource_straps *straps)
1095 {
1096 	generic_reg_get(ctx, mmDC_PINSTRAPS + BASE(mmDC_PINSTRAPS_BASE_IDX),
1097 		FN(DC_PINSTRAPS, DC_PINSTRAPS_AUDIO), &straps->dc_pinstraps_audio);
1098 
1099 }
1100 
1101 static struct audio *dcn30_create_audio(
1102 		struct dc_context *ctx, unsigned int inst)
1103 {
1104 	return dce_audio_create(ctx, inst,
1105 			&audio_regs[inst], &audio_shift, &audio_mask);
1106 }
1107 
1108 static struct vpg *dcn30_vpg_create(
1109 	struct dc_context *ctx,
1110 	uint32_t inst)
1111 {
1112 	struct dcn30_vpg *vpg3 = kzalloc(sizeof(struct dcn30_vpg), GFP_KERNEL);
1113 
1114 	if (!vpg3)
1115 		return NULL;
1116 
1117 	vpg3_construct(vpg3, ctx, inst,
1118 			&vpg_regs[inst],
1119 			&vpg_shift,
1120 			&vpg_mask);
1121 
1122 	return &vpg3->base;
1123 }
1124 
1125 static struct afmt *dcn30_afmt_create(
1126 	struct dc_context *ctx,
1127 	uint32_t inst)
1128 {
1129 	struct dcn30_afmt *afmt3 = kzalloc(sizeof(struct dcn30_afmt), GFP_KERNEL);
1130 
1131 	if (!afmt3)
1132 		return NULL;
1133 
1134 	afmt3_construct(afmt3, ctx, inst,
1135 			&afmt_regs[inst],
1136 			&afmt_shift,
1137 			&afmt_mask);
1138 
1139 	return &afmt3->base;
1140 }
1141 
1142 struct stream_encoder *dcn30_stream_encoder_create(
1143 	enum engine_id eng_id,
1144 	struct dc_context *ctx)
1145 {
1146 	struct dcn10_stream_encoder *enc1;
1147 	struct vpg *vpg;
1148 	struct afmt *afmt;
1149 	int vpg_inst;
1150 	int afmt_inst;
1151 
1152 	/* Mapping of VPG, AFMT, DME register blocks to DIO block instance */
1153 	if (eng_id <= ENGINE_ID_DIGF) {
1154 		vpg_inst = eng_id;
1155 		afmt_inst = eng_id;
1156 	} else
1157 		return NULL;
1158 
1159 	enc1 = kzalloc(sizeof(struct dcn10_stream_encoder), GFP_KERNEL);
1160 	vpg = dcn30_vpg_create(ctx, vpg_inst);
1161 	afmt = dcn30_afmt_create(ctx, afmt_inst);
1162 
1163 	if (!enc1 || !vpg || !afmt)
1164 		return NULL;
1165 
1166 	dcn30_dio_stream_encoder_construct(enc1, ctx, ctx->dc_bios,
1167 					eng_id, vpg, afmt,
1168 					&stream_enc_regs[eng_id],
1169 					&se_shift, &se_mask);
1170 
1171 	return &enc1->base;
1172 }
1173 
1174 struct dce_hwseq *dcn30_hwseq_create(
1175 	struct dc_context *ctx)
1176 {
1177 	struct dce_hwseq *hws = kzalloc(sizeof(struct dce_hwseq), GFP_KERNEL);
1178 
1179 	if (hws) {
1180 		hws->ctx = ctx;
1181 		hws->regs = &hwseq_reg;
1182 		hws->shifts = &hwseq_shift;
1183 		hws->masks = &hwseq_mask;
1184 	}
1185 	return hws;
1186 }
1187 static const struct resource_create_funcs res_create_funcs = {
1188 	.read_dce_straps = read_dce_straps,
1189 	.create_audio = dcn30_create_audio,
1190 	.create_stream_encoder = dcn30_stream_encoder_create,
1191 	.create_hwseq = dcn30_hwseq_create,
1192 };
1193 
1194 static const struct resource_create_funcs res_create_maximus_funcs = {
1195 	.read_dce_straps = NULL,
1196 	.create_audio = NULL,
1197 	.create_stream_encoder = NULL,
1198 	.create_hwseq = dcn30_hwseq_create,
1199 };
1200 
1201 static void dcn30_resource_destruct(struct dcn30_resource_pool *pool)
1202 {
1203 	unsigned int i;
1204 
1205 	for (i = 0; i < pool->base.stream_enc_count; i++) {
1206 		if (pool->base.stream_enc[i] != NULL) {
1207 			if (pool->base.stream_enc[i]->vpg != NULL) {
1208 				kfree(DCN30_VPG_FROM_VPG(pool->base.stream_enc[i]->vpg));
1209 				pool->base.stream_enc[i]->vpg = NULL;
1210 			}
1211 			if (pool->base.stream_enc[i]->afmt != NULL) {
1212 				kfree(DCN30_AFMT_FROM_AFMT(pool->base.stream_enc[i]->afmt));
1213 				pool->base.stream_enc[i]->afmt = NULL;
1214 			}
1215 			kfree(DCN10STRENC_FROM_STRENC(pool->base.stream_enc[i]));
1216 			pool->base.stream_enc[i] = NULL;
1217 		}
1218 	}
1219 
1220 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
1221 		if (pool->base.dscs[i] != NULL)
1222 			dcn20_dsc_destroy(&pool->base.dscs[i]);
1223 	}
1224 
1225 	if (pool->base.mpc != NULL) {
1226 		kfree(TO_DCN20_MPC(pool->base.mpc));
1227 		pool->base.mpc = NULL;
1228 	}
1229 	if (pool->base.hubbub != NULL) {
1230 		kfree(pool->base.hubbub);
1231 		pool->base.hubbub = NULL;
1232 	}
1233 	for (i = 0; i < pool->base.pipe_count; i++) {
1234 		if (pool->base.dpps[i] != NULL)
1235 			dcn30_dpp_destroy(&pool->base.dpps[i]);
1236 
1237 		if (pool->base.ipps[i] != NULL)
1238 			pool->base.ipps[i]->funcs->ipp_destroy(&pool->base.ipps[i]);
1239 
1240 		if (pool->base.hubps[i] != NULL) {
1241 			kfree(TO_DCN20_HUBP(pool->base.hubps[i]));
1242 			pool->base.hubps[i] = NULL;
1243 		}
1244 
1245 		if (pool->base.irqs != NULL) {
1246 			dal_irq_service_destroy(&pool->base.irqs);
1247 		}
1248 	}
1249 
1250 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1251 		if (pool->base.engines[i] != NULL)
1252 			dce110_engine_destroy(&pool->base.engines[i]);
1253 		if (pool->base.hw_i2cs[i] != NULL) {
1254 			kfree(pool->base.hw_i2cs[i]);
1255 			pool->base.hw_i2cs[i] = NULL;
1256 		}
1257 		if (pool->base.sw_i2cs[i] != NULL) {
1258 			kfree(pool->base.sw_i2cs[i]);
1259 			pool->base.sw_i2cs[i] = NULL;
1260 		}
1261 	}
1262 
1263 	for (i = 0; i < pool->base.res_cap->num_opp; i++) {
1264 		if (pool->base.opps[i] != NULL)
1265 			pool->base.opps[i]->funcs->opp_destroy(&pool->base.opps[i]);
1266 	}
1267 
1268 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
1269 		if (pool->base.timing_generators[i] != NULL)	{
1270 			kfree(DCN10TG_FROM_TG(pool->base.timing_generators[i]));
1271 			pool->base.timing_generators[i] = NULL;
1272 		}
1273 	}
1274 
1275 	for (i = 0; i < pool->base.res_cap->num_dwb; i++) {
1276 		if (pool->base.dwbc[i] != NULL) {
1277 			kfree(TO_DCN30_DWBC(pool->base.dwbc[i]));
1278 			pool->base.dwbc[i] = NULL;
1279 		}
1280 		if (pool->base.mcif_wb[i] != NULL) {
1281 			kfree(TO_DCN30_MMHUBBUB(pool->base.mcif_wb[i]));
1282 			pool->base.mcif_wb[i] = NULL;
1283 		}
1284 	}
1285 
1286 	for (i = 0; i < pool->base.audio_count; i++) {
1287 		if (pool->base.audios[i])
1288 			dce_aud_destroy(&pool->base.audios[i]);
1289 	}
1290 
1291 	for (i = 0; i < pool->base.clk_src_count; i++) {
1292 		if (pool->base.clock_sources[i] != NULL) {
1293 			dcn20_clock_source_destroy(&pool->base.clock_sources[i]);
1294 			pool->base.clock_sources[i] = NULL;
1295 		}
1296 	}
1297 
1298 	for (i = 0; i < pool->base.res_cap->num_mpc_3dlut; i++) {
1299 		if (pool->base.mpc_lut[i] != NULL) {
1300 			dc_3dlut_func_release(pool->base.mpc_lut[i]);
1301 			pool->base.mpc_lut[i] = NULL;
1302 		}
1303 		if (pool->base.mpc_shaper[i] != NULL) {
1304 			dc_transfer_func_release(pool->base.mpc_shaper[i]);
1305 			pool->base.mpc_shaper[i] = NULL;
1306 		}
1307 	}
1308 
1309 	if (pool->base.dp_clock_source != NULL) {
1310 		dcn20_clock_source_destroy(&pool->base.dp_clock_source);
1311 		pool->base.dp_clock_source = NULL;
1312 	}
1313 
1314 	for (i = 0; i < pool->base.pipe_count; i++) {
1315 		if (pool->base.multiple_abms[i] != NULL)
1316 			dce_abm_destroy(&pool->base.multiple_abms[i]);
1317 	}
1318 
1319 	if (pool->base.psr != NULL)
1320 		dmub_psr_destroy(&pool->base.psr);
1321 
1322 	if (pool->base.dccg != NULL)
1323 		dcn_dccg_destroy(&pool->base.dccg);
1324 }
1325 
1326 static struct hubp *dcn30_hubp_create(
1327 	struct dc_context *ctx,
1328 	uint32_t inst)
1329 {
1330 	struct dcn20_hubp *hubp2 =
1331 		kzalloc(sizeof(struct dcn20_hubp), GFP_KERNEL);
1332 
1333 	if (!hubp2)
1334 		return NULL;
1335 
1336 	if (hubp3_construct(hubp2, ctx, inst,
1337 			&hubp_regs[inst], &hubp_shift, &hubp_mask))
1338 		return &hubp2->base;
1339 
1340 	BREAK_TO_DEBUGGER();
1341 	kfree(hubp2);
1342 	return NULL;
1343 }
1344 
1345 static bool dcn30_dwbc_create(struct dc_context *ctx, struct resource_pool *pool)
1346 {
1347 	int i;
1348 	uint32_t pipe_count = pool->res_cap->num_dwb;
1349 
1350 	for (i = 0; i < pipe_count; i++) {
1351 		struct dcn30_dwbc *dwbc30 = kzalloc(sizeof(struct dcn30_dwbc),
1352 						    GFP_KERNEL);
1353 
1354 		if (!dwbc30) {
1355 			dm_error("DC: failed to create dwbc30!\n");
1356 			return false;
1357 		}
1358 
1359 		dcn30_dwbc_construct(dwbc30, ctx,
1360 				&dwbc30_regs[i],
1361 				&dwbc30_shift,
1362 				&dwbc30_mask,
1363 				i);
1364 
1365 		pool->dwbc[i] = &dwbc30->base;
1366 	}
1367 	return true;
1368 }
1369 
1370 static bool dcn30_mmhubbub_create(struct dc_context *ctx, struct resource_pool *pool)
1371 {
1372 	int i;
1373 	uint32_t pipe_count = pool->res_cap->num_dwb;
1374 
1375 	for (i = 0; i < pipe_count; i++) {
1376 		struct dcn30_mmhubbub *mcif_wb30 = kzalloc(sizeof(struct dcn30_mmhubbub),
1377 						    GFP_KERNEL);
1378 
1379 		if (!mcif_wb30) {
1380 			dm_error("DC: failed to create mcif_wb30!\n");
1381 			return false;
1382 		}
1383 
1384 		dcn30_mmhubbub_construct(mcif_wb30, ctx,
1385 				&mcif_wb30_regs[i],
1386 				&mcif_wb30_shift,
1387 				&mcif_wb30_mask,
1388 				i);
1389 
1390 		pool->mcif_wb[i] = &mcif_wb30->base;
1391 	}
1392 	return true;
1393 }
1394 
1395 static struct display_stream_compressor *dcn30_dsc_create(
1396 	struct dc_context *ctx, uint32_t inst)
1397 {
1398 	struct dcn20_dsc *dsc =
1399 		kzalloc(sizeof(struct dcn20_dsc), GFP_KERNEL);
1400 
1401 	if (!dsc) {
1402 		BREAK_TO_DEBUGGER();
1403 		return NULL;
1404 	}
1405 
1406 	dsc2_construct(dsc, ctx, inst, &dsc_regs[inst], &dsc_shift, &dsc_mask);
1407 	return &dsc->base;
1408 }
1409 
1410 enum dc_status dcn30_add_stream_to_ctx(struct dc *dc, struct dc_state *new_ctx, struct dc_stream_state *dc_stream)
1411 {
1412 
1413 	return dcn20_add_stream_to_ctx(dc, new_ctx, dc_stream);
1414 }
1415 
1416 static void dcn30_destroy_resource_pool(struct resource_pool **pool)
1417 {
1418 	struct dcn30_resource_pool *dcn30_pool = TO_DCN30_RES_POOL(*pool);
1419 
1420 	dcn30_resource_destruct(dcn30_pool);
1421 	kfree(dcn30_pool);
1422 	*pool = NULL;
1423 }
1424 
1425 static struct clock_source *dcn30_clock_source_create(
1426 		struct dc_context *ctx,
1427 		struct dc_bios *bios,
1428 		enum clock_source_id id,
1429 		const struct dce110_clk_src_regs *regs,
1430 		bool dp_clk_src)
1431 {
1432 	struct dce110_clk_src *clk_src =
1433 		kzalloc(sizeof(struct dce110_clk_src), GFP_KERNEL);
1434 
1435 	if (!clk_src)
1436 		return NULL;
1437 
1438 	if (dcn3_clk_src_construct(clk_src, ctx, bios, id,
1439 			regs, &cs_shift, &cs_mask)) {
1440 		clk_src->base.dp_clk_src = dp_clk_src;
1441 		return &clk_src->base;
1442 	}
1443 
1444 	BREAK_TO_DEBUGGER();
1445 	return NULL;
1446 }
1447 
1448 int dcn30_populate_dml_pipes_from_context(
1449 	struct dc *dc, struct dc_state *context,
1450 	display_e2e_pipe_params_st *pipes)
1451 {
1452 	int i, pipe_cnt;
1453 	struct resource_context *res_ctx = &context->res_ctx;
1454 
1455 	dcn20_populate_dml_pipes_from_context(dc, context, pipes);
1456 
1457 	for (i = 0, pipe_cnt = 0; i < dc->res_pool->pipe_count; i++) {
1458 		if (!res_ctx->pipe_ctx[i].stream)
1459 			continue;
1460 
1461 		pipes[pipe_cnt++].pipe.scale_ratio_depth.lb_depth =
1462 			dm_lb_16;
1463 	}
1464 
1465 	return pipe_cnt;
1466 }
1467 
1468 void dcn30_populate_dml_writeback_from_context(
1469 		struct dc *dc, struct resource_context *res_ctx, display_e2e_pipe_params_st *pipes)
1470 {
1471 	int pipe_cnt, i, j;
1472 	double max_calc_writeback_dispclk;
1473 	double writeback_dispclk;
1474 	struct writeback_st dout_wb;
1475 
1476 	for (i = 0, pipe_cnt = 0; i < dc->res_pool->pipe_count; i++) {
1477 		struct dc_stream_state *stream = res_ctx->pipe_ctx[i].stream;
1478 
1479 		if (!stream)
1480 			continue;
1481 		max_calc_writeback_dispclk = 0;
1482 
1483 		/* Set writeback information */
1484 		pipes[pipe_cnt].dout.wb_enable = 0;
1485 		pipes[pipe_cnt].dout.num_active_wb = 0;
1486 		for (j = 0; j < stream->num_wb_info; j++) {
1487 			struct dc_writeback_info *wb_info = &stream->writeback_info[j];
1488 
1489 			if (wb_info->wb_enabled && wb_info->writeback_source_plane &&
1490 					(wb_info->writeback_source_plane == res_ctx->pipe_ctx[i].plane_state)) {
1491 				pipes[pipe_cnt].dout.wb_enable = 1;
1492 				pipes[pipe_cnt].dout.num_active_wb++;
1493 				dout_wb.wb_src_height = wb_info->dwb_params.cnv_params.crop_en ?
1494 					wb_info->dwb_params.cnv_params.crop_height :
1495 					wb_info->dwb_params.cnv_params.src_height;
1496 				dout_wb.wb_src_width = wb_info->dwb_params.cnv_params.crop_en ?
1497 					wb_info->dwb_params.cnv_params.crop_width :
1498 					wb_info->dwb_params.cnv_params.src_width;
1499 				dout_wb.wb_dst_width = wb_info->dwb_params.dest_width;
1500 				dout_wb.wb_dst_height = wb_info->dwb_params.dest_height;
1501 
1502 				/* For IP that doesn't support WB scaling, set h/v taps to 1 to avoid DML validation failure */
1503 				if (dc->dml.ip.writeback_max_hscl_taps > 1) {
1504 					dout_wb.wb_htaps_luma = wb_info->dwb_params.scaler_taps.h_taps;
1505 					dout_wb.wb_vtaps_luma = wb_info->dwb_params.scaler_taps.v_taps;
1506 				} else {
1507 					dout_wb.wb_htaps_luma = 1;
1508 					dout_wb.wb_vtaps_luma = 1;
1509 				}
1510 				dout_wb.wb_htaps_chroma = 0;
1511 				dout_wb.wb_vtaps_chroma = 0;
1512 				dout_wb.wb_hratio = wb_info->dwb_params.cnv_params.crop_en ?
1513 					(double)wb_info->dwb_params.cnv_params.crop_width /
1514 						(double)wb_info->dwb_params.dest_width :
1515 					(double)wb_info->dwb_params.cnv_params.src_width /
1516 						(double)wb_info->dwb_params.dest_width;
1517 				dout_wb.wb_vratio = wb_info->dwb_params.cnv_params.crop_en ?
1518 					(double)wb_info->dwb_params.cnv_params.crop_height /
1519 						(double)wb_info->dwb_params.dest_height :
1520 					(double)wb_info->dwb_params.cnv_params.src_height /
1521 						(double)wb_info->dwb_params.dest_height;
1522 				if (wb_info->dwb_params.cnv_params.fc_out_format == DWB_OUT_FORMAT_64BPP_ARGB ||
1523 					wb_info->dwb_params.cnv_params.fc_out_format == DWB_OUT_FORMAT_64BPP_RGBA)
1524 					dout_wb.wb_pixel_format = dm_444_64;
1525 				else
1526 					dout_wb.wb_pixel_format = dm_444_32;
1527 
1528 				/* Workaround for cases where multiple writebacks are connected to same plane
1529 				 * In which case, need to compute worst case and set the associated writeback parameters
1530 				 * This workaround is necessary due to DML computation assuming only 1 set of writeback
1531 				 * parameters per pipe
1532 				 */
1533 				writeback_dispclk = dml30_CalculateWriteBackDISPCLK(
1534 						dout_wb.wb_pixel_format,
1535 						pipes[pipe_cnt].pipe.dest.pixel_rate_mhz,
1536 						dout_wb.wb_hratio,
1537 						dout_wb.wb_vratio,
1538 						dout_wb.wb_htaps_luma,
1539 						dout_wb.wb_vtaps_luma,
1540 						dout_wb.wb_src_width,
1541 						dout_wb.wb_dst_width,
1542 						pipes[pipe_cnt].pipe.dest.htotal,
1543 						dc->current_state->bw_ctx.dml.ip.writeback_line_buffer_buffer_size);
1544 
1545 				if (writeback_dispclk > max_calc_writeback_dispclk) {
1546 					max_calc_writeback_dispclk = writeback_dispclk;
1547 					pipes[pipe_cnt].dout.wb = dout_wb;
1548 				}
1549 			}
1550 		}
1551 
1552 		pipe_cnt++;
1553 	}
1554 
1555 }
1556 
1557 unsigned int dcn30_calc_max_scaled_time(
1558 		unsigned int time_per_pixel,
1559 		enum mmhubbub_wbif_mode mode,
1560 		unsigned int urgent_watermark)
1561 {
1562 	unsigned int time_per_byte = 0;
1563 	unsigned int total_free_entry = 0xb40;
1564 	unsigned int buf_lh_capability;
1565 	unsigned int max_scaled_time;
1566 
1567 	if (mode == PACKED_444) /* packed mode 32 bpp */
1568 		time_per_byte = time_per_pixel/4;
1569 	else if (mode == PACKED_444_FP16) /* packed mode 64 bpp */
1570 		time_per_byte = time_per_pixel/8;
1571 
1572 	if (time_per_byte == 0)
1573 		time_per_byte = 1;
1574 
1575 	buf_lh_capability = (total_free_entry*time_per_byte*32) >> 6; /* time_per_byte is in u6.6*/
1576 	max_scaled_time   = buf_lh_capability - urgent_watermark;
1577 	return max_scaled_time;
1578 }
1579 
1580 void dcn30_set_mcif_arb_params(
1581 		struct dc *dc,
1582 		struct dc_state *context,
1583 		display_e2e_pipe_params_st *pipes,
1584 		int pipe_cnt)
1585 {
1586 	enum mmhubbub_wbif_mode wbif_mode;
1587 	struct display_mode_lib *dml = &context->bw_ctx.dml;
1588 	struct mcif_arb_params *wb_arb_params;
1589 	int i, j, k, dwb_pipe;
1590 
1591 	/* Writeback MCIF_WB arbitration parameters */
1592 	dwb_pipe = 0;
1593 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1594 
1595 		if (!context->res_ctx.pipe_ctx[i].stream)
1596 			continue;
1597 
1598 		for (j = 0; j < MAX_DWB_PIPES; j++) {
1599 			struct dc_writeback_info *writeback_info = &context->res_ctx.pipe_ctx[i].stream->writeback_info[j];
1600 
1601 			if (writeback_info->wb_enabled == false)
1602 				continue;
1603 
1604 			//wb_arb_params = &context->res_ctx.pipe_ctx[i].stream->writeback_info[j].mcif_arb_params;
1605 			wb_arb_params = &context->bw_ctx.bw.dcn.bw_writeback.mcif_wb_arb[dwb_pipe];
1606 
1607 			if (writeback_info->dwb_params.cnv_params.fc_out_format == DWB_OUT_FORMAT_64BPP_ARGB ||
1608 				writeback_info->dwb_params.cnv_params.fc_out_format == DWB_OUT_FORMAT_64BPP_RGBA)
1609 				wbif_mode = PACKED_444_FP16;
1610 			else
1611 				wbif_mode = PACKED_444;
1612 
1613 			for (k = 0; k < sizeof(wb_arb_params->cli_watermark)/sizeof(wb_arb_params->cli_watermark[0]); k++) {
1614 				wb_arb_params->cli_watermark[k] = get_wm_writeback_urgent(dml, pipes, pipe_cnt) * 1000;
1615 				wb_arb_params->pstate_watermark[k] = get_wm_writeback_dram_clock_change(dml, pipes, pipe_cnt) * 1000;
1616 			}
1617 			wb_arb_params->time_per_pixel = (1000000 << 6) / context->res_ctx.pipe_ctx[i].stream->phy_pix_clk; /* time_per_pixel should be in u6.6 format */
1618 			wb_arb_params->slice_lines = 32;
1619 			wb_arb_params->arbitration_slice = 2; /* irrelevant since there is no YUV output */
1620 			wb_arb_params->max_scaled_time = dcn30_calc_max_scaled_time(wb_arb_params->time_per_pixel,
1621 					wbif_mode,
1622 					wb_arb_params->cli_watermark[0]); /* assume 4 watermark sets have the same value */
1623 			wb_arb_params->dram_speed_change_duration = dml->vba.WritebackAllowDRAMClockChangeEndPosition[j] * pipes[0].clks_cfg.refclk_mhz; /* num_clock_cycles = us * MHz */
1624 
1625 			dwb_pipe++;
1626 
1627 			if (dwb_pipe >= MAX_DWB_PIPES)
1628 				return;
1629 		}
1630 		if (dwb_pipe >= MAX_DWB_PIPES)
1631 			return;
1632 	}
1633 
1634 }
1635 
1636 static struct dc_cap_funcs cap_funcs = {
1637 	.get_dcc_compression_cap = dcn20_get_dcc_compression_cap
1638 };
1639 
1640 bool dcn30_acquire_post_bldn_3dlut(
1641 		struct resource_context *res_ctx,
1642 		const struct resource_pool *pool,
1643 		int mpcc_id,
1644 		struct dc_3dlut **lut,
1645 		struct dc_transfer_func **shaper)
1646 {
1647 	int i;
1648 	bool ret = false;
1649 	union dc_3dlut_state *state;
1650 
1651 	ASSERT(*lut == NULL && *shaper == NULL);
1652 	*lut = NULL;
1653 	*shaper = NULL;
1654 
1655 	for (i = 0; i < pool->res_cap->num_mpc_3dlut; i++) {
1656 		if (!res_ctx->is_mpc_3dlut_acquired[i]) {
1657 			*lut = pool->mpc_lut[i];
1658 			*shaper = pool->mpc_shaper[i];
1659 			state = &pool->mpc_lut[i]->state;
1660 			res_ctx->is_mpc_3dlut_acquired[i] = true;
1661 			state->bits.rmu_idx_valid = 1;
1662 			state->bits.rmu_mux_num = i;
1663 			if (state->bits.rmu_mux_num == 0)
1664 				state->bits.mpc_rmu0_mux = mpcc_id;
1665 			else if (state->bits.rmu_mux_num == 1)
1666 				state->bits.mpc_rmu1_mux = mpcc_id;
1667 			else if (state->bits.rmu_mux_num == 2)
1668 				state->bits.mpc_rmu2_mux = mpcc_id;
1669 			ret = true;
1670 			break;
1671 			}
1672 		}
1673 	return ret;
1674 }
1675 
1676 bool dcn30_release_post_bldn_3dlut(
1677 		struct resource_context *res_ctx,
1678 		const struct resource_pool *pool,
1679 		struct dc_3dlut **lut,
1680 		struct dc_transfer_func **shaper)
1681 {
1682 	int i;
1683 	bool ret = false;
1684 
1685 	for (i = 0; i < pool->res_cap->num_mpc_3dlut; i++) {
1686 		if (pool->mpc_lut[i] == *lut && pool->mpc_shaper[i] == *shaper) {
1687 			res_ctx->is_mpc_3dlut_acquired[i] = false;
1688 			pool->mpc_lut[i]->state.raw = 0;
1689 			*lut = NULL;
1690 			*shaper = NULL;
1691 			ret = true;
1692 			break;
1693 		}
1694 	}
1695 	return ret;
1696 }
1697 
1698 #define fixed16_to_double(x) (((double) x) / ((double) (1 << 16)))
1699 #define fixed16_to_double_to_cpu(x) fixed16_to_double(le32_to_cpu(x))
1700 
1701 static bool is_soc_bounding_box_valid(struct dc *dc)
1702 {
1703 	uint32_t hw_internal_rev = dc->ctx->asic_id.hw_internal_rev;
1704 
1705 	if (ASICREV_IS_SIENNA_CICHLID_P(hw_internal_rev))
1706 		return true;
1707 
1708 	return false;
1709 }
1710 
1711 static bool init_soc_bounding_box(struct dc *dc,
1712 				  struct dcn30_resource_pool *pool)
1713 {
1714 	const struct gpu_info_soc_bounding_box_v1_0 *bb = dc->soc_bounding_box;
1715 	struct _vcs_dpi_soc_bounding_box_st *loaded_bb = &dcn3_0_soc;
1716 	struct _vcs_dpi_ip_params_st *loaded_ip = &dcn3_0_ip;
1717 
1718 	DC_LOGGER_INIT(dc->ctx->logger);
1719 
1720 	if (!bb && !is_soc_bounding_box_valid(dc)) {
1721 		DC_LOG_ERROR("%s: not valid soc bounding box/n", __func__);
1722 		return false;
1723 	}
1724 
1725 	if (bb && !is_soc_bounding_box_valid(dc)) {
1726 		int i;
1727 
1728 		dcn3_0_soc.sr_exit_time_us =
1729 				fixed16_to_double_to_cpu(bb->sr_exit_time_us);
1730 		dcn3_0_soc.sr_enter_plus_exit_time_us =
1731 				fixed16_to_double_to_cpu(bb->sr_enter_plus_exit_time_us);
1732 		dcn3_0_soc.urgent_latency_us =
1733 				fixed16_to_double_to_cpu(bb->urgent_latency_us);
1734 		dcn3_0_soc.urgent_latency_pixel_data_only_us =
1735 				fixed16_to_double_to_cpu(bb->urgent_latency_pixel_data_only_us);
1736 		dcn3_0_soc.urgent_latency_pixel_mixed_with_vm_data_us =
1737 				fixed16_to_double_to_cpu(bb->urgent_latency_pixel_mixed_with_vm_data_us);
1738 		dcn3_0_soc.urgent_latency_vm_data_only_us =
1739 				fixed16_to_double_to_cpu(bb->urgent_latency_vm_data_only_us);
1740 		dcn3_0_soc.urgent_out_of_order_return_per_channel_pixel_only_bytes =
1741 				le32_to_cpu(bb->urgent_out_of_order_return_per_channel_pixel_only_bytes);
1742 		dcn3_0_soc.urgent_out_of_order_return_per_channel_pixel_and_vm_bytes =
1743 				le32_to_cpu(bb->urgent_out_of_order_return_per_channel_pixel_and_vm_bytes);
1744 		dcn3_0_soc.urgent_out_of_order_return_per_channel_vm_only_bytes =
1745 				le32_to_cpu(bb->urgent_out_of_order_return_per_channel_vm_only_bytes);
1746 		dcn3_0_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_only =
1747 				fixed16_to_double_to_cpu(bb->pct_ideal_dram_sdp_bw_after_urgent_pixel_only);
1748 		dcn3_0_soc.pct_ideal_dram_sdp_bw_after_urgent_pixel_and_vm =
1749 				fixed16_to_double_to_cpu(bb->pct_ideal_dram_sdp_bw_after_urgent_pixel_and_vm);
1750 		dcn3_0_soc.pct_ideal_dram_sdp_bw_after_urgent_vm_only =
1751 				fixed16_to_double_to_cpu(bb->pct_ideal_dram_sdp_bw_after_urgent_vm_only);
1752 		dcn3_0_soc.max_avg_sdp_bw_use_normal_percent =
1753 				fixed16_to_double_to_cpu(bb->max_avg_sdp_bw_use_normal_percent);
1754 		dcn3_0_soc.max_avg_dram_bw_use_normal_percent =
1755 				fixed16_to_double_to_cpu(bb->max_avg_dram_bw_use_normal_percent);
1756 		dcn3_0_soc.writeback_latency_us =
1757 				fixed16_to_double_to_cpu(bb->writeback_latency_us);
1758 		dcn3_0_soc.ideal_dram_bw_after_urgent_percent =
1759 				fixed16_to_double_to_cpu(bb->ideal_dram_bw_after_urgent_percent);
1760 		dcn3_0_soc.max_request_size_bytes =
1761 				le32_to_cpu(bb->max_request_size_bytes);
1762 		dcn3_0_soc.dram_channel_width_bytes =
1763 				le32_to_cpu(bb->dram_channel_width_bytes);
1764 		dcn3_0_soc.fabric_datapath_to_dcn_data_return_bytes =
1765 				le32_to_cpu(bb->fabric_datapath_to_dcn_data_return_bytes);
1766 		dcn3_0_soc.dcn_downspread_percent =
1767 				fixed16_to_double_to_cpu(bb->dcn_downspread_percent);
1768 		dcn3_0_soc.downspread_percent =
1769 				fixed16_to_double_to_cpu(bb->downspread_percent);
1770 		dcn3_0_soc.dram_page_open_time_ns =
1771 				fixed16_to_double_to_cpu(bb->dram_page_open_time_ns);
1772 		dcn3_0_soc.dram_rw_turnaround_time_ns =
1773 				fixed16_to_double_to_cpu(bb->dram_rw_turnaround_time_ns);
1774 		dcn3_0_soc.dram_return_buffer_per_channel_bytes =
1775 				le32_to_cpu(bb->dram_return_buffer_per_channel_bytes);
1776 		dcn3_0_soc.round_trip_ping_latency_dcfclk_cycles =
1777 				le32_to_cpu(bb->round_trip_ping_latency_dcfclk_cycles);
1778 		dcn3_0_soc.urgent_out_of_order_return_per_channel_bytes =
1779 				le32_to_cpu(bb->urgent_out_of_order_return_per_channel_bytes);
1780 		dcn3_0_soc.channel_interleave_bytes =
1781 				le32_to_cpu(bb->channel_interleave_bytes);
1782 		dcn3_0_soc.num_banks =
1783 				le32_to_cpu(bb->num_banks);
1784 		dcn3_0_soc.num_chans =
1785 				le32_to_cpu(bb->num_chans);
1786 		dcn3_0_soc.gpuvm_min_page_size_bytes =
1787 				le32_to_cpu(bb->vmm_page_size_bytes);
1788 		dcn3_0_soc.dram_clock_change_latency_us =
1789 				fixed16_to_double_to_cpu(bb->dram_clock_change_latency_us);
1790 		dcn3_0_soc.writeback_dram_clock_change_latency_us =
1791 				fixed16_to_double_to_cpu(bb->writeback_dram_clock_change_latency_us);
1792 		dcn3_0_soc.return_bus_width_bytes =
1793 				le32_to_cpu(bb->return_bus_width_bytes);
1794 		dcn3_0_soc.dispclk_dppclk_vco_speed_mhz =
1795 				le32_to_cpu(bb->dispclk_dppclk_vco_speed_mhz);
1796 		dcn3_0_soc.xfc_bus_transport_time_us =
1797 				le32_to_cpu(bb->xfc_bus_transport_time_us);
1798 		dcn3_0_soc.xfc_xbuf_latency_tolerance_us =
1799 				le32_to_cpu(bb->xfc_xbuf_latency_tolerance_us);
1800 		dcn3_0_soc.use_urgent_burst_bw =
1801 				le32_to_cpu(bb->use_urgent_burst_bw);
1802 		dcn3_0_soc.num_states =
1803 				le32_to_cpu(bb->num_states);
1804 
1805 		for (i = 0; i < dcn3_0_soc.num_states; i++) {
1806 			dcn3_0_soc.clock_limits[i].state =
1807 					le32_to_cpu(bb->clock_limits[i].state);
1808 			dcn3_0_soc.clock_limits[i].dcfclk_mhz =
1809 					fixed16_to_double_to_cpu(bb->clock_limits[i].dcfclk_mhz);
1810 			dcn3_0_soc.clock_limits[i].fabricclk_mhz =
1811 					fixed16_to_double_to_cpu(bb->clock_limits[i].fabricclk_mhz);
1812 			dcn3_0_soc.clock_limits[i].dispclk_mhz =
1813 					fixed16_to_double_to_cpu(bb->clock_limits[i].dispclk_mhz);
1814 			dcn3_0_soc.clock_limits[i].dppclk_mhz =
1815 					fixed16_to_double_to_cpu(bb->clock_limits[i].dppclk_mhz);
1816 			dcn3_0_soc.clock_limits[i].phyclk_mhz =
1817 					fixed16_to_double_to_cpu(bb->clock_limits[i].phyclk_mhz);
1818 			dcn3_0_soc.clock_limits[i].socclk_mhz =
1819 					fixed16_to_double_to_cpu(bb->clock_limits[i].socclk_mhz);
1820 			dcn3_0_soc.clock_limits[i].dscclk_mhz =
1821 					fixed16_to_double_to_cpu(bb->clock_limits[i].dscclk_mhz);
1822 			dcn3_0_soc.clock_limits[i].dram_speed_mts =
1823 					fixed16_to_double_to_cpu(bb->clock_limits[i].dram_speed_mts);
1824 		}
1825 	}
1826 
1827 	loaded_ip->max_num_otg = pool->base.res_cap->num_timing_generator;
1828 	loaded_ip->max_num_dpp = pool->base.pipe_count;
1829 	loaded_ip->clamp_min_dcfclk = dc->config.clamp_min_dcfclk;
1830 	dcn20_patch_bounding_box(dc, loaded_bb);
1831 
1832 	if (!bb && dc->ctx->dc_bios->funcs->get_soc_bb_info) {
1833 		struct bp_soc_bb_info bb_info = {0};
1834 
1835 		if (dc->ctx->dc_bios->funcs->get_soc_bb_info(dc->ctx->dc_bios, &bb_info) == BP_RESULT_OK) {
1836 			if (bb_info.dram_clock_change_latency_100ns > 0)
1837 				dcn3_0_soc.dram_clock_change_latency_us = bb_info.dram_clock_change_latency_100ns * 10;
1838 
1839 			if (bb_info.dram_sr_enter_exit_latency_100ns > 0)
1840 				dcn3_0_soc.sr_enter_plus_exit_time_us = bb_info.dram_sr_enter_exit_latency_100ns * 10;
1841 
1842 			if (bb_info.dram_sr_exit_latency_100ns > 0)
1843 				dcn3_0_soc.sr_exit_time_us = bb_info.dram_sr_exit_latency_100ns * 10;
1844 		}
1845 	}
1846 
1847 	return true;
1848 }
1849 
1850 static bool dcn30_split_stream_for_mpc_or_odm(
1851 		const struct dc *dc,
1852 		struct resource_context *res_ctx,
1853 		struct pipe_ctx *pri_pipe,
1854 		struct pipe_ctx *sec_pipe,
1855 		bool odm)
1856 {
1857 	int pipe_idx = sec_pipe->pipe_idx;
1858 	const struct resource_pool *pool = dc->res_pool;
1859 
1860 	*sec_pipe = *pri_pipe;
1861 
1862 	sec_pipe->pipe_idx = pipe_idx;
1863 	sec_pipe->plane_res.mi = pool->mis[pipe_idx];
1864 	sec_pipe->plane_res.hubp = pool->hubps[pipe_idx];
1865 	sec_pipe->plane_res.ipp = pool->ipps[pipe_idx];
1866 	sec_pipe->plane_res.xfm = pool->transforms[pipe_idx];
1867 	sec_pipe->plane_res.dpp = pool->dpps[pipe_idx];
1868 	sec_pipe->plane_res.mpcc_inst = pool->dpps[pipe_idx]->inst;
1869 	sec_pipe->stream_res.dsc = NULL;
1870 	if (odm) {
1871 		if (pri_pipe->next_odm_pipe) {
1872 			ASSERT(pri_pipe->next_odm_pipe != sec_pipe);
1873 			sec_pipe->next_odm_pipe = pri_pipe->next_odm_pipe;
1874 			sec_pipe->next_odm_pipe->prev_odm_pipe = sec_pipe;
1875 		}
1876 		pri_pipe->next_odm_pipe = sec_pipe;
1877 		sec_pipe->prev_odm_pipe = pri_pipe;
1878 		ASSERT(sec_pipe->top_pipe == NULL);
1879 
1880 		sec_pipe->stream_res.opp = pool->opps[pipe_idx];
1881 		if (sec_pipe->stream->timing.flags.DSC == 1) {
1882 			dcn20_acquire_dsc(dc, res_ctx, &sec_pipe->stream_res.dsc, pipe_idx);
1883 			ASSERT(sec_pipe->stream_res.dsc);
1884 			if (sec_pipe->stream_res.dsc == NULL)
1885 				return false;
1886 		}
1887 	} else {
1888 		if (pri_pipe->bottom_pipe) {
1889 			ASSERT(pri_pipe->bottom_pipe != sec_pipe);
1890 			sec_pipe->bottom_pipe = pri_pipe->bottom_pipe;
1891 			sec_pipe->bottom_pipe->top_pipe = sec_pipe;
1892 		}
1893 		pri_pipe->bottom_pipe = sec_pipe;
1894 		sec_pipe->top_pipe = pri_pipe;
1895 
1896 		ASSERT(pri_pipe->plane_state);
1897 	}
1898 
1899 	return true;
1900 }
1901 
1902 static struct pipe_ctx *dcn30_find_split_pipe(
1903 		struct dc *dc,
1904 		struct dc_state *context,
1905 		int old_index)
1906 {
1907 	struct pipe_ctx *pipe = NULL;
1908 	int i;
1909 
1910 	if (old_index >= 0 && context->res_ctx.pipe_ctx[old_index].stream == NULL) {
1911 		pipe = &context->res_ctx.pipe_ctx[old_index];
1912 		pipe->pipe_idx = old_index;
1913 	}
1914 
1915 	if (!pipe)
1916 		for (i = dc->res_pool->pipe_count - 1; i >= 0; i--) {
1917 			if (dc->current_state->res_ctx.pipe_ctx[i].top_pipe == NULL
1918 					&& dc->current_state->res_ctx.pipe_ctx[i].prev_odm_pipe == NULL) {
1919 				if (context->res_ctx.pipe_ctx[i].stream == NULL) {
1920 					pipe = &context->res_ctx.pipe_ctx[i];
1921 					pipe->pipe_idx = i;
1922 					break;
1923 				}
1924 			}
1925 		}
1926 
1927 	/*
1928 	 * May need to fix pipes getting tossed from 1 opp to another on flip
1929 	 * Add for debugging transient underflow during topology updates:
1930 	 * ASSERT(pipe);
1931 	 */
1932 	if (!pipe)
1933 		for (i = dc->res_pool->pipe_count - 1; i >= 0; i--) {
1934 			if (context->res_ctx.pipe_ctx[i].stream == NULL) {
1935 				pipe = &context->res_ctx.pipe_ctx[i];
1936 				pipe->pipe_idx = i;
1937 				break;
1938 			}
1939 		}
1940 
1941 	return pipe;
1942 }
1943 
1944 static bool dcn30_internal_validate_bw(
1945 		struct dc *dc,
1946 		struct dc_state *context,
1947 		display_e2e_pipe_params_st *pipes,
1948 		int *pipe_cnt_out,
1949 		int *vlevel_out,
1950 		bool fast_validate)
1951 {
1952 	bool out = false;
1953 	bool repopulate_pipes = false;
1954 	int split[MAX_PIPES] = { 0 };
1955 	bool merge[MAX_PIPES] = { false };
1956 	bool newly_split[MAX_PIPES] = { false };
1957 	int pipe_cnt, i, pipe_idx, vlevel;
1958 	struct vba_vars_st *vba = &context->bw_ctx.dml.vba;
1959 
1960 	ASSERT(pipes);
1961 	if (!pipes)
1962 		return false;
1963 
1964 	pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes);
1965 
1966 	if (!pipe_cnt) {
1967 		out = true;
1968 		goto validate_out;
1969 	}
1970 
1971 	dml_log_pipe_params(&context->bw_ctx.dml, pipes, pipe_cnt);
1972 
1973 	if (!fast_validate) {
1974 		/*
1975 		 * DML favors voltage over p-state, but we're more interested in
1976 		 * supporting p-state over voltage. We can't support p-state in
1977 		 * prefetch mode > 0 so try capping the prefetch mode to start.
1978 		 */
1979 		context->bw_ctx.dml.soc.allow_dram_self_refresh_or_dram_clock_change_in_vblank =
1980 			dm_allow_self_refresh_and_mclk_switch;
1981 		vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
1982 		/* This may adjust vlevel and maxMpcComb */
1983 		if (vlevel < context->bw_ctx.dml.soc.num_states)
1984 			vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, vlevel, split, merge);
1985 	}
1986 	if (fast_validate || vlevel == context->bw_ctx.dml.soc.num_states ||
1987 			vba->DRAMClockChangeSupport[vlevel][vba->maxMpcComb] == dm_dram_clock_change_unsupported) {
1988 		/*
1989 		 * If mode is unsupported or there's still no p-state support then
1990 		 * fall back to favoring voltage.
1991 		 *
1992 		 * We don't actually support prefetch mode 2, so require that we
1993 		 * at least support prefetch mode 1.
1994 		 */
1995 		context->bw_ctx.dml.soc.allow_dram_self_refresh_or_dram_clock_change_in_vblank =
1996 			dm_allow_self_refresh;
1997 
1998 		vlevel = dml_get_voltage_level(&context->bw_ctx.dml, pipes, pipe_cnt);
1999 		if (vlevel < context->bw_ctx.dml.soc.num_states) {
2000 			memset(split, 0, sizeof(split));
2001 			memset(merge, 0, sizeof(merge));
2002 			vlevel = dcn20_validate_apply_pipe_split_flags(dc, context, vlevel, split, merge);
2003 		}
2004 	}
2005 
2006 	dml_log_mode_support_params(&context->bw_ctx.dml);
2007 
2008 	/* TODO: Need to check calculated vlevel why that fails validation of below resolutions */
2009 	if (context->res_ctx.pipe_ctx[0].stream != NULL) {
2010 		if (context->res_ctx.pipe_ctx[0].stream->timing.h_addressable == 640  && context->res_ctx.pipe_ctx[0].stream->timing.v_addressable == 480)
2011 			vlevel = 0;
2012 		if (context->res_ctx.pipe_ctx[0].stream->timing.h_addressable == 1280 && context->res_ctx.pipe_ctx[0].stream->timing.v_addressable == 800)
2013 			vlevel = 0;
2014 		if (context->res_ctx.pipe_ctx[0].stream->timing.h_addressable == 1280 && context->res_ctx.pipe_ctx[0].stream->timing.v_addressable == 768)
2015 			vlevel = 0;
2016 		if (context->res_ctx.pipe_ctx[0].stream->timing.h_addressable == 1280 && context->res_ctx.pipe_ctx[0].stream->timing.v_addressable == 1024)
2017 			vlevel = 0;
2018 		if (context->res_ctx.pipe_ctx[0].stream->timing.h_addressable == 2048 && context->res_ctx.pipe_ctx[0].stream->timing.v_addressable == 1536)
2019 			vlevel = 0;
2020 	}
2021 
2022 	if (vlevel == context->bw_ctx.dml.soc.num_states)
2023 		goto validate_fail;
2024 
2025 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2026 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2027 		struct pipe_ctx *mpo_pipe = pipe->bottom_pipe;
2028 
2029 		if (!pipe->stream)
2030 			continue;
2031 
2032 		/* We only support full screen mpo with ODM */
2033 		if (vba->ODMCombineEnabled[vba->pipe_plane[pipe_idx]] != dm_odm_combine_mode_disabled
2034 				&& pipe->plane_state && mpo_pipe
2035 				&& memcmp(&mpo_pipe->plane_res.scl_data.recout,
2036 						&pipe->plane_res.scl_data.recout,
2037 						sizeof(struct rect)) != 0) {
2038 			ASSERT(mpo_pipe->plane_state != pipe->plane_state);
2039 			goto validate_fail;
2040 		}
2041 		pipe_idx++;
2042 	}
2043 
2044 	/* merge pipes if necessary */
2045 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2046 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2047 
2048 		/*skip pipes that don't need merging*/
2049 		if (!merge[i])
2050 			continue;
2051 
2052 		/* if ODM merge we ignore mpc tree, mpo pipes will have their own flags */
2053 		if (pipe->prev_odm_pipe) {
2054 			/*split off odm pipe*/
2055 			pipe->prev_odm_pipe->next_odm_pipe = pipe->next_odm_pipe;
2056 			if (pipe->next_odm_pipe)
2057 				pipe->next_odm_pipe->prev_odm_pipe = pipe->prev_odm_pipe;
2058 
2059 			pipe->bottom_pipe = NULL;
2060 			pipe->next_odm_pipe = NULL;
2061 			pipe->plane_state = NULL;
2062 			pipe->stream = NULL;
2063 			pipe->top_pipe = NULL;
2064 			pipe->prev_odm_pipe = NULL;
2065 			if (pipe->stream_res.dsc)
2066 				dcn20_release_dsc(&context->res_ctx, dc->res_pool, &pipe->stream_res.dsc);
2067 			memset(&pipe->plane_res, 0, sizeof(pipe->plane_res));
2068 			memset(&pipe->stream_res, 0, sizeof(pipe->stream_res));
2069 			repopulate_pipes = true;
2070 		} else if (pipe->top_pipe && pipe->top_pipe->plane_state == pipe->plane_state) {
2071 			struct pipe_ctx *top_pipe = pipe->top_pipe;
2072 			struct pipe_ctx *bottom_pipe = pipe->bottom_pipe;
2073 
2074 			top_pipe->bottom_pipe = bottom_pipe;
2075 			if (bottom_pipe)
2076 				bottom_pipe->top_pipe = top_pipe;
2077 
2078 			pipe->top_pipe = NULL;
2079 			pipe->bottom_pipe = NULL;
2080 			pipe->plane_state = NULL;
2081 			pipe->stream = NULL;
2082 			memset(&pipe->plane_res, 0, sizeof(pipe->plane_res));
2083 			memset(&pipe->stream_res, 0, sizeof(pipe->stream_res));
2084 			repopulate_pipes = true;
2085 		} else
2086 			ASSERT(0); /* Should never try to merge master pipe */
2087 
2088 	}
2089 
2090 	for (i = 0, pipe_idx = -1; i < dc->res_pool->pipe_count; i++) {
2091 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2092 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2093 		struct pipe_ctx *hsplit_pipe = NULL;
2094 		bool odm;
2095 		int old_index = -1;
2096 
2097 		if (!pipe->stream || newly_split[i])
2098 			continue;
2099 
2100 		pipe_idx++;
2101 		odm = vba->ODMCombineEnabled[vba->pipe_plane[pipe_idx]] != dm_odm_combine_mode_disabled;
2102 
2103 		if (!pipe->plane_state && !odm)
2104 			continue;
2105 
2106 		if (split[i]) {
2107 			if (odm) {
2108 				if (split[i] == 4 && old_pipe->next_odm_pipe && old_pipe->next_odm_pipe->next_odm_pipe)
2109 					old_index = old_pipe->next_odm_pipe->next_odm_pipe->pipe_idx;
2110 				else if (old_pipe->next_odm_pipe)
2111 					old_index = old_pipe->next_odm_pipe->pipe_idx;
2112 			} else {
2113 				if (split[i] == 4 && old_pipe->bottom_pipe && old_pipe->bottom_pipe->bottom_pipe &&
2114 						old_pipe->bottom_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2115 					old_index = old_pipe->bottom_pipe->bottom_pipe->pipe_idx;
2116 				else if (old_pipe->bottom_pipe &&
2117 						old_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2118 					old_index = old_pipe->bottom_pipe->pipe_idx;
2119 			}
2120 			hsplit_pipe = dcn30_find_split_pipe(dc, context, old_index);
2121 			ASSERT(hsplit_pipe);
2122 			if (!hsplit_pipe)
2123 				goto validate_fail;
2124 
2125 			if (!dcn30_split_stream_for_mpc_or_odm(
2126 					dc, &context->res_ctx,
2127 					pipe, hsplit_pipe, odm))
2128 				goto validate_fail;
2129 
2130 			newly_split[hsplit_pipe->pipe_idx] = true;
2131 			repopulate_pipes = true;
2132 		}
2133 		if (split[i] == 4) {
2134 			struct pipe_ctx *pipe_4to1;
2135 
2136 			if (odm && old_pipe->next_odm_pipe)
2137 				old_index = old_pipe->next_odm_pipe->pipe_idx;
2138 			else if (!odm && old_pipe->bottom_pipe &&
2139 						old_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2140 				old_index = old_pipe->bottom_pipe->pipe_idx;
2141 			else
2142 				old_index = -1;
2143 			pipe_4to1 = dcn30_find_split_pipe(dc, context, old_index);
2144 			ASSERT(pipe_4to1);
2145 			if (!pipe_4to1)
2146 				goto validate_fail;
2147 			if (!dcn30_split_stream_for_mpc_or_odm(
2148 					dc, &context->res_ctx,
2149 					pipe, pipe_4to1, odm))
2150 				goto validate_fail;
2151 			newly_split[pipe_4to1->pipe_idx] = true;
2152 
2153 			if (odm && old_pipe->next_odm_pipe && old_pipe->next_odm_pipe->next_odm_pipe
2154 					&& old_pipe->next_odm_pipe->next_odm_pipe->next_odm_pipe)
2155 				old_index = old_pipe->next_odm_pipe->next_odm_pipe->next_odm_pipe->pipe_idx;
2156 			else if (!odm && old_pipe->bottom_pipe && old_pipe->bottom_pipe->bottom_pipe &&
2157 					old_pipe->bottom_pipe->bottom_pipe->bottom_pipe &&
2158 					old_pipe->bottom_pipe->bottom_pipe->bottom_pipe->plane_state == old_pipe->plane_state)
2159 				old_index = old_pipe->bottom_pipe->bottom_pipe->bottom_pipe->pipe_idx;
2160 			else
2161 				old_index = -1;
2162 			pipe_4to1 = dcn30_find_split_pipe(dc, context, old_index);
2163 			ASSERT(pipe_4to1);
2164 			if (!pipe_4to1)
2165 				goto validate_fail;
2166 			if (!dcn30_split_stream_for_mpc_or_odm(
2167 					dc, &context->res_ctx,
2168 					hsplit_pipe, pipe_4to1, odm))
2169 				goto validate_fail;
2170 			newly_split[pipe_4to1->pipe_idx] = true;
2171 		}
2172 		if (odm)
2173 			dcn20_build_mapped_resource(dc, context, pipe->stream);
2174 	}
2175 
2176 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2177 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2178 
2179 		if (pipe->plane_state) {
2180 			if (!resource_build_scaling_params(pipe))
2181 				goto validate_fail;
2182 		}
2183 	}
2184 
2185 	/* Actual dsc count per stream dsc validation*/
2186 	if (!dcn20_validate_dsc(dc, context)) {
2187 		vba->ValidationStatus[vba->soc.num_states] = DML_FAIL_DSC_VALIDATION_FAILURE;
2188 		goto validate_fail;
2189 	}
2190 
2191 	if (repopulate_pipes)
2192 		pipe_cnt = dc->res_pool->funcs->populate_dml_pipes(dc, context, pipes);
2193 	*vlevel_out = vlevel;
2194 	*pipe_cnt_out = pipe_cnt;
2195 
2196 	out = true;
2197 	goto validate_out;
2198 
2199 validate_fail:
2200 	out = false;
2201 
2202 validate_out:
2203 	return out;
2204 }
2205 
2206 void dcn30_calculate_wm_and_dlg(
2207 		struct dc *dc, struct dc_state *context,
2208 		display_e2e_pipe_params_st *pipes,
2209 		int pipe_cnt,
2210 		int vlevel)
2211 {
2212 	int i, pipe_idx;
2213 	double dcfclk = context->bw_ctx.dml.vba.DCFCLKState[vlevel][context->bw_ctx.dml.vba.maxMpcComb];
2214 	bool pstate_en = context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] !=
2215 			dm_dram_clock_change_unsupported;
2216 
2217 	if (context->bw_ctx.dml.soc.min_dcfclk > dcfclk)
2218 		dcfclk = context->bw_ctx.dml.soc.min_dcfclk;
2219 
2220 	pipes[0].clks_cfg.voltage = vlevel;
2221 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
2222 	pipes[0].clks_cfg.socclk_mhz = context->bw_ctx.dml.soc.clock_limits[vlevel].socclk_mhz;
2223 
2224 	/* Set B:
2225 	 * DCFCLK: 1GHz or min required above 1GHz
2226 	 * FCLK/UCLK: Max
2227 	 */
2228 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].valid) {
2229 		if (vlevel == 0) {
2230 			pipes[0].clks_cfg.voltage = 1;
2231 			pipes[0].clks_cfg.dcfclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dcfclk_mhz;
2232 		}
2233 		context->bw_ctx.dml.soc.dram_clock_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].dml_input.pstate_latency_us;
2234 		context->bw_ctx.dml.soc.sr_enter_plus_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].dml_input.sr_enter_plus_exit_time_us;
2235 		context->bw_ctx.dml.soc.sr_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_B].dml_input.sr_exit_time_us;
2236 	}
2237 	context->bw_ctx.bw.dcn.watermarks.b.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2238 	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;
2239 	context->bw_ctx.bw.dcn.watermarks.b.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2240 	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;
2241 	context->bw_ctx.bw.dcn.watermarks.b.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2242 	context->bw_ctx.bw.dcn.watermarks.b.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2243 	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;
2244 	context->bw_ctx.bw.dcn.watermarks.b.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2245 
2246 	pipes[0].clks_cfg.voltage = vlevel;
2247 	pipes[0].clks_cfg.dcfclk_mhz = dcfclk;
2248 
2249 	/* Set D:
2250 	 * DCFCLK: Min Required
2251 	 * FCLK(proportional to UCLK): 1GHz or Max
2252 	 * sr_enter_exit = 4, sr_exit = 2us
2253 	 */
2254 	/*
2255 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].valid) {
2256 		context->bw_ctx.dml.soc.dram_clock_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].dml_input.pstate_latency_us;
2257 		context->bw_ctx.dml.soc.sr_enter_plus_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].dml_input.sr_enter_plus_exit_time_us;
2258 		context->bw_ctx.dml.soc.sr_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_D].dml_input.sr_exit_time_us;
2259 	}
2260 	context->bw_ctx.bw.dcn.watermarks.d.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2261 	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;
2262 	context->bw_ctx.bw.dcn.watermarks.d.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2263 	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;
2264 	context->bw_ctx.bw.dcn.watermarks.d.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2265 	context->bw_ctx.bw.dcn.watermarks.d.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2266 	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;
2267 	context->bw_ctx.bw.dcn.watermarks.d.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2268 	*/
2269 
2270 	/* Set C:
2271 	 * DCFCLK: Min Required
2272 	 * FCLK(proportional to UCLK): 1GHz or Max
2273 	 * pstate latency overridden to 5us
2274 	 */
2275 	if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].valid) {
2276 		unsigned int min_dram_speed_mts = context->bw_ctx.dml.vba.DRAMSpeed;
2277 		unsigned int min_dram_speed_mts_margin = 160;
2278 
2279 		context->bw_ctx.dml.soc.dram_clock_change_latency_us = dc->clk_mgr->bw_params->dummy_pstate_table[0].dummy_pstate_latency_us;
2280 
2281 		if (context->bw_ctx.dml.vba.DRAMClockChangeSupport[vlevel][context->bw_ctx.dml.vba.maxMpcComb] == dm_dram_clock_change_unsupported)
2282 			min_dram_speed_mts = dc->clk_mgr->bw_params->clk_table.entries[dc->clk_mgr->bw_params->clk_table.num_entries - 1].memclk_mhz * 16;
2283 
2284 		for (i = 3; i > 0; i--) {
2285 			if ((min_dram_speed_mts + min_dram_speed_mts_margin > dc->clk_mgr->bw_params->dummy_pstate_table[i].dram_speed_mts) &&
2286 					(min_dram_speed_mts - min_dram_speed_mts_margin < dc->clk_mgr->bw_params->dummy_pstate_table[i].dram_speed_mts))
2287 				context->bw_ctx.dml.soc.dram_clock_change_latency_us = dc->clk_mgr->bw_params->dummy_pstate_table[i].dummy_pstate_latency_us;
2288 		}
2289 
2290 		context->bw_ctx.dml.soc.sr_enter_plus_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].dml_input.sr_enter_plus_exit_time_us;
2291 		context->bw_ctx.dml.soc.sr_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_C].dml_input.sr_exit_time_us;
2292 	}
2293 	context->bw_ctx.bw.dcn.watermarks.c.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2294 	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;
2295 	context->bw_ctx.bw.dcn.watermarks.c.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2296 	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;
2297 	context->bw_ctx.bw.dcn.watermarks.c.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2298 	context->bw_ctx.bw.dcn.watermarks.c.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2299 	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;
2300 	context->bw_ctx.bw.dcn.watermarks.c.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2301 
2302 	if (!pstate_en) {
2303 		/* The only difference between A and C is p-state latency, if p-state is not supported we want to
2304 		 * calculate DLG based on dummy p-state latency, and max out the set A p-state watermark
2305 		 */
2306 		context->bw_ctx.bw.dcn.watermarks.a = context->bw_ctx.bw.dcn.watermarks.c;
2307 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns = 0x13FFFF;
2308 	} else {
2309 		/* Set A:
2310 		 * DCFCLK: Min Required
2311 		 * FCLK(proportional to UCLK): 1GHz or Max
2312 		 *
2313 		 * Set A calculated last so that following calculations are based on Set A
2314 		 */
2315 		if (dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].valid) {
2316 			context->bw_ctx.dml.soc.dram_clock_change_latency_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
2317 			context->bw_ctx.dml.soc.sr_enter_plus_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.sr_enter_plus_exit_time_us;
2318 			context->bw_ctx.dml.soc.sr_exit_time_us = dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.sr_exit_time_us;
2319 		}
2320 		context->bw_ctx.bw.dcn.watermarks.a.urgent_ns = get_wm_urgent(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2321 		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;
2322 		context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.cstate_exit_ns = get_wm_stutter_exit(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2323 		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;
2324 		context->bw_ctx.bw.dcn.watermarks.a.pte_meta_urgent_ns = get_wm_memory_trip(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2325 		context->bw_ctx.bw.dcn.watermarks.a.frac_urg_bw_nom = get_fraction_of_urgent_bandwidth(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2326 		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;
2327 		context->bw_ctx.bw.dcn.watermarks.a.urgent_latency_ns = get_urgent_latency(&context->bw_ctx.dml, pipes, pipe_cnt) * 1000;
2328 	}
2329 
2330 	context->perf_params.stutter_period_us = context->bw_ctx.dml.vba.StutterPeriod;
2331 
2332 	/* Make set D = set A until set D is enabled */
2333 	context->bw_ctx.bw.dcn.watermarks.d = context->bw_ctx.bw.dcn.watermarks.a;
2334 
2335 	for (i = 0, pipe_idx = 0; i < dc->res_pool->pipe_count; i++) {
2336 		if (!context->res_ctx.pipe_ctx[i].stream)
2337 			continue;
2338 
2339 		pipes[pipe_idx].clks_cfg.dispclk_mhz = get_dispclk_calculated(&context->bw_ctx.dml, pipes, pipe_cnt);
2340 		pipes[pipe_idx].clks_cfg.dppclk_mhz = get_dppclk_calculated(&context->bw_ctx.dml, pipes, pipe_cnt, pipe_idx);
2341 
2342 		if (dc->config.forced_clocks) {
2343 			pipes[pipe_idx].clks_cfg.dispclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dispclk_mhz;
2344 			pipes[pipe_idx].clks_cfg.dppclk_mhz = context->bw_ctx.dml.soc.clock_limits[0].dppclk_mhz;
2345 		}
2346 		if (dc->debug.min_disp_clk_khz > pipes[pipe_idx].clks_cfg.dispclk_mhz * 1000)
2347 			pipes[pipe_idx].clks_cfg.dispclk_mhz = dc->debug.min_disp_clk_khz / 1000.0;
2348 		if (dc->debug.min_dpp_clk_khz > pipes[pipe_idx].clks_cfg.dppclk_mhz * 1000)
2349 			pipes[pipe_idx].clks_cfg.dppclk_mhz = dc->debug.min_dpp_clk_khz / 1000.0;
2350 
2351 		pipe_idx++;
2352 	}
2353 
2354 	dcn20_calculate_dlg_params(dc, context, pipes, pipe_cnt, vlevel);
2355 
2356 	if (!pstate_en)
2357 		/* Restore full p-state latency */
2358 		context->bw_ctx.dml.soc.dram_clock_change_latency_us =
2359 				dc->clk_mgr->bw_params->wm_table.nv_entries[WM_A].dml_input.pstate_latency_us;
2360 }
2361 
2362 bool dcn30_validate_bandwidth(struct dc *dc,
2363 		struct dc_state *context,
2364 		bool fast_validate)
2365 {
2366 	bool out = false;
2367 
2368 	BW_VAL_TRACE_SETUP();
2369 
2370 	int vlevel = 0;
2371 	int pipe_cnt = 0;
2372 	display_e2e_pipe_params_st *pipes = kzalloc(dc->res_pool->pipe_count * sizeof(display_e2e_pipe_params_st), GFP_KERNEL);
2373 	DC_LOGGER_INIT(dc->ctx->logger);
2374 
2375 	BW_VAL_TRACE_COUNT();
2376 
2377 	out = dcn30_internal_validate_bw(dc, context, pipes, &pipe_cnt, &vlevel, fast_validate);
2378 
2379 	if (pipe_cnt == 0)
2380 		goto validate_out;
2381 
2382 	if (!out)
2383 		goto validate_fail;
2384 
2385 	BW_VAL_TRACE_END_VOLTAGE_LEVEL();
2386 
2387 	if (fast_validate) {
2388 		BW_VAL_TRACE_SKIP(fast);
2389 		goto validate_out;
2390 	}
2391 
2392 	dc->res_pool->funcs->calculate_wm_and_dlg(dc, context, pipes, pipe_cnt, vlevel);
2393 
2394 	BW_VAL_TRACE_END_WATERMARKS();
2395 
2396 	goto validate_out;
2397 
2398 validate_fail:
2399 	DC_LOG_WARNING("Mode Validation Warning: %s failed validation.\n",
2400 		dml_get_status_message(context->bw_ctx.dml.vba.ValidationStatus[context->bw_ctx.dml.vba.soc.num_states]));
2401 
2402 	BW_VAL_TRACE_SKIP(fail);
2403 	out = false;
2404 
2405 validate_out:
2406 	kfree(pipes);
2407 
2408 	BW_VAL_TRACE_FINISH();
2409 
2410 	return out;
2411 }
2412 
2413 static void get_optimal_dcfclk_fclk_for_uclk(unsigned int uclk_mts,
2414                                                        unsigned int *optimal_dcfclk,
2415                                                        unsigned int *optimal_fclk)
2416 {
2417        double bw_from_dram, bw_from_dram1, bw_from_dram2;
2418 
2419        bw_from_dram1 = uclk_mts * dcn3_0_soc.num_chans *
2420                        dcn3_0_soc.dram_channel_width_bytes * (dcn3_0_soc.max_avg_dram_bw_use_normal_percent / 100);
2421        bw_from_dram2 = uclk_mts * dcn3_0_soc.num_chans *
2422                        dcn3_0_soc.dram_channel_width_bytes * (dcn3_0_soc.max_avg_sdp_bw_use_normal_percent / 100);
2423 
2424        bw_from_dram = (bw_from_dram1 < bw_from_dram2) ? bw_from_dram1 : bw_from_dram2;
2425 
2426        if (optimal_fclk)
2427                *optimal_fclk = bw_from_dram /
2428                (dcn3_0_soc.fabric_datapath_to_dcn_data_return_bytes * (dcn3_0_soc.max_avg_sdp_bw_use_normal_percent / 100));
2429 
2430        if (optimal_dcfclk)
2431                *optimal_dcfclk =  bw_from_dram /
2432                (dcn3_0_soc.return_bus_width_bytes * (dcn3_0_soc.max_avg_sdp_bw_use_normal_percent / 100));
2433 }
2434 
2435 void dcn30_update_bw_bounding_box(struct dc *dc, struct clk_bw_params *bw_params)
2436 {
2437 	unsigned int i, j;
2438 	unsigned int num_states = 0;
2439 
2440 	unsigned int dcfclk_mhz[DC__VOLTAGE_STATES] = {0};
2441 	unsigned int dram_speed_mts[DC__VOLTAGE_STATES] = {0};
2442 	unsigned int optimal_uclk_for_dcfclk_sta_targets[DC__VOLTAGE_STATES] = {0};
2443 	unsigned int optimal_dcfclk_for_uclk[DC__VOLTAGE_STATES] = {0};
2444 
2445 	unsigned int dcfclk_sta_targets[DC__VOLTAGE_STATES] = {694, 875, 1000, 1200};
2446 	unsigned int num_dcfclk_sta_targets = 4;
2447 	unsigned int num_uclk_states;
2448 
2449 	if (dc->ctx->dc_bios->vram_info.num_chans)
2450 		dcn3_0_soc.num_chans = dc->ctx->dc_bios->vram_info.num_chans;
2451 
2452 	if (dc->ctx->dc_bios->vram_info.dram_channel_width_bytes)
2453 		dcn3_0_soc.dram_channel_width_bytes = dc->ctx->dc_bios->vram_info.dram_channel_width_bytes;
2454 
2455 	dcn3_0_soc.dispclk_dppclk_vco_speed_mhz = dc->clk_mgr->dentist_vco_freq_khz / 1000.0;
2456 	dc->dml.soc.dispclk_dppclk_vco_speed_mhz = dc->clk_mgr->dentist_vco_freq_khz / 1000.0;
2457 
2458 	if (bw_params->clk_table.entries[0].memclk_mhz) {
2459 
2460 		if (bw_params->clk_table.entries[1].dcfclk_mhz > dcfclk_sta_targets[num_dcfclk_sta_targets-1]) {
2461 			// If max DCFCLK is greater than the max DCFCLK STA target, insert into the DCFCLK STA target array
2462 			dcfclk_sta_targets[num_dcfclk_sta_targets] = bw_params->clk_table.entries[1].dcfclk_mhz;
2463 			num_dcfclk_sta_targets++;
2464 		} else if (bw_params->clk_table.entries[1].dcfclk_mhz < dcfclk_sta_targets[num_dcfclk_sta_targets-1]) {
2465 			// If max DCFCLK is less than the max DCFCLK STA target, cap values and remove duplicates
2466 			for (i = 0; i < num_dcfclk_sta_targets; i++) {
2467 				if (dcfclk_sta_targets[i] > bw_params->clk_table.entries[1].dcfclk_mhz) {
2468 					dcfclk_sta_targets[i] = bw_params->clk_table.entries[1].dcfclk_mhz;
2469 					break;
2470 				}
2471 			}
2472 			// Update size of array since we "removed" duplicates
2473 			num_dcfclk_sta_targets = i + 1;
2474 		}
2475 
2476 		num_uclk_states = bw_params->clk_table.num_entries;
2477 
2478 		// Calculate optimal dcfclk for each uclk
2479 		for (i = 0; i < num_uclk_states; i++) {
2480 			get_optimal_dcfclk_fclk_for_uclk(bw_params->clk_table.entries[i].memclk_mhz * 16,
2481 					&optimal_dcfclk_for_uclk[i], NULL);
2482 			if (optimal_dcfclk_for_uclk[i] < bw_params->clk_table.entries[0].dcfclk_mhz) {
2483 				optimal_dcfclk_for_uclk[i] = bw_params->clk_table.entries[0].dcfclk_mhz;
2484 			}
2485 		}
2486 
2487 		// Calculate optimal uclk for each dcfclk sta target
2488 		for (i = 0; i < num_dcfclk_sta_targets; i++) {
2489 			for (j = 0; j < num_uclk_states; j++) {
2490 				if (dcfclk_sta_targets[i] < optimal_dcfclk_for_uclk[j]) {
2491 					optimal_uclk_for_dcfclk_sta_targets[i] =
2492 							bw_params->clk_table.entries[j].memclk_mhz * 16;
2493 					break;
2494 				}
2495 			}
2496 		}
2497 
2498 		i = 0;
2499 		j = 0;
2500 		// create the final dcfclk and uclk table
2501 		while (i < num_dcfclk_sta_targets && j < num_uclk_states && num_states < DC__VOLTAGE_STATES) {
2502 			if (dcfclk_sta_targets[i] < optimal_dcfclk_for_uclk[j] && i < num_dcfclk_sta_targets) {
2503 				dcfclk_mhz[num_states] = dcfclk_sta_targets[i];
2504 				dram_speed_mts[num_states++] = optimal_uclk_for_dcfclk_sta_targets[i++];
2505 			} else {
2506 				if (j < num_uclk_states && optimal_dcfclk_for_uclk[j] <= bw_params->clk_table.entries[1].dcfclk_mhz) {
2507 					dcfclk_mhz[num_states] = optimal_dcfclk_for_uclk[j];
2508 					dram_speed_mts[num_states++] = bw_params->clk_table.entries[j++].memclk_mhz * 16;
2509 				} else {
2510 					j = num_uclk_states;
2511 				}
2512 			}
2513 		}
2514 
2515 		while (i < num_dcfclk_sta_targets && num_states < DC__VOLTAGE_STATES) {
2516 			dcfclk_mhz[num_states] = dcfclk_sta_targets[i];
2517 			dram_speed_mts[num_states++] = optimal_uclk_for_dcfclk_sta_targets[i++];
2518 		}
2519 
2520 		while (j < num_uclk_states && num_states < DC__VOLTAGE_STATES &&
2521 				optimal_dcfclk_for_uclk[j] <= bw_params->clk_table.entries[1].dcfclk_mhz) {
2522 			dcfclk_mhz[num_states] = optimal_dcfclk_for_uclk[j];
2523 			dram_speed_mts[num_states++] = bw_params->clk_table.entries[j++].memclk_mhz * 16;
2524 		}
2525 
2526 		for (i = 0; i < dcn3_0_soc.num_states; i++) {
2527 			dcn3_0_soc.clock_limits[i].state = i;
2528 			dcn3_0_soc.clock_limits[i].dcfclk_mhz = dcfclk_mhz[i];
2529 			dcn3_0_soc.clock_limits[i].fabricclk_mhz = dcfclk_mhz[i];
2530 			dcn3_0_soc.clock_limits[i].dram_speed_mts = dram_speed_mts[i];
2531 
2532 			/* Fill all states with max values of all other clocks */
2533 			dcn3_0_soc.clock_limits[i].dispclk_mhz = bw_params->clk_table.entries[1].dispclk_mhz;
2534 			dcn3_0_soc.clock_limits[i].dppclk_mhz  = bw_params->clk_table.entries[1].dppclk_mhz;
2535 			dcn3_0_soc.clock_limits[i].phyclk_mhz  = bw_params->clk_table.entries[1].phyclk_mhz;
2536 			dcn3_0_soc.clock_limits[i].dtbclk_mhz = dcn3_0_soc.clock_limits[0].dtbclk_mhz;
2537 			/* These clocks cannot come from bw_params, always fill from dcn3_0_soc[1] */
2538 			/* FCLK, PHYCLK_D18, SOCCLK, DSCCLK */
2539 			dcn3_0_soc.clock_limits[i].phyclk_d18_mhz = dcn3_0_soc.clock_limits[0].phyclk_d18_mhz;
2540 			dcn3_0_soc.clock_limits[i].socclk_mhz = dcn3_0_soc.clock_limits[0].socclk_mhz;
2541 			dcn3_0_soc.clock_limits[i].dscclk_mhz = dcn3_0_soc.clock_limits[0].dscclk_mhz;
2542 		}
2543 		/* re-init DML with updated bb */
2544 		dml_init_instance(&dc->dml, &dcn3_0_soc, &dcn3_0_ip, DML_PROJECT_DCN30);
2545 		if (dc->current_state)
2546 			dml_init_instance(&dc->current_state->bw_ctx.dml, &dcn3_0_soc, &dcn3_0_ip, DML_PROJECT_DCN30);
2547 	}
2548 
2549 	/* re-init DML with updated bb */
2550 	dml_init_instance(&dc->dml, &dcn3_0_soc, &dcn3_0_ip, DML_PROJECT_DCN30);
2551 	if (dc->current_state)
2552 		dml_init_instance(&dc->current_state->bw_ctx.dml, &dcn3_0_soc, &dcn3_0_ip, DML_PROJECT_DCN30);
2553 }
2554 
2555 static const struct resource_funcs dcn30_res_pool_funcs = {
2556 	.destroy = dcn30_destroy_resource_pool,
2557 	.link_enc_create = dcn30_link_encoder_create,
2558 	.panel_cntl_create = dcn30_panel_cntl_create,
2559 	.validate_bandwidth = dcn30_validate_bandwidth,
2560 	.calculate_wm_and_dlg = dcn30_calculate_wm_and_dlg,
2561 	.populate_dml_pipes = dcn30_populate_dml_pipes_from_context,
2562 	.acquire_idle_pipe_for_layer = dcn20_acquire_idle_pipe_for_layer,
2563 	.add_stream_to_ctx = dcn30_add_stream_to_ctx,
2564 	.add_dsc_to_stream_resource = dcn20_add_dsc_to_stream_resource,
2565 	.remove_stream_from_ctx = dcn20_remove_stream_from_ctx,
2566 	.populate_dml_writeback_from_context = dcn30_populate_dml_writeback_from_context,
2567 	.set_mcif_arb_params = dcn30_set_mcif_arb_params,
2568 	.find_first_free_match_stream_enc_for_link = dcn10_find_first_free_match_stream_enc_for_link,
2569 	.acquire_post_bldn_3dlut = dcn30_acquire_post_bldn_3dlut,
2570 	.release_post_bldn_3dlut = dcn30_release_post_bldn_3dlut,
2571 	.update_bw_bounding_box = dcn30_update_bw_bounding_box,
2572 	.patch_unknown_plane_state = dcn20_patch_unknown_plane_state,
2573 };
2574 
2575 static bool dcn30_resource_construct(
2576 	uint8_t num_virtual_links,
2577 	struct dc *dc,
2578 	struct dcn30_resource_pool *pool)
2579 {
2580 	int i;
2581 	struct dc_context *ctx = dc->ctx;
2582 	struct irq_service_init_data init_data;
2583 
2584 	ctx->dc_bios->regs = &bios_regs;
2585 
2586 	pool->base.res_cap = &res_cap_dcn3;
2587 
2588 	pool->base.funcs = &dcn30_res_pool_funcs;
2589 
2590 	/*************************************************
2591 	 *  Resource + asic cap harcoding                *
2592 	 *************************************************/
2593 	pool->base.underlay_pipe_index = NO_UNDERLAY_PIPE;
2594 	pool->base.pipe_count = pool->base.res_cap->num_timing_generator;
2595 	pool->base.mpcc_count = pool->base.res_cap->num_timing_generator;
2596 	dc->caps.max_downscale_ratio = 600;
2597 	dc->caps.i2c_speed_in_khz = 100;
2598 	dc->caps.max_cursor_size = 256;
2599 	dc->caps.dmdata_alloc_size = 2048;
2600 
2601 	dc->caps.max_slave_planes = 1;
2602 	dc->caps.post_blend_color_processing = true;
2603 	dc->caps.force_dp_tps4_for_cp2520 = true;
2604 	dc->caps.extended_aux_timeout_support = true;
2605 	dc->caps.dmcub_support = true;
2606 
2607 	/* Color pipeline capabilities */
2608 	dc->caps.color.dpp.dcn_arch = 1;
2609 	dc->caps.color.dpp.input_lut_shared = 0;
2610 	dc->caps.color.dpp.icsc = 1;
2611 	dc->caps.color.dpp.dgam_ram = 0; // must use gamma_corr
2612 	dc->caps.color.dpp.dgam_rom_caps.srgb = 1;
2613 	dc->caps.color.dpp.dgam_rom_caps.bt2020 = 1;
2614 	dc->caps.color.dpp.dgam_rom_caps.gamma2_2 = 1;
2615 	dc->caps.color.dpp.dgam_rom_caps.pq = 1;
2616 	dc->caps.color.dpp.dgam_rom_caps.hlg = 1;
2617 	dc->caps.color.dpp.post_csc = 1;
2618 	dc->caps.color.dpp.gamma_corr = 1;
2619 
2620 	dc->caps.color.dpp.hw_3d_lut = 1;
2621 	dc->caps.color.dpp.ogam_ram = 1;
2622 	// no OGAM ROM on DCN3
2623 	dc->caps.color.dpp.ogam_rom_caps.srgb = 0;
2624 	dc->caps.color.dpp.ogam_rom_caps.bt2020 = 0;
2625 	dc->caps.color.dpp.ogam_rom_caps.gamma2_2 = 0;
2626 	dc->caps.color.dpp.ogam_rom_caps.pq = 0;
2627 	dc->caps.color.dpp.ogam_rom_caps.hlg = 0;
2628 	dc->caps.color.dpp.ocsc = 0;
2629 
2630 	dc->caps.color.mpc.gamut_remap = 1;
2631 	dc->caps.color.mpc.num_3dluts = pool->base.res_cap->num_mpc_3dlut; //3
2632 	dc->caps.color.mpc.ogam_ram = 1;
2633 	dc->caps.color.mpc.ogam_rom_caps.srgb = 0;
2634 	dc->caps.color.mpc.ogam_rom_caps.bt2020 = 0;
2635 	dc->caps.color.mpc.ogam_rom_caps.gamma2_2 = 0;
2636 	dc->caps.color.mpc.ogam_rom_caps.pq = 0;
2637 	dc->caps.color.mpc.ogam_rom_caps.hlg = 0;
2638 	dc->caps.color.mpc.ocsc = 1;
2639 
2640 	if (dc->ctx->dce_environment == DCE_ENV_PRODUCTION_DRV)
2641 		dc->debug = debug_defaults_drv;
2642 	else if (dc->ctx->dce_environment == DCE_ENV_FPGA_MAXIMUS) {
2643 		dc->debug = debug_defaults_diags;
2644 	} else
2645 		dc->debug = debug_defaults_diags;
2646 	// Init the vm_helper
2647 	if (dc->vm_helper)
2648 		vm_helper_init(dc->vm_helper, 16);
2649 
2650 	/*************************************************
2651 	 *  Create resources                             *
2652 	 *************************************************/
2653 
2654 	/* Clock Sources for Pixel Clock*/
2655 	pool->base.clock_sources[DCN30_CLK_SRC_PLL0] =
2656 			dcn30_clock_source_create(ctx, ctx->dc_bios,
2657 				CLOCK_SOURCE_COMBO_PHY_PLL0,
2658 				&clk_src_regs[0], false);
2659 	pool->base.clock_sources[DCN30_CLK_SRC_PLL1] =
2660 			dcn30_clock_source_create(ctx, ctx->dc_bios,
2661 				CLOCK_SOURCE_COMBO_PHY_PLL1,
2662 				&clk_src_regs[1], false);
2663 	pool->base.clock_sources[DCN30_CLK_SRC_PLL2] =
2664 			dcn30_clock_source_create(ctx, ctx->dc_bios,
2665 				CLOCK_SOURCE_COMBO_PHY_PLL2,
2666 				&clk_src_regs[2], false);
2667 	pool->base.clock_sources[DCN30_CLK_SRC_PLL3] =
2668 			dcn30_clock_source_create(ctx, ctx->dc_bios,
2669 				CLOCK_SOURCE_COMBO_PHY_PLL3,
2670 				&clk_src_regs[3], false);
2671 	pool->base.clock_sources[DCN30_CLK_SRC_PLL4] =
2672 			dcn30_clock_source_create(ctx, ctx->dc_bios,
2673 				CLOCK_SOURCE_COMBO_PHY_PLL4,
2674 				&clk_src_regs[4], false);
2675 	pool->base.clock_sources[DCN30_CLK_SRC_PLL5] =
2676 			dcn30_clock_source_create(ctx, ctx->dc_bios,
2677 				CLOCK_SOURCE_COMBO_PHY_PLL5,
2678 				&clk_src_regs[5], false);
2679 
2680 	pool->base.clk_src_count = DCN30_CLK_SRC_TOTAL;
2681 
2682 	/* todo: not reuse phy_pll registers */
2683 	pool->base.dp_clock_source =
2684 			dcn30_clock_source_create(ctx, ctx->dc_bios,
2685 				CLOCK_SOURCE_ID_DP_DTO,
2686 				&clk_src_regs[0], true);
2687 
2688 	for (i = 0; i < pool->base.clk_src_count; i++) {
2689 		if (pool->base.clock_sources[i] == NULL) {
2690 			dm_error("DC: failed to create clock sources!\n");
2691 			BREAK_TO_DEBUGGER();
2692 			goto create_fail;
2693 		}
2694 	}
2695 
2696 	/* DCCG */
2697 	pool->base.dccg = dccg30_create(ctx, &dccg_regs, &dccg_shift, &dccg_mask);
2698 	if (pool->base.dccg == NULL) {
2699 		dm_error("DC: failed to create dccg!\n");
2700 		BREAK_TO_DEBUGGER();
2701 		goto create_fail;
2702 	}
2703 
2704 	/* PP Lib and SMU interfaces */
2705 	init_soc_bounding_box(dc, pool);
2706 
2707 	dml_init_instance(&dc->dml, &dcn3_0_soc, &dcn3_0_ip, DML_PROJECT_DCN30);
2708 
2709 	/* IRQ */
2710 	init_data.ctx = dc->ctx;
2711 	pool->base.irqs = dal_irq_service_dcn30_create(&init_data);
2712 	if (!pool->base.irqs)
2713 		goto create_fail;
2714 
2715 	/* HUBBUB */
2716 	pool->base.hubbub = dcn30_hubbub_create(ctx);
2717 	if (pool->base.hubbub == NULL) {
2718 		BREAK_TO_DEBUGGER();
2719 		dm_error("DC: failed to create hubbub!\n");
2720 		goto create_fail;
2721 	}
2722 
2723 	/* HUBPs, DPPs, OPPs and TGs */
2724 	for (i = 0; i < pool->base.pipe_count; i++) {
2725 		pool->base.hubps[i] = dcn30_hubp_create(ctx, i);
2726 		if (pool->base.hubps[i] == NULL) {
2727 			BREAK_TO_DEBUGGER();
2728 			dm_error(
2729 				"DC: failed to create hubps!\n");
2730 			goto create_fail;
2731 		}
2732 
2733 		pool->base.dpps[i] = dcn30_dpp_create(ctx, i);
2734 		if (pool->base.dpps[i] == NULL) {
2735 			BREAK_TO_DEBUGGER();
2736 			dm_error(
2737 				"DC: failed to create dpps!\n");
2738 			goto create_fail;
2739 		}
2740 	}
2741 
2742 	for (i = 0; i < pool->base.res_cap->num_opp; i++) {
2743 		pool->base.opps[i] = dcn30_opp_create(ctx, i);
2744 		if (pool->base.opps[i] == NULL) {
2745 			BREAK_TO_DEBUGGER();
2746 			dm_error(
2747 				"DC: failed to create output pixel processor!\n");
2748 			goto create_fail;
2749 		}
2750 	}
2751 
2752 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
2753 		pool->base.timing_generators[i] = dcn30_timing_generator_create(
2754 				ctx, i);
2755 		if (pool->base.timing_generators[i] == NULL) {
2756 			BREAK_TO_DEBUGGER();
2757 			dm_error("DC: failed to create tg!\n");
2758 			goto create_fail;
2759 		}
2760 	}
2761 	pool->base.timing_generator_count = i;
2762 	/* PSR */
2763 	pool->base.psr = dmub_psr_create(ctx);
2764 
2765 	if (pool->base.psr == NULL) {
2766 		dm_error("DC: failed to create PSR obj!\n");
2767 		BREAK_TO_DEBUGGER();
2768 		goto create_fail;
2769 	}
2770 
2771 	/* ABM */
2772 	for (i = 0; i < pool->base.res_cap->num_timing_generator; i++) {
2773 		pool->base.multiple_abms[i] = dmub_abm_create(ctx,
2774 				&abm_regs[i],
2775 				&abm_shift,
2776 				&abm_mask);
2777 		if (pool->base.multiple_abms[i] == NULL) {
2778 			dm_error("DC: failed to create abm for pipe %d!\n", i);
2779 			BREAK_TO_DEBUGGER();
2780 			goto create_fail;
2781 		}
2782 	}
2783 	/* MPC and DSC */
2784 	pool->base.mpc = dcn30_mpc_create(ctx, pool->base.mpcc_count, pool->base.res_cap->num_mpc_3dlut);
2785 	if (pool->base.mpc == NULL) {
2786 		BREAK_TO_DEBUGGER();
2787 		dm_error("DC: failed to create mpc!\n");
2788 		goto create_fail;
2789 	}
2790 
2791 	for (i = 0; i < pool->base.res_cap->num_dsc; i++) {
2792 		pool->base.dscs[i] = dcn30_dsc_create(ctx, i);
2793 		if (pool->base.dscs[i] == NULL) {
2794 			BREAK_TO_DEBUGGER();
2795 			dm_error("DC: failed to create display stream compressor %d!\n", i);
2796 			goto create_fail;
2797 		}
2798 	}
2799 
2800 	/* DWB and MMHUBBUB */
2801 	if (!dcn30_dwbc_create(ctx, &pool->base)) {
2802 		BREAK_TO_DEBUGGER();
2803 		dm_error("DC: failed to create dwbc!\n");
2804 		goto create_fail;
2805 	}
2806 
2807 	if (!dcn30_mmhubbub_create(ctx, &pool->base)) {
2808 		BREAK_TO_DEBUGGER();
2809 		dm_error("DC: failed to create mcif_wb!\n");
2810 		goto create_fail;
2811 	}
2812 
2813 	/* AUX and I2C */
2814 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
2815 		pool->base.engines[i] = dcn30_aux_engine_create(ctx, i);
2816 		if (pool->base.engines[i] == NULL) {
2817 			BREAK_TO_DEBUGGER();
2818 			dm_error(
2819 				"DC:failed to create aux engine!!\n");
2820 			goto create_fail;
2821 		}
2822 		pool->base.hw_i2cs[i] = dcn30_i2c_hw_create(ctx, i);
2823 		if (pool->base.hw_i2cs[i] == NULL) {
2824 			BREAK_TO_DEBUGGER();
2825 			dm_error(
2826 				"DC:failed to create hw i2c!!\n");
2827 			goto create_fail;
2828 		}
2829 		pool->base.sw_i2cs[i] = NULL;
2830 	}
2831 
2832 	/* Audio, Stream Encoders including DIG and virtual, MPC 3D LUTs */
2833 	if (!resource_construct(num_virtual_links, dc, &pool->base,
2834 			(!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment) ?
2835 			&res_create_funcs : &res_create_maximus_funcs)))
2836 		goto create_fail;
2837 
2838 	/* HW Sequencer and Plane caps */
2839 	dcn30_hw_sequencer_construct(dc);
2840 
2841 	dc->caps.max_planes =  pool->base.pipe_count;
2842 
2843 	for (i = 0; i < dc->caps.max_planes; ++i)
2844 		dc->caps.planes[i] = plane_cap;
2845 
2846 	dc->cap_funcs = cap_funcs;
2847 
2848 	return true;
2849 
2850 create_fail:
2851 
2852 	dcn30_resource_destruct(pool);
2853 
2854 	return false;
2855 }
2856 
2857 struct resource_pool *dcn30_create_resource_pool(
2858 		const struct dc_init_data *init_data,
2859 		struct dc *dc)
2860 {
2861 	struct dcn30_resource_pool *pool =
2862 		kzalloc(sizeof(struct dcn30_resource_pool), GFP_KERNEL);
2863 
2864 	if (!pool)
2865 		return NULL;
2866 
2867 	if (dcn30_resource_construct(init_data->num_virtual_links, dc, pool))
2868 		return &pool->base;
2869 
2870 	BREAK_TO_DEBUGGER();
2871 	kfree(pool);
2872 	return NULL;
2873 }
2874