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