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 #include <linux/delay.h>
26 
27 #include "dm_services.h"
28 #include "dm_helpers.h"
29 #include "core_types.h"
30 #include "resource.h"
31 #include "dcn20/dcn20_resource.h"
32 #include "dce110/dce110_hw_sequencer.h"
33 #include "dcn10/dcn10_hw_sequencer.h"
34 #include "dcn20_hwseq.h"
35 #include "dce/dce_hwseq.h"
36 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
37 #include "dcn20/dcn20_dsc.h"
38 #endif
39 #include "abm.h"
40 #include "clk_mgr.h"
41 #include "dmcu.h"
42 #include "hubp.h"
43 #include "timing_generator.h"
44 #include "opp.h"
45 #include "ipp.h"
46 #include "mpc.h"
47 #include "mcif_wb.h"
48 #include "reg_helper.h"
49 #include "dcn10/dcn10_cm_common.h"
50 #include "dcn10/dcn10_hubbub.h"
51 #include "dcn10/dcn10_optc.h"
52 #include "dc_link_dp.h"
53 #include "vm_helper.h"
54 #include "dccg.h"
55 
56 #define DC_LOGGER_INIT(logger)
57 
58 #define CTX \
59 	hws->ctx
60 #define REG(reg)\
61 	hws->regs->reg
62 
63 #undef FN
64 #define FN(reg_name, field_name) \
65 	hws->shifts->field_name, hws->masks->field_name
66 
67 static void bios_golden_init(struct dc *dc)
68 {
69 	struct dc_bios *bp = dc->ctx->dc_bios;
70 	int i;
71 
72 	/* initialize dcn global */
73 	bp->funcs->enable_disp_power_gating(bp,
74 			CONTROLLER_ID_D0, ASIC_PIPE_INIT);
75 
76 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
77 		/* initialize dcn per pipe */
78 		bp->funcs->enable_disp_power_gating(bp,
79 				CONTROLLER_ID_D0 + i, ASIC_PIPE_DISABLE);
80 	}
81 }
82 
83 static void enable_power_gating_plane(
84 	struct dce_hwseq *hws,
85 	bool enable)
86 {
87 	bool force_on = 1; /* disable power gating */
88 
89 	if (enable)
90 		force_on = 0;
91 
92 	/* DCHUBP0/1/2/3/4/5 */
93 	REG_UPDATE(DOMAIN0_PG_CONFIG, DOMAIN0_POWER_FORCEON, force_on);
94 	REG_UPDATE(DOMAIN2_PG_CONFIG, DOMAIN2_POWER_FORCEON, force_on);
95 	REG_UPDATE(DOMAIN4_PG_CONFIG, DOMAIN4_POWER_FORCEON, force_on);
96 	REG_UPDATE(DOMAIN6_PG_CONFIG, DOMAIN6_POWER_FORCEON, force_on);
97 	REG_UPDATE(DOMAIN8_PG_CONFIG, DOMAIN8_POWER_FORCEON, force_on);
98 	/*Do not power gate DCHUB5, should be left at HW default, power on permanently*/
99 	/*REG_UPDATE(DOMAIN10_PG_CONFIG, DOMAIN10_POWER_FORCEON, force_on);*/
100 
101 	/* DPP0/1/2/3/4/5 */
102 	REG_UPDATE(DOMAIN1_PG_CONFIG, DOMAIN1_POWER_FORCEON, force_on);
103 	REG_UPDATE(DOMAIN3_PG_CONFIG, DOMAIN3_POWER_FORCEON, force_on);
104 	REG_UPDATE(DOMAIN5_PG_CONFIG, DOMAIN5_POWER_FORCEON, force_on);
105 	REG_UPDATE(DOMAIN7_PG_CONFIG, DOMAIN7_POWER_FORCEON, force_on);
106 	REG_UPDATE(DOMAIN9_PG_CONFIG, DOMAIN9_POWER_FORCEON, force_on);
107 	/*Do not power gate DPP5, should be left at HW default, power on permanently*/
108 	/*REG_UPDATE(DOMAIN11_PG_CONFIG, DOMAIN11_POWER_FORCEON, force_on);*/
109 
110 	REG_UPDATE(DOMAIN16_PG_CONFIG, DOMAIN16_POWER_FORCEON, force_on);
111 	REG_UPDATE(DOMAIN17_PG_CONFIG, DOMAIN17_POWER_FORCEON, force_on);
112 	REG_UPDATE(DOMAIN18_PG_CONFIG, DOMAIN18_POWER_FORCEON, force_on);
113 	REG_UPDATE(DOMAIN19_PG_CONFIG, DOMAIN19_POWER_FORCEON, force_on);
114 	REG_UPDATE(DOMAIN20_PG_CONFIG, DOMAIN20_POWER_FORCEON, force_on);
115 	REG_UPDATE(DOMAIN21_PG_CONFIG, DOMAIN21_POWER_FORCEON, force_on);
116 }
117 
118 static void dcn20_dccg_init(struct dce_hwseq *hws)
119 {
120 	/*
121 	 * set MICROSECOND_TIME_BASE_DIV
122 	 * 100Mhz refclk -> 0x120264
123 	 * 27Mhz refclk -> 0x12021b
124 	 * 48Mhz refclk -> 0x120230
125 	 *
126 	 */
127 	REG_WRITE(MICROSECOND_TIME_BASE_DIV, 0x120264);
128 
129 	/*
130 	 * set MILLISECOND_TIME_BASE_DIV
131 	 * 100Mhz refclk -> 0x1186a0
132 	 * 27Mhz refclk -> 0x106978
133 	 * 48Mhz refclk -> 0x10bb80
134 	 *
135 	 */
136 	REG_WRITE(MILLISECOND_TIME_BASE_DIV, 0x1186a0);
137 
138 	/* This value is dependent on the hardware pipeline delay so set once per SOC */
139 	REG_WRITE(DISPCLK_FREQ_CHANGE_CNTL, 0x801003c);
140 }
141 
142 static void disable_vga(
143 	struct dce_hwseq *hws)
144 {
145 	REG_WRITE(D1VGA_CONTROL, 0);
146 	REG_WRITE(D2VGA_CONTROL, 0);
147 	REG_WRITE(D3VGA_CONTROL, 0);
148 	REG_WRITE(D4VGA_CONTROL, 0);
149 	REG_WRITE(D5VGA_CONTROL, 0);
150 	REG_WRITE(D6VGA_CONTROL, 0);
151 }
152 
153 void dcn20_program_tripleBuffer(
154 	const struct dc *dc,
155 	struct pipe_ctx *pipe_ctx,
156 	bool enableTripleBuffer)
157 {
158 	if (pipe_ctx->plane_res.hubp && pipe_ctx->plane_res.hubp->funcs) {
159 		pipe_ctx->plane_res.hubp->funcs->hubp_enable_tripleBuffer(
160 			pipe_ctx->plane_res.hubp,
161 			enableTripleBuffer);
162 	}
163 }
164 
165 /* Blank pixel data during initialization */
166 static void dcn20_init_blank(
167 		struct dc *dc,
168 		struct timing_generator *tg)
169 {
170 	enum dc_color_space color_space;
171 	struct tg_color black_color = {0};
172 	struct output_pixel_processor *opp = NULL;
173 	struct output_pixel_processor *bottom_opp = NULL;
174 	uint32_t num_opps, opp_id_src0, opp_id_src1;
175 	uint32_t otg_active_width, otg_active_height;
176 
177 	/* program opp dpg blank color */
178 	color_space = COLOR_SPACE_SRGB;
179 	color_space_to_black_color(dc, color_space, &black_color);
180 
181 	/* get the OTG active size */
182 	tg->funcs->get_otg_active_size(tg,
183 			&otg_active_width,
184 			&otg_active_height);
185 
186 	/* get the OPTC source */
187 	tg->funcs->get_optc_source(tg, &num_opps, &opp_id_src0, &opp_id_src1);
188 	ASSERT(opp_id_src0 < dc->res_pool->res_cap->num_opp);
189 	opp = dc->res_pool->opps[opp_id_src0];
190 
191 	if (num_opps == 2) {
192 		otg_active_width = otg_active_width / 2;
193 		ASSERT(opp_id_src1 < dc->res_pool->res_cap->num_opp);
194 		bottom_opp = dc->res_pool->opps[opp_id_src1];
195 	}
196 
197 	opp->funcs->opp_set_disp_pattern_generator(
198 			opp,
199 			CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR,
200 			COLOR_DEPTH_UNDEFINED,
201 			&black_color,
202 			otg_active_width,
203 			otg_active_height);
204 
205 	if (num_opps == 2) {
206 		bottom_opp->funcs->opp_set_disp_pattern_generator(
207 				bottom_opp,
208 				CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR,
209 				COLOR_DEPTH_UNDEFINED,
210 				&black_color,
211 				otg_active_width,
212 				otg_active_height);
213 	}
214 
215 	dcn20_hwss_wait_for_blank_complete(opp);
216 }
217 
218 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
219 static void dcn20_dsc_pg_control(
220 		struct dce_hwseq *hws,
221 		unsigned int dsc_inst,
222 		bool power_on)
223 {
224 	uint32_t power_gate = power_on ? 0 : 1;
225 	uint32_t pwr_status = power_on ? 0 : 2;
226 	uint32_t org_ip_request_cntl = 0;
227 
228 	if (hws->ctx->dc->debug.disable_dsc_power_gate)
229 		return;
230 
231 	if (REG(DOMAIN16_PG_CONFIG) == 0)
232 		return;
233 
234 	REG_GET(DC_IP_REQUEST_CNTL, IP_REQUEST_EN, &org_ip_request_cntl);
235 	if (org_ip_request_cntl == 0)
236 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 1);
237 
238 	switch (dsc_inst) {
239 	case 0: /* DSC0 */
240 		REG_UPDATE(DOMAIN16_PG_CONFIG,
241 				DOMAIN16_POWER_GATE, power_gate);
242 
243 		REG_WAIT(DOMAIN16_PG_STATUS,
244 				DOMAIN16_PGFSM_PWR_STATUS, pwr_status,
245 				1, 1000);
246 		break;
247 	case 1: /* DSC1 */
248 		REG_UPDATE(DOMAIN17_PG_CONFIG,
249 				DOMAIN17_POWER_GATE, power_gate);
250 
251 		REG_WAIT(DOMAIN17_PG_STATUS,
252 				DOMAIN17_PGFSM_PWR_STATUS, pwr_status,
253 				1, 1000);
254 		break;
255 	case 2: /* DSC2 */
256 		REG_UPDATE(DOMAIN18_PG_CONFIG,
257 				DOMAIN18_POWER_GATE, power_gate);
258 
259 		REG_WAIT(DOMAIN18_PG_STATUS,
260 				DOMAIN18_PGFSM_PWR_STATUS, pwr_status,
261 				1, 1000);
262 		break;
263 	case 3: /* DSC3 */
264 		REG_UPDATE(DOMAIN19_PG_CONFIG,
265 				DOMAIN19_POWER_GATE, power_gate);
266 
267 		REG_WAIT(DOMAIN19_PG_STATUS,
268 				DOMAIN19_PGFSM_PWR_STATUS, pwr_status,
269 				1, 1000);
270 		break;
271 	case 4: /* DSC4 */
272 		REG_UPDATE(DOMAIN20_PG_CONFIG,
273 				DOMAIN20_POWER_GATE, power_gate);
274 
275 		REG_WAIT(DOMAIN20_PG_STATUS,
276 				DOMAIN20_PGFSM_PWR_STATUS, pwr_status,
277 				1, 1000);
278 		break;
279 	case 5: /* DSC5 */
280 		REG_UPDATE(DOMAIN21_PG_CONFIG,
281 				DOMAIN21_POWER_GATE, power_gate);
282 
283 		REG_WAIT(DOMAIN21_PG_STATUS,
284 				DOMAIN21_PGFSM_PWR_STATUS, pwr_status,
285 				1, 1000);
286 		break;
287 	default:
288 		BREAK_TO_DEBUGGER();
289 		break;
290 	}
291 
292 	if (org_ip_request_cntl == 0)
293 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 0);
294 }
295 #endif
296 
297 static void dcn20_dpp_pg_control(
298 		struct dce_hwseq *hws,
299 		unsigned int dpp_inst,
300 		bool power_on)
301 {
302 	uint32_t power_gate = power_on ? 0 : 1;
303 	uint32_t pwr_status = power_on ? 0 : 2;
304 
305 	if (hws->ctx->dc->debug.disable_dpp_power_gate)
306 		return;
307 	if (REG(DOMAIN1_PG_CONFIG) == 0)
308 		return;
309 
310 	switch (dpp_inst) {
311 	case 0: /* DPP0 */
312 		REG_UPDATE(DOMAIN1_PG_CONFIG,
313 				DOMAIN1_POWER_GATE, power_gate);
314 
315 		REG_WAIT(DOMAIN1_PG_STATUS,
316 				DOMAIN1_PGFSM_PWR_STATUS, pwr_status,
317 				1, 1000);
318 		break;
319 	case 1: /* DPP1 */
320 		REG_UPDATE(DOMAIN3_PG_CONFIG,
321 				DOMAIN3_POWER_GATE, power_gate);
322 
323 		REG_WAIT(DOMAIN3_PG_STATUS,
324 				DOMAIN3_PGFSM_PWR_STATUS, pwr_status,
325 				1, 1000);
326 		break;
327 	case 2: /* DPP2 */
328 		REG_UPDATE(DOMAIN5_PG_CONFIG,
329 				DOMAIN5_POWER_GATE, power_gate);
330 
331 		REG_WAIT(DOMAIN5_PG_STATUS,
332 				DOMAIN5_PGFSM_PWR_STATUS, pwr_status,
333 				1, 1000);
334 		break;
335 	case 3: /* DPP3 */
336 		REG_UPDATE(DOMAIN7_PG_CONFIG,
337 				DOMAIN7_POWER_GATE, power_gate);
338 
339 		REG_WAIT(DOMAIN7_PG_STATUS,
340 				DOMAIN7_PGFSM_PWR_STATUS, pwr_status,
341 				1, 1000);
342 		break;
343 	case 4: /* DPP4 */
344 		REG_UPDATE(DOMAIN9_PG_CONFIG,
345 				DOMAIN9_POWER_GATE, power_gate);
346 
347 		REG_WAIT(DOMAIN9_PG_STATUS,
348 				DOMAIN9_PGFSM_PWR_STATUS, pwr_status,
349 				1, 1000);
350 		break;
351 	case 5: /* DPP5 */
352 		/*
353 		 * Do not power gate DPP5, should be left at HW default, power on permanently.
354 		 * PG on Pipe5 is De-featured, attempting to put it to PG state may result in hard
355 		 * reset.
356 		 * REG_UPDATE(DOMAIN11_PG_CONFIG,
357 		 *		DOMAIN11_POWER_GATE, power_gate);
358 		 *
359 		 * REG_WAIT(DOMAIN11_PG_STATUS,
360 		 *		DOMAIN11_PGFSM_PWR_STATUS, pwr_status,
361 		 * 		1, 1000);
362 		 */
363 		break;
364 	default:
365 		BREAK_TO_DEBUGGER();
366 		break;
367 	}
368 }
369 
370 
371 static void dcn20_hubp_pg_control(
372 		struct dce_hwseq *hws,
373 		unsigned int hubp_inst,
374 		bool power_on)
375 {
376 	uint32_t power_gate = power_on ? 0 : 1;
377 	uint32_t pwr_status = power_on ? 0 : 2;
378 
379 	if (hws->ctx->dc->debug.disable_hubp_power_gate)
380 		return;
381 	if (REG(DOMAIN0_PG_CONFIG) == 0)
382 		return;
383 
384 	switch (hubp_inst) {
385 	case 0: /* DCHUBP0 */
386 		REG_UPDATE(DOMAIN0_PG_CONFIG,
387 				DOMAIN0_POWER_GATE, power_gate);
388 
389 		REG_WAIT(DOMAIN0_PG_STATUS,
390 				DOMAIN0_PGFSM_PWR_STATUS, pwr_status,
391 				1, 1000);
392 		break;
393 	case 1: /* DCHUBP1 */
394 		REG_UPDATE(DOMAIN2_PG_CONFIG,
395 				DOMAIN2_POWER_GATE, power_gate);
396 
397 		REG_WAIT(DOMAIN2_PG_STATUS,
398 				DOMAIN2_PGFSM_PWR_STATUS, pwr_status,
399 				1, 1000);
400 		break;
401 	case 2: /* DCHUBP2 */
402 		REG_UPDATE(DOMAIN4_PG_CONFIG,
403 				DOMAIN4_POWER_GATE, power_gate);
404 
405 		REG_WAIT(DOMAIN4_PG_STATUS,
406 				DOMAIN4_PGFSM_PWR_STATUS, pwr_status,
407 				1, 1000);
408 		break;
409 	case 3: /* DCHUBP3 */
410 		REG_UPDATE(DOMAIN6_PG_CONFIG,
411 				DOMAIN6_POWER_GATE, power_gate);
412 
413 		REG_WAIT(DOMAIN6_PG_STATUS,
414 				DOMAIN6_PGFSM_PWR_STATUS, pwr_status,
415 				1, 1000);
416 		break;
417 	case 4: /* DCHUBP4 */
418 		REG_UPDATE(DOMAIN8_PG_CONFIG,
419 				DOMAIN8_POWER_GATE, power_gate);
420 
421 		REG_WAIT(DOMAIN8_PG_STATUS,
422 				DOMAIN8_PGFSM_PWR_STATUS, pwr_status,
423 				1, 1000);
424 		break;
425 	case 5: /* DCHUBP5 */
426 		/*
427 		 * Do not power gate DCHUB5, should be left at HW default, power on permanently.
428 		 * PG on Pipe5 is De-featured, attempting to put it to PG state may result in hard
429 		 * reset.
430 		 * REG_UPDATE(DOMAIN10_PG_CONFIG,
431 		 *		DOMAIN10_POWER_GATE, power_gate);
432 		 *
433 		 * REG_WAIT(DOMAIN10_PG_STATUS,
434 		 *		DOMAIN10_PGFSM_PWR_STATUS, pwr_status,
435 		 *		1, 1000);
436 		 */
437 		break;
438 	default:
439 		BREAK_TO_DEBUGGER();
440 		break;
441 	}
442 }
443 
444 
445 
446 static void dcn20_plane_atomic_power_down(struct dc *dc, struct pipe_ctx *pipe_ctx)
447 {
448 	struct dce_hwseq *hws = dc->hwseq;
449 	struct dpp *dpp = pipe_ctx->plane_res.dpp;
450 
451 	DC_LOGGER_INIT(dc->ctx->logger);
452 
453 	if (REG(DC_IP_REQUEST_CNTL)) {
454 		REG_SET(DC_IP_REQUEST_CNTL, 0,
455 				IP_REQUEST_EN, 1);
456 		dcn20_dpp_pg_control(hws, dpp->inst, false);
457 		dcn20_hubp_pg_control(hws, pipe_ctx->plane_res.hubp->inst, false);
458 		dpp->funcs->dpp_reset(dpp);
459 		REG_SET(DC_IP_REQUEST_CNTL, 0,
460 				IP_REQUEST_EN, 0);
461 		DC_LOG_DEBUG(
462 				"Power gated front end %d\n", pipe_ctx->pipe_idx);
463 	}
464 }
465 
466 
467 
468 /* disable HW used by plane.
469  * note:  cannot disable until disconnect is complete
470  */
471 static void dcn20_plane_atomic_disable(struct dc *dc, struct pipe_ctx *pipe_ctx)
472 {
473 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
474 	struct dpp *dpp = pipe_ctx->plane_res.dpp;
475 
476 	dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe_ctx);
477 
478 	/* In flip immediate with pipe splitting case GSL is used for
479 	 * synchronization so we must disable it when the plane is disabled.
480 	 */
481 	if (pipe_ctx->stream_res.gsl_group != 0)
482 		dcn20_setup_gsl_group_as_lock(dc, pipe_ctx, false);
483 
484 	dc->hwss.set_flip_control_gsl(pipe_ctx, false);
485 
486 	hubp->funcs->hubp_clk_cntl(hubp, false);
487 
488 	dpp->funcs->dpp_dppclk_control(dpp, false, false);
489 
490 	hubp->power_gated = true;
491 	dc->optimized_required = false; /* We're powering off, no need to optimize */
492 
493 	dcn20_plane_atomic_power_down(dc, pipe_ctx);
494 
495 	pipe_ctx->stream = NULL;
496 	memset(&pipe_ctx->stream_res, 0, sizeof(pipe_ctx->stream_res));
497 	memset(&pipe_ctx->plane_res, 0, sizeof(pipe_ctx->plane_res));
498 	pipe_ctx->top_pipe = NULL;
499 	pipe_ctx->bottom_pipe = NULL;
500 	pipe_ctx->plane_state = NULL;
501 }
502 
503 
504 void dcn20_disable_plane(struct dc *dc, struct pipe_ctx *pipe_ctx)
505 {
506 	DC_LOGGER_INIT(dc->ctx->logger);
507 
508 	if (!pipe_ctx->plane_res.hubp || pipe_ctx->plane_res.hubp->power_gated)
509 		return;
510 
511 	dcn20_plane_atomic_disable(dc, pipe_ctx);
512 
513 	DC_LOG_DC("Power down front end %d\n",
514 					pipe_ctx->pipe_idx);
515 }
516 
517 static void dcn20_init_hw(struct dc *dc)
518 {
519 	int i, j;
520 	struct abm *abm = dc->res_pool->abm;
521 	struct dmcu *dmcu = dc->res_pool->dmcu;
522 	struct dce_hwseq *hws = dc->hwseq;
523 	struct dc_bios *dcb = dc->ctx->dc_bios;
524 	struct resource_pool *res_pool = dc->res_pool;
525 	struct dc_state  *context = dc->current_state;
526 	struct dc_firmware_info fw_info = { { 0 } };
527 
528 	if (dc->clk_mgr && dc->clk_mgr->funcs->init_clocks)
529 		dc->clk_mgr->funcs->init_clocks(dc->clk_mgr);
530 
531 	// Initialize the dccg
532 	if (res_pool->dccg->funcs->dccg_init)
533 		res_pool->dccg->funcs->dccg_init(res_pool->dccg);
534 
535 	//Enable ability to power gate / don't force power on permanently
536 	enable_power_gating_plane(dc->hwseq, true);
537 
538 	if (IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
539 		REG_WRITE(RBBMIF_TIMEOUT_DIS, 0xFFFFFFFF);
540 		REG_WRITE(RBBMIF_TIMEOUT_DIS_2, 0xFFFFFFFF);
541 
542 		dcn20_dccg_init(hws);
543 
544 		REG_UPDATE(DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_REFDIV, 2);
545 		REG_UPDATE(DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_ENABLE, 1);
546 		REG_WRITE(REFCLK_CNTL, 0);
547 	} else {
548 		if (!dcb->funcs->is_accelerated_mode(dcb)) {
549 			bios_golden_init(dc);
550 			if (dc->ctx->dc_bios->funcs->get_firmware_info(
551 					dc->ctx->dc_bios, &fw_info) == BP_RESULT_OK) {
552 				res_pool->ref_clocks.xtalin_clock_inKhz = fw_info.pll_info.crystal_frequency;
553 
554 				if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
555 					if (res_pool->dccg && res_pool->hubbub) {
556 
557 						(res_pool->dccg->funcs->get_dccg_ref_freq)(res_pool->dccg,
558 								fw_info.pll_info.crystal_frequency,
559 								&res_pool->ref_clocks.dccg_ref_clock_inKhz);
560 
561 						(res_pool->hubbub->funcs->get_dchub_ref_freq)(res_pool->hubbub,
562 								res_pool->ref_clocks.dccg_ref_clock_inKhz,
563 								&res_pool->ref_clocks.dchub_ref_clock_inKhz);
564 					} else {
565 						// Not all ASICs have DCCG sw component
566 						res_pool->ref_clocks.dccg_ref_clock_inKhz =
567 								res_pool->ref_clocks.xtalin_clock_inKhz;
568 						res_pool->ref_clocks.dchub_ref_clock_inKhz =
569 								res_pool->ref_clocks.xtalin_clock_inKhz;
570 					}
571 				}
572 			} else
573 				ASSERT_CRITICAL(false);
574 			disable_vga(dc->hwseq);
575 		}
576 
577 		for (i = 0; i < dc->link_count; i++) {
578 			/* Power up AND update implementation according to the
579 			 * required signal (which may be different from the
580 			 * default signal on connector).
581 			 */
582 			struct dc_link *link = dc->links[i];
583 
584 			link->link_enc->funcs->hw_init(link->link_enc);
585 		}
586 	}
587 
588 	/* Blank pixel data with OPP DPG */
589 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
590 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
591 
592 		if (tg->funcs->is_tg_enabled(tg)) {
593 			dcn20_init_blank(dc, tg);
594 		}
595 	}
596 
597 	for (i = 0; i < res_pool->timing_generator_count; i++) {
598 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
599 
600 		if (tg->funcs->is_tg_enabled(tg))
601 			tg->funcs->lock(tg);
602 	}
603 
604 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
605 		struct dpp *dpp = res_pool->dpps[i];
606 
607 		dpp->funcs->dpp_reset(dpp);
608 	}
609 
610 	/* Reset all MPCC muxes */
611 	res_pool->mpc->funcs->mpc_init(res_pool->mpc);
612 
613 	/* initialize OPP mpc_tree parameter */
614 	for (i = 0; i < dc->res_pool->res_cap->num_opp; i++) {
615 		res_pool->opps[i]->mpc_tree_params.opp_id = res_pool->opps[i]->inst;
616 		res_pool->opps[i]->mpc_tree_params.opp_list = NULL;
617 		for (j = 0; j < MAX_PIPES; j++)
618 			res_pool->opps[i]->mpcc_disconnect_pending[j] = false;
619 	}
620 
621 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
622 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
623 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
624 		struct hubp *hubp = dc->res_pool->hubps[i];
625 		struct dpp *dpp = dc->res_pool->dpps[i];
626 
627 		pipe_ctx->stream_res.tg = tg;
628 		pipe_ctx->pipe_idx = i;
629 
630 		pipe_ctx->plane_res.hubp = hubp;
631 		pipe_ctx->plane_res.dpp = dpp;
632 		pipe_ctx->plane_res.mpcc_inst = dpp->inst;
633 		hubp->mpcc_id = dpp->inst;
634 		hubp->opp_id = OPP_ID_INVALID;
635 		hubp->power_gated = false;
636 		pipe_ctx->stream_res.opp = NULL;
637 
638 		hubp->funcs->hubp_init(hubp);
639 
640 		//dc->res_pool->opps[i]->mpc_tree_params.opp_id = dc->res_pool->opps[i]->inst;
641 		//dc->res_pool->opps[i]->mpc_tree_params.opp_list = NULL;
642 		dc->res_pool->opps[i]->mpcc_disconnect_pending[pipe_ctx->plane_res.mpcc_inst] = true;
643 		pipe_ctx->stream_res.opp = dc->res_pool->opps[i];
644 		/*to do*/
645 		hwss1_plane_atomic_disconnect(dc, pipe_ctx);
646 	}
647 
648 	/* initialize DWB pointer to MCIF_WB */
649 	for (i = 0; i < res_pool->res_cap->num_dwb; i++)
650 		res_pool->dwbc[i]->mcif = res_pool->mcif_wb[i];
651 
652 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
653 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
654 
655 		if (tg->funcs->is_tg_enabled(tg))
656 			tg->funcs->unlock(tg);
657 	}
658 
659 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
660 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
661 
662 		dc->hwss.disable_plane(dc, pipe_ctx);
663 
664 		pipe_ctx->stream_res.tg = NULL;
665 		pipe_ctx->plane_res.hubp = NULL;
666 	}
667 
668 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
669 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
670 
671 		tg->funcs->tg_init(tg);
672 	}
673 
674 	/* end of FPGA. Below if real ASIC */
675 	if (IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment))
676 		return;
677 
678 
679 	for (i = 0; i < res_pool->audio_count; i++) {
680 		struct audio *audio = res_pool->audios[i];
681 
682 		audio->funcs->hw_init(audio);
683 	}
684 
685 	if (abm != NULL) {
686 		abm->funcs->init_backlight(abm);
687 		abm->funcs->abm_init(abm);
688 	}
689 
690 	if (dmcu != NULL)
691 		dmcu->funcs->dmcu_init(dmcu);
692 
693 	if (abm != NULL && dmcu != NULL)
694 		abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
695 
696 	/* power AFMT HDMI memory TODO: may move to dis/en output save power*/
697 	REG_WRITE(DIO_MEM_PWR_CTRL, 0);
698 
699 	if (!dc->debug.disable_clock_gate) {
700 		/* enable all DCN clock gating */
701 		REG_WRITE(DCCG_GATE_DISABLE_CNTL, 0);
702 
703 		REG_WRITE(DCCG_GATE_DISABLE_CNTL2, 0);
704 
705 		REG_UPDATE(DCFCLK_CNTL, DCFCLK_GATE_DIS, 0);
706 	}
707 
708 }
709 
710 enum dc_status dcn20_enable_stream_timing(
711 		struct pipe_ctx *pipe_ctx,
712 		struct dc_state *context,
713 		struct dc *dc)
714 {
715 	struct dc_stream_state *stream = pipe_ctx->stream;
716 	struct drr_params params = {0};
717 	unsigned int event_triggers = 0;
718 
719 
720 #if defined(CONFIG_DRM_AMD_DC_DCN2_0)
721 	struct pipe_ctx *odm_pipe = dc_res_get_odm_bottom_pipe(pipe_ctx);
722 #endif
723 
724 	/* by upper caller loop, pipe0 is parent pipe and be called first.
725 	 * back end is set up by for pipe0. Other children pipe share back end
726 	 * with pipe 0. No program is needed.
727 	 */
728 	if (pipe_ctx->top_pipe != NULL)
729 		return DC_OK;
730 
731 	/* TODO check if timing_changed, disable stream if timing changed */
732 
733 	if (odm_pipe)
734 		pipe_ctx->stream_res.tg->funcs->set_odm_combine(
735 				pipe_ctx->stream_res.tg,
736 				odm_pipe->stream_res.opp->inst,
737 				pipe_ctx->stream->timing.h_addressable/2,
738 				pipe_ctx->stream->timing.pixel_encoding);
739 	/* HW program guide assume display already disable
740 	 * by unplug sequence. OTG assume stop.
741 	 */
742 	pipe_ctx->stream_res.tg->funcs->enable_optc_clock(pipe_ctx->stream_res.tg, true);
743 
744 	if (false == pipe_ctx->clock_source->funcs->program_pix_clk(
745 			pipe_ctx->clock_source,
746 			&pipe_ctx->stream_res.pix_clk_params,
747 			&pipe_ctx->pll_settings)) {
748 		BREAK_TO_DEBUGGER();
749 		return DC_ERROR_UNEXPECTED;
750 	}
751 
752 	pipe_ctx->stream_res.tg->funcs->program_timing(
753 			pipe_ctx->stream_res.tg,
754 			&stream->timing,
755 			pipe_ctx->pipe_dlg_param.vready_offset,
756 			pipe_ctx->pipe_dlg_param.vstartup_start,
757 			pipe_ctx->pipe_dlg_param.vupdate_offset,
758 			pipe_ctx->pipe_dlg_param.vupdate_width,
759 			pipe_ctx->stream->signal,
760 			true);
761 
762 	if (pipe_ctx->stream_res.tg->funcs->setup_global_lock)
763 		pipe_ctx->stream_res.tg->funcs->setup_global_lock(
764 				pipe_ctx->stream_res.tg);
765 
766 	if (odm_pipe)
767 		odm_pipe->stream_res.opp->funcs->opp_pipe_clock_control(
768 				odm_pipe->stream_res.opp,
769 				true);
770 
771 	pipe_ctx->stream_res.opp->funcs->opp_pipe_clock_control(
772 			pipe_ctx->stream_res.opp,
773 			true);
774 
775 	dc->hwss.blank_pixel_data(dc, pipe_ctx, true);
776 
777 	/* VTG is  within DCHUB command block. DCFCLK is always on */
778 	if (false == pipe_ctx->stream_res.tg->funcs->enable_crtc(pipe_ctx->stream_res.tg)) {
779 		BREAK_TO_DEBUGGER();
780 		return DC_ERROR_UNEXPECTED;
781 	}
782 
783 	dcn20_hwss_wait_for_blank_complete(pipe_ctx->stream_res.opp);
784 
785 	params.vertical_total_min = stream->adjust.v_total_min;
786 	params.vertical_total_max = stream->adjust.v_total_max;
787 	if (pipe_ctx->stream_res.tg->funcs->set_drr)
788 		pipe_ctx->stream_res.tg->funcs->set_drr(
789 			pipe_ctx->stream_res.tg, &params);
790 
791 	// DRR should set trigger event to monitor surface update event
792 	if (stream->adjust.v_total_min != 0 && stream->adjust.v_total_max != 0)
793 		event_triggers = 0x80;
794 	if (pipe_ctx->stream_res.tg->funcs->set_static_screen_control)
795 		pipe_ctx->stream_res.tg->funcs->set_static_screen_control(
796 				pipe_ctx->stream_res.tg, event_triggers);
797 
798 	/* TODO program crtc source select for non-virtual signal*/
799 	/* TODO program FMT */
800 	/* TODO setup link_enc */
801 	/* TODO set stream attributes */
802 	/* TODO program audio */
803 	/* TODO enable stream if timing changed */
804 	/* TODO unblank stream if DP */
805 
806 	return DC_OK;
807 }
808 
809 void dcn20_program_output_csc(struct dc *dc,
810 		struct pipe_ctx *pipe_ctx,
811 		enum dc_color_space colorspace,
812 		uint16_t *matrix,
813 		int opp_id)
814 {
815 	struct mpc *mpc = dc->res_pool->mpc;
816 	enum mpc_output_csc_mode ocsc_mode = MPC_OUTPUT_CSC_COEF_A;
817 
818 	if (pipe_ctx->stream->csc_color_matrix.enable_adjustment == true) {
819 		if (mpc->funcs->set_output_csc != NULL)
820 			mpc->funcs->set_output_csc(mpc,
821 					opp_id,
822 					matrix,
823 					ocsc_mode);
824 	} else {
825 		if (mpc->funcs->set_ocsc_default != NULL)
826 			mpc->funcs->set_ocsc_default(mpc,
827 					opp_id,
828 					colorspace,
829 					ocsc_mode);
830 	}
831 }
832 
833 bool dcn20_set_output_transfer_func(struct pipe_ctx *pipe_ctx,
834 				const struct dc_stream_state *stream)
835 {
836 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
837 	struct mpc *mpc = pipe_ctx->stream_res.opp->ctx->dc->res_pool->mpc;
838 	struct pwl_params *params = NULL;
839 	/*
840 	 * program OGAM only for the top pipe
841 	 * if there is a pipe split then fix diagnostic is required:
842 	 * how to pass OGAM parameter for stream.
843 	 * if programming for all pipes is required then remove condition
844 	 * pipe_ctx->top_pipe == NULL ,but then fix the diagnostic.
845 	 */
846 	if ((pipe_ctx->top_pipe == NULL || dc_res_is_odm_head_pipe(pipe_ctx))
847 			&& mpc->funcs->set_output_gamma && stream->out_transfer_func) {
848 		if (stream->out_transfer_func->type == TF_TYPE_HWPWL)
849 			params = &stream->out_transfer_func->pwl;
850 		else if (pipe_ctx->stream->out_transfer_func->type ==
851 			TF_TYPE_DISTRIBUTED_POINTS &&
852 			cm_helper_translate_curve_to_hw_format(
853 			stream->out_transfer_func,
854 			&mpc->blender_params, false))
855 			params = &mpc->blender_params;
856 		/*
857 		 * there is no ROM
858 		 */
859 		if (stream->out_transfer_func->type == TF_TYPE_PREDEFINED)
860 			BREAK_TO_DEBUGGER();
861 	}
862 	/*
863 	 * if above if is not executed then 'params' equal to 0 and set in bypass
864 	 */
865 	mpc->funcs->set_output_gamma(mpc, mpcc_id, params);
866 
867 	return true;
868 }
869 
870 static bool dcn20_set_blend_lut(
871 	struct pipe_ctx *pipe_ctx, const struct dc_plane_state *plane_state)
872 {
873 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
874 	bool result = true;
875 	struct pwl_params *blend_lut = NULL;
876 
877 	if (plane_state->blend_tf) {
878 		if (plane_state->blend_tf->type == TF_TYPE_HWPWL)
879 			blend_lut = &plane_state->blend_tf->pwl;
880 		else if (plane_state->blend_tf->type == TF_TYPE_DISTRIBUTED_POINTS) {
881 			cm_helper_translate_curve_to_hw_format(
882 					plane_state->blend_tf,
883 					&dpp_base->regamma_params, false);
884 			blend_lut = &dpp_base->regamma_params;
885 		}
886 	}
887 	result = dpp_base->funcs->dpp_program_blnd_lut(dpp_base, blend_lut);
888 
889 	return result;
890 }
891 
892 static bool dcn20_set_shaper_3dlut(
893 	struct pipe_ctx *pipe_ctx, const struct dc_plane_state *plane_state)
894 {
895 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
896 	bool result = true;
897 	struct pwl_params *shaper_lut = NULL;
898 
899 	if (plane_state->in_shaper_func) {
900 		if (plane_state->in_shaper_func->type == TF_TYPE_HWPWL)
901 			shaper_lut = &plane_state->in_shaper_func->pwl;
902 		else if (plane_state->in_shaper_func->type == TF_TYPE_DISTRIBUTED_POINTS) {
903 			cm_helper_translate_curve_to_hw_format(
904 					plane_state->in_shaper_func,
905 					&dpp_base->shaper_params, true);
906 			shaper_lut = &dpp_base->shaper_params;
907 		}
908 	}
909 
910 	result = dpp_base->funcs->dpp_program_shaper_lut(dpp_base, shaper_lut);
911 	if (plane_state->lut3d_func &&
912 		plane_state->lut3d_func->initialized == true)
913 		result = dpp_base->funcs->dpp_program_3dlut(dpp_base,
914 								&plane_state->lut3d_func->lut_3d);
915 	else
916 		result = dpp_base->funcs->dpp_program_3dlut(dpp_base, NULL);
917 
918 	if (plane_state->lut3d_func &&
919 		plane_state->lut3d_func->initialized == true &&
920 		plane_state->lut3d_func->hdr_multiplier != 0)
921 		dpp_base->funcs->dpp_set_hdr_multiplier(dpp_base,
922 				plane_state->lut3d_func->hdr_multiplier);
923 	else
924 		dpp_base->funcs->dpp_set_hdr_multiplier(dpp_base, 0x1f000);
925 
926 	return result;
927 }
928 
929 bool dcn20_set_input_transfer_func(struct pipe_ctx *pipe_ctx,
930 					  const struct dc_plane_state *plane_state)
931 {
932 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
933 	const struct dc_transfer_func *tf = NULL;
934 	bool result = true;
935 	bool use_degamma_ram = false;
936 
937 	if (dpp_base == NULL || plane_state == NULL)
938 		return false;
939 
940 	dcn20_set_shaper_3dlut(pipe_ctx, plane_state);
941 	dcn20_set_blend_lut(pipe_ctx, plane_state);
942 
943 	if (plane_state->in_transfer_func)
944 		tf = plane_state->in_transfer_func;
945 
946 
947 	if (tf == NULL) {
948 		dpp_base->funcs->dpp_set_degamma(dpp_base,
949 				IPP_DEGAMMA_MODE_BYPASS);
950 		return true;
951 	}
952 
953 	if (tf->type == TF_TYPE_HWPWL || tf->type == TF_TYPE_DISTRIBUTED_POINTS)
954 		use_degamma_ram = true;
955 
956 	if (use_degamma_ram == true) {
957 		if (tf->type == TF_TYPE_HWPWL)
958 			dpp_base->funcs->dpp_program_degamma_pwl(dpp_base,
959 					&tf->pwl);
960 		else if (tf->type == TF_TYPE_DISTRIBUTED_POINTS) {
961 			cm_helper_translate_curve_to_degamma_hw_format(tf,
962 					&dpp_base->degamma_params);
963 			dpp_base->funcs->dpp_program_degamma_pwl(dpp_base,
964 				&dpp_base->degamma_params);
965 		}
966 		return true;
967 	}
968 	/* handle here the optimized cases when de-gamma ROM could be used.
969 	 *
970 	 */
971 	if (tf->type == TF_TYPE_PREDEFINED) {
972 		switch (tf->tf) {
973 		case TRANSFER_FUNCTION_SRGB:
974 			dpp_base->funcs->dpp_set_degamma(dpp_base,
975 					IPP_DEGAMMA_MODE_HW_sRGB);
976 			break;
977 		case TRANSFER_FUNCTION_BT709:
978 			dpp_base->funcs->dpp_set_degamma(dpp_base,
979 					IPP_DEGAMMA_MODE_HW_xvYCC);
980 			break;
981 		case TRANSFER_FUNCTION_LINEAR:
982 			dpp_base->funcs->dpp_set_degamma(dpp_base,
983 					IPP_DEGAMMA_MODE_BYPASS);
984 			break;
985 		case TRANSFER_FUNCTION_PQ:
986 		default:
987 			result = false;
988 			break;
989 		}
990 	} else if (tf->type == TF_TYPE_BYPASS)
991 		dpp_base->funcs->dpp_set_degamma(dpp_base,
992 				IPP_DEGAMMA_MODE_BYPASS);
993 	else {
994 		/*
995 		 * if we are here, we did not handle correctly.
996 		 * fix is required for this use case
997 		 */
998 		BREAK_TO_DEBUGGER();
999 		dpp_base->funcs->dpp_set_degamma(dpp_base,
1000 				IPP_DEGAMMA_MODE_BYPASS);
1001 	}
1002 
1003 	return result;
1004 }
1005 
1006 static void dcn20_update_odm(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe_ctx)
1007 {
1008 	struct pipe_ctx *combine_pipe = dc_res_get_odm_bottom_pipe(pipe_ctx);
1009 
1010 	if (combine_pipe)
1011 		pipe_ctx->stream_res.tg->funcs->set_odm_combine(
1012 				pipe_ctx->stream_res.tg,
1013 				combine_pipe->stream_res.opp->inst,
1014 				pipe_ctx->plane_res.scl_data.h_active,
1015 				pipe_ctx->stream->timing.pixel_encoding);
1016 	else
1017 		pipe_ctx->stream_res.tg->funcs->set_odm_bypass(
1018 				pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1019 }
1020 
1021 void dcn20_blank_pixel_data(
1022 		struct dc *dc,
1023 		struct pipe_ctx *pipe_ctx,
1024 		bool blank)
1025 {
1026 	struct tg_color black_color = {0};
1027 	struct stream_resource *stream_res = &pipe_ctx->stream_res;
1028 	struct dc_stream_state *stream = pipe_ctx->stream;
1029 	enum dc_color_space color_space = stream->output_color_space;
1030 	enum controller_dp_test_pattern test_pattern = CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR;
1031 	struct pipe_ctx *bot_odm_pipe = dc_res_get_odm_bottom_pipe(pipe_ctx);
1032 
1033 	int width = stream->timing.h_addressable + stream->timing.h_border_left + stream->timing.h_border_right;
1034 	int height = stream->timing.v_addressable + stream->timing.v_border_bottom + stream->timing.v_border_top;
1035 
1036 	/* get opp dpg blank color */
1037 	color_space_to_black_color(dc, color_space, &black_color);
1038 
1039 	if (bot_odm_pipe)
1040 		width = width / 2;
1041 
1042 	if (blank) {
1043 		if (stream_res->abm)
1044 			stream_res->abm->funcs->set_abm_immediate_disable(stream_res->abm);
1045 
1046 		if (dc->debug.visual_confirm != VISUAL_CONFIRM_DISABLE)
1047 			test_pattern = CONTROLLER_DP_TEST_PATTERN_COLORSQUARES;
1048 	} else {
1049 		test_pattern = CONTROLLER_DP_TEST_PATTERN_VIDEOMODE;
1050 	}
1051 
1052 	stream_res->opp->funcs->opp_set_disp_pattern_generator(
1053 			stream_res->opp,
1054 			test_pattern,
1055 			stream->timing.display_color_depth,
1056 			&black_color,
1057 			width,
1058 			height);
1059 
1060 	if (bot_odm_pipe) {
1061 		bot_odm_pipe->stream_res.opp->funcs->opp_set_disp_pattern_generator(
1062 				bot_odm_pipe->stream_res.opp,
1063 				dc->debug.visual_confirm != VISUAL_CONFIRM_DISABLE ?
1064 						CONTROLLER_DP_TEST_PATTERN_COLORRAMP : test_pattern,
1065 				stream->timing.display_color_depth,
1066 				&black_color,
1067 				width,
1068 				height);
1069 	}
1070 
1071 	if (!blank)
1072 		if (stream_res->abm) {
1073 			stream_res->abm->funcs->set_pipe(stream_res->abm, stream_res->tg->inst + 1);
1074 			stream_res->abm->funcs->set_abm_level(stream_res->abm, stream->abm_level);
1075 		}
1076 }
1077 
1078 
1079 static void dcn20_power_on_plane(
1080 	struct dce_hwseq *hws,
1081 	struct pipe_ctx *pipe_ctx)
1082 {
1083 	DC_LOGGER_INIT(hws->ctx->logger);
1084 	if (REG(DC_IP_REQUEST_CNTL)) {
1085 		REG_SET(DC_IP_REQUEST_CNTL, 0,
1086 				IP_REQUEST_EN, 1);
1087 		dcn20_dpp_pg_control(hws, pipe_ctx->plane_res.dpp->inst, true);
1088 		dcn20_hubp_pg_control(hws, pipe_ctx->plane_res.hubp->inst, true);
1089 		REG_SET(DC_IP_REQUEST_CNTL, 0,
1090 				IP_REQUEST_EN, 0);
1091 		DC_LOG_DEBUG(
1092 				"Un-gated front end for pipe %d\n", pipe_ctx->plane_res.hubp->inst);
1093 	}
1094 }
1095 
1096 void dcn20_enable_plane(
1097 	struct dc *dc,
1098 	struct pipe_ctx *pipe_ctx,
1099 	struct dc_state *context)
1100 {
1101 	//if (dc->debug.sanity_checks) {
1102 	//	dcn10_verify_allow_pstate_change_high(dc);
1103 	//}
1104 	dcn20_power_on_plane(dc->hwseq, pipe_ctx);
1105 
1106 	/* enable DCFCLK current DCHUB */
1107 	pipe_ctx->plane_res.hubp->funcs->hubp_clk_cntl(pipe_ctx->plane_res.hubp, true);
1108 
1109 	/* make sure OPP_PIPE_CLOCK_EN = 1 */
1110 	pipe_ctx->stream_res.opp->funcs->opp_pipe_clock_control(
1111 			pipe_ctx->stream_res.opp,
1112 			true);
1113 
1114 /* TODO: enable/disable in dm as per update type.
1115 	if (plane_state) {
1116 		DC_LOG_DC(dc->ctx->logger,
1117 				"Pipe:%d 0x%x: addr hi:0x%x, "
1118 				"addr low:0x%x, "
1119 				"src: %d, %d, %d,"
1120 				" %d; dst: %d, %d, %d, %d;\n",
1121 				pipe_ctx->pipe_idx,
1122 				plane_state,
1123 				plane_state->address.grph.addr.high_part,
1124 				plane_state->address.grph.addr.low_part,
1125 				plane_state->src_rect.x,
1126 				plane_state->src_rect.y,
1127 				plane_state->src_rect.width,
1128 				plane_state->src_rect.height,
1129 				plane_state->dst_rect.x,
1130 				plane_state->dst_rect.y,
1131 				plane_state->dst_rect.width,
1132 				plane_state->dst_rect.height);
1133 
1134 		DC_LOG_DC(dc->ctx->logger,
1135 				"Pipe %d: width, height, x, y         format:%d\n"
1136 				"viewport:%d, %d, %d, %d\n"
1137 				"recout:  %d, %d, %d, %d\n",
1138 				pipe_ctx->pipe_idx,
1139 				plane_state->format,
1140 				pipe_ctx->plane_res.scl_data.viewport.width,
1141 				pipe_ctx->plane_res.scl_data.viewport.height,
1142 				pipe_ctx->plane_res.scl_data.viewport.x,
1143 				pipe_ctx->plane_res.scl_data.viewport.y,
1144 				pipe_ctx->plane_res.scl_data.recout.width,
1145 				pipe_ctx->plane_res.scl_data.recout.height,
1146 				pipe_ctx->plane_res.scl_data.recout.x,
1147 				pipe_ctx->plane_res.scl_data.recout.y);
1148 		print_rq_dlg_ttu(dc, pipe_ctx);
1149 	}
1150 */
1151 	if (dc->vm_pa_config.valid) {
1152 		struct vm_system_aperture_param apt;
1153 
1154 		apt.sys_default.quad_part = 0;
1155 
1156 		apt.sys_high.quad_part = dc->vm_pa_config.system_aperture.start_addr;
1157 		apt.sys_low.quad_part = dc->vm_pa_config.system_aperture.end_addr;
1158 
1159 		// Program system aperture settings
1160 		pipe_ctx->plane_res.hubp->funcs->hubp_set_vm_system_aperture_settings(pipe_ctx->plane_res.hubp, &apt);
1161 	}
1162 
1163 //	if (dc->debug.sanity_checks) {
1164 //		dcn10_verify_allow_pstate_change_high(dc);
1165 //	}
1166 }
1167 
1168 
1169 static void dcn20_program_pipe(
1170 		struct dc *dc,
1171 		struct pipe_ctx *pipe_ctx,
1172 		struct dc_state *context)
1173 {
1174 	pipe_ctx->plane_state->update_flags.bits.full_update =
1175 			context->commit_hints.full_update_needed ? 1 : pipe_ctx->plane_state->update_flags.bits.full_update;
1176 
1177 	if (pipe_ctx->plane_state->update_flags.bits.full_update)
1178 		dcn20_enable_plane(dc, pipe_ctx, context);
1179 
1180 	update_dchubp_dpp(dc, pipe_ctx, context);
1181 
1182 	set_hdr_multiplier(pipe_ctx);
1183 
1184 	if (pipe_ctx->plane_state->update_flags.bits.full_update ||
1185 			pipe_ctx->plane_state->update_flags.bits.in_transfer_func_change ||
1186 			pipe_ctx->plane_state->update_flags.bits.gamma_change)
1187 		dc->hwss.set_input_transfer_func(pipe_ctx, pipe_ctx->plane_state);
1188 
1189 	/* dcn10_translate_regamma_to_hw_format takes 750us to finish
1190 	 * only do gamma programming for full update.
1191 	 * TODO: This can be further optimized/cleaned up
1192 	 * Always call this for now since it does memcmp inside before
1193 	 * doing heavy calculation and programming
1194 	 */
1195 	if (pipe_ctx->plane_state->update_flags.bits.full_update)
1196 		dc->hwss.set_output_transfer_func(pipe_ctx, pipe_ctx->stream);
1197 }
1198 
1199 static void dcn20_program_all_pipe_in_tree(
1200 		struct dc *dc,
1201 		struct pipe_ctx *pipe_ctx,
1202 		struct dc_state *context)
1203 {
1204 	if (pipe_ctx->top_pipe == NULL) {
1205 		bool blank = !is_pipe_tree_visible(pipe_ctx);
1206 
1207 		pipe_ctx->stream_res.tg->funcs->program_global_sync(
1208 				pipe_ctx->stream_res.tg,
1209 				pipe_ctx->pipe_dlg_param.vready_offset,
1210 				pipe_ctx->pipe_dlg_param.vstartup_start,
1211 				pipe_ctx->pipe_dlg_param.vupdate_offset,
1212 				pipe_ctx->pipe_dlg_param.vupdate_width);
1213 
1214 		pipe_ctx->stream_res.tg->funcs->set_vtg_params(
1215 				pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1216 
1217 		dc->hwss.blank_pixel_data(dc, pipe_ctx, blank);
1218 
1219 		if (dc->hwss.update_odm)
1220 			dc->hwss.update_odm(dc, context, pipe_ctx);
1221 	}
1222 
1223 	if (pipe_ctx->plane_state != NULL)
1224 		dcn20_program_pipe(dc, pipe_ctx, context);
1225 
1226 	if (pipe_ctx->bottom_pipe != NULL && pipe_ctx->bottom_pipe != pipe_ctx)
1227 		dcn20_program_all_pipe_in_tree(dc, pipe_ctx->bottom_pipe, context);
1228 }
1229 
1230 void dcn20_pipe_control_lock_global(
1231 		struct dc *dc,
1232 		struct pipe_ctx *pipe,
1233 		bool lock)
1234 {
1235 	if (lock) {
1236 		pipe->stream_res.tg->funcs->lock_doublebuffer_enable(
1237 				pipe->stream_res.tg);
1238 		pipe->stream_res.tg->funcs->lock(pipe->stream_res.tg);
1239 	} else {
1240 		pipe->stream_res.tg->funcs->unlock(pipe->stream_res.tg);
1241 		pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg,
1242 				CRTC_STATE_VACTIVE);
1243 		pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg,
1244 				CRTC_STATE_VBLANK);
1245 		pipe->stream_res.tg->funcs->lock_doublebuffer_disable(
1246 				pipe->stream_res.tg);
1247 	}
1248 }
1249 
1250 void dcn20_pipe_control_lock(
1251 	struct dc *dc,
1252 	struct pipe_ctx *pipe,
1253 	bool lock)
1254 {
1255 	bool flip_immediate = false;
1256 
1257 	/* use TG master update lock to lock everything on the TG
1258 	 * therefore only top pipe need to lock
1259 	 */
1260 	if (pipe->top_pipe)
1261 		return;
1262 
1263 	if (pipe->plane_state != NULL)
1264 		flip_immediate = pipe->plane_state->flip_immediate;
1265 
1266 	/* In flip immediate and pipe splitting case, we need to use GSL
1267 	 * for synchronization. Only do setup on locking and on flip type change.
1268 	 */
1269 	if (lock && pipe->bottom_pipe != NULL)
1270 		if ((flip_immediate && pipe->stream_res.gsl_group == 0) ||
1271 		    (!flip_immediate && pipe->stream_res.gsl_group > 0))
1272 			dcn20_setup_gsl_group_as_lock(dc, pipe, flip_immediate);
1273 
1274 	if (pipe->plane_state != NULL && pipe->plane_state->triplebuffer_flips) {
1275 		if (lock)
1276 			pipe->stream_res.tg->funcs->triplebuffer_lock(pipe->stream_res.tg);
1277 		else
1278 			pipe->stream_res.tg->funcs->triplebuffer_unlock(pipe->stream_res.tg);
1279 	} else {
1280 		if (lock)
1281 			pipe->stream_res.tg->funcs->lock(pipe->stream_res.tg);
1282 		else
1283 			pipe->stream_res.tg->funcs->unlock(pipe->stream_res.tg);
1284 	}
1285 }
1286 
1287 static void dcn20_apply_ctx_for_surface(
1288 		struct dc *dc,
1289 		const struct dc_stream_state *stream,
1290 		int num_planes,
1291 		struct dc_state *context)
1292 {
1293 
1294 	int i;
1295 	struct timing_generator *tg;
1296 	bool removed_pipe[6] = { false };
1297 	bool interdependent_update = false;
1298 	struct pipe_ctx *top_pipe_to_program =
1299 			find_top_pipe_for_stream(dc, context, stream);
1300 	DC_LOGGER_INIT(dc->ctx->logger);
1301 
1302 	if (!top_pipe_to_program)
1303 		return;
1304 
1305 	tg = top_pipe_to_program->stream_res.tg;
1306 
1307 	interdependent_update = top_pipe_to_program->plane_state &&
1308 		top_pipe_to_program->plane_state->update_flags.bits.full_update;
1309 
1310 	if (interdependent_update)
1311 		lock_all_pipes(dc, context, true);
1312 	else
1313 		dcn20_pipe_control_lock(dc, top_pipe_to_program, true);
1314 
1315 	if (num_planes == 0) {
1316 		/* OTG blank before remove all front end */
1317 		dc->hwss.blank_pixel_data(dc, top_pipe_to_program, true);
1318 	}
1319 
1320 	/* Disconnect unused mpcc */
1321 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1322 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1323 		struct pipe_ctx *old_pipe_ctx =
1324 				&dc->current_state->res_ctx.pipe_ctx[i];
1325 		/*
1326 		 * Powergate reused pipes that are not powergated
1327 		 * fairly hacky right now, using opp_id as indicator
1328 		 * TODO: After move dc_post to dc_update, this will
1329 		 * be removed.
1330 		 */
1331 		if (pipe_ctx->plane_state && !old_pipe_ctx->plane_state) {
1332 			if (old_pipe_ctx->stream_res.tg == tg &&
1333 			    old_pipe_ctx->plane_res.hubp &&
1334 			    old_pipe_ctx->plane_res.hubp->opp_id != OPP_ID_INVALID)
1335 				dcn20_disable_plane(dc, old_pipe_ctx);
1336 		}
1337 
1338 		if ((!pipe_ctx->plane_state ||
1339 		     pipe_ctx->stream_res.tg != old_pipe_ctx->stream_res.tg) &&
1340 		     old_pipe_ctx->plane_state &&
1341 		     old_pipe_ctx->stream_res.tg == tg) {
1342 
1343 			dc->hwss.plane_atomic_disconnect(dc, old_pipe_ctx);
1344 			removed_pipe[i] = true;
1345 
1346 			DC_LOG_DC("Reset mpcc for pipe %d\n",
1347 					old_pipe_ctx->pipe_idx);
1348 		}
1349 	}
1350 
1351 	if (num_planes > 0)
1352 		dcn20_program_all_pipe_in_tree(dc, top_pipe_to_program, context);
1353 
1354 	/* Program secondary blending tree and writeback pipes */
1355 	if ((stream->num_wb_info > 0) && (dc->hwss.program_all_writeback_pipes_in_tree))
1356 		dc->hwss.program_all_writeback_pipes_in_tree(dc, stream, context);
1357 
1358 	if (interdependent_update)
1359 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
1360 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1361 
1362 			/* Skip inactive pipes and ones already updated */
1363 			if (!pipe_ctx->stream || pipe_ctx->stream == stream ||
1364 			    !pipe_ctx->plane_state || !tg->funcs->is_tg_enabled(tg))
1365 				continue;
1366 
1367 			pipe_ctx->plane_res.hubp->funcs->hubp_setup_interdependent(
1368 				pipe_ctx->plane_res.hubp,
1369 				&pipe_ctx->dlg_regs,
1370 				&pipe_ctx->ttu_regs);
1371 		}
1372 
1373 	if (interdependent_update)
1374 		lock_all_pipes(dc, context, false);
1375 	else
1376 		dcn20_pipe_control_lock(dc, top_pipe_to_program, false);
1377 
1378 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1379 		if (removed_pipe[i])
1380 			dcn20_disable_plane(dc, &dc->current_state->res_ctx.pipe_ctx[i]);
1381 }
1382 
1383 
1384 void dcn20_prepare_bandwidth(
1385 		struct dc *dc,
1386 		struct dc_state *context)
1387 {
1388 	struct hubbub *hubbub = dc->res_pool->hubbub;
1389 
1390 	/* program dchubbub watermarks */
1391 	hubbub->funcs->program_watermarks(hubbub,
1392 					&context->bw_ctx.bw.dcn.watermarks,
1393 					dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000,
1394 					false);
1395 
1396 	dc->clk_mgr->funcs->update_clocks(
1397 			dc->clk_mgr,
1398 			context,
1399 			false);
1400 }
1401 
1402 void dcn20_optimize_bandwidth(
1403 		struct dc *dc,
1404 		struct dc_state *context)
1405 {
1406 	struct hubbub *hubbub = dc->res_pool->hubbub;
1407 
1408 	/* program dchubbub watermarks */
1409 	hubbub->funcs->program_watermarks(hubbub,
1410 					&context->bw_ctx.bw.dcn.watermarks,
1411 					dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000,
1412 					true);
1413 
1414 	dc->clk_mgr->funcs->update_clocks(
1415 			dc->clk_mgr,
1416 			context,
1417 			true);
1418 }
1419 
1420 bool dcn20_update_bandwidth(
1421 		struct dc *dc,
1422 		struct dc_state *context)
1423 {
1424 	int i;
1425 
1426 	/* recalculate DML parameters */
1427 	if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false))
1428 		return false;
1429 
1430 	/* apply updated bandwidth parameters */
1431 	dc->hwss.prepare_bandwidth(dc, context);
1432 
1433 	/* update hubp configs for all pipes */
1434 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1435 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1436 
1437 		if (pipe_ctx->plane_state == NULL)
1438 			continue;
1439 
1440 		if (pipe_ctx->top_pipe == NULL) {
1441 			bool blank = !is_pipe_tree_visible(pipe_ctx);
1442 
1443 			pipe_ctx->stream_res.tg->funcs->program_global_sync(
1444 					pipe_ctx->stream_res.tg,
1445 					pipe_ctx->pipe_dlg_param.vready_offset,
1446 					pipe_ctx->pipe_dlg_param.vstartup_start,
1447 					pipe_ctx->pipe_dlg_param.vupdate_offset,
1448 					pipe_ctx->pipe_dlg_param.vupdate_width);
1449 
1450 			pipe_ctx->stream_res.tg->funcs->set_vtg_params(
1451 					pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1452 
1453 			dc->hwss.blank_pixel_data(dc, pipe_ctx, blank);
1454 		}
1455 
1456 		pipe_ctx->plane_res.hubp->funcs->hubp_setup(
1457 				pipe_ctx->plane_res.hubp,
1458 					&pipe_ctx->dlg_regs,
1459 					&pipe_ctx->ttu_regs,
1460 					&pipe_ctx->rq_regs,
1461 					&pipe_ctx->pipe_dlg_param);
1462 	}
1463 
1464 	return true;
1465 }
1466 
1467 static void dcn20_enable_writeback(
1468 		struct dc *dc,
1469 		const struct dc_stream_status *stream_status,
1470 		struct dc_writeback_info *wb_info)
1471 {
1472 	struct dwbc *dwb;
1473 	struct mcif_wb *mcif_wb;
1474 	struct timing_generator *optc;
1475 
1476 	ASSERT(wb_info->dwb_pipe_inst < MAX_DWB_PIPES);
1477 	ASSERT(wb_info->wb_enabled);
1478 	dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
1479 	mcif_wb = dc->res_pool->mcif_wb[wb_info->dwb_pipe_inst];
1480 
1481 	/* set the OPTC source mux */
1482 	ASSERT(stream_status->primary_otg_inst < MAX_PIPES);
1483 	optc = dc->res_pool->timing_generators[stream_status->primary_otg_inst];
1484 	optc->funcs->set_dwb_source(optc, wb_info->dwb_pipe_inst);
1485 	/* set MCIF_WB buffer and arbitration configuration */
1486 	mcif_wb->funcs->config_mcif_buf(mcif_wb, &wb_info->mcif_buf_params, wb_info->dwb_params.dest_height);
1487 	mcif_wb->funcs->config_mcif_arb(mcif_wb, &dc->current_state->bw_ctx.bw.dcn.bw_writeback.mcif_wb_arb[wb_info->dwb_pipe_inst]);
1488 	/* Enable MCIF_WB */
1489 	mcif_wb->funcs->enable_mcif(mcif_wb);
1490 	/* Enable DWB */
1491 	dwb->funcs->enable(dwb, &wb_info->dwb_params);
1492 	/* TODO: add sequence to enable/disable warmup */
1493 }
1494 
1495 void dcn20_disable_writeback(
1496 		struct dc *dc,
1497 		unsigned int dwb_pipe_inst)
1498 {
1499 	struct dwbc *dwb;
1500 	struct mcif_wb *mcif_wb;
1501 
1502 	ASSERT(dwb_pipe_inst < MAX_DWB_PIPES);
1503 	dwb = dc->res_pool->dwbc[dwb_pipe_inst];
1504 	mcif_wb = dc->res_pool->mcif_wb[dwb_pipe_inst];
1505 
1506 	dwb->funcs->disable(dwb);
1507 	mcif_wb->funcs->disable_mcif(mcif_wb);
1508 }
1509 
1510 bool dcn20_hwss_wait_for_blank_complete(
1511 		struct output_pixel_processor *opp)
1512 {
1513 	int counter;
1514 
1515 	for (counter = 0; counter < 1000; counter++) {
1516 		if (opp->funcs->dpg_is_blanked(opp))
1517 			break;
1518 
1519 		udelay(100);
1520 	}
1521 
1522 	if (counter == 1000) {
1523 		dm_error("DC: failed to blank crtc!\n");
1524 		return false;
1525 	}
1526 
1527 	return true;
1528 }
1529 
1530 bool dcn20_dmdata_status_done(struct pipe_ctx *pipe_ctx)
1531 {
1532 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
1533 
1534 	if (!hubp)
1535 		return false;
1536 	return hubp->funcs->dmdata_status_done(hubp);
1537 }
1538 
1539 static void dcn20_disable_stream_gating(struct dc *dc, struct pipe_ctx *pipe_ctx)
1540 {
1541 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
1542 	struct dce_hwseq *hws = dc->hwseq;
1543 	struct pipe_ctx *bot_odm_pipe = dc_res_get_odm_bottom_pipe(pipe_ctx);
1544 
1545 	if (pipe_ctx->stream_res.dsc) {
1546 		dcn20_dsc_pg_control(hws, pipe_ctx->stream_res.dsc->inst, true);
1547 		if (bot_odm_pipe)
1548 			dcn20_dsc_pg_control(hws, bot_odm_pipe->stream_res.dsc->inst, true);
1549 	}
1550 #endif
1551 }
1552 
1553 static void dcn20_enable_stream_gating(struct dc *dc, struct pipe_ctx *pipe_ctx)
1554 {
1555 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
1556 	struct dce_hwseq *hws = dc->hwseq;
1557 	struct pipe_ctx *bot_odm_pipe = dc_res_get_odm_bottom_pipe(pipe_ctx);
1558 
1559 	if (pipe_ctx->stream_res.dsc) {
1560 		dcn20_dsc_pg_control(hws, pipe_ctx->stream_res.dsc->inst, false);
1561 		if (bot_odm_pipe)
1562 			dcn20_dsc_pg_control(hws, bot_odm_pipe->stream_res.dsc->inst, false);
1563 	}
1564 #endif
1565 }
1566 
1567 void dcn20_set_dmdata_attributes(struct pipe_ctx *pipe_ctx)
1568 {
1569 	struct dc_dmdata_attributes attr = { 0 };
1570 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
1571 
1572 	attr.dmdata_mode = DMDATA_HW_MODE;
1573 	attr.dmdata_size =
1574 		dc_is_hdmi_signal(pipe_ctx->stream->signal) ? 32 : 36;
1575 	attr.address.quad_part =
1576 			pipe_ctx->stream->dmdata_address.quad_part;
1577 	attr.dmdata_dl_delta = 0;
1578 	attr.dmdata_qos_mode = 0;
1579 	attr.dmdata_qos_level = 0;
1580 	attr.dmdata_repeat = 1; /* always repeat */
1581 	attr.dmdata_updated = 1;
1582 	attr.dmdata_sw_data = NULL;
1583 
1584 	hubp->funcs->dmdata_set_attributes(hubp, &attr);
1585 }
1586 
1587 void dcn20_disable_stream(struct pipe_ctx *pipe_ctx, int option)
1588 {
1589 	dce110_disable_stream(pipe_ctx, option);
1590 }
1591 
1592 static void dcn20_init_vm_ctx(
1593 		struct dce_hwseq *hws,
1594 		struct dc *dc,
1595 		struct dc_virtual_addr_space_config *va_config,
1596 		int vmid)
1597 {
1598 	struct dcn_hubbub_virt_addr_config config;
1599 
1600 	if (vmid == 0) {
1601 		ASSERT(0); /* VMID cannot be 0 for vm context */
1602 		return;
1603 	}
1604 
1605 	config.page_table_start_addr = va_config->page_table_start_addr;
1606 	config.page_table_end_addr = va_config->page_table_end_addr;
1607 	config.page_table_block_size = va_config->page_table_block_size_in_bytes;
1608 	config.page_table_depth = va_config->page_table_depth;
1609 	config.page_table_base_addr = va_config->page_table_base_addr;
1610 
1611 	dc->res_pool->hubbub->funcs->init_vm_ctx(dc->res_pool->hubbub, &config, vmid);
1612 }
1613 
1614 static int dcn20_init_sys_ctx(struct dce_hwseq *hws, struct dc *dc, struct dc_phy_addr_space_config *pa_config)
1615 {
1616 	struct dcn_hubbub_phys_addr_config config;
1617 
1618 	config.system_aperture.fb_top = pa_config->system_aperture.fb_top;
1619 	config.system_aperture.fb_offset = pa_config->system_aperture.fb_offset;
1620 	config.system_aperture.fb_base = pa_config->system_aperture.fb_base;
1621 	config.system_aperture.agp_top = pa_config->system_aperture.agp_top;
1622 	config.system_aperture.agp_bot = pa_config->system_aperture.agp_bot;
1623 	config.system_aperture.agp_base = pa_config->system_aperture.agp_base;
1624 	config.gart_config.page_table_start_addr = pa_config->gart_config.page_table_start_addr;
1625 	config.gart_config.page_table_end_addr = pa_config->gart_config.page_table_end_addr;
1626 	config.gart_config.page_table_base_addr = pa_config->gart_config.page_table_base_addr;
1627 
1628 	return dc->res_pool->hubbub->funcs->init_dchub_sys_ctx(dc->res_pool->hubbub, &config);
1629 }
1630 
1631 static bool patch_address_for_sbs_tb_stereo(
1632 		struct pipe_ctx *pipe_ctx, PHYSICAL_ADDRESS_LOC *addr)
1633 {
1634 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1635 	bool sec_split = pipe_ctx->top_pipe &&
1636 			pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state;
1637 	if (sec_split && plane_state->address.type == PLN_ADDR_TYPE_GRPH_STEREO &&
1638 			(pipe_ctx->stream->timing.timing_3d_format ==
1639 			TIMING_3D_FORMAT_SIDE_BY_SIDE ||
1640 			pipe_ctx->stream->timing.timing_3d_format ==
1641 			TIMING_3D_FORMAT_TOP_AND_BOTTOM)) {
1642 		*addr = plane_state->address.grph_stereo.left_addr;
1643 		plane_state->address.grph_stereo.left_addr =
1644 				plane_state->address.grph_stereo.right_addr;
1645 		return true;
1646 	}
1647 
1648 	if (pipe_ctx->stream->view_format != VIEW_3D_FORMAT_NONE &&
1649 			plane_state->address.type != PLN_ADDR_TYPE_GRPH_STEREO) {
1650 		plane_state->address.type = PLN_ADDR_TYPE_GRPH_STEREO;
1651 		plane_state->address.grph_stereo.right_addr =
1652 				plane_state->address.grph_stereo.left_addr;
1653 	}
1654 	return false;
1655 }
1656 
1657 
1658 static void dcn20_update_plane_addr(const struct dc *dc, struct pipe_ctx *pipe_ctx)
1659 {
1660 	bool addr_patched = false;
1661 	PHYSICAL_ADDRESS_LOC addr;
1662 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1663 
1664 	if (plane_state == NULL)
1665 		return;
1666 
1667 	addr_patched = patch_address_for_sbs_tb_stereo(pipe_ctx, &addr);
1668 
1669 	// Call Helper to track VMID use
1670 	vm_helper_mark_vmid_used(dc->vm_helper, plane_state->address.vmid, pipe_ctx->plane_res.hubp->inst);
1671 
1672 	pipe_ctx->plane_res.hubp->funcs->hubp_program_surface_flip_and_addr(
1673 			pipe_ctx->plane_res.hubp,
1674 			&plane_state->address,
1675 			plane_state->flip_immediate);
1676 
1677 	plane_state->status.requested_address = plane_state->address;
1678 
1679 	if (plane_state->flip_immediate)
1680 		plane_state->status.current_address = plane_state->address;
1681 
1682 	if (addr_patched)
1683 		pipe_ctx->plane_state->address.grph_stereo.left_addr = addr;
1684 }
1685 
1686 void dcn20_unblank_stream(struct pipe_ctx *pipe_ctx,
1687 		struct dc_link_settings *link_settings)
1688 {
1689 	struct encoder_unblank_param params = { { 0 } };
1690 	struct dc_stream_state *stream = pipe_ctx->stream;
1691 	struct dc_link *link = stream->link;
1692 	params.odm = dc_res_get_odm_bottom_pipe(pipe_ctx);
1693 
1694 	/* only 3 items below are used by unblank */
1695 	params.timing = pipe_ctx->stream->timing;
1696 
1697 	params.link_settings.link_rate = link_settings->link_rate;
1698 
1699 	if (dc_is_dp_signal(pipe_ctx->stream->signal)) {
1700 		if (optc1_is_two_pixels_per_containter(&stream->timing) || params.odm)
1701 			params.timing.pix_clk_100hz /= 2;
1702 		pipe_ctx->stream_res.stream_enc->funcs->dp_set_odm_combine(
1703 				pipe_ctx->stream_res.stream_enc, params.odm);
1704 		pipe_ctx->stream_res.stream_enc->funcs->dp_unblank(pipe_ctx->stream_res.stream_enc, &params);
1705 	}
1706 
1707 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1708 		link->dc->hwss.edp_backlight_control(link, true);
1709 	}
1710 }
1711 
1712 void dcn20_setup_vupdate_interrupt(struct pipe_ctx *pipe_ctx)
1713 {
1714 	struct timing_generator *tg = pipe_ctx->stream_res.tg;
1715 	int start_line = get_vupdate_offset_from_vsync(pipe_ctx);
1716 
1717 	if (start_line < 0)
1718 		start_line = 0;
1719 
1720 	if (tg->funcs->setup_vertical_interrupt2)
1721 		tg->funcs->setup_vertical_interrupt2(tg, start_line);
1722 }
1723 
1724 static void dcn20_reset_back_end_for_pipe(
1725 		struct dc *dc,
1726 		struct pipe_ctx *pipe_ctx,
1727 		struct dc_state *context)
1728 {
1729 	int i;
1730 	DC_LOGGER_INIT(dc->ctx->logger);
1731 	if (pipe_ctx->stream_res.stream_enc == NULL) {
1732 		pipe_ctx->stream = NULL;
1733 		return;
1734 	}
1735 
1736 	if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
1737 		/* DPMS may already disable */
1738 		if (!pipe_ctx->stream->dpms_off)
1739 			core_link_disable_stream(pipe_ctx, FREE_ACQUIRED_RESOURCE);
1740 		else if (pipe_ctx->stream_res.audio) {
1741 			dc->hwss.disable_audio_stream(pipe_ctx, FREE_ACQUIRED_RESOURCE);
1742 		}
1743 
1744 	}
1745 
1746 	/* by upper caller loop, parent pipe: pipe0, will be reset last.
1747 	 * back end share by all pipes and will be disable only when disable
1748 	 * parent pipe.
1749 	 */
1750 	if (pipe_ctx->top_pipe == NULL) {
1751 		pipe_ctx->stream_res.tg->funcs->disable_crtc(pipe_ctx->stream_res.tg);
1752 
1753 		pipe_ctx->stream_res.tg->funcs->enable_optc_clock(pipe_ctx->stream_res.tg, false);
1754 		if (pipe_ctx->stream_res.tg->funcs->set_odm_bypass)
1755 			pipe_ctx->stream_res.tg->funcs->set_odm_bypass(
1756 					pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1757 	}
1758 
1759 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1760 		if (&dc->current_state->res_ctx.pipe_ctx[i] == pipe_ctx)
1761 			break;
1762 
1763 	if (i == dc->res_pool->pipe_count)
1764 		return;
1765 
1766 	pipe_ctx->stream = NULL;
1767 	DC_LOG_DEBUG("Reset back end for pipe %d, tg:%d\n",
1768 					pipe_ctx->pipe_idx, pipe_ctx->stream_res.tg->inst);
1769 }
1770 
1771 static void dcn20_reset_hw_ctx_wrap(
1772 		struct dc *dc,
1773 		struct dc_state *context)
1774 {
1775 	int i;
1776 
1777 	/* Reset Back End*/
1778 	for (i = dc->res_pool->pipe_count - 1; i >= 0 ; i--) {
1779 		struct pipe_ctx *pipe_ctx_old =
1780 			&dc->current_state->res_ctx.pipe_ctx[i];
1781 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1782 
1783 		if (!pipe_ctx_old->stream)
1784 			continue;
1785 
1786 		if (pipe_ctx_old->top_pipe)
1787 			continue;
1788 
1789 		if (!pipe_ctx->stream ||
1790 				pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
1791 			struct clock_source *old_clk = pipe_ctx_old->clock_source;
1792 
1793 			dcn20_reset_back_end_for_pipe(dc, pipe_ctx_old, dc->current_state);
1794 			if (dc->hwss.enable_stream_gating)
1795 				dc->hwss.enable_stream_gating(dc, pipe_ctx);
1796 			if (old_clk)
1797 				old_clk->funcs->cs_power_down(old_clk);
1798 		}
1799 	}
1800 }
1801 
1802 static void dcn20_update_mpcc(struct dc *dc, struct pipe_ctx *pipe_ctx)
1803 {
1804 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
1805 	struct mpcc_blnd_cfg blnd_cfg = { {0} };
1806 	bool per_pixel_alpha = pipe_ctx->plane_state->per_pixel_alpha && pipe_ctx->bottom_pipe;
1807 	int mpcc_id;
1808 	struct mpcc *new_mpcc;
1809 	struct mpc *mpc = dc->res_pool->mpc;
1810 	struct mpc_tree *mpc_tree_params = &(pipe_ctx->stream_res.opp->mpc_tree_params);
1811 
1812 	// input to MPCC is always RGB, by default leave black_color at 0
1813 	if (dc->debug.visual_confirm == VISUAL_CONFIRM_HDR) {
1814 		dcn10_get_hdr_visual_confirm_color(
1815 				pipe_ctx, &blnd_cfg.black_color);
1816 	} else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SURFACE) {
1817 		dcn10_get_surface_visual_confirm_color(
1818 				pipe_ctx, &blnd_cfg.black_color);
1819 	}
1820 
1821 	if (per_pixel_alpha)
1822 		blnd_cfg.alpha_mode = MPCC_ALPHA_BLEND_MODE_PER_PIXEL_ALPHA;
1823 	else
1824 		blnd_cfg.alpha_mode = MPCC_ALPHA_BLEND_MODE_GLOBAL_ALPHA;
1825 
1826 	blnd_cfg.overlap_only = false;
1827 	blnd_cfg.global_gain = 0xff;
1828 
1829 	if (pipe_ctx->plane_state->global_alpha)
1830 		blnd_cfg.global_alpha = pipe_ctx->plane_state->global_alpha_value;
1831 	else
1832 		blnd_cfg.global_alpha = 0xff;
1833 
1834 	blnd_cfg.background_color_bpc = 4;
1835 	blnd_cfg.bottom_gain_mode = 0;
1836 	blnd_cfg.top_gain = 0x1f000;
1837 	blnd_cfg.bottom_inside_gain = 0x1f000;
1838 	blnd_cfg.bottom_outside_gain = 0x1f000;
1839 	blnd_cfg.pre_multiplied_alpha = per_pixel_alpha;
1840 
1841 	/*
1842 	 * TODO: remove hack
1843 	 * Note: currently there is a bug in init_hw such that
1844 	 * on resume from hibernate, BIOS sets up MPCC0, and
1845 	 * we do mpcc_remove but the mpcc cannot go to idle
1846 	 * after remove. This cause us to pick mpcc1 here,
1847 	 * which causes a pstate hang for yet unknown reason.
1848 	 */
1849 	mpcc_id = hubp->inst;
1850 
1851 	/* If there is no full update, don't need to touch MPC tree*/
1852 	if (!pipe_ctx->plane_state->update_flags.bits.full_update) {
1853 		mpc->funcs->update_blending(mpc, &blnd_cfg, mpcc_id);
1854 		return;
1855 	}
1856 
1857 	/* check if this MPCC is already being used */
1858 	new_mpcc = mpc->funcs->get_mpcc_for_dpp(mpc_tree_params, mpcc_id);
1859 	/* remove MPCC if being used */
1860 	if (new_mpcc != NULL)
1861 		mpc->funcs->remove_mpcc(mpc, mpc_tree_params, new_mpcc);
1862 	else
1863 		if (dc->debug.sanity_checks)
1864 			mpc->funcs->assert_mpcc_idle_before_connect(
1865 					dc->res_pool->mpc, mpcc_id);
1866 
1867 	/* Call MPC to insert new plane */
1868 	new_mpcc = mpc->funcs->insert_plane(dc->res_pool->mpc,
1869 			mpc_tree_params,
1870 			&blnd_cfg,
1871 			NULL,
1872 			NULL,
1873 			hubp->inst,
1874 			mpcc_id);
1875 
1876 	ASSERT(new_mpcc != NULL);
1877 	hubp->opp_id = pipe_ctx->stream_res.opp->inst;
1878 	hubp->mpcc_id = mpcc_id;
1879 }
1880 
1881 static int find_free_gsl_group(const struct dc *dc)
1882 {
1883 	if (dc->res_pool->gsl_groups.gsl_0 == 0)
1884 		return 1;
1885 	if (dc->res_pool->gsl_groups.gsl_1 == 0)
1886 		return 2;
1887 	if (dc->res_pool->gsl_groups.gsl_2 == 0)
1888 		return 3;
1889 
1890 	return 0;
1891 }
1892 
1893 /* NOTE: This is not a generic setup_gsl function (hence the suffix as_lock)
1894  * This is only used to lock pipes in pipe splitting case with immediate flip
1895  * Ordinary MPC/OTG locks suppress VUPDATE which doesn't help with immediate,
1896  * so we get tearing with freesync since we cannot flip multiple pipes
1897  * atomically.
1898  * We use GSL for this:
1899  * - immediate flip: find first available GSL group if not already assigned
1900  *                   program gsl with that group, set current OTG as master
1901  *                   and always us 0x4 = AND of flip_ready from all pipes
1902  * - vsync flip: disable GSL if used
1903  *
1904  * Groups in stream_res are stored as +1 from HW registers, i.e.
1905  * gsl_0 <=> pipe_ctx->stream_res.gsl_group == 1
1906  * Using a magic value like -1 would require tracking all inits/resets
1907  */
1908 void dcn20_setup_gsl_group_as_lock(
1909 		const struct dc *dc,
1910 		struct pipe_ctx *pipe_ctx,
1911 		bool enable)
1912 {
1913 	struct gsl_params gsl;
1914 	int group_idx;
1915 
1916 	memset(&gsl, 0, sizeof(struct gsl_params));
1917 
1918 	if (enable) {
1919 		/* return if group already assigned since GSL was set up
1920 		 * for vsync flip, we would unassign so it can't be "left over"
1921 		 */
1922 		if (pipe_ctx->stream_res.gsl_group > 0)
1923 			return;
1924 
1925 		group_idx = find_free_gsl_group(dc);
1926 		ASSERT(group_idx != 0);
1927 		pipe_ctx->stream_res.gsl_group = group_idx;
1928 
1929 		/* set gsl group reg field and mark resource used */
1930 		switch (group_idx) {
1931 		case 1:
1932 			gsl.gsl0_en = 1;
1933 			dc->res_pool->gsl_groups.gsl_0 = 1;
1934 			break;
1935 		case 2:
1936 			gsl.gsl1_en = 1;
1937 			dc->res_pool->gsl_groups.gsl_1 = 1;
1938 			break;
1939 		case 3:
1940 			gsl.gsl2_en = 1;
1941 			dc->res_pool->gsl_groups.gsl_2 = 1;
1942 			break;
1943 		default:
1944 			BREAK_TO_DEBUGGER();
1945 			return; // invalid case
1946 		}
1947 		gsl.gsl_master_en = 1;
1948 	} else {
1949 		group_idx = pipe_ctx->stream_res.gsl_group;
1950 		if (group_idx == 0)
1951 			return; // if not in use, just return
1952 
1953 		pipe_ctx->stream_res.gsl_group = 0;
1954 
1955 		/* unset gsl group reg field and mark resource free */
1956 		switch (group_idx) {
1957 		case 1:
1958 			gsl.gsl0_en = 0;
1959 			dc->res_pool->gsl_groups.gsl_0 = 0;
1960 			break;
1961 		case 2:
1962 			gsl.gsl1_en = 0;
1963 			dc->res_pool->gsl_groups.gsl_1 = 0;
1964 			break;
1965 		case 3:
1966 			gsl.gsl2_en = 0;
1967 			dc->res_pool->gsl_groups.gsl_2 = 0;
1968 			break;
1969 		default:
1970 			BREAK_TO_DEBUGGER();
1971 			return;
1972 		}
1973 		gsl.gsl_master_en = 0;
1974 	}
1975 
1976 	/* at this point we want to program whether it's to enable or disable */
1977 	if (pipe_ctx->stream_res.tg->funcs->set_gsl != NULL &&
1978 		pipe_ctx->stream_res.tg->funcs->set_gsl_source_select != NULL) {
1979 		pipe_ctx->stream_res.tg->funcs->set_gsl(
1980 			pipe_ctx->stream_res.tg,
1981 			&gsl);
1982 
1983 		pipe_ctx->stream_res.tg->funcs->set_gsl_source_select(
1984 			pipe_ctx->stream_res.tg, group_idx,	enable ? 4 : 0);
1985 	} else
1986 		BREAK_TO_DEBUGGER();
1987 }
1988 
1989 static void dcn20_set_flip_control_gsl(
1990 		struct pipe_ctx *pipe_ctx,
1991 		bool flip_immediate)
1992 {
1993 	if (pipe_ctx && pipe_ctx->plane_res.hubp->funcs->hubp_set_flip_control_surface_gsl)
1994 		pipe_ctx->plane_res.hubp->funcs->hubp_set_flip_control_surface_gsl(
1995 				pipe_ctx->plane_res.hubp, flip_immediate);
1996 
1997 }
1998 
1999 void dcn20_hw_sequencer_construct(struct dc *dc)
2000 {
2001 	dcn10_hw_sequencer_construct(dc);
2002 	dc->hwss.init_hw = dcn20_init_hw;
2003 	dc->hwss.init_pipes = NULL;
2004 	dc->hwss.unblank_stream = dcn20_unblank_stream;
2005 	dc->hwss.update_plane_addr = dcn20_update_plane_addr;
2006 	dc->hwss.disable_plane = dcn20_disable_plane,
2007 	dc->hwss.enable_stream_timing = dcn20_enable_stream_timing;
2008 	dc->hwss.program_triplebuffer = dcn20_program_tripleBuffer;
2009 	dc->hwss.set_input_transfer_func = dcn20_set_input_transfer_func;
2010 	dc->hwss.set_output_transfer_func = dcn20_set_output_transfer_func;
2011 	dc->hwss.apply_ctx_for_surface = dcn20_apply_ctx_for_surface;
2012 	dc->hwss.pipe_control_lock = dcn20_pipe_control_lock;
2013 	dc->hwss.pipe_control_lock_global = dcn20_pipe_control_lock_global;
2014 	dc->hwss.optimize_bandwidth = dcn20_optimize_bandwidth;
2015 	dc->hwss.prepare_bandwidth = dcn20_prepare_bandwidth;
2016 	dc->hwss.update_bandwidth = dcn20_update_bandwidth;
2017 	dc->hwss.enable_writeback = dcn20_enable_writeback;
2018 	dc->hwss.disable_writeback = dcn20_disable_writeback;
2019 	dc->hwss.program_output_csc = dcn20_program_output_csc;
2020 	dc->hwss.update_odm = dcn20_update_odm;
2021 	dc->hwss.blank_pixel_data = dcn20_blank_pixel_data;
2022 	dc->hwss.dmdata_status_done = dcn20_dmdata_status_done;
2023 	dc->hwss.disable_stream = dcn20_disable_stream;
2024 	dc->hwss.init_sys_ctx = dcn20_init_sys_ctx;
2025 	dc->hwss.init_vm_ctx = dcn20_init_vm_ctx;
2026 	dc->hwss.disable_stream_gating = dcn20_disable_stream_gating;
2027 	dc->hwss.enable_stream_gating = dcn20_enable_stream_gating;
2028 	dc->hwss.setup_vupdate_interrupt = dcn20_setup_vupdate_interrupt;
2029 	dc->hwss.reset_hw_ctx_wrap = dcn20_reset_hw_ctx_wrap;
2030 	dc->hwss.update_mpcc = dcn20_update_mpcc;
2031 	dc->hwss.set_flip_control_gsl = dcn20_set_flip_control_gsl;
2032 	dc->hwss.did_underflow_occur = dcn10_did_underflow_occur;
2033 }
2034