1 /*
2 * Copyright 2016 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include <linux/slab.h>
27 
28 #include "dm_services.h"
29 #include "dc.h"
30 
31 #include "dcn10_init.h"
32 
33 #include "resource.h"
34 #include "include/irq_service_interface.h"
35 #include "dcn10_resource.h"
36 #include "dcn10_ipp.h"
37 #include "dcn10_mpc.h"
38 #include "irq/dcn10/irq_service_dcn10.h"
39 #include "dcn10_dpp.h"
40 #include "dcn10_optc.h"
41 #include "dcn10_hw_sequencer.h"
42 #include "dce110/dce110_hw_sequencer.h"
43 #include "dcn10_opp.h"
44 #include "dcn10_link_encoder.h"
45 #include "dcn10_stream_encoder.h"
46 #include "dce/dce_clock_source.h"
47 #include "dce/dce_audio.h"
48 #include "dce/dce_hwseq.h"
49 #include "virtual/virtual_stream_encoder.h"
50 #include "dce110/dce110_resource.h"
51 #include "dce112/dce112_resource.h"
52 #include "dcn10_hubp.h"
53 #include "dcn10_hubbub.h"
54 #include "dce/dce_panel_cntl.h"
55 
56 #include "soc15_hw_ip.h"
57 #include "vega10_ip_offset.h"
58 
59 #include "dcn/dcn_1_0_offset.h"
60 #include "dcn/dcn_1_0_sh_mask.h"
61 
62 #include "nbio/nbio_7_0_offset.h"
63 
64 #include "mmhub/mmhub_9_1_offset.h"
65 #include "mmhub/mmhub_9_1_sh_mask.h"
66 
67 #include "reg_helper.h"
68 #include "dce/dce_abm.h"
69 #include "dce/dce_dmcu.h"
70 #include "dce/dce_aux.h"
71 #include "dce/dce_i2c.h"
72 
73 const struct _vcs_dpi_ip_params_st dcn1_0_ip = {
74 	.rob_buffer_size_kbytes = 64,
75 	.det_buffer_size_kbytes = 164,
76 	.dpte_buffer_size_in_pte_reqs_luma = 42,
77 	.dpp_output_buffer_pixels = 2560,
78 	.opp_output_buffer_lines = 1,
79 	.pixel_chunk_size_kbytes = 8,
80 	.pte_enable = 1,
81 	.pte_chunk_size_kbytes = 2,
82 	.meta_chunk_size_kbytes = 2,
83 	.writeback_chunk_size_kbytes = 2,
84 	.line_buffer_size_bits = 589824,
85 	.max_line_buffer_lines = 12,
86 	.IsLineBufferBppFixed = 0,
87 	.LineBufferFixedBpp = -1,
88 	.writeback_luma_buffer_size_kbytes = 12,
89 	.writeback_chroma_buffer_size_kbytes = 8,
90 	.max_num_dpp = 4,
91 	.max_num_wb = 2,
92 	.max_dchub_pscl_bw_pix_per_clk = 4,
93 	.max_pscl_lb_bw_pix_per_clk = 2,
94 	.max_lb_vscl_bw_pix_per_clk = 4,
95 	.max_vscl_hscl_bw_pix_per_clk = 4,
96 	.max_hscl_ratio = 4,
97 	.max_vscl_ratio = 4,
98 	.hscl_mults = 4,
99 	.vscl_mults = 4,
100 	.max_hscl_taps = 8,
101 	.max_vscl_taps = 8,
102 	.dispclk_ramp_margin_percent = 1,
103 	.underscan_factor = 1.10,
104 	.min_vblank_lines = 14,
105 	.dppclk_delay_subtotal = 90,
106 	.dispclk_delay_subtotal = 42,
107 	.dcfclk_cstate_latency = 10,
108 	.max_inter_dcn_tile_repeaters = 8,
109 	.can_vstartup_lines_exceed_vsync_plus_back_porch_lines_minus_one = 0,
110 	.bug_forcing_LC_req_same_size_fixed = 0,
111 };
112 
113 const struct _vcs_dpi_soc_bounding_box_st dcn1_0_soc = {
114 	.sr_exit_time_us = 9.0,
115 	.sr_enter_plus_exit_time_us = 11.0,
116 	.urgent_latency_us = 4.0,
117 	.writeback_latency_us = 12.0,
118 	.ideal_dram_bw_after_urgent_percent = 80.0,
119 	.max_request_size_bytes = 256,
120 	.downspread_percent = 0.5,
121 	.dram_page_open_time_ns = 50.0,
122 	.dram_rw_turnaround_time_ns = 17.5,
123 	.dram_return_buffer_per_channel_bytes = 8192,
124 	.round_trip_ping_latency_dcfclk_cycles = 128,
125 	.urgent_out_of_order_return_per_channel_bytes = 256,
126 	.channel_interleave_bytes = 256,
127 	.num_banks = 8,
128 	.num_chans = 2,
129 	.vmm_page_size_bytes = 4096,
130 	.dram_clock_change_latency_us = 17.0,
131 	.writeback_dram_clock_change_latency_us = 23.0,
132 	.return_bus_width_bytes = 64,
133 };
134 
135 #ifndef mmDP0_DP_DPHY_INTERNAL_CTRL
136 	#define mmDP0_DP_DPHY_INTERNAL_CTRL		0x210f
137 	#define mmDP0_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
138 	#define mmDP1_DP_DPHY_INTERNAL_CTRL		0x220f
139 	#define mmDP1_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
140 	#define mmDP2_DP_DPHY_INTERNAL_CTRL		0x230f
141 	#define mmDP2_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
142 	#define mmDP3_DP_DPHY_INTERNAL_CTRL		0x240f
143 	#define mmDP3_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
144 	#define mmDP4_DP_DPHY_INTERNAL_CTRL		0x250f
145 	#define mmDP4_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
146 	#define mmDP5_DP_DPHY_INTERNAL_CTRL		0x260f
147 	#define mmDP5_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
148 	#define mmDP6_DP_DPHY_INTERNAL_CTRL		0x270f
149 	#define mmDP6_DP_DPHY_INTERNAL_CTRL_BASE_IDX	2
150 #endif
151 
152 
153 enum dcn10_clk_src_array_id {
154 	DCN10_CLK_SRC_PLL0,
155 	DCN10_CLK_SRC_PLL1,
156 	DCN10_CLK_SRC_PLL2,
157 	DCN10_CLK_SRC_PLL3,
158 	DCN10_CLK_SRC_TOTAL,
159 	DCN101_CLK_SRC_TOTAL = DCN10_CLK_SRC_PLL3
160 };
161 
162 /* begin *********************
163  * macros to expend register list macro defined in HW object header file */
164 
165 /* DCN */
166 #define BASE_INNER(seg) \
167 	DCE_BASE__INST0_SEG ## seg
168 
169 #define BASE(seg) \
170 	BASE_INNER(seg)
171 
172 #define SR(reg_name)\
173 		.reg_name = BASE(mm ## reg_name ## _BASE_IDX) +  \
174 					mm ## reg_name
175 
176 #define SRI(reg_name, block, id)\
177 	.reg_name = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
178 					mm ## block ## id ## _ ## reg_name
179 
180 
181 #define SRII(reg_name, block, id)\
182 	.reg_name[id] = BASE(mm ## block ## id ## _ ## reg_name ## _BASE_IDX) + \
183 					mm ## block ## id ## _ ## reg_name
184 
185 #define VUPDATE_SRII(reg_name, block, id)\
186 	.reg_name[id] = BASE(mm ## reg_name ## 0 ## _ ## block ## id ## _BASE_IDX) + \
187 					mm ## reg_name ## 0 ## _ ## block ## id
188 
189 /* set field/register/bitfield name */
190 #define SFRB(field_name, reg_name, bitfield, post_fix)\
191 	.field_name = reg_name ## __ ## bitfield ## post_fix
192 
193 /* NBIO */
194 #define NBIO_BASE_INNER(seg) \
195 	NBIF_BASE__INST0_SEG ## seg
196 
197 #define NBIO_BASE(seg) \
198 	NBIO_BASE_INNER(seg)
199 
200 #define NBIO_SR(reg_name)\
201 		.reg_name = NBIO_BASE(mm ## reg_name ## _BASE_IDX) +  \
202 					mm ## reg_name
203 
204 /* MMHUB */
205 #define MMHUB_BASE_INNER(seg) \
206 	MMHUB_BASE__INST0_SEG ## seg
207 
208 #define MMHUB_BASE(seg) \
209 	MMHUB_BASE_INNER(seg)
210 
211 #define MMHUB_SR(reg_name)\
212 		.reg_name = MMHUB_BASE(mm ## reg_name ## _BASE_IDX) +  \
213 					mm ## reg_name
214 
215 /* macros to expend register list macro defined in HW object header file
216  * end *********************/
217 
218 
219 static const struct dce_dmcu_registers dmcu_regs = {
220 		DMCU_DCN10_REG_LIST()
221 };
222 
223 static const struct dce_dmcu_shift dmcu_shift = {
224 		DMCU_MASK_SH_LIST_DCN10(__SHIFT)
225 };
226 
227 static const struct dce_dmcu_mask dmcu_mask = {
228 		DMCU_MASK_SH_LIST_DCN10(_MASK)
229 };
230 
231 static const struct dce_abm_registers abm_regs = {
232 		ABM_DCN10_REG_LIST(0)
233 };
234 
235 static const struct dce_abm_shift abm_shift = {
236 		ABM_MASK_SH_LIST_DCN10(__SHIFT)
237 };
238 
239 static const struct dce_abm_mask abm_mask = {
240 		ABM_MASK_SH_LIST_DCN10(_MASK)
241 };
242 
243 #define stream_enc_regs(id)\
244 [id] = {\
245 	SE_DCN_REG_LIST(id)\
246 }
247 
248 static const struct dcn10_stream_enc_registers stream_enc_regs[] = {
249 	stream_enc_regs(0),
250 	stream_enc_regs(1),
251 	stream_enc_regs(2),
252 	stream_enc_regs(3),
253 };
254 
255 static const struct dcn10_stream_encoder_shift se_shift = {
256 		SE_COMMON_MASK_SH_LIST_DCN10(__SHIFT)
257 };
258 
259 static const struct dcn10_stream_encoder_mask se_mask = {
260 		SE_COMMON_MASK_SH_LIST_DCN10(_MASK)
261 };
262 
263 #define audio_regs(id)\
264 [id] = {\
265 		AUD_COMMON_REG_LIST(id)\
266 }
267 
268 static const struct dce_audio_registers audio_regs[] = {
269 	audio_regs(0),
270 	audio_regs(1),
271 	audio_regs(2),
272 	audio_regs(3),
273 };
274 
275 #define DCE120_AUD_COMMON_MASK_SH_LIST(mask_sh)\
276 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_INDEX, AZALIA_ENDPOINT_REG_INDEX, mask_sh),\
277 		SF(AZF0ENDPOINT0_AZALIA_F0_CODEC_ENDPOINT_DATA, AZALIA_ENDPOINT_REG_DATA, mask_sh),\
278 		AUD_COMMON_MASK_SH_LIST_BASE(mask_sh)
279 
280 static const struct dce_audio_shift audio_shift = {
281 		DCE120_AUD_COMMON_MASK_SH_LIST(__SHIFT)
282 };
283 
284 static const struct dce_audio_mask audio_mask = {
285 		DCE120_AUD_COMMON_MASK_SH_LIST(_MASK)
286 };
287 
288 #define aux_regs(id)\
289 [id] = {\
290 	AUX_REG_LIST(id)\
291 }
292 
293 static const struct dcn10_link_enc_aux_registers link_enc_aux_regs[] = {
294 		aux_regs(0),
295 		aux_regs(1),
296 		aux_regs(2),
297 		aux_regs(3)
298 };
299 
300 #define hpd_regs(id)\
301 [id] = {\
302 	HPD_REG_LIST(id)\
303 }
304 
305 static const struct dcn10_link_enc_hpd_registers link_enc_hpd_regs[] = {
306 		hpd_regs(0),
307 		hpd_regs(1),
308 		hpd_regs(2),
309 		hpd_regs(3)
310 };
311 
312 #define link_regs(id)\
313 [id] = {\
314 	LE_DCN10_REG_LIST(id), \
315 	SRI(DP_DPHY_INTERNAL_CTRL, DP, id) \
316 }
317 
318 static const struct dcn10_link_enc_registers link_enc_regs[] = {
319 	link_regs(0),
320 	link_regs(1),
321 	link_regs(2),
322 	link_regs(3)
323 };
324 
325 static const struct dcn10_link_enc_shift le_shift = {
326 		LINK_ENCODER_MASK_SH_LIST_DCN10(__SHIFT)
327 };
328 
329 static const struct dcn10_link_enc_mask le_mask = {
330 		LINK_ENCODER_MASK_SH_LIST_DCN10(_MASK)
331 };
332 
333 static const struct dce_panel_cntl_registers panel_cntl_regs[] = {
334 	{ DCN_PANEL_CNTL_REG_LIST() }
335 };
336 
337 static const struct dce_panel_cntl_shift panel_cntl_shift = {
338 	DCE_PANEL_CNTL_MASK_SH_LIST(__SHIFT)
339 };
340 
341 static const struct dce_panel_cntl_mask panel_cntl_mask = {
342 	DCE_PANEL_CNTL_MASK_SH_LIST(_MASK)
343 };
344 
345 static const struct dce110_aux_registers_shift aux_shift = {
346 	DCN10_AUX_MASK_SH_LIST(__SHIFT)
347 };
348 
349 static const struct dce110_aux_registers_mask aux_mask = {
350 	DCN10_AUX_MASK_SH_LIST(_MASK)
351 };
352 
353 #define ipp_regs(id)\
354 [id] = {\
355 	IPP_REG_LIST_DCN10(id),\
356 }
357 
358 static const struct dcn10_ipp_registers ipp_regs[] = {
359 	ipp_regs(0),
360 	ipp_regs(1),
361 	ipp_regs(2),
362 	ipp_regs(3),
363 };
364 
365 static const struct dcn10_ipp_shift ipp_shift = {
366 		IPP_MASK_SH_LIST_DCN10(__SHIFT)
367 };
368 
369 static const struct dcn10_ipp_mask ipp_mask = {
370 		IPP_MASK_SH_LIST_DCN10(_MASK),
371 };
372 
373 #define opp_regs(id)\
374 [id] = {\
375 	OPP_REG_LIST_DCN10(id),\
376 }
377 
378 static const struct dcn10_opp_registers opp_regs[] = {
379 	opp_regs(0),
380 	opp_regs(1),
381 	opp_regs(2),
382 	opp_regs(3),
383 };
384 
385 static const struct dcn10_opp_shift opp_shift = {
386 		OPP_MASK_SH_LIST_DCN10(__SHIFT)
387 };
388 
389 static const struct dcn10_opp_mask opp_mask = {
390 		OPP_MASK_SH_LIST_DCN10(_MASK),
391 };
392 
393 #define aux_engine_regs(id)\
394 [id] = {\
395 	AUX_COMMON_REG_LIST(id), \
396 	.AUX_RESET_MASK = 0 \
397 }
398 
399 static const struct dce110_aux_registers aux_engine_regs[] = {
400 		aux_engine_regs(0),
401 		aux_engine_regs(1),
402 		aux_engine_regs(2),
403 		aux_engine_regs(3),
404 		aux_engine_regs(4),
405 		aux_engine_regs(5)
406 };
407 
408 #define tf_regs(id)\
409 [id] = {\
410 	TF_REG_LIST_DCN10(id),\
411 }
412 
413 static const struct dcn_dpp_registers tf_regs[] = {
414 	tf_regs(0),
415 	tf_regs(1),
416 	tf_regs(2),
417 	tf_regs(3),
418 };
419 
420 static const struct dcn_dpp_shift tf_shift = {
421 	TF_REG_LIST_SH_MASK_DCN10(__SHIFT),
422 	TF_DEBUG_REG_LIST_SH_DCN10
423 
424 };
425 
426 static const struct dcn_dpp_mask tf_mask = {
427 	TF_REG_LIST_SH_MASK_DCN10(_MASK),
428 	TF_DEBUG_REG_LIST_MASK_DCN10
429 };
430 
431 static const struct dcn_mpc_registers mpc_regs = {
432 		MPC_COMMON_REG_LIST_DCN1_0(0),
433 		MPC_COMMON_REG_LIST_DCN1_0(1),
434 		MPC_COMMON_REG_LIST_DCN1_0(2),
435 		MPC_COMMON_REG_LIST_DCN1_0(3),
436 		MPC_OUT_MUX_COMMON_REG_LIST_DCN1_0(0),
437 		MPC_OUT_MUX_COMMON_REG_LIST_DCN1_0(1),
438 		MPC_OUT_MUX_COMMON_REG_LIST_DCN1_0(2),
439 		MPC_OUT_MUX_COMMON_REG_LIST_DCN1_0(3)
440 };
441 
442 static const struct dcn_mpc_shift mpc_shift = {
443 	MPC_COMMON_MASK_SH_LIST_DCN1_0(__SHIFT),\
444 	SFRB(CUR_VUPDATE_LOCK_SET, CUR0_VUPDATE_LOCK_SET0, CUR0_VUPDATE_LOCK_SET, __SHIFT)
445 };
446 
447 static const struct dcn_mpc_mask mpc_mask = {
448 	MPC_COMMON_MASK_SH_LIST_DCN1_0(_MASK),\
449 	SFRB(CUR_VUPDATE_LOCK_SET, CUR0_VUPDATE_LOCK_SET0, CUR0_VUPDATE_LOCK_SET, _MASK)
450 };
451 
452 #define tg_regs(id)\
453 [id] = {TG_COMMON_REG_LIST_DCN1_0(id)}
454 
455 static const struct dcn_optc_registers tg_regs[] = {
456 	tg_regs(0),
457 	tg_regs(1),
458 	tg_regs(2),
459 	tg_regs(3),
460 };
461 
462 static const struct dcn_optc_shift tg_shift = {
463 	TG_COMMON_MASK_SH_LIST_DCN1_0(__SHIFT)
464 };
465 
466 static const struct dcn_optc_mask tg_mask = {
467 	TG_COMMON_MASK_SH_LIST_DCN1_0(_MASK)
468 };
469 
470 static const struct bios_registers bios_regs = {
471 		NBIO_SR(BIOS_SCRATCH_3),
472 		NBIO_SR(BIOS_SCRATCH_6)
473 };
474 
475 #define hubp_regs(id)\
476 [id] = {\
477 	HUBP_REG_LIST_DCN10(id)\
478 }
479 
480 static const struct dcn_mi_registers hubp_regs[] = {
481 	hubp_regs(0),
482 	hubp_regs(1),
483 	hubp_regs(2),
484 	hubp_regs(3),
485 };
486 
487 static const struct dcn_mi_shift hubp_shift = {
488 		HUBP_MASK_SH_LIST_DCN10(__SHIFT)
489 };
490 
491 static const struct dcn_mi_mask hubp_mask = {
492 		HUBP_MASK_SH_LIST_DCN10(_MASK)
493 };
494 
495 static const struct dcn_hubbub_registers hubbub_reg = {
496 		HUBBUB_REG_LIST_DCN10(0)
497 };
498 
499 static const struct dcn_hubbub_shift hubbub_shift = {
500 		HUBBUB_MASK_SH_LIST_DCN10(__SHIFT)
501 };
502 
503 static const struct dcn_hubbub_mask hubbub_mask = {
504 		HUBBUB_MASK_SH_LIST_DCN10(_MASK)
505 };
506 
507 static int map_transmitter_id_to_phy_instance(
508 	enum transmitter transmitter)
509 {
510 	switch (transmitter) {
511 	case TRANSMITTER_UNIPHY_A:
512 		return 0;
513 	break;
514 	case TRANSMITTER_UNIPHY_B:
515 		return 1;
516 	break;
517 	case TRANSMITTER_UNIPHY_C:
518 		return 2;
519 	break;
520 	case TRANSMITTER_UNIPHY_D:
521 		return 3;
522 	break;
523 	default:
524 		ASSERT(0);
525 		return 0;
526 	}
527 }
528 
529 #define clk_src_regs(index, pllid)\
530 [index] = {\
531 	CS_COMMON_REG_LIST_DCN1_0(index, pllid),\
532 }
533 
534 static const struct dce110_clk_src_regs clk_src_regs[] = {
535 	clk_src_regs(0, A),
536 	clk_src_regs(1, B),
537 	clk_src_regs(2, C),
538 	clk_src_regs(3, D)
539 };
540 
541 static const struct dce110_clk_src_shift cs_shift = {
542 		CS_COMMON_MASK_SH_LIST_DCN1_0(__SHIFT)
543 };
544 
545 static const struct dce110_clk_src_mask cs_mask = {
546 		CS_COMMON_MASK_SH_LIST_DCN1_0(_MASK)
547 };
548 
549 static const struct resource_caps res_cap = {
550 		.num_timing_generator = 4,
551 		.num_opp = 4,
552 		.num_video_plane = 4,
553 		.num_audio = 4,
554 		.num_stream_encoder = 4,
555 		.num_pll = 4,
556 		.num_ddc = 4,
557 };
558 
559 static const struct resource_caps rv2_res_cap = {
560 		.num_timing_generator = 3,
561 		.num_opp = 3,
562 		.num_video_plane = 3,
563 		.num_audio = 3,
564 		.num_stream_encoder = 3,
565 		.num_pll = 3,
566 		.num_ddc = 4,
567 };
568 
569 static const struct dc_plane_cap plane_cap = {
570 	.type = DC_PLANE_TYPE_DCN_UNIVERSAL,
571 	.blends_with_above = true,
572 	.blends_with_below = true,
573 	.per_pixel_alpha = true,
574 
575 	.pixel_format_support = {
576 			.argb8888 = true,
577 			.nv12 = true,
578 			.fp16 = true,
579 			.p010 = true
580 	},
581 
582 	.max_upscale_factor = {
583 			.argb8888 = 16000,
584 			.nv12 = 16000,
585 			.fp16 = 1
586 	},
587 
588 	.max_downscale_factor = {
589 			.argb8888 = 250,
590 			.nv12 = 250,
591 			.fp16 = 1
592 	}
593 };
594 
595 static const struct dc_debug_options debug_defaults_drv = {
596 		.sanity_checks = true,
597 		.disable_dmcu = false,
598 		.force_abm_enable = false,
599 		.timing_trace = false,
600 		.clock_trace = true,
601 
602 		/* raven smu dones't allow 0 disp clk,
603 		 * smu min disp clk limit is 50Mhz
604 		 * keep min disp clk 100Mhz avoid smu hang
605 		 */
606 		.min_disp_clk_khz = 100000,
607 
608 		.disable_pplib_clock_request = false,
609 		.disable_pplib_wm_range = false,
610 		.pplib_wm_report_mode = WM_REPORT_DEFAULT,
611 		.pipe_split_policy = MPC_SPLIT_AVOID,
612 		.force_single_disp_pipe_split = false,
613 		.disable_dcc = DCC_ENABLE,
614 		.voltage_align_fclk = true,
615 		.disable_stereo_support = true,
616 		.vsr_support = true,
617 		.performance_trace = false,
618 		.az_endpoint_mute_only = true,
619 		.recovery_enabled = false, /*enable this by default after testing.*/
620 		.max_downscale_src_width = 3840,
621 		.underflow_assert_delay_us = 0xFFFFFFFF,
622 };
623 
624 static const struct dc_debug_options debug_defaults_diags = {
625 		.disable_dmcu = false,
626 		.force_abm_enable = false,
627 		.timing_trace = true,
628 		.clock_trace = true,
629 		.disable_stutter = true,
630 		.disable_pplib_clock_request = true,
631 		.disable_pplib_wm_range = true,
632 		.underflow_assert_delay_us = 0xFFFFFFFF,
633 };
634 
635 static void dcn10_dpp_destroy(struct dpp **dpp)
636 {
637 	kfree(TO_DCN10_DPP(*dpp));
638 	*dpp = NULL;
639 }
640 
641 static struct dpp *dcn10_dpp_create(
642 	struct dc_context *ctx,
643 	uint32_t inst)
644 {
645 	struct dcn10_dpp *dpp =
646 		kzalloc(sizeof(struct dcn10_dpp), GFP_KERNEL);
647 
648 	if (!dpp)
649 		return NULL;
650 
651 	dpp1_construct(dpp, ctx, inst,
652 		       &tf_regs[inst], &tf_shift, &tf_mask);
653 	return &dpp->base;
654 }
655 
656 static struct input_pixel_processor *dcn10_ipp_create(
657 	struct dc_context *ctx, uint32_t inst)
658 {
659 	struct dcn10_ipp *ipp =
660 		kzalloc(sizeof(struct dcn10_ipp), GFP_KERNEL);
661 
662 	if (!ipp) {
663 		BREAK_TO_DEBUGGER();
664 		return NULL;
665 	}
666 
667 	dcn10_ipp_construct(ipp, ctx, inst,
668 			&ipp_regs[inst], &ipp_shift, &ipp_mask);
669 	return &ipp->base;
670 }
671 
672 
673 static struct output_pixel_processor *dcn10_opp_create(
674 	struct dc_context *ctx, uint32_t inst)
675 {
676 	struct dcn10_opp *opp =
677 		kzalloc(sizeof(struct dcn10_opp), GFP_KERNEL);
678 
679 	if (!opp) {
680 		BREAK_TO_DEBUGGER();
681 		return NULL;
682 	}
683 
684 	dcn10_opp_construct(opp, ctx, inst,
685 			&opp_regs[inst], &opp_shift, &opp_mask);
686 	return &opp->base;
687 }
688 
689 static struct dce_aux *dcn10_aux_engine_create(struct dc_context *ctx,
690 					       uint32_t inst)
691 {
692 	struct aux_engine_dce110 *aux_engine =
693 		kzalloc(sizeof(struct aux_engine_dce110), GFP_KERNEL);
694 
695 	if (!aux_engine)
696 		return NULL;
697 
698 	dce110_aux_engine_construct(aux_engine, ctx, inst,
699 				    SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD,
700 				    &aux_engine_regs[inst],
701 					&aux_mask,
702 					&aux_shift,
703 					ctx->dc->caps.extended_aux_timeout_support);
704 
705 	return &aux_engine->base;
706 }
707 #define i2c_inst_regs(id) { I2C_HW_ENGINE_COMMON_REG_LIST(id) }
708 
709 static const struct dce_i2c_registers i2c_hw_regs[] = {
710 		i2c_inst_regs(1),
711 		i2c_inst_regs(2),
712 		i2c_inst_regs(3),
713 		i2c_inst_regs(4),
714 		i2c_inst_regs(5),
715 		i2c_inst_regs(6),
716 };
717 
718 static const struct dce_i2c_shift i2c_shifts = {
719 		I2C_COMMON_MASK_SH_LIST_DCE110(__SHIFT)
720 };
721 
722 static const struct dce_i2c_mask i2c_masks = {
723 		I2C_COMMON_MASK_SH_LIST_DCE110(_MASK)
724 };
725 
726 static struct dce_i2c_hw *dcn10_i2c_hw_create(struct dc_context *ctx,
727 					      uint32_t inst)
728 {
729 	struct dce_i2c_hw *dce_i2c_hw =
730 		kzalloc(sizeof(struct dce_i2c_hw), GFP_KERNEL);
731 
732 	if (!dce_i2c_hw)
733 		return NULL;
734 
735 	dcn1_i2c_hw_construct(dce_i2c_hw, ctx, inst,
736 				    &i2c_hw_regs[inst], &i2c_shifts, &i2c_masks);
737 
738 	return dce_i2c_hw;
739 }
740 static struct mpc *dcn10_mpc_create(struct dc_context *ctx)
741 {
742 	struct dcn10_mpc *mpc10 = kzalloc(sizeof(struct dcn10_mpc),
743 					  GFP_KERNEL);
744 
745 	if (!mpc10)
746 		return NULL;
747 
748 	dcn10_mpc_construct(mpc10, ctx,
749 			&mpc_regs,
750 			&mpc_shift,
751 			&mpc_mask,
752 			4);
753 
754 	return &mpc10->base;
755 }
756 
757 static struct hubbub *dcn10_hubbub_create(struct dc_context *ctx)
758 {
759 	struct dcn10_hubbub *dcn10_hubbub = kzalloc(sizeof(struct dcn10_hubbub),
760 					  GFP_KERNEL);
761 
762 	if (!dcn10_hubbub)
763 		return NULL;
764 
765 	hubbub1_construct(&dcn10_hubbub->base, ctx,
766 			&hubbub_reg,
767 			&hubbub_shift,
768 			&hubbub_mask);
769 
770 	return &dcn10_hubbub->base;
771 }
772 
773 static struct timing_generator *dcn10_timing_generator_create(
774 		struct dc_context *ctx,
775 		uint32_t instance)
776 {
777 	struct optc *tgn10 =
778 		kzalloc(sizeof(struct optc), GFP_KERNEL);
779 
780 	if (!tgn10)
781 		return NULL;
782 
783 	tgn10->base.inst = instance;
784 	tgn10->base.ctx = ctx;
785 
786 	tgn10->tg_regs = &tg_regs[instance];
787 	tgn10->tg_shift = &tg_shift;
788 	tgn10->tg_mask = &tg_mask;
789 
790 	dcn10_timing_generator_init(tgn10);
791 
792 	return &tgn10->base;
793 }
794 
795 static const struct encoder_feature_support link_enc_feature = {
796 		.max_hdmi_deep_color = COLOR_DEPTH_121212,
797 		.max_hdmi_pixel_clock = 600000,
798 		.hdmi_ycbcr420_supported = true,
799 		.dp_ycbcr420_supported = true,
800 		.flags.bits.IS_HBR2_CAPABLE = true,
801 		.flags.bits.IS_HBR3_CAPABLE = true,
802 		.flags.bits.IS_TPS3_CAPABLE = true,
803 		.flags.bits.IS_TPS4_CAPABLE = true
804 };
805 
806 static struct link_encoder *dcn10_link_encoder_create(
807 	const struct encoder_init_data *enc_init_data)
808 {
809 	struct dcn10_link_encoder *enc10 =
810 		kzalloc(sizeof(struct dcn10_link_encoder), GFP_KERNEL);
811 	int link_regs_id;
812 
813 	if (!enc10)
814 		return NULL;
815 
816 	link_regs_id =
817 		map_transmitter_id_to_phy_instance(enc_init_data->transmitter);
818 
819 	dcn10_link_encoder_construct(enc10,
820 				      enc_init_data,
821 				      &link_enc_feature,
822 				      &link_enc_regs[link_regs_id],
823 				      &link_enc_aux_regs[enc_init_data->channel - 1],
824 				      &link_enc_hpd_regs[enc_init_data->hpd_source],
825 				      &le_shift,
826 				      &le_mask);
827 
828 	return &enc10->base;
829 }
830 
831 static struct panel_cntl *dcn10_panel_cntl_create(const struct panel_cntl_init_data *init_data)
832 {
833 	struct dce_panel_cntl *panel_cntl =
834 		kzalloc(sizeof(struct dce_panel_cntl), GFP_KERNEL);
835 
836 	if (!panel_cntl)
837 		return NULL;
838 
839 	dce_panel_cntl_construct(panel_cntl,
840 			init_data,
841 			&panel_cntl_regs[init_data->inst],
842 			&panel_cntl_shift,
843 			&panel_cntl_mask);
844 
845 	return &panel_cntl->base;
846 }
847 
848 static struct clock_source *dcn10_clock_source_create(
849 	struct dc_context *ctx,
850 	struct dc_bios *bios,
851 	enum clock_source_id id,
852 	const struct dce110_clk_src_regs *regs,
853 	bool dp_clk_src)
854 {
855 	struct dce110_clk_src *clk_src =
856 		kzalloc(sizeof(struct dce110_clk_src), GFP_KERNEL);
857 
858 	if (!clk_src)
859 		return NULL;
860 
861 	if (dce112_clk_src_construct(clk_src, ctx, bios, id,
862 			regs, &cs_shift, &cs_mask)) {
863 		clk_src->base.dp_clk_src = dp_clk_src;
864 		return &clk_src->base;
865 	}
866 
867 	kfree(clk_src);
868 	BREAK_TO_DEBUGGER();
869 	return NULL;
870 }
871 
872 static void read_dce_straps(
873 	struct dc_context *ctx,
874 	struct resource_straps *straps)
875 {
876 	generic_reg_get(ctx, mmDC_PINSTRAPS + BASE(mmDC_PINSTRAPS_BASE_IDX),
877 		FN(DC_PINSTRAPS, DC_PINSTRAPS_AUDIO), &straps->dc_pinstraps_audio);
878 }
879 
880 static struct audio *create_audio(
881 		struct dc_context *ctx, unsigned int inst)
882 {
883 	return dce_audio_create(ctx, inst,
884 			&audio_regs[inst], &audio_shift, &audio_mask);
885 }
886 
887 static struct stream_encoder *dcn10_stream_encoder_create(
888 	enum engine_id eng_id,
889 	struct dc_context *ctx)
890 {
891 	struct dcn10_stream_encoder *enc1 =
892 		kzalloc(sizeof(struct dcn10_stream_encoder), GFP_KERNEL);
893 
894 	if (!enc1)
895 		return NULL;
896 
897 	dcn10_stream_encoder_construct(enc1, ctx, ctx->dc_bios, eng_id,
898 					&stream_enc_regs[eng_id],
899 					&se_shift, &se_mask);
900 	return &enc1->base;
901 }
902 
903 static const struct dce_hwseq_registers hwseq_reg = {
904 		HWSEQ_DCN1_REG_LIST()
905 };
906 
907 static const struct dce_hwseq_shift hwseq_shift = {
908 		HWSEQ_DCN1_MASK_SH_LIST(__SHIFT)
909 };
910 
911 static const struct dce_hwseq_mask hwseq_mask = {
912 		HWSEQ_DCN1_MASK_SH_LIST(_MASK)
913 };
914 
915 static struct dce_hwseq *dcn10_hwseq_create(
916 	struct dc_context *ctx)
917 {
918 	struct dce_hwseq *hws = kzalloc(sizeof(struct dce_hwseq), GFP_KERNEL);
919 
920 	if (hws) {
921 		hws->ctx = ctx;
922 		hws->regs = &hwseq_reg;
923 		hws->shifts = &hwseq_shift;
924 		hws->masks = &hwseq_mask;
925 		hws->wa.DEGVIDCN10_253 = true;
926 		hws->wa.false_optc_underflow = true;
927 		hws->wa.DEGVIDCN10_254 = true;
928 	}
929 	return hws;
930 }
931 
932 static const struct resource_create_funcs res_create_funcs = {
933 	.read_dce_straps = read_dce_straps,
934 	.create_audio = create_audio,
935 	.create_stream_encoder = dcn10_stream_encoder_create,
936 	.create_hwseq = dcn10_hwseq_create,
937 };
938 
939 static const struct resource_create_funcs res_create_maximus_funcs = {
940 	.read_dce_straps = NULL,
941 	.create_audio = NULL,
942 	.create_stream_encoder = NULL,
943 	.create_hwseq = dcn10_hwseq_create,
944 };
945 
946 static void dcn10_clock_source_destroy(struct clock_source **clk_src)
947 {
948 	kfree(TO_DCE110_CLK_SRC(*clk_src));
949 	*clk_src = NULL;
950 }
951 
952 static struct pp_smu_funcs *dcn10_pp_smu_create(struct dc_context *ctx)
953 {
954 	struct pp_smu_funcs *pp_smu = kzalloc(sizeof(*pp_smu), GFP_KERNEL);
955 
956 	if (!pp_smu)
957 		return pp_smu;
958 
959 	dm_pp_get_funcs(ctx, pp_smu);
960 	return pp_smu;
961 }
962 
963 static void dcn10_resource_destruct(struct dcn10_resource_pool *pool)
964 {
965 	unsigned int i;
966 
967 	for (i = 0; i < pool->base.stream_enc_count; i++) {
968 		if (pool->base.stream_enc[i] != NULL) {
969 			kfree(DCN10STRENC_FROM_STRENC(pool->base.stream_enc[i]));
970 			pool->base.stream_enc[i] = NULL;
971 		}
972 	}
973 
974 	if (pool->base.mpc != NULL) {
975 		kfree(TO_DCN10_MPC(pool->base.mpc));
976 		pool->base.mpc = NULL;
977 	}
978 
979 	kfree(pool->base.hubbub);
980 	pool->base.hubbub = NULL;
981 
982 	for (i = 0; i < pool->base.pipe_count; i++) {
983 		if (pool->base.opps[i] != NULL)
984 			pool->base.opps[i]->funcs->opp_destroy(&pool->base.opps[i]);
985 
986 		if (pool->base.dpps[i] != NULL)
987 			dcn10_dpp_destroy(&pool->base.dpps[i]);
988 
989 		if (pool->base.ipps[i] != NULL)
990 			pool->base.ipps[i]->funcs->ipp_destroy(&pool->base.ipps[i]);
991 
992 		if (pool->base.hubps[i] != NULL) {
993 			kfree(TO_DCN10_HUBP(pool->base.hubps[i]));
994 			pool->base.hubps[i] = NULL;
995 		}
996 
997 		if (pool->base.irqs != NULL) {
998 			dal_irq_service_destroy(&pool->base.irqs);
999 		}
1000 
1001 		if (pool->base.timing_generators[i] != NULL)	{
1002 			kfree(DCN10TG_FROM_TG(pool->base.timing_generators[i]));
1003 			pool->base.timing_generators[i] = NULL;
1004 		}
1005 	}
1006 
1007 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1008 		if (pool->base.engines[i] != NULL)
1009 			dce110_engine_destroy(&pool->base.engines[i]);
1010 		kfree(pool->base.hw_i2cs[i]);
1011 		pool->base.hw_i2cs[i] = NULL;
1012 		kfree(pool->base.sw_i2cs[i]);
1013 		pool->base.sw_i2cs[i] = NULL;
1014 	}
1015 
1016 	for (i = 0; i < pool->base.audio_count; i++) {
1017 		if (pool->base.audios[i])
1018 			dce_aud_destroy(&pool->base.audios[i]);
1019 	}
1020 
1021 	for (i = 0; i < pool->base.clk_src_count; i++) {
1022 		if (pool->base.clock_sources[i] != NULL) {
1023 			dcn10_clock_source_destroy(&pool->base.clock_sources[i]);
1024 			pool->base.clock_sources[i] = NULL;
1025 		}
1026 	}
1027 
1028 	if (pool->base.dp_clock_source != NULL) {
1029 		dcn10_clock_source_destroy(&pool->base.dp_clock_source);
1030 		pool->base.dp_clock_source = NULL;
1031 	}
1032 
1033 	if (pool->base.abm != NULL)
1034 		dce_abm_destroy(&pool->base.abm);
1035 
1036 	if (pool->base.dmcu != NULL)
1037 		dce_dmcu_destroy(&pool->base.dmcu);
1038 
1039 	kfree(pool->base.pp_smu);
1040 }
1041 
1042 static struct hubp *dcn10_hubp_create(
1043 	struct dc_context *ctx,
1044 	uint32_t inst)
1045 {
1046 	struct dcn10_hubp *hubp1 =
1047 		kzalloc(sizeof(struct dcn10_hubp), GFP_KERNEL);
1048 
1049 	if (!hubp1)
1050 		return NULL;
1051 
1052 	dcn10_hubp_construct(hubp1, ctx, inst,
1053 			     &hubp_regs[inst], &hubp_shift, &hubp_mask);
1054 	return &hubp1->base;
1055 }
1056 
1057 static void get_pixel_clock_parameters(
1058 	const struct pipe_ctx *pipe_ctx,
1059 	struct pixel_clk_params *pixel_clk_params)
1060 {
1061 	const struct dc_stream_state *stream = pipe_ctx->stream;
1062 	pixel_clk_params->requested_pix_clk_100hz = stream->timing.pix_clk_100hz;
1063 	pixel_clk_params->encoder_object_id = stream->link->link_enc->id;
1064 	pixel_clk_params->signal_type = pipe_ctx->stream->signal;
1065 	pixel_clk_params->controller_id = pipe_ctx->stream_res.tg->inst + 1;
1066 	/* TODO: un-hardcode*/
1067 	pixel_clk_params->requested_sym_clk = LINK_RATE_LOW *
1068 		LINK_RATE_REF_FREQ_IN_KHZ;
1069 	pixel_clk_params->flags.ENABLE_SS = 0;
1070 	pixel_clk_params->color_depth =
1071 		stream->timing.display_color_depth;
1072 	pixel_clk_params->flags.DISPLAY_BLANKED = 1;
1073 	pixel_clk_params->pixel_encoding = stream->timing.pixel_encoding;
1074 
1075 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
1076 		pixel_clk_params->color_depth = COLOR_DEPTH_888;
1077 
1078 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420)
1079 		pixel_clk_params->requested_pix_clk_100hz  /= 2;
1080 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
1081 		pixel_clk_params->requested_pix_clk_100hz *= 2;
1082 
1083 }
1084 
1085 static void build_clamping_params(struct dc_stream_state *stream)
1086 {
1087 	stream->clamping.clamping_level = CLAMPING_FULL_RANGE;
1088 	stream->clamping.c_depth = stream->timing.display_color_depth;
1089 	stream->clamping.pixel_encoding = stream->timing.pixel_encoding;
1090 }
1091 
1092 static void build_pipe_hw_param(struct pipe_ctx *pipe_ctx)
1093 {
1094 
1095 	get_pixel_clock_parameters(pipe_ctx, &pipe_ctx->stream_res.pix_clk_params);
1096 
1097 	pipe_ctx->clock_source->funcs->get_pix_clk_dividers(
1098 		pipe_ctx->clock_source,
1099 		&pipe_ctx->stream_res.pix_clk_params,
1100 		&pipe_ctx->pll_settings);
1101 
1102 	pipe_ctx->stream->clamping.pixel_encoding = pipe_ctx->stream->timing.pixel_encoding;
1103 
1104 	resource_build_bit_depth_reduction_params(pipe_ctx->stream,
1105 					&pipe_ctx->stream->bit_depth_params);
1106 	build_clamping_params(pipe_ctx->stream);
1107 }
1108 
1109 static enum dc_status build_mapped_resource(
1110 		const struct dc *dc,
1111 		struct dc_state *context,
1112 		struct dc_stream_state *stream)
1113 {
1114 	struct pipe_ctx *pipe_ctx = resource_get_head_pipe_for_stream(&context->res_ctx, stream);
1115 
1116 	if (!pipe_ctx)
1117 		return DC_ERROR_UNEXPECTED;
1118 
1119 	build_pipe_hw_param(pipe_ctx);
1120 	return DC_OK;
1121 }
1122 
1123 static enum dc_status dcn10_add_stream_to_ctx(
1124 		struct dc *dc,
1125 		struct dc_state *new_ctx,
1126 		struct dc_stream_state *dc_stream)
1127 {
1128 	enum dc_status result = DC_ERROR_UNEXPECTED;
1129 
1130 	result = resource_map_pool_resources(dc, new_ctx, dc_stream);
1131 
1132 	if (result == DC_OK)
1133 		result = resource_map_phy_clock_resources(dc, new_ctx, dc_stream);
1134 
1135 
1136 	if (result == DC_OK)
1137 		result = build_mapped_resource(dc, new_ctx, dc_stream);
1138 
1139 	return result;
1140 }
1141 
1142 static struct pipe_ctx *dcn10_acquire_idle_pipe_for_layer(
1143 		struct dc_state *context,
1144 		const struct resource_pool *pool,
1145 		struct dc_stream_state *stream)
1146 {
1147 	struct resource_context *res_ctx = &context->res_ctx;
1148 	struct pipe_ctx *head_pipe = resource_get_head_pipe_for_stream(res_ctx, stream);
1149 	struct pipe_ctx *idle_pipe = find_idle_secondary_pipe(res_ctx, pool, head_pipe);
1150 
1151 	if (!head_pipe) {
1152 		ASSERT(0);
1153 		return NULL;
1154 	}
1155 
1156 	if (!idle_pipe)
1157 		return NULL;
1158 
1159 	idle_pipe->stream = head_pipe->stream;
1160 	idle_pipe->stream_res.tg = head_pipe->stream_res.tg;
1161 	idle_pipe->stream_res.abm = head_pipe->stream_res.abm;
1162 	idle_pipe->stream_res.opp = head_pipe->stream_res.opp;
1163 
1164 	idle_pipe->plane_res.hubp = pool->hubps[idle_pipe->pipe_idx];
1165 	idle_pipe->plane_res.ipp = pool->ipps[idle_pipe->pipe_idx];
1166 	idle_pipe->plane_res.dpp = pool->dpps[idle_pipe->pipe_idx];
1167 	idle_pipe->plane_res.mpcc_inst = pool->dpps[idle_pipe->pipe_idx]->inst;
1168 
1169 	return idle_pipe;
1170 }
1171 
1172 static bool dcn10_get_dcc_compression_cap(const struct dc *dc,
1173 		const struct dc_dcc_surface_param *input,
1174 		struct dc_surface_dcc_cap *output)
1175 {
1176 	return dc->res_pool->hubbub->funcs->get_dcc_compression_cap(
1177 			dc->res_pool->hubbub,
1178 			input,
1179 			output);
1180 }
1181 
1182 static void dcn10_destroy_resource_pool(struct resource_pool **pool)
1183 {
1184 	struct dcn10_resource_pool *dcn10_pool = TO_DCN10_RES_POOL(*pool);
1185 
1186 	dcn10_resource_destruct(dcn10_pool);
1187 	kfree(dcn10_pool);
1188 	*pool = NULL;
1189 }
1190 
1191 static enum dc_status dcn10_validate_plane(const struct dc_plane_state *plane_state, struct dc_caps *caps)
1192 {
1193 	if (plane_state->format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN
1194 			&& caps->max_video_width != 0
1195 			&& plane_state->src_rect.width > caps->max_video_width)
1196 		return DC_FAIL_SURFACE_VALIDATE;
1197 
1198 	return DC_OK;
1199 }
1200 
1201 static enum dc_status dcn10_validate_global(struct dc *dc, struct dc_state *context)
1202 {
1203 	int i, j;
1204 	bool video_down_scaled = false;
1205 	bool video_large = false;
1206 	bool desktop_large = false;
1207 	bool dcc_disabled = false;
1208 	bool mpo_enabled = false;
1209 
1210 	for (i = 0; i < context->stream_count; i++) {
1211 		if (context->stream_status[i].plane_count == 0)
1212 			continue;
1213 
1214 		if (context->stream_status[i].plane_count > 2)
1215 			return DC_FAIL_UNSUPPORTED_1;
1216 
1217 		if (context->stream_status[i].plane_count > 1)
1218 			mpo_enabled = true;
1219 
1220 		for (j = 0; j < context->stream_status[i].plane_count; j++) {
1221 			struct dc_plane_state *plane =
1222 				context->stream_status[i].plane_states[j];
1223 
1224 
1225 			if (plane->format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
1226 
1227 				if (plane->src_rect.width > plane->dst_rect.width ||
1228 						plane->src_rect.height > plane->dst_rect.height)
1229 					video_down_scaled = true;
1230 
1231 				if (plane->src_rect.width >= 3840)
1232 					video_large = true;
1233 
1234 			} else {
1235 				if (plane->src_rect.width >= 3840)
1236 					desktop_large = true;
1237 				if (!plane->dcc.enable)
1238 					dcc_disabled = true;
1239 			}
1240 		}
1241 	}
1242 
1243 	/* Disable MPO in multi-display configurations. */
1244 	if (context->stream_count > 1 && mpo_enabled)
1245 		return DC_FAIL_UNSUPPORTED_1;
1246 
1247 	/*
1248 	 * Workaround: On DCN10 there is UMC issue that causes underflow when
1249 	 * playing 4k video on 4k desktop with video downscaled and single channel
1250 	 * memory
1251 	 */
1252 	if (video_large && desktop_large && video_down_scaled && dcc_disabled &&
1253 			dc->dcn_soc->number_of_channels == 1)
1254 		return DC_FAIL_SURFACE_VALIDATE;
1255 
1256 	return DC_OK;
1257 }
1258 
1259 static enum dc_status dcn10_patch_unknown_plane_state(struct dc_plane_state *plane_state)
1260 {
1261 	enum surface_pixel_format surf_pix_format = plane_state->format;
1262 	unsigned int bpp = resource_pixel_format_to_bpp(surf_pix_format);
1263 
1264 	enum swizzle_mode_values swizzle = DC_SW_LINEAR;
1265 
1266 	if (bpp == 64)
1267 		swizzle = DC_SW_64KB_D;
1268 	else
1269 		swizzle = DC_SW_64KB_S;
1270 
1271 	plane_state->tiling_info.gfx9.swizzle = swizzle;
1272 	return DC_OK;
1273 }
1274 
1275 struct stream_encoder *dcn10_find_first_free_match_stream_enc_for_link(
1276 		struct resource_context *res_ctx,
1277 		const struct resource_pool *pool,
1278 		struct dc_stream_state *stream)
1279 {
1280 	int i;
1281 	int j = -1;
1282 	struct dc_link *link = stream->link;
1283 
1284 	for (i = 0; i < pool->stream_enc_count; i++) {
1285 		if (!res_ctx->is_stream_enc_acquired[i] &&
1286 				pool->stream_enc[i]) {
1287 			/* Store first available for MST second display
1288 			 * in daisy chain use case
1289 			 */
1290 			j = i;
1291 			if (link->ep_type == DISPLAY_ENDPOINT_PHY && pool->stream_enc[i]->id ==
1292 					link->link_enc->preferred_engine)
1293 				return pool->stream_enc[i];
1294 		}
1295 	}
1296 
1297 	/*
1298 	 * For CZ and later, we can allow DIG FE and BE to differ for all display types
1299 	 */
1300 
1301 	if (j >= 0)
1302 		return pool->stream_enc[j];
1303 
1304 	return NULL;
1305 }
1306 
1307 static const struct dc_cap_funcs cap_funcs = {
1308 	.get_dcc_compression_cap = dcn10_get_dcc_compression_cap
1309 };
1310 
1311 static const struct resource_funcs dcn10_res_pool_funcs = {
1312 	.destroy = dcn10_destroy_resource_pool,
1313 	.link_enc_create = dcn10_link_encoder_create,
1314 	.panel_cntl_create = dcn10_panel_cntl_create,
1315 	.validate_bandwidth = dcn10_validate_bandwidth,
1316 	.acquire_idle_pipe_for_layer = dcn10_acquire_idle_pipe_for_layer,
1317 	.validate_plane = dcn10_validate_plane,
1318 	.validate_global = dcn10_validate_global,
1319 	.add_stream_to_ctx = dcn10_add_stream_to_ctx,
1320 	.patch_unknown_plane_state = dcn10_patch_unknown_plane_state,
1321 	.find_first_free_match_stream_enc_for_link = dcn10_find_first_free_match_stream_enc_for_link
1322 };
1323 
1324 static uint32_t read_pipe_fuses(struct dc_context *ctx)
1325 {
1326 	uint32_t value = dm_read_reg_soc15(ctx, mmCC_DC_PIPE_DIS, 0);
1327 	/* RV1 support max 4 pipes */
1328 	value = value & 0xf;
1329 	return value;
1330 }
1331 
1332 /*
1333  * Some architectures don't support soft-float (e.g. aarch64), on those
1334  * this function has to be called with hardfloat enabled, make sure not
1335  * to inline it so whatever fp stuff is done stays inside
1336  */
1337 static noinline void dcn10_resource_construct_fp(
1338 	struct dc *dc)
1339 {
1340 	if (dc->ctx->dce_version == DCN_VERSION_1_01) {
1341 		struct dcn_soc_bounding_box *dcn_soc = dc->dcn_soc;
1342 		struct dcn_ip_params *dcn_ip = dc->dcn_ip;
1343 		struct display_mode_lib *dml = &dc->dml;
1344 
1345 		dml->ip.max_num_dpp = 3;
1346 		/* TODO how to handle 23.84? */
1347 		dcn_soc->dram_clock_change_latency = 23;
1348 		dcn_ip->max_num_dpp = 3;
1349 	}
1350 	if (ASICREV_IS_RV1_F0(dc->ctx->asic_id.hw_internal_rev)) {
1351 		dc->dcn_soc->urgent_latency = 3;
1352 		dc->debug.disable_dmcu = true;
1353 		dc->dcn_soc->fabric_and_dram_bandwidth_vmax0p9 = 41.60f;
1354 	}
1355 
1356 
1357 	dc->dcn_soc->number_of_channels = dc->ctx->asic_id.vram_width / ddr4_dram_width;
1358 	ASSERT(dc->dcn_soc->number_of_channels < 3);
1359 	if (dc->dcn_soc->number_of_channels == 0)/*old sbios bug*/
1360 		dc->dcn_soc->number_of_channels = 2;
1361 
1362 	if (dc->dcn_soc->number_of_channels == 1) {
1363 		dc->dcn_soc->fabric_and_dram_bandwidth_vmax0p9 = 19.2f;
1364 		dc->dcn_soc->fabric_and_dram_bandwidth_vnom0p8 = 17.066f;
1365 		dc->dcn_soc->fabric_and_dram_bandwidth_vmid0p72 = 14.933f;
1366 		dc->dcn_soc->fabric_and_dram_bandwidth_vmin0p65 = 12.8f;
1367 		if (ASICREV_IS_RV1_F0(dc->ctx->asic_id.hw_internal_rev)) {
1368 			dc->dcn_soc->fabric_and_dram_bandwidth_vmax0p9 = 20.80f;
1369 		}
1370 	}
1371 }
1372 
1373 static bool dcn10_resource_construct(
1374 	uint8_t num_virtual_links,
1375 	struct dc *dc,
1376 	struct dcn10_resource_pool *pool)
1377 {
1378 	int i;
1379 	int j;
1380 	struct dc_context *ctx = dc->ctx;
1381 	uint32_t pipe_fuses = read_pipe_fuses(ctx);
1382 
1383 	ctx->dc_bios->regs = &bios_regs;
1384 
1385 	if (ctx->dce_version == DCN_VERSION_1_01)
1386 		pool->base.res_cap = &rv2_res_cap;
1387 	else
1388 		pool->base.res_cap = &res_cap;
1389 	pool->base.funcs = &dcn10_res_pool_funcs;
1390 
1391 	/*
1392 	 * TODO fill in from actual raven resource when we create
1393 	 * more than virtual encoder
1394 	 */
1395 
1396 	/*************************************************
1397 	 *  Resource + asic cap harcoding                *
1398 	 *************************************************/
1399 	pool->base.underlay_pipe_index = NO_UNDERLAY_PIPE;
1400 
1401 	/* max pipe num for ASIC before check pipe fuses */
1402 	pool->base.pipe_count = pool->base.res_cap->num_timing_generator;
1403 
1404 	if (dc->ctx->dce_version == DCN_VERSION_1_01)
1405 		pool->base.pipe_count = 3;
1406 	dc->caps.max_video_width = 3840;
1407 	dc->caps.max_downscale_ratio = 200;
1408 	dc->caps.i2c_speed_in_khz = 100;
1409 	dc->caps.i2c_speed_in_khz_hdcp = 100; /*1.4 w/a not applied by default*/
1410 	dc->caps.max_cursor_size = 256;
1411 	dc->caps.min_horizontal_blanking_period = 80;
1412 	dc->caps.max_slave_planes = 1;
1413 	dc->caps.max_slave_yuv_planes = 1;
1414 	dc->caps.max_slave_rgb_planes = 0;
1415 	dc->caps.is_apu = true;
1416 	dc->caps.post_blend_color_processing = false;
1417 	dc->caps.extended_aux_timeout_support = false;
1418 
1419 	/* Raven DP PHY HBR2 eye diagram pattern is not stable. Use TP4 */
1420 	dc->caps.force_dp_tps4_for_cp2520 = true;
1421 
1422 	/* Color pipeline capabilities */
1423 	dc->caps.color.dpp.dcn_arch = 1;
1424 	dc->caps.color.dpp.input_lut_shared = 1;
1425 	dc->caps.color.dpp.icsc = 1;
1426 	dc->caps.color.dpp.dgam_ram = 1;
1427 	dc->caps.color.dpp.dgam_rom_caps.srgb = 1;
1428 	dc->caps.color.dpp.dgam_rom_caps.bt2020 = 1;
1429 	dc->caps.color.dpp.dgam_rom_caps.gamma2_2 = 0;
1430 	dc->caps.color.dpp.dgam_rom_caps.pq = 0;
1431 	dc->caps.color.dpp.dgam_rom_caps.hlg = 0;
1432 	dc->caps.color.dpp.post_csc = 0;
1433 	dc->caps.color.dpp.gamma_corr = 0;
1434 	dc->caps.color.dpp.dgam_rom_for_yuv = 1;
1435 
1436 	dc->caps.color.dpp.hw_3d_lut = 0;
1437 	dc->caps.color.dpp.ogam_ram = 1; // RGAM on DCN1
1438 	dc->caps.color.dpp.ogam_rom_caps.srgb = 1;
1439 	dc->caps.color.dpp.ogam_rom_caps.bt2020 = 1;
1440 	dc->caps.color.dpp.ogam_rom_caps.gamma2_2 = 0;
1441 	dc->caps.color.dpp.ogam_rom_caps.pq = 0;
1442 	dc->caps.color.dpp.ogam_rom_caps.hlg = 0;
1443 	dc->caps.color.dpp.ocsc = 1;
1444 
1445 	/* no post-blend color operations */
1446 	dc->caps.color.mpc.gamut_remap = 0;
1447 	dc->caps.color.mpc.num_3dluts = 0;
1448 	dc->caps.color.mpc.shared_3d_lut = 0;
1449 	dc->caps.color.mpc.ogam_ram = 0;
1450 	dc->caps.color.mpc.ogam_rom_caps.srgb = 0;
1451 	dc->caps.color.mpc.ogam_rom_caps.bt2020 = 0;
1452 	dc->caps.color.mpc.ogam_rom_caps.gamma2_2 = 0;
1453 	dc->caps.color.mpc.ogam_rom_caps.pq = 0;
1454 	dc->caps.color.mpc.ogam_rom_caps.hlg = 0;
1455 	dc->caps.color.mpc.ocsc = 0;
1456 
1457 	if (dc->ctx->dce_environment == DCE_ENV_PRODUCTION_DRV)
1458 		dc->debug = debug_defaults_drv;
1459 	else
1460 		dc->debug = debug_defaults_diags;
1461 
1462 	/*************************************************
1463 	 *  Create resources                             *
1464 	 *************************************************/
1465 
1466 	pool->base.clock_sources[DCN10_CLK_SRC_PLL0] =
1467 			dcn10_clock_source_create(ctx, ctx->dc_bios,
1468 				CLOCK_SOURCE_COMBO_PHY_PLL0,
1469 				&clk_src_regs[0], false);
1470 	pool->base.clock_sources[DCN10_CLK_SRC_PLL1] =
1471 			dcn10_clock_source_create(ctx, ctx->dc_bios,
1472 				CLOCK_SOURCE_COMBO_PHY_PLL1,
1473 				&clk_src_regs[1], false);
1474 	pool->base.clock_sources[DCN10_CLK_SRC_PLL2] =
1475 			dcn10_clock_source_create(ctx, ctx->dc_bios,
1476 				CLOCK_SOURCE_COMBO_PHY_PLL2,
1477 				&clk_src_regs[2], false);
1478 
1479 	if (dc->ctx->dce_version == DCN_VERSION_1_0) {
1480 		pool->base.clock_sources[DCN10_CLK_SRC_PLL3] =
1481 				dcn10_clock_source_create(ctx, ctx->dc_bios,
1482 					CLOCK_SOURCE_COMBO_PHY_PLL3,
1483 					&clk_src_regs[3], false);
1484 	}
1485 
1486 	pool->base.clk_src_count = DCN10_CLK_SRC_TOTAL;
1487 
1488 	if (dc->ctx->dce_version == DCN_VERSION_1_01)
1489 		pool->base.clk_src_count = DCN101_CLK_SRC_TOTAL;
1490 
1491 	pool->base.dp_clock_source =
1492 			dcn10_clock_source_create(ctx, ctx->dc_bios,
1493 				CLOCK_SOURCE_ID_DP_DTO,
1494 				/* todo: not reuse phy_pll registers */
1495 				&clk_src_regs[0], true);
1496 
1497 	for (i = 0; i < pool->base.clk_src_count; i++) {
1498 		if (pool->base.clock_sources[i] == NULL) {
1499 			dm_error("DC: failed to create clock sources!\n");
1500 			BREAK_TO_DEBUGGER();
1501 			goto fail;
1502 		}
1503 	}
1504 
1505 	pool->base.dmcu = dcn10_dmcu_create(ctx,
1506 			&dmcu_regs,
1507 			&dmcu_shift,
1508 			&dmcu_mask);
1509 	if (pool->base.dmcu == NULL) {
1510 		dm_error("DC: failed to create dmcu!\n");
1511 		BREAK_TO_DEBUGGER();
1512 		goto fail;
1513 	}
1514 
1515 	pool->base.abm = dce_abm_create(ctx,
1516 			&abm_regs,
1517 			&abm_shift,
1518 			&abm_mask);
1519 	if (pool->base.abm == NULL) {
1520 		dm_error("DC: failed to create abm!\n");
1521 		BREAK_TO_DEBUGGER();
1522 		goto fail;
1523 	}
1524 
1525 	dml_init_instance(&dc->dml, &dcn1_0_soc, &dcn1_0_ip, DML_PROJECT_RAVEN1);
1526 	memcpy(dc->dcn_ip, &dcn10_ip_defaults, sizeof(dcn10_ip_defaults));
1527 	memcpy(dc->dcn_soc, &dcn10_soc_defaults, sizeof(dcn10_soc_defaults));
1528 
1529 	/* Other architectures we build for build this with soft-float */
1530 	dcn10_resource_construct_fp(dc);
1531 
1532 	pool->base.pp_smu = dcn10_pp_smu_create(ctx);
1533 
1534 	/*
1535 	 * Right now SMU/PPLIB and DAL all have the AZ D3 force PME notification *
1536 	 * implemented. So AZ D3 should work.For issue 197007.                   *
1537 	 */
1538 	if (pool->base.pp_smu != NULL
1539 			&& pool->base.pp_smu->rv_funcs.set_pme_wa_enable != NULL)
1540 		dc->debug.az_endpoint_mute_only = false;
1541 
1542 	if (!dc->debug.disable_pplib_clock_request)
1543 		dcn_bw_update_from_pplib(dc);
1544 	dcn_bw_sync_calcs_and_dml(dc);
1545 	if (!dc->debug.disable_pplib_wm_range) {
1546 		dc->res_pool = &pool->base;
1547 		dcn_bw_notify_pplib_of_wm_ranges(dc);
1548 	}
1549 
1550 	{
1551 		struct irq_service_init_data init_data;
1552 		init_data.ctx = dc->ctx;
1553 		pool->base.irqs = dal_irq_service_dcn10_create(&init_data);
1554 		if (!pool->base.irqs)
1555 			goto fail;
1556 	}
1557 
1558 	/* index to valid pipe resource  */
1559 	j = 0;
1560 	/* mem input -> ipp -> dpp -> opp -> TG */
1561 	for (i = 0; i < pool->base.pipe_count; i++) {
1562 		/* if pipe is disabled, skip instance of HW pipe,
1563 		 * i.e, skip ASIC register instance
1564 		 */
1565 		if ((pipe_fuses & (1 << i)) != 0)
1566 			continue;
1567 
1568 		pool->base.hubps[j] = dcn10_hubp_create(ctx, i);
1569 		if (pool->base.hubps[j] == NULL) {
1570 			BREAK_TO_DEBUGGER();
1571 			dm_error(
1572 				"DC: failed to create memory input!\n");
1573 			goto fail;
1574 		}
1575 
1576 		pool->base.ipps[j] = dcn10_ipp_create(ctx, i);
1577 		if (pool->base.ipps[j] == NULL) {
1578 			BREAK_TO_DEBUGGER();
1579 			dm_error(
1580 				"DC: failed to create input pixel processor!\n");
1581 			goto fail;
1582 		}
1583 
1584 		pool->base.dpps[j] = dcn10_dpp_create(ctx, i);
1585 		if (pool->base.dpps[j] == NULL) {
1586 			BREAK_TO_DEBUGGER();
1587 			dm_error(
1588 				"DC: failed to create dpp!\n");
1589 			goto fail;
1590 		}
1591 
1592 		pool->base.opps[j] = dcn10_opp_create(ctx, i);
1593 		if (pool->base.opps[j] == NULL) {
1594 			BREAK_TO_DEBUGGER();
1595 			dm_error(
1596 				"DC: failed to create output pixel processor!\n");
1597 			goto fail;
1598 		}
1599 
1600 		pool->base.timing_generators[j] = dcn10_timing_generator_create(
1601 				ctx, i);
1602 		if (pool->base.timing_generators[j] == NULL) {
1603 			BREAK_TO_DEBUGGER();
1604 			dm_error("DC: failed to create tg!\n");
1605 			goto fail;
1606 		}
1607 		/* check next valid pipe */
1608 		j++;
1609 	}
1610 
1611 	for (i = 0; i < pool->base.res_cap->num_ddc; i++) {
1612 		pool->base.engines[i] = dcn10_aux_engine_create(ctx, i);
1613 		if (pool->base.engines[i] == NULL) {
1614 			BREAK_TO_DEBUGGER();
1615 			dm_error(
1616 				"DC:failed to create aux engine!!\n");
1617 			goto fail;
1618 		}
1619 		pool->base.hw_i2cs[i] = dcn10_i2c_hw_create(ctx, i);
1620 		if (pool->base.hw_i2cs[i] == NULL) {
1621 			BREAK_TO_DEBUGGER();
1622 			dm_error(
1623 				"DC:failed to create hw i2c!!\n");
1624 			goto fail;
1625 		}
1626 		pool->base.sw_i2cs[i] = NULL;
1627 	}
1628 
1629 	/* valid pipe num */
1630 	pool->base.pipe_count = j;
1631 	pool->base.timing_generator_count = j;
1632 
1633 	/* within dml lib, it is hard code to 4. If ASIC pipe is fused,
1634 	 * the value may be changed
1635 	 */
1636 	dc->dml.ip.max_num_dpp = pool->base.pipe_count;
1637 	dc->dcn_ip->max_num_dpp = pool->base.pipe_count;
1638 
1639 	pool->base.mpc = dcn10_mpc_create(ctx);
1640 	if (pool->base.mpc == NULL) {
1641 		BREAK_TO_DEBUGGER();
1642 		dm_error("DC: failed to create mpc!\n");
1643 		goto fail;
1644 	}
1645 
1646 	pool->base.hubbub = dcn10_hubbub_create(ctx);
1647 	if (pool->base.hubbub == NULL) {
1648 		BREAK_TO_DEBUGGER();
1649 		dm_error("DC: failed to create hubbub!\n");
1650 		goto fail;
1651 	}
1652 
1653 	if (!resource_construct(num_virtual_links, dc, &pool->base,
1654 			(!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment) ?
1655 			&res_create_funcs : &res_create_maximus_funcs)))
1656 			goto fail;
1657 
1658 	dcn10_hw_sequencer_construct(dc);
1659 	dc->caps.max_planes =  pool->base.pipe_count;
1660 
1661 	for (i = 0; i < dc->caps.max_planes; ++i)
1662 		dc->caps.planes[i] = plane_cap;
1663 
1664 	dc->cap_funcs = cap_funcs;
1665 
1666 	return true;
1667 
1668 fail:
1669 
1670 	dcn10_resource_destruct(pool);
1671 
1672 	return false;
1673 }
1674 
1675 struct resource_pool *dcn10_create_resource_pool(
1676 		const struct dc_init_data *init_data,
1677 		struct dc *dc)
1678 {
1679 	struct dcn10_resource_pool *pool =
1680 		kzalloc(sizeof(struct dcn10_resource_pool), GFP_KERNEL);
1681 
1682 	if (!pool)
1683 		return NULL;
1684 
1685 	if (dcn10_resource_construct(init_data->num_virtual_links, dc, pool))
1686 		return &pool->base;
1687 
1688 	kfree(pool);
1689 	BREAK_TO_DEBUGGER();
1690 	return NULL;
1691 }
1692