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 dcn20_enable_power_gating_plane(
68 	struct dce_hwseq *hws,
69 	bool enable)
70 {
71 	bool force_on = 1; /* disable power gating */
72 
73 	if (enable)
74 		force_on = 0;
75 
76 	/* DCHUBP0/1/2/3/4/5 */
77 	REG_UPDATE(DOMAIN0_PG_CONFIG, DOMAIN0_POWER_FORCEON, force_on);
78 	REG_UPDATE(DOMAIN2_PG_CONFIG, DOMAIN2_POWER_FORCEON, force_on);
79 	REG_UPDATE(DOMAIN4_PG_CONFIG, DOMAIN4_POWER_FORCEON, force_on);
80 	REG_UPDATE(DOMAIN6_PG_CONFIG, DOMAIN6_POWER_FORCEON, force_on);
81 	if (REG(DOMAIN8_PG_CONFIG))
82 		REG_UPDATE(DOMAIN8_PG_CONFIG, DOMAIN8_POWER_FORCEON, force_on);
83 	if (REG(DOMAIN10_PG_CONFIG))
84 		REG_UPDATE(DOMAIN10_PG_CONFIG, DOMAIN8_POWER_FORCEON, force_on);
85 
86 	/* DPP0/1/2/3/4/5 */
87 	REG_UPDATE(DOMAIN1_PG_CONFIG, DOMAIN1_POWER_FORCEON, force_on);
88 	REG_UPDATE(DOMAIN3_PG_CONFIG, DOMAIN3_POWER_FORCEON, force_on);
89 	REG_UPDATE(DOMAIN5_PG_CONFIG, DOMAIN5_POWER_FORCEON, force_on);
90 	REG_UPDATE(DOMAIN7_PG_CONFIG, DOMAIN7_POWER_FORCEON, force_on);
91 	if (REG(DOMAIN9_PG_CONFIG))
92 		REG_UPDATE(DOMAIN9_PG_CONFIG, DOMAIN9_POWER_FORCEON, force_on);
93 	if (REG(DOMAIN11_PG_CONFIG))
94 		REG_UPDATE(DOMAIN11_PG_CONFIG, DOMAIN9_POWER_FORCEON, force_on);
95 
96 	/* DCS0/1/2/3/4/5 */
97 	REG_UPDATE(DOMAIN16_PG_CONFIG, DOMAIN16_POWER_FORCEON, force_on);
98 	REG_UPDATE(DOMAIN17_PG_CONFIG, DOMAIN17_POWER_FORCEON, force_on);
99 	REG_UPDATE(DOMAIN18_PG_CONFIG, DOMAIN18_POWER_FORCEON, force_on);
100 	if (REG(DOMAIN19_PG_CONFIG))
101 		REG_UPDATE(DOMAIN19_PG_CONFIG, DOMAIN19_POWER_FORCEON, force_on);
102 	if (REG(DOMAIN20_PG_CONFIG))
103 		REG_UPDATE(DOMAIN20_PG_CONFIG, DOMAIN20_POWER_FORCEON, force_on);
104 	if (REG(DOMAIN21_PG_CONFIG))
105 		REG_UPDATE(DOMAIN21_PG_CONFIG, DOMAIN21_POWER_FORCEON, force_on);
106 }
107 
108 void dcn20_dccg_init(struct dce_hwseq *hws)
109 {
110 	/*
111 	 * set MICROSECOND_TIME_BASE_DIV
112 	 * 100Mhz refclk -> 0x120264
113 	 * 27Mhz refclk -> 0x12021b
114 	 * 48Mhz refclk -> 0x120230
115 	 *
116 	 */
117 	REG_WRITE(MICROSECOND_TIME_BASE_DIV, 0x120264);
118 
119 	/*
120 	 * set MILLISECOND_TIME_BASE_DIV
121 	 * 100Mhz refclk -> 0x1186a0
122 	 * 27Mhz refclk -> 0x106978
123 	 * 48Mhz refclk -> 0x10bb80
124 	 *
125 	 */
126 	REG_WRITE(MILLISECOND_TIME_BASE_DIV, 0x1186a0);
127 
128 	/* This value is dependent on the hardware pipeline delay so set once per SOC */
129 	REG_WRITE(DISPCLK_FREQ_CHANGE_CNTL, 0x801003c);
130 }
131 void dcn20_display_init(struct dc *dc)
132 {
133 	struct dce_hwseq *hws = dc->hwseq;
134 
135 	/* RBBMIF
136 	 * disable RBBMIF timeout detection for all clients
137 	 * Ensure RBBMIF does not drop register accesses due to the per-client timeout
138 	 */
139 	REG_WRITE(RBBMIF_TIMEOUT_DIS, 0xFFFFFFFF);
140 	REG_WRITE(RBBMIF_TIMEOUT_DIS_2, 0xFFFFFFFF);
141 
142 	/* DCCG */
143 	dcn20_dccg_init(hws);
144 
145 	REG_UPDATE(DC_MEM_GLOBAL_PWR_REQ_CNTL, DC_MEM_GLOBAL_PWR_REQ_DIS, 0);
146 
147 	/* DCHUB/MMHUBBUB
148 	 * set global timer refclk divider
149 	 * 100Mhz refclk -> 2
150 	 * 27Mhz refclk ->  1
151 	 * 48Mhz refclk ->  1
152 	 */
153 	REG_UPDATE(DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_REFDIV, 2);
154 	REG_UPDATE(DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_ENABLE, 1);
155 	REG_WRITE(REFCLK_CNTL, 0);
156 
157 	/* OPTC
158 	 * OTG_CONTROL.OTG_DISABLE_POINT_CNTL = 0x3; will be set during optc2_enable_crtc
159 	 */
160 
161 	/* AZ
162 	 * default value is 0x64 for 100Mhz ref clock, if the ref clock is 100Mhz, no need to program this regiser,
163 	 * if not, it should be programmed according to the ref clock
164 	 */
165 	REG_UPDATE(AZALIA_AUDIO_DTO, AZALIA_AUDIO_DTO_MODULE, 0x64);
166 	/* Enable controller clock gating */
167 	REG_WRITE(AZALIA_CONTROLLER_CLOCK_GATING, 0x1);
168 }
169 
170 void dcn20_disable_vga(
171 	struct dce_hwseq *hws)
172 {
173 	REG_WRITE(D1VGA_CONTROL, 0);
174 	REG_WRITE(D2VGA_CONTROL, 0);
175 	REG_WRITE(D3VGA_CONTROL, 0);
176 	REG_WRITE(D4VGA_CONTROL, 0);
177 	REG_WRITE(D5VGA_CONTROL, 0);
178 	REG_WRITE(D6VGA_CONTROL, 0);
179 }
180 
181 void dcn20_program_tripleBuffer(
182 	const struct dc *dc,
183 	struct pipe_ctx *pipe_ctx,
184 	bool enableTripleBuffer)
185 {
186 	if (pipe_ctx->plane_res.hubp && pipe_ctx->plane_res.hubp->funcs) {
187 		pipe_ctx->plane_res.hubp->funcs->hubp_enable_tripleBuffer(
188 			pipe_ctx->plane_res.hubp,
189 			enableTripleBuffer);
190 	}
191 }
192 
193 /* Blank pixel data during initialization */
194 void dcn20_init_blank(
195 		struct dc *dc,
196 		struct timing_generator *tg)
197 {
198 	enum dc_color_space color_space;
199 	struct tg_color black_color = {0};
200 	struct output_pixel_processor *opp = NULL;
201 	struct output_pixel_processor *bottom_opp = NULL;
202 	uint32_t num_opps, opp_id_src0, opp_id_src1;
203 	uint32_t otg_active_width, otg_active_height;
204 
205 	/* program opp dpg blank color */
206 	color_space = COLOR_SPACE_SRGB;
207 	color_space_to_black_color(dc, color_space, &black_color);
208 
209 	/* get the OTG active size */
210 	tg->funcs->get_otg_active_size(tg,
211 			&otg_active_width,
212 			&otg_active_height);
213 
214 	/* get the OPTC source */
215 	tg->funcs->get_optc_source(tg, &num_opps, &opp_id_src0, &opp_id_src1);
216 	ASSERT(opp_id_src0 < dc->res_pool->res_cap->num_opp);
217 	opp = dc->res_pool->opps[opp_id_src0];
218 
219 	if (num_opps == 2) {
220 		otg_active_width = otg_active_width / 2;
221 		ASSERT(opp_id_src1 < dc->res_pool->res_cap->num_opp);
222 		bottom_opp = dc->res_pool->opps[opp_id_src1];
223 	}
224 
225 	opp->funcs->opp_set_disp_pattern_generator(
226 			opp,
227 			CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR,
228 			COLOR_DEPTH_UNDEFINED,
229 			&black_color,
230 			otg_active_width,
231 			otg_active_height);
232 
233 	if (num_opps == 2) {
234 		bottom_opp->funcs->opp_set_disp_pattern_generator(
235 				bottom_opp,
236 				CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR,
237 				COLOR_DEPTH_UNDEFINED,
238 				&black_color,
239 				otg_active_width,
240 				otg_active_height);
241 	}
242 
243 	dcn20_hwss_wait_for_blank_complete(opp);
244 }
245 
246 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
247 static void dcn20_dsc_pg_control(
248 		struct dce_hwseq *hws,
249 		unsigned int dsc_inst,
250 		bool power_on)
251 {
252 	uint32_t power_gate = power_on ? 0 : 1;
253 	uint32_t pwr_status = power_on ? 0 : 2;
254 	uint32_t org_ip_request_cntl = 0;
255 
256 	if (hws->ctx->dc->debug.disable_dsc_power_gate)
257 		return;
258 
259 	if (REG(DOMAIN16_PG_CONFIG) == 0)
260 		return;
261 
262 	REG_GET(DC_IP_REQUEST_CNTL, IP_REQUEST_EN, &org_ip_request_cntl);
263 	if (org_ip_request_cntl == 0)
264 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 1);
265 
266 	switch (dsc_inst) {
267 	case 0: /* DSC0 */
268 		REG_UPDATE(DOMAIN16_PG_CONFIG,
269 				DOMAIN16_POWER_GATE, power_gate);
270 
271 		REG_WAIT(DOMAIN16_PG_STATUS,
272 				DOMAIN16_PGFSM_PWR_STATUS, pwr_status,
273 				1, 1000);
274 		break;
275 	case 1: /* DSC1 */
276 		REG_UPDATE(DOMAIN17_PG_CONFIG,
277 				DOMAIN17_POWER_GATE, power_gate);
278 
279 		REG_WAIT(DOMAIN17_PG_STATUS,
280 				DOMAIN17_PGFSM_PWR_STATUS, pwr_status,
281 				1, 1000);
282 		break;
283 	case 2: /* DSC2 */
284 		REG_UPDATE(DOMAIN18_PG_CONFIG,
285 				DOMAIN18_POWER_GATE, power_gate);
286 
287 		REG_WAIT(DOMAIN18_PG_STATUS,
288 				DOMAIN18_PGFSM_PWR_STATUS, pwr_status,
289 				1, 1000);
290 		break;
291 	case 3: /* DSC3 */
292 		REG_UPDATE(DOMAIN19_PG_CONFIG,
293 				DOMAIN19_POWER_GATE, power_gate);
294 
295 		REG_WAIT(DOMAIN19_PG_STATUS,
296 				DOMAIN19_PGFSM_PWR_STATUS, pwr_status,
297 				1, 1000);
298 		break;
299 	case 4: /* DSC4 */
300 		REG_UPDATE(DOMAIN20_PG_CONFIG,
301 				DOMAIN20_POWER_GATE, power_gate);
302 
303 		REG_WAIT(DOMAIN20_PG_STATUS,
304 				DOMAIN20_PGFSM_PWR_STATUS, pwr_status,
305 				1, 1000);
306 		break;
307 	case 5: /* DSC5 */
308 		REG_UPDATE(DOMAIN21_PG_CONFIG,
309 				DOMAIN21_POWER_GATE, power_gate);
310 
311 		REG_WAIT(DOMAIN21_PG_STATUS,
312 				DOMAIN21_PGFSM_PWR_STATUS, pwr_status,
313 				1, 1000);
314 		break;
315 	default:
316 		BREAK_TO_DEBUGGER();
317 		break;
318 	}
319 
320 	if (org_ip_request_cntl == 0)
321 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 0);
322 }
323 #endif
324 
325 static void dcn20_dpp_pg_control(
326 		struct dce_hwseq *hws,
327 		unsigned int dpp_inst,
328 		bool power_on)
329 {
330 	uint32_t power_gate = power_on ? 0 : 1;
331 	uint32_t pwr_status = power_on ? 0 : 2;
332 
333 	if (hws->ctx->dc->debug.disable_dpp_power_gate)
334 		return;
335 	if (REG(DOMAIN1_PG_CONFIG) == 0)
336 		return;
337 
338 	switch (dpp_inst) {
339 	case 0: /* DPP0 */
340 		REG_UPDATE(DOMAIN1_PG_CONFIG,
341 				DOMAIN1_POWER_GATE, power_gate);
342 
343 		REG_WAIT(DOMAIN1_PG_STATUS,
344 				DOMAIN1_PGFSM_PWR_STATUS, pwr_status,
345 				1, 1000);
346 		break;
347 	case 1: /* DPP1 */
348 		REG_UPDATE(DOMAIN3_PG_CONFIG,
349 				DOMAIN3_POWER_GATE, power_gate);
350 
351 		REG_WAIT(DOMAIN3_PG_STATUS,
352 				DOMAIN3_PGFSM_PWR_STATUS, pwr_status,
353 				1, 1000);
354 		break;
355 	case 2: /* DPP2 */
356 		REG_UPDATE(DOMAIN5_PG_CONFIG,
357 				DOMAIN5_POWER_GATE, power_gate);
358 
359 		REG_WAIT(DOMAIN5_PG_STATUS,
360 				DOMAIN5_PGFSM_PWR_STATUS, pwr_status,
361 				1, 1000);
362 		break;
363 	case 3: /* DPP3 */
364 		REG_UPDATE(DOMAIN7_PG_CONFIG,
365 				DOMAIN7_POWER_GATE, power_gate);
366 
367 		REG_WAIT(DOMAIN7_PG_STATUS,
368 				DOMAIN7_PGFSM_PWR_STATUS, pwr_status,
369 				1, 1000);
370 		break;
371 	case 4: /* DPP4 */
372 		REG_UPDATE(DOMAIN9_PG_CONFIG,
373 				DOMAIN9_POWER_GATE, power_gate);
374 
375 		REG_WAIT(DOMAIN9_PG_STATUS,
376 				DOMAIN9_PGFSM_PWR_STATUS, pwr_status,
377 				1, 1000);
378 		break;
379 	case 5: /* DPP5 */
380 		/*
381 		 * Do not power gate DPP5, should be left at HW default, power on permanently.
382 		 * PG on Pipe5 is De-featured, attempting to put it to PG state may result in hard
383 		 * reset.
384 		 * REG_UPDATE(DOMAIN11_PG_CONFIG,
385 		 *		DOMAIN11_POWER_GATE, power_gate);
386 		 *
387 		 * REG_WAIT(DOMAIN11_PG_STATUS,
388 		 *		DOMAIN11_PGFSM_PWR_STATUS, pwr_status,
389 		 * 		1, 1000);
390 		 */
391 		break;
392 	default:
393 		BREAK_TO_DEBUGGER();
394 		break;
395 	}
396 }
397 
398 
399 static void dcn20_hubp_pg_control(
400 		struct dce_hwseq *hws,
401 		unsigned int hubp_inst,
402 		bool power_on)
403 {
404 	uint32_t power_gate = power_on ? 0 : 1;
405 	uint32_t pwr_status = power_on ? 0 : 2;
406 
407 	if (hws->ctx->dc->debug.disable_hubp_power_gate)
408 		return;
409 	if (REG(DOMAIN0_PG_CONFIG) == 0)
410 		return;
411 
412 	switch (hubp_inst) {
413 	case 0: /* DCHUBP0 */
414 		REG_UPDATE(DOMAIN0_PG_CONFIG,
415 				DOMAIN0_POWER_GATE, power_gate);
416 
417 		REG_WAIT(DOMAIN0_PG_STATUS,
418 				DOMAIN0_PGFSM_PWR_STATUS, pwr_status,
419 				1, 1000);
420 		break;
421 	case 1: /* DCHUBP1 */
422 		REG_UPDATE(DOMAIN2_PG_CONFIG,
423 				DOMAIN2_POWER_GATE, power_gate);
424 
425 		REG_WAIT(DOMAIN2_PG_STATUS,
426 				DOMAIN2_PGFSM_PWR_STATUS, pwr_status,
427 				1, 1000);
428 		break;
429 	case 2: /* DCHUBP2 */
430 		REG_UPDATE(DOMAIN4_PG_CONFIG,
431 				DOMAIN4_POWER_GATE, power_gate);
432 
433 		REG_WAIT(DOMAIN4_PG_STATUS,
434 				DOMAIN4_PGFSM_PWR_STATUS, pwr_status,
435 				1, 1000);
436 		break;
437 	case 3: /* DCHUBP3 */
438 		REG_UPDATE(DOMAIN6_PG_CONFIG,
439 				DOMAIN6_POWER_GATE, power_gate);
440 
441 		REG_WAIT(DOMAIN6_PG_STATUS,
442 				DOMAIN6_PGFSM_PWR_STATUS, pwr_status,
443 				1, 1000);
444 		break;
445 	case 4: /* DCHUBP4 */
446 		REG_UPDATE(DOMAIN8_PG_CONFIG,
447 				DOMAIN8_POWER_GATE, power_gate);
448 
449 		REG_WAIT(DOMAIN8_PG_STATUS,
450 				DOMAIN8_PGFSM_PWR_STATUS, pwr_status,
451 				1, 1000);
452 		break;
453 	case 5: /* DCHUBP5 */
454 		/*
455 		 * Do not power gate DCHUB5, should be left at HW default, power on permanently.
456 		 * PG on Pipe5 is De-featured, attempting to put it to PG state may result in hard
457 		 * reset.
458 		 * REG_UPDATE(DOMAIN10_PG_CONFIG,
459 		 *		DOMAIN10_POWER_GATE, power_gate);
460 		 *
461 		 * REG_WAIT(DOMAIN10_PG_STATUS,
462 		 *		DOMAIN10_PGFSM_PWR_STATUS, pwr_status,
463 		 *		1, 1000);
464 		 */
465 		break;
466 	default:
467 		BREAK_TO_DEBUGGER();
468 		break;
469 	}
470 }
471 
472 
473 /* disable HW used by plane.
474  * note:  cannot disable until disconnect is complete
475  */
476 static void dcn20_plane_atomic_disable(struct dc *dc, struct pipe_ctx *pipe_ctx)
477 {
478 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
479 	struct dpp *dpp = pipe_ctx->plane_res.dpp;
480 
481 	dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe_ctx);
482 
483 	/* In flip immediate with pipe splitting case GSL is used for
484 	 * synchronization so we must disable it when the plane is disabled.
485 	 */
486 	if (pipe_ctx->stream_res.gsl_group != 0)
487 		dcn20_setup_gsl_group_as_lock(dc, pipe_ctx, false);
488 
489 	dc->hwss.set_flip_control_gsl(pipe_ctx, false);
490 
491 	hubp->funcs->hubp_clk_cntl(hubp, false);
492 
493 	dpp->funcs->dpp_dppclk_control(dpp, false, false);
494 
495 	hubp->power_gated = true;
496 	dc->optimized_required = false; /* We're powering off, no need to optimize */
497 
498 	dc->hwss.plane_atomic_power_down(dc,
499 			pipe_ctx->plane_res.dpp,
500 			pipe_ctx->plane_res.hubp);
501 
502 	pipe_ctx->stream = NULL;
503 	memset(&pipe_ctx->stream_res, 0, sizeof(pipe_ctx->stream_res));
504 	memset(&pipe_ctx->plane_res, 0, sizeof(pipe_ctx->plane_res));
505 	pipe_ctx->top_pipe = NULL;
506 	pipe_ctx->bottom_pipe = NULL;
507 	pipe_ctx->plane_state = NULL;
508 }
509 
510 
511 void dcn20_disable_plane(struct dc *dc, struct pipe_ctx *pipe_ctx)
512 {
513 	DC_LOGGER_INIT(dc->ctx->logger);
514 
515 	if (!pipe_ctx->plane_res.hubp || pipe_ctx->plane_res.hubp->power_gated)
516 		return;
517 
518 	dcn20_plane_atomic_disable(dc, pipe_ctx);
519 
520 	DC_LOG_DC("Power down front end %d\n",
521 					pipe_ctx->pipe_idx);
522 }
523 
524 enum dc_status dcn20_enable_stream_timing(
525 		struct pipe_ctx *pipe_ctx,
526 		struct dc_state *context,
527 		struct dc *dc)
528 {
529 	struct dc_stream_state *stream = pipe_ctx->stream;
530 	struct drr_params params = {0};
531 	unsigned int event_triggers = 0;
532 	struct pipe_ctx *odm_pipe;
533 	int opp_cnt = 1;
534 	int opp_inst[MAX_PIPES] = { pipe_ctx->stream_res.opp->inst };
535 
536 	/* by upper caller loop, pipe0 is parent pipe and be called first.
537 	 * back end is set up by for pipe0. Other children pipe share back end
538 	 * with pipe 0. No program is needed.
539 	 */
540 	if (pipe_ctx->top_pipe != NULL)
541 		return DC_OK;
542 
543 	/* TODO check if timing_changed, disable stream if timing changed */
544 
545 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
546 		opp_inst[opp_cnt] = odm_pipe->stream_res.opp->inst;
547 		opp_cnt++;
548 	}
549 
550 	if (opp_cnt > 1)
551 		pipe_ctx->stream_res.tg->funcs->set_odm_combine(
552 				pipe_ctx->stream_res.tg,
553 				opp_inst, opp_cnt,
554 				&pipe_ctx->stream->timing);
555 
556 	/* HW program guide assume display already disable
557 	 * by unplug sequence. OTG assume stop.
558 	 */
559 	pipe_ctx->stream_res.tg->funcs->enable_optc_clock(pipe_ctx->stream_res.tg, true);
560 
561 	if (false == pipe_ctx->clock_source->funcs->program_pix_clk(
562 			pipe_ctx->clock_source,
563 			&pipe_ctx->stream_res.pix_clk_params,
564 			&pipe_ctx->pll_settings)) {
565 		BREAK_TO_DEBUGGER();
566 		return DC_ERROR_UNEXPECTED;
567 	}
568 
569 	pipe_ctx->stream_res.tg->funcs->program_timing(
570 			pipe_ctx->stream_res.tg,
571 			&stream->timing,
572 			pipe_ctx->pipe_dlg_param.vready_offset,
573 			pipe_ctx->pipe_dlg_param.vstartup_start,
574 			pipe_ctx->pipe_dlg_param.vupdate_offset,
575 			pipe_ctx->pipe_dlg_param.vupdate_width,
576 			pipe_ctx->stream->signal,
577 			true);
578 
579 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
580 		odm_pipe->stream_res.opp->funcs->opp_pipe_clock_control(
581 				odm_pipe->stream_res.opp,
582 				true);
583 
584 	pipe_ctx->stream_res.opp->funcs->opp_pipe_clock_control(
585 			pipe_ctx->stream_res.opp,
586 			true);
587 
588 	dc->hwss.blank_pixel_data(dc, pipe_ctx, true);
589 
590 	/* VTG is  within DCHUB command block. DCFCLK is always on */
591 	if (false == pipe_ctx->stream_res.tg->funcs->enable_crtc(pipe_ctx->stream_res.tg)) {
592 		BREAK_TO_DEBUGGER();
593 		return DC_ERROR_UNEXPECTED;
594 	}
595 
596 	dcn20_hwss_wait_for_blank_complete(pipe_ctx->stream_res.opp);
597 
598 	params.vertical_total_min = stream->adjust.v_total_min;
599 	params.vertical_total_max = stream->adjust.v_total_max;
600 	params.vertical_total_mid = stream->adjust.v_total_mid;
601 	params.vertical_total_mid_frame_num = stream->adjust.v_total_mid_frame_num;
602 	if (pipe_ctx->stream_res.tg->funcs->set_drr)
603 		pipe_ctx->stream_res.tg->funcs->set_drr(
604 			pipe_ctx->stream_res.tg, &params);
605 
606 	// DRR should set trigger event to monitor surface update event
607 	if (stream->adjust.v_total_min != 0 && stream->adjust.v_total_max != 0)
608 		event_triggers = 0x80;
609 	if (pipe_ctx->stream_res.tg->funcs->set_static_screen_control)
610 		pipe_ctx->stream_res.tg->funcs->set_static_screen_control(
611 				pipe_ctx->stream_res.tg, event_triggers);
612 
613 	/* TODO program crtc source select for non-virtual signal*/
614 	/* TODO program FMT */
615 	/* TODO setup link_enc */
616 	/* TODO set stream attributes */
617 	/* TODO program audio */
618 	/* TODO enable stream if timing changed */
619 	/* TODO unblank stream if DP */
620 
621 	return DC_OK;
622 }
623 
624 void dcn20_program_output_csc(struct dc *dc,
625 		struct pipe_ctx *pipe_ctx,
626 		enum dc_color_space colorspace,
627 		uint16_t *matrix,
628 		int opp_id)
629 {
630 	struct mpc *mpc = dc->res_pool->mpc;
631 	enum mpc_output_csc_mode ocsc_mode = MPC_OUTPUT_CSC_COEF_A;
632 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
633 
634 	if (mpc->funcs->power_on_mpc_mem_pwr)
635 		mpc->funcs->power_on_mpc_mem_pwr(mpc, mpcc_id, true);
636 
637 	if (pipe_ctx->stream->csc_color_matrix.enable_adjustment == true) {
638 		if (mpc->funcs->set_output_csc != NULL)
639 			mpc->funcs->set_output_csc(mpc,
640 					opp_id,
641 					matrix,
642 					ocsc_mode);
643 	} else {
644 		if (mpc->funcs->set_ocsc_default != NULL)
645 			mpc->funcs->set_ocsc_default(mpc,
646 					opp_id,
647 					colorspace,
648 					ocsc_mode);
649 	}
650 }
651 
652 bool dcn20_set_output_transfer_func(struct pipe_ctx *pipe_ctx,
653 				const struct dc_stream_state *stream)
654 {
655 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
656 	struct mpc *mpc = pipe_ctx->stream_res.opp->ctx->dc->res_pool->mpc;
657 	struct pwl_params *params = NULL;
658 	/*
659 	 * program OGAM only for the top pipe
660 	 * if there is a pipe split then fix diagnostic is required:
661 	 * how to pass OGAM parameter for stream.
662 	 * if programming for all pipes is required then remove condition
663 	 * pipe_ctx->top_pipe == NULL ,but then fix the diagnostic.
664 	 */
665 	if (mpc->funcs->power_on_mpc_mem_pwr)
666 		mpc->funcs->power_on_mpc_mem_pwr(mpc, mpcc_id, true);
667 	if (pipe_ctx->top_pipe == NULL
668 			&& mpc->funcs->set_output_gamma && stream->out_transfer_func) {
669 		if (stream->out_transfer_func->type == TF_TYPE_HWPWL)
670 			params = &stream->out_transfer_func->pwl;
671 		else if (pipe_ctx->stream->out_transfer_func->type ==
672 			TF_TYPE_DISTRIBUTED_POINTS &&
673 			cm_helper_translate_curve_to_hw_format(
674 			stream->out_transfer_func,
675 			&mpc->blender_params, false))
676 			params = &mpc->blender_params;
677 		/*
678 		 * there is no ROM
679 		 */
680 		if (stream->out_transfer_func->type == TF_TYPE_PREDEFINED)
681 			BREAK_TO_DEBUGGER();
682 	}
683 	/*
684 	 * if above if is not executed then 'params' equal to 0 and set in bypass
685 	 */
686 	mpc->funcs->set_output_gamma(mpc, mpcc_id, params);
687 
688 	return true;
689 }
690 
691 bool dcn20_set_blend_lut(
692 	struct pipe_ctx *pipe_ctx, const struct dc_plane_state *plane_state)
693 {
694 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
695 	bool result = true;
696 	struct pwl_params *blend_lut = NULL;
697 
698 	if (plane_state->blend_tf) {
699 		if (plane_state->blend_tf->type == TF_TYPE_HWPWL)
700 			blend_lut = &plane_state->blend_tf->pwl;
701 		else if (plane_state->blend_tf->type == TF_TYPE_DISTRIBUTED_POINTS) {
702 			cm_helper_translate_curve_to_hw_format(
703 					plane_state->blend_tf,
704 					&dpp_base->regamma_params, false);
705 			blend_lut = &dpp_base->regamma_params;
706 		}
707 	}
708 	result = dpp_base->funcs->dpp_program_blnd_lut(dpp_base, blend_lut);
709 
710 	return result;
711 }
712 
713 bool dcn20_set_shaper_3dlut(
714 	struct pipe_ctx *pipe_ctx, const struct dc_plane_state *plane_state)
715 {
716 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
717 	bool result = true;
718 	struct pwl_params *shaper_lut = NULL;
719 
720 	if (plane_state->in_shaper_func) {
721 		if (plane_state->in_shaper_func->type == TF_TYPE_HWPWL)
722 			shaper_lut = &plane_state->in_shaper_func->pwl;
723 		else if (plane_state->in_shaper_func->type == TF_TYPE_DISTRIBUTED_POINTS) {
724 			cm_helper_translate_curve_to_hw_format(
725 					plane_state->in_shaper_func,
726 					&dpp_base->shaper_params, true);
727 			shaper_lut = &dpp_base->shaper_params;
728 		}
729 	}
730 
731 	result = dpp_base->funcs->dpp_program_shaper_lut(dpp_base, shaper_lut);
732 	if (plane_state->lut3d_func &&
733 		plane_state->lut3d_func->state.bits.initialized == 1)
734 		result = dpp_base->funcs->dpp_program_3dlut(dpp_base,
735 								&plane_state->lut3d_func->lut_3d);
736 	else
737 		result = dpp_base->funcs->dpp_program_3dlut(dpp_base, NULL);
738 
739 	if (plane_state->lut3d_func &&
740 		plane_state->lut3d_func->state.bits.initialized == 1 &&
741 		plane_state->lut3d_func->hdr_multiplier != 0)
742 		dpp_base->funcs->dpp_set_hdr_multiplier(dpp_base,
743 				plane_state->lut3d_func->hdr_multiplier);
744 	else
745 		dpp_base->funcs->dpp_set_hdr_multiplier(dpp_base, 0x1f000);
746 
747 	return result;
748 }
749 
750 bool dcn20_set_input_transfer_func(struct pipe_ctx *pipe_ctx,
751 					  const struct dc_plane_state *plane_state)
752 {
753 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
754 	const struct dc_transfer_func *tf = NULL;
755 	bool result = true;
756 	bool use_degamma_ram = false;
757 
758 	if (dpp_base == NULL || plane_state == NULL)
759 		return false;
760 
761 	dcn20_set_shaper_3dlut(pipe_ctx, plane_state);
762 	dcn20_set_blend_lut(pipe_ctx, plane_state);
763 
764 	if (plane_state->in_transfer_func)
765 		tf = plane_state->in_transfer_func;
766 
767 
768 	if (tf == NULL) {
769 		dpp_base->funcs->dpp_set_degamma(dpp_base,
770 				IPP_DEGAMMA_MODE_BYPASS);
771 		return true;
772 	}
773 
774 	if (tf->type == TF_TYPE_HWPWL || tf->type == TF_TYPE_DISTRIBUTED_POINTS)
775 		use_degamma_ram = true;
776 
777 	if (use_degamma_ram == true) {
778 		if (tf->type == TF_TYPE_HWPWL)
779 			dpp_base->funcs->dpp_program_degamma_pwl(dpp_base,
780 					&tf->pwl);
781 		else if (tf->type == TF_TYPE_DISTRIBUTED_POINTS) {
782 			cm_helper_translate_curve_to_degamma_hw_format(tf,
783 					&dpp_base->degamma_params);
784 			dpp_base->funcs->dpp_program_degamma_pwl(dpp_base,
785 				&dpp_base->degamma_params);
786 		}
787 		return true;
788 	}
789 	/* handle here the optimized cases when de-gamma ROM could be used.
790 	 *
791 	 */
792 	if (tf->type == TF_TYPE_PREDEFINED) {
793 		switch (tf->tf) {
794 		case TRANSFER_FUNCTION_SRGB:
795 			dpp_base->funcs->dpp_set_degamma(dpp_base,
796 					IPP_DEGAMMA_MODE_HW_sRGB);
797 			break;
798 		case TRANSFER_FUNCTION_BT709:
799 			dpp_base->funcs->dpp_set_degamma(dpp_base,
800 					IPP_DEGAMMA_MODE_HW_xvYCC);
801 			break;
802 		case TRANSFER_FUNCTION_LINEAR:
803 			dpp_base->funcs->dpp_set_degamma(dpp_base,
804 					IPP_DEGAMMA_MODE_BYPASS);
805 			break;
806 		case TRANSFER_FUNCTION_PQ:
807 		default:
808 			result = false;
809 			break;
810 		}
811 	} else if (tf->type == TF_TYPE_BYPASS)
812 		dpp_base->funcs->dpp_set_degamma(dpp_base,
813 				IPP_DEGAMMA_MODE_BYPASS);
814 	else {
815 		/*
816 		 * if we are here, we did not handle correctly.
817 		 * fix is required for this use case
818 		 */
819 		BREAK_TO_DEBUGGER();
820 		dpp_base->funcs->dpp_set_degamma(dpp_base,
821 				IPP_DEGAMMA_MODE_BYPASS);
822 	}
823 
824 	return result;
825 }
826 
827 static void dcn20_update_odm(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe_ctx)
828 {
829 	struct pipe_ctx *odm_pipe;
830 	int opp_cnt = 1;
831 	int opp_inst[MAX_PIPES] = { pipe_ctx->stream_res.opp->inst };
832 
833 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
834 		opp_inst[opp_cnt] = odm_pipe->stream_res.opp->inst;
835 		opp_cnt++;
836 	}
837 
838 	if (opp_cnt > 1)
839 		pipe_ctx->stream_res.tg->funcs->set_odm_combine(
840 				pipe_ctx->stream_res.tg,
841 				opp_inst, opp_cnt,
842 				&pipe_ctx->stream->timing);
843 	else
844 		pipe_ctx->stream_res.tg->funcs->set_odm_bypass(
845 				pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
846 }
847 
848 void dcn20_blank_pixel_data(
849 		struct dc *dc,
850 		struct pipe_ctx *pipe_ctx,
851 		bool blank)
852 {
853 	struct tg_color black_color = {0};
854 	struct stream_resource *stream_res = &pipe_ctx->stream_res;
855 	struct dc_stream_state *stream = pipe_ctx->stream;
856 	enum dc_color_space color_space = stream->output_color_space;
857 	enum controller_dp_test_pattern test_pattern = CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR;
858 	struct pipe_ctx *odm_pipe;
859 	int odm_cnt = 1;
860 
861 	int width = stream->timing.h_addressable + stream->timing.h_border_left + stream->timing.h_border_right;
862 	int height = stream->timing.v_addressable + stream->timing.v_border_bottom + stream->timing.v_border_top;
863 
864 	/* get opp dpg blank color */
865 	color_space_to_black_color(dc, color_space, &black_color);
866 
867 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
868 		odm_cnt++;
869 
870 	width = width / odm_cnt;
871 
872 	if (blank) {
873 		if (stream_res->abm)
874 			stream_res->abm->funcs->set_abm_immediate_disable(stream_res->abm);
875 
876 		if (dc->debug.visual_confirm != VISUAL_CONFIRM_DISABLE)
877 			test_pattern = CONTROLLER_DP_TEST_PATTERN_COLORSQUARES;
878 	} else {
879 		test_pattern = CONTROLLER_DP_TEST_PATTERN_VIDEOMODE;
880 	}
881 
882 	stream_res->opp->funcs->opp_set_disp_pattern_generator(
883 			stream_res->opp,
884 			test_pattern,
885 			stream->timing.display_color_depth,
886 			&black_color,
887 			width,
888 			height);
889 
890 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
891 		odm_pipe->stream_res.opp->funcs->opp_set_disp_pattern_generator(
892 				odm_pipe->stream_res.opp,
893 				dc->debug.visual_confirm != VISUAL_CONFIRM_DISABLE && blank ?
894 						CONTROLLER_DP_TEST_PATTERN_COLORRAMP : test_pattern,
895 				stream->timing.display_color_depth,
896 				&black_color,
897 				width,
898 				height);
899 	}
900 
901 	if (!blank)
902 		if (stream_res->abm) {
903 			stream_res->abm->funcs->set_pipe(stream_res->abm, stream_res->tg->inst + 1);
904 			stream_res->abm->funcs->set_abm_level(stream_res->abm, stream->abm_level);
905 		}
906 }
907 
908 
909 static void dcn20_power_on_plane(
910 	struct dce_hwseq *hws,
911 	struct pipe_ctx *pipe_ctx)
912 {
913 	DC_LOGGER_INIT(hws->ctx->logger);
914 	if (REG(DC_IP_REQUEST_CNTL)) {
915 		REG_SET(DC_IP_REQUEST_CNTL, 0,
916 				IP_REQUEST_EN, 1);
917 		dcn20_dpp_pg_control(hws, pipe_ctx->plane_res.dpp->inst, true);
918 		dcn20_hubp_pg_control(hws, pipe_ctx->plane_res.hubp->inst, true);
919 		REG_SET(DC_IP_REQUEST_CNTL, 0,
920 				IP_REQUEST_EN, 0);
921 		DC_LOG_DEBUG(
922 				"Un-gated front end for pipe %d\n", pipe_ctx->plane_res.hubp->inst);
923 	}
924 }
925 
926 void dcn20_enable_plane(
927 	struct dc *dc,
928 	struct pipe_ctx *pipe_ctx,
929 	struct dc_state *context)
930 {
931 	//if (dc->debug.sanity_checks) {
932 	//	dcn10_verify_allow_pstate_change_high(dc);
933 	//}
934 	dcn20_power_on_plane(dc->hwseq, pipe_ctx);
935 
936 	/* enable DCFCLK current DCHUB */
937 	pipe_ctx->plane_res.hubp->funcs->hubp_clk_cntl(pipe_ctx->plane_res.hubp, true);
938 
939 	/* initialize HUBP on power up */
940 	pipe_ctx->plane_res.hubp->funcs->hubp_init(pipe_ctx->plane_res.hubp);
941 
942 	/* make sure OPP_PIPE_CLOCK_EN = 1 */
943 	pipe_ctx->stream_res.opp->funcs->opp_pipe_clock_control(
944 			pipe_ctx->stream_res.opp,
945 			true);
946 
947 /* TODO: enable/disable in dm as per update type.
948 	if (plane_state) {
949 		DC_LOG_DC(dc->ctx->logger,
950 				"Pipe:%d 0x%x: addr hi:0x%x, "
951 				"addr low:0x%x, "
952 				"src: %d, %d, %d,"
953 				" %d; dst: %d, %d, %d, %d;\n",
954 				pipe_ctx->pipe_idx,
955 				plane_state,
956 				plane_state->address.grph.addr.high_part,
957 				plane_state->address.grph.addr.low_part,
958 				plane_state->src_rect.x,
959 				plane_state->src_rect.y,
960 				plane_state->src_rect.width,
961 				plane_state->src_rect.height,
962 				plane_state->dst_rect.x,
963 				plane_state->dst_rect.y,
964 				plane_state->dst_rect.width,
965 				plane_state->dst_rect.height);
966 
967 		DC_LOG_DC(dc->ctx->logger,
968 				"Pipe %d: width, height, x, y         format:%d\n"
969 				"viewport:%d, %d, %d, %d\n"
970 				"recout:  %d, %d, %d, %d\n",
971 				pipe_ctx->pipe_idx,
972 				plane_state->format,
973 				pipe_ctx->plane_res.scl_data.viewport.width,
974 				pipe_ctx->plane_res.scl_data.viewport.height,
975 				pipe_ctx->plane_res.scl_data.viewport.x,
976 				pipe_ctx->plane_res.scl_data.viewport.y,
977 				pipe_ctx->plane_res.scl_data.recout.width,
978 				pipe_ctx->plane_res.scl_data.recout.height,
979 				pipe_ctx->plane_res.scl_data.recout.x,
980 				pipe_ctx->plane_res.scl_data.recout.y);
981 		print_rq_dlg_ttu(dc, pipe_ctx);
982 	}
983 */
984 	if (dc->vm_pa_config.valid) {
985 		struct vm_system_aperture_param apt;
986 
987 		apt.sys_default.quad_part = 0;
988 
989 		apt.sys_low.quad_part = dc->vm_pa_config.system_aperture.start_addr;
990 		apt.sys_high.quad_part = dc->vm_pa_config.system_aperture.end_addr;
991 
992 		// Program system aperture settings
993 		pipe_ctx->plane_res.hubp->funcs->hubp_set_vm_system_aperture_settings(pipe_ctx->plane_res.hubp, &apt);
994 	}
995 
996 //	if (dc->debug.sanity_checks) {
997 //		dcn10_verify_allow_pstate_change_high(dc);
998 //	}
999 }
1000 
1001 
1002 void dcn20_pipe_control_lock_global(
1003 		struct dc *dc,
1004 		struct pipe_ctx *pipe,
1005 		bool lock)
1006 {
1007 	if (lock) {
1008 		pipe->stream_res.tg->funcs->lock_doublebuffer_enable(
1009 				pipe->stream_res.tg);
1010 		pipe->stream_res.tg->funcs->lock(pipe->stream_res.tg);
1011 	} else {
1012 		pipe->stream_res.tg->funcs->unlock(pipe->stream_res.tg);
1013 		pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg,
1014 				CRTC_STATE_VACTIVE);
1015 		pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg,
1016 				CRTC_STATE_VBLANK);
1017 		pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg,
1018 				CRTC_STATE_VACTIVE);
1019 		pipe->stream_res.tg->funcs->lock_doublebuffer_disable(
1020 				pipe->stream_res.tg);
1021 	}
1022 }
1023 
1024 void dcn20_pipe_control_lock(
1025 	struct dc *dc,
1026 	struct pipe_ctx *pipe,
1027 	bool lock)
1028 {
1029 	bool flip_immediate = false;
1030 
1031 	/* use TG master update lock to lock everything on the TG
1032 	 * therefore only top pipe need to lock
1033 	 */
1034 	if (pipe->top_pipe)
1035 		return;
1036 
1037 	if (pipe->plane_state != NULL)
1038 		flip_immediate = pipe->plane_state->flip_immediate;
1039 
1040 	/* In flip immediate and pipe splitting case, we need to use GSL
1041 	 * for synchronization. Only do setup on locking and on flip type change.
1042 	 */
1043 	if (lock && pipe->bottom_pipe != NULL)
1044 		if ((flip_immediate && pipe->stream_res.gsl_group == 0) ||
1045 		    (!flip_immediate && pipe->stream_res.gsl_group > 0))
1046 			dcn20_setup_gsl_group_as_lock(dc, pipe, flip_immediate);
1047 
1048 	if (pipe->plane_state != NULL && pipe->plane_state->triplebuffer_flips) {
1049 		if (lock)
1050 			pipe->stream_res.tg->funcs->triplebuffer_lock(pipe->stream_res.tg);
1051 		else
1052 			pipe->stream_res.tg->funcs->triplebuffer_unlock(pipe->stream_res.tg);
1053 	} else {
1054 		if (lock)
1055 			pipe->stream_res.tg->funcs->lock(pipe->stream_res.tg);
1056 		else
1057 			pipe->stream_res.tg->funcs->unlock(pipe->stream_res.tg);
1058 	}
1059 }
1060 
1061 static void dcn20_detect_pipe_changes(struct pipe_ctx *old_pipe, struct pipe_ctx *new_pipe)
1062 {
1063 	new_pipe->update_flags.raw = 0;
1064 
1065 	/* Exit on unchanged, unused pipe */
1066 	if (!old_pipe->plane_state && !new_pipe->plane_state)
1067 		return;
1068 	/* Detect pipe enable/disable */
1069 	if (!old_pipe->plane_state && new_pipe->plane_state) {
1070 		new_pipe->update_flags.bits.enable = 1;
1071 		new_pipe->update_flags.bits.mpcc = 1;
1072 		new_pipe->update_flags.bits.dppclk = 1;
1073 		new_pipe->update_flags.bits.hubp_interdependent = 1;
1074 		new_pipe->update_flags.bits.hubp_rq_dlg_ttu = 1;
1075 		new_pipe->update_flags.bits.gamut_remap = 1;
1076 		new_pipe->update_flags.bits.scaler = 1;
1077 		new_pipe->update_flags.bits.viewport = 1;
1078 		if (!new_pipe->top_pipe && !new_pipe->prev_odm_pipe) {
1079 			new_pipe->update_flags.bits.odm = 1;
1080 			new_pipe->update_flags.bits.global_sync = 1;
1081 		}
1082 		return;
1083 	}
1084 	if (old_pipe->plane_state && !new_pipe->plane_state) {
1085 		new_pipe->update_flags.bits.disable = 1;
1086 		return;
1087 	}
1088 
1089 	/* Detect top pipe only changes */
1090 	if (!new_pipe->top_pipe && !new_pipe->prev_odm_pipe) {
1091 		/* Detect odm changes */
1092 		if ((old_pipe->next_odm_pipe && new_pipe->next_odm_pipe
1093 			&& old_pipe->next_odm_pipe->pipe_idx != new_pipe->next_odm_pipe->pipe_idx)
1094 				|| (!old_pipe->next_odm_pipe && new_pipe->next_odm_pipe)
1095 				|| (old_pipe->next_odm_pipe && !new_pipe->next_odm_pipe)
1096 				|| old_pipe->stream_res.opp != new_pipe->stream_res.opp)
1097 			new_pipe->update_flags.bits.odm = 1;
1098 
1099 		/* Detect global sync changes */
1100 		if (old_pipe->pipe_dlg_param.vready_offset != new_pipe->pipe_dlg_param.vready_offset
1101 				|| old_pipe->pipe_dlg_param.vstartup_start != new_pipe->pipe_dlg_param.vstartup_start
1102 				|| old_pipe->pipe_dlg_param.vupdate_offset != new_pipe->pipe_dlg_param.vupdate_offset
1103 				|| old_pipe->pipe_dlg_param.vupdate_width != new_pipe->pipe_dlg_param.vupdate_width)
1104 			new_pipe->update_flags.bits.global_sync = 1;
1105 	}
1106 
1107 	/*
1108 	 * Detect opp / tg change, only set on change, not on enable
1109 	 * Assume mpcc inst = pipe index, if not this code needs to be updated
1110 	 * since mpcc is what is affected by these. In fact all of our sequence
1111 	 * makes this assumption at the moment with how hubp reset is matched to
1112 	 * same index mpcc reset.
1113 	 */
1114 	if (old_pipe->stream_res.opp != new_pipe->stream_res.opp)
1115 		new_pipe->update_flags.bits.opp_changed = 1;
1116 	if (old_pipe->stream_res.tg != new_pipe->stream_res.tg)
1117 		new_pipe->update_flags.bits.tg_changed = 1;
1118 
1119 	/* Detect mpcc blending changes, only dpp inst and bot matter here */
1120 	if (old_pipe->plane_res.dpp != new_pipe->plane_res.dpp
1121 			|| old_pipe->stream_res.opp != new_pipe->stream_res.opp
1122 			|| (!old_pipe->bottom_pipe && new_pipe->bottom_pipe)
1123 			|| (old_pipe->bottom_pipe && !new_pipe->bottom_pipe)
1124 			|| (old_pipe->bottom_pipe && new_pipe->bottom_pipe
1125 				&& old_pipe->bottom_pipe->plane_res.mpcc_inst
1126 					!= new_pipe->bottom_pipe->plane_res.mpcc_inst))
1127 		new_pipe->update_flags.bits.mpcc = 1;
1128 
1129 	/* Detect dppclk change */
1130 	if (old_pipe->plane_res.bw.dppclk_khz != new_pipe->plane_res.bw.dppclk_khz)
1131 		new_pipe->update_flags.bits.dppclk = 1;
1132 
1133 	/* Check for scl update */
1134 	if (memcmp(&old_pipe->plane_res.scl_data, &new_pipe->plane_res.scl_data, sizeof(struct scaler_data)))
1135 			new_pipe->update_flags.bits.scaler = 1;
1136 	/* Check for vp update */
1137 	if (memcmp(&old_pipe->plane_res.scl_data.viewport, &new_pipe->plane_res.scl_data.viewport, sizeof(struct rect))
1138 			|| memcmp(&old_pipe->plane_res.scl_data.viewport_c,
1139 				&new_pipe->plane_res.scl_data.viewport_c, sizeof(struct rect)))
1140 		new_pipe->update_flags.bits.viewport = 1;
1141 
1142 	/* Detect dlg/ttu/rq updates */
1143 	{
1144 		struct _vcs_dpi_display_dlg_regs_st old_dlg_attr = old_pipe->dlg_regs;
1145 		struct _vcs_dpi_display_ttu_regs_st old_ttu_attr = old_pipe->ttu_regs;
1146 		struct _vcs_dpi_display_dlg_regs_st *new_dlg_attr = &new_pipe->dlg_regs;
1147 		struct _vcs_dpi_display_ttu_regs_st *new_ttu_attr = &new_pipe->ttu_regs;
1148 
1149 		/* Detect pipe interdependent updates */
1150 		if (old_dlg_attr.dst_y_prefetch != new_dlg_attr->dst_y_prefetch ||
1151 				old_dlg_attr.vratio_prefetch != new_dlg_attr->vratio_prefetch ||
1152 				old_dlg_attr.vratio_prefetch_c != new_dlg_attr->vratio_prefetch_c ||
1153 				old_dlg_attr.dst_y_per_vm_vblank != new_dlg_attr->dst_y_per_vm_vblank ||
1154 				old_dlg_attr.dst_y_per_row_vblank != new_dlg_attr->dst_y_per_row_vblank ||
1155 				old_dlg_attr.dst_y_per_vm_flip != new_dlg_attr->dst_y_per_vm_flip ||
1156 				old_dlg_attr.dst_y_per_row_flip != new_dlg_attr->dst_y_per_row_flip ||
1157 				old_dlg_attr.refcyc_per_meta_chunk_vblank_l != new_dlg_attr->refcyc_per_meta_chunk_vblank_l ||
1158 				old_dlg_attr.refcyc_per_meta_chunk_vblank_c != new_dlg_attr->refcyc_per_meta_chunk_vblank_c ||
1159 				old_dlg_attr.refcyc_per_meta_chunk_flip_l != new_dlg_attr->refcyc_per_meta_chunk_flip_l ||
1160 				old_dlg_attr.refcyc_per_line_delivery_pre_l != new_dlg_attr->refcyc_per_line_delivery_pre_l ||
1161 				old_dlg_attr.refcyc_per_line_delivery_pre_c != new_dlg_attr->refcyc_per_line_delivery_pre_c ||
1162 				old_ttu_attr.refcyc_per_req_delivery_pre_l != new_ttu_attr->refcyc_per_req_delivery_pre_l ||
1163 				old_ttu_attr.refcyc_per_req_delivery_pre_c != new_ttu_attr->refcyc_per_req_delivery_pre_c ||
1164 				old_ttu_attr.refcyc_per_req_delivery_pre_cur0 != new_ttu_attr->refcyc_per_req_delivery_pre_cur0 ||
1165 				old_ttu_attr.refcyc_per_req_delivery_pre_cur1 != new_ttu_attr->refcyc_per_req_delivery_pre_cur1 ||
1166 				old_ttu_attr.min_ttu_vblank != new_ttu_attr->min_ttu_vblank ||
1167 				old_ttu_attr.qos_level_flip != new_ttu_attr->qos_level_flip) {
1168 			old_dlg_attr.dst_y_prefetch = new_dlg_attr->dst_y_prefetch;
1169 			old_dlg_attr.vratio_prefetch = new_dlg_attr->vratio_prefetch;
1170 			old_dlg_attr.vratio_prefetch_c = new_dlg_attr->vratio_prefetch_c;
1171 			old_dlg_attr.dst_y_per_vm_vblank = new_dlg_attr->dst_y_per_vm_vblank;
1172 			old_dlg_attr.dst_y_per_row_vblank = new_dlg_attr->dst_y_per_row_vblank;
1173 			old_dlg_attr.dst_y_per_vm_flip = new_dlg_attr->dst_y_per_vm_flip;
1174 			old_dlg_attr.dst_y_per_row_flip = new_dlg_attr->dst_y_per_row_flip;
1175 			old_dlg_attr.refcyc_per_meta_chunk_vblank_l = new_dlg_attr->refcyc_per_meta_chunk_vblank_l;
1176 			old_dlg_attr.refcyc_per_meta_chunk_vblank_c = new_dlg_attr->refcyc_per_meta_chunk_vblank_c;
1177 			old_dlg_attr.refcyc_per_meta_chunk_flip_l = new_dlg_attr->refcyc_per_meta_chunk_flip_l;
1178 			old_dlg_attr.refcyc_per_line_delivery_pre_l = new_dlg_attr->refcyc_per_line_delivery_pre_l;
1179 			old_dlg_attr.refcyc_per_line_delivery_pre_c = new_dlg_attr->refcyc_per_line_delivery_pre_c;
1180 			old_ttu_attr.refcyc_per_req_delivery_pre_l = new_ttu_attr->refcyc_per_req_delivery_pre_l;
1181 			old_ttu_attr.refcyc_per_req_delivery_pre_c = new_ttu_attr->refcyc_per_req_delivery_pre_c;
1182 			old_ttu_attr.refcyc_per_req_delivery_pre_cur0 = new_ttu_attr->refcyc_per_req_delivery_pre_cur0;
1183 			old_ttu_attr.refcyc_per_req_delivery_pre_cur1 = new_ttu_attr->refcyc_per_req_delivery_pre_cur1;
1184 			old_ttu_attr.min_ttu_vblank = new_ttu_attr->min_ttu_vblank;
1185 			old_ttu_attr.qos_level_flip = new_ttu_attr->qos_level_flip;
1186 			new_pipe->update_flags.bits.hubp_interdependent = 1;
1187 		}
1188 		/* Detect any other updates to ttu/rq/dlg */
1189 		if (memcmp(&old_dlg_attr, &new_pipe->dlg_regs, sizeof(old_dlg_attr)) ||
1190 				memcmp(&old_ttu_attr, &new_pipe->ttu_regs, sizeof(old_ttu_attr)) ||
1191 				memcmp(&old_pipe->rq_regs, &new_pipe->rq_regs, sizeof(old_pipe->rq_regs)))
1192 			new_pipe->update_flags.bits.hubp_rq_dlg_ttu = 1;
1193 	}
1194 }
1195 
1196 static void dcn20_update_dchubp_dpp(
1197 	struct dc *dc,
1198 	struct pipe_ctx *pipe_ctx,
1199 	struct dc_state *context)
1200 {
1201 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
1202 	struct dpp *dpp = pipe_ctx->plane_res.dpp;
1203 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1204 
1205 	if (pipe_ctx->update_flags.bits.dppclk) {
1206 		dpp->funcs->dpp_dppclk_control(dpp, false, true);
1207 
1208 		dc->res_pool->dccg->funcs->update_dpp_dto(
1209 				dc->res_pool->dccg,
1210 				dpp->inst,
1211 				pipe_ctx->plane_res.bw.dppclk_khz);
1212 	}
1213 
1214 	/* TODO: Need input parameter to tell current DCHUB pipe tie to which OTG
1215 	 * VTG is within DCHUBBUB which is commond block share by each pipe HUBP.
1216 	 * VTG is 1:1 mapping with OTG. Each pipe HUBP will select which VTG
1217 	 */
1218 	if (pipe_ctx->update_flags.bits.hubp_rq_dlg_ttu) {
1219 		hubp->funcs->hubp_vtg_sel(hubp, pipe_ctx->stream_res.tg->inst);
1220 
1221 		hubp->funcs->hubp_setup(
1222 			hubp,
1223 			&pipe_ctx->dlg_regs,
1224 			&pipe_ctx->ttu_regs,
1225 			&pipe_ctx->rq_regs,
1226 			&pipe_ctx->pipe_dlg_param);
1227 	}
1228 	if (pipe_ctx->update_flags.bits.hubp_interdependent)
1229 		hubp->funcs->hubp_setup_interdependent(
1230 			hubp,
1231 			&pipe_ctx->dlg_regs,
1232 			&pipe_ctx->ttu_regs);
1233 
1234 	if (pipe_ctx->update_flags.bits.enable ||
1235 			plane_state->update_flags.bits.bpp_change ||
1236 			plane_state->update_flags.bits.input_csc_change ||
1237 			plane_state->update_flags.bits.color_space_change ||
1238 			plane_state->update_flags.bits.coeff_reduction_change) {
1239 		struct dc_bias_and_scale bns_params = {0};
1240 
1241 		// program the input csc
1242 		dpp->funcs->dpp_setup(dpp,
1243 				plane_state->format,
1244 				EXPANSION_MODE_ZERO,
1245 				plane_state->input_csc_color_matrix,
1246 				plane_state->color_space,
1247 				NULL);
1248 
1249 		if (dpp->funcs->dpp_program_bias_and_scale) {
1250 			//TODO :for CNVC set scale and bias registers if necessary
1251 			dcn10_build_prescale_params(&bns_params, plane_state);
1252 			dpp->funcs->dpp_program_bias_and_scale(dpp, &bns_params);
1253 		}
1254 	}
1255 
1256 	if (pipe_ctx->update_flags.bits.mpcc
1257 			|| plane_state->update_flags.bits.global_alpha_change
1258 			|| plane_state->update_flags.bits.per_pixel_alpha_change) {
1259 		/* Need mpcc to be idle if changing opp */
1260 		if (pipe_ctx->update_flags.bits.opp_changed) {
1261 			struct pipe_ctx *old_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[pipe_ctx->pipe_idx];
1262 			int mpcc_inst;
1263 
1264 			for (mpcc_inst = 0; mpcc_inst < MAX_PIPES; mpcc_inst++) {
1265 				if (!old_pipe_ctx->stream_res.opp->mpcc_disconnect_pending[mpcc_inst])
1266 					continue;
1267 				dc->res_pool->mpc->funcs->wait_for_idle(dc->res_pool->mpc, mpcc_inst);
1268 				old_pipe_ctx->stream_res.opp->mpcc_disconnect_pending[mpcc_inst] = false;
1269 			}
1270 		}
1271 		dc->hwss.update_mpcc(dc, pipe_ctx);
1272 	}
1273 
1274 	if (pipe_ctx->update_flags.bits.scaler ||
1275 			plane_state->update_flags.bits.scaling_change ||
1276 			plane_state->update_flags.bits.position_change ||
1277 			plane_state->update_flags.bits.per_pixel_alpha_change ||
1278 			pipe_ctx->stream->update_flags.bits.scaling) {
1279 		pipe_ctx->plane_res.scl_data.lb_params.alpha_en = pipe_ctx->plane_state->per_pixel_alpha;
1280 		ASSERT(pipe_ctx->plane_res.scl_data.lb_params.depth == LB_PIXEL_DEPTH_30BPP);
1281 		/* scaler configuration */
1282 		pipe_ctx->plane_res.dpp->funcs->dpp_set_scaler(
1283 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data);
1284 	}
1285 
1286 	if (pipe_ctx->update_flags.bits.viewport ||
1287 			(context == dc->current_state && plane_state->update_flags.bits.scaling_change) ||
1288 			(context == dc->current_state && pipe_ctx->stream->update_flags.bits.scaling))
1289 		hubp->funcs->mem_program_viewport(
1290 			hubp,
1291 			&pipe_ctx->plane_res.scl_data.viewport,
1292 			&pipe_ctx->plane_res.scl_data.viewport_c);
1293 
1294 	/* Any updates are handled in dc interface, just need to apply existing for plane enable */
1295 	if ((pipe_ctx->update_flags.bits.enable || pipe_ctx->update_flags.bits.opp_changed)
1296 			&& pipe_ctx->stream->cursor_attributes.address.quad_part != 0) {
1297 		dc->hwss.set_cursor_position(pipe_ctx);
1298 		dc->hwss.set_cursor_attribute(pipe_ctx);
1299 
1300 		if (dc->hwss.set_cursor_sdr_white_level)
1301 			dc->hwss.set_cursor_sdr_white_level(pipe_ctx);
1302 	}
1303 
1304 	/* Any updates are handled in dc interface, just need
1305 	 * to apply existing for plane enable / opp change */
1306 	if (pipe_ctx->update_flags.bits.enable || pipe_ctx->update_flags.bits.opp_changed
1307 			|| pipe_ctx->stream->update_flags.bits.gamut_remap
1308 			|| pipe_ctx->stream->update_flags.bits.out_csc) {
1309 			/* dpp/cm gamut remap*/
1310 			dc->hwss.program_gamut_remap(pipe_ctx);
1311 
1312 		/*call the dcn2 method which uses mpc csc*/
1313 		dc->hwss.program_output_csc(dc,
1314 				pipe_ctx,
1315 				pipe_ctx->stream->output_color_space,
1316 				pipe_ctx->stream->csc_color_matrix.matrix,
1317 				hubp->opp_id);
1318 	}
1319 
1320 	if (pipe_ctx->update_flags.bits.enable ||
1321 			pipe_ctx->update_flags.bits.opp_changed ||
1322 			plane_state->update_flags.bits.pixel_format_change ||
1323 			plane_state->update_flags.bits.horizontal_mirror_change ||
1324 			plane_state->update_flags.bits.rotation_change ||
1325 			plane_state->update_flags.bits.swizzle_change ||
1326 			plane_state->update_flags.bits.dcc_change ||
1327 			plane_state->update_flags.bits.bpp_change ||
1328 			plane_state->update_flags.bits.scaling_change ||
1329 			plane_state->update_flags.bits.plane_size_change) {
1330 		struct plane_size size = plane_state->plane_size;
1331 
1332 		size.surface_size = pipe_ctx->plane_res.scl_data.viewport;
1333 		hubp->funcs->hubp_program_surface_config(
1334 			hubp,
1335 			plane_state->format,
1336 			&plane_state->tiling_info,
1337 			&size,
1338 			plane_state->rotation,
1339 			&plane_state->dcc,
1340 			plane_state->horizontal_mirror,
1341 			0);
1342 		hubp->power_gated = false;
1343 	}
1344 
1345 	if (pipe_ctx->update_flags.bits.enable || plane_state->update_flags.bits.addr_update)
1346 		dc->hwss.update_plane_addr(dc, pipe_ctx);
1347 
1348 	if (pipe_ctx->update_flags.bits.enable)
1349 		hubp->funcs->set_blank(hubp, false);
1350 }
1351 
1352 
1353 static void dcn20_program_pipe(
1354 		struct dc *dc,
1355 		struct pipe_ctx *pipe_ctx,
1356 		struct dc_state *context)
1357 {
1358 	/* Only need to unblank on top pipe */
1359 	if ((pipe_ctx->update_flags.bits.enable || pipe_ctx->stream->update_flags.bits.abm_level)
1360 			&& !pipe_ctx->top_pipe && !pipe_ctx->prev_odm_pipe)
1361 		dc->hwss.blank_pixel_data(dc, pipe_ctx, !pipe_ctx->plane_state->visible);
1362 
1363 	if (pipe_ctx->update_flags.bits.global_sync) {
1364 		pipe_ctx->stream_res.tg->funcs->program_global_sync(
1365 				pipe_ctx->stream_res.tg,
1366 				pipe_ctx->pipe_dlg_param.vready_offset,
1367 				pipe_ctx->pipe_dlg_param.vstartup_start,
1368 				pipe_ctx->pipe_dlg_param.vupdate_offset,
1369 				pipe_ctx->pipe_dlg_param.vupdate_width);
1370 
1371 		pipe_ctx->stream_res.tg->funcs->set_vtg_params(
1372 				pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1373 
1374 		if (dc->hwss.setup_vupdate_interrupt)
1375 			dc->hwss.setup_vupdate_interrupt(pipe_ctx);
1376 	}
1377 
1378 	if (pipe_ctx->update_flags.bits.odm)
1379 		dc->hwss.update_odm(dc, context, pipe_ctx);
1380 
1381 	if (pipe_ctx->update_flags.bits.enable)
1382 		dcn20_enable_plane(dc, pipe_ctx, context);
1383 
1384 	if (pipe_ctx->update_flags.raw || pipe_ctx->plane_state->update_flags.raw || pipe_ctx->stream->update_flags.raw)
1385 		dcn20_update_dchubp_dpp(dc, pipe_ctx, context);
1386 
1387 	if (pipe_ctx->update_flags.bits.enable
1388 			|| pipe_ctx->plane_state->update_flags.bits.sdr_white_level)
1389 		set_hdr_multiplier(pipe_ctx);
1390 
1391 	if (pipe_ctx->update_flags.bits.enable ||
1392 			pipe_ctx->plane_state->update_flags.bits.in_transfer_func_change ||
1393 			pipe_ctx->plane_state->update_flags.bits.gamma_change)
1394 		dc->hwss.set_input_transfer_func(pipe_ctx, pipe_ctx->plane_state);
1395 
1396 	/* dcn10_translate_regamma_to_hw_format takes 750us to finish
1397 	 * only do gamma programming for powering on, internal memcmp to avoid
1398 	 * updating on slave planes
1399 	 */
1400 	if (pipe_ctx->update_flags.bits.enable || pipe_ctx->stream->update_flags.bits.out_tf)
1401 		dc->hwss.set_output_transfer_func(pipe_ctx, pipe_ctx->stream);
1402 
1403 	/* If the pipe has been enabled or has a different opp, we
1404 	 * should reprogram the fmt. This deals with cases where
1405 	 * interation between mpc and odm combine on different streams
1406 	 * causes a different pipe to be chosen to odm combine with.
1407 	 */
1408 	if (pipe_ctx->update_flags.bits.enable
1409 	    || pipe_ctx->update_flags.bits.opp_changed) {
1410 
1411 		pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
1412 			pipe_ctx->stream_res.opp,
1413 			COLOR_SPACE_YCBCR601,
1414 			pipe_ctx->stream->timing.display_color_depth,
1415 			pipe_ctx->stream->signal);
1416 
1417 		pipe_ctx->stream_res.opp->funcs->opp_program_fmt(
1418 			pipe_ctx->stream_res.opp,
1419 			&pipe_ctx->stream->bit_depth_params,
1420 			&pipe_ctx->stream->clamping);
1421 	}
1422 }
1423 
1424 static bool does_pipe_need_lock(struct pipe_ctx *pipe)
1425 {
1426 	if ((pipe->plane_state && pipe->plane_state->update_flags.raw)
1427 			|| pipe->update_flags.raw)
1428 		return true;
1429 	if (pipe->bottom_pipe)
1430 		return does_pipe_need_lock(pipe->bottom_pipe);
1431 
1432 	return false;
1433 }
1434 
1435 static void dcn20_program_front_end_for_ctx(
1436 		struct dc *dc,
1437 		struct dc_state *context)
1438 {
1439 	const unsigned int TIMEOUT_FOR_PIPE_ENABLE_MS = 100;
1440 	int i;
1441 	bool pipe_locked[MAX_PIPES] = {false};
1442 	DC_LOGGER_INIT(dc->ctx->logger);
1443 
1444 	/* Carry over GSL groups in case the context is changing. */
1445 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1446 		if (context->res_ctx.pipe_ctx[i].stream == dc->current_state->res_ctx.pipe_ctx[i].stream)
1447 			context->res_ctx.pipe_ctx[i].stream_res.gsl_group =
1448 				dc->current_state->res_ctx.pipe_ctx[i].stream_res.gsl_group;
1449 
1450 	/* Set pipe update flags and lock pipes */
1451 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1452 		dcn20_detect_pipe_changes(&dc->current_state->res_ctx.pipe_ctx[i],
1453 				&context->res_ctx.pipe_ctx[i]);
1454 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1455 		if (!context->res_ctx.pipe_ctx[i].top_pipe &&
1456 				does_pipe_need_lock(&context->res_ctx.pipe_ctx[i])) {
1457 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1458 
1459 			if (pipe_ctx->update_flags.bits.tg_changed || pipe_ctx->update_flags.bits.enable)
1460 				dc->hwss.pipe_control_lock(dc, pipe_ctx, true);
1461 			if (!pipe_ctx->update_flags.bits.enable)
1462 				dc->hwss.pipe_control_lock(dc, &dc->current_state->res_ctx.pipe_ctx[i], true);
1463 			pipe_locked[i] = true;
1464 		}
1465 
1466 	/* OTG blank before disabling all front ends */
1467 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1468 		if (context->res_ctx.pipe_ctx[i].update_flags.bits.disable
1469 				&& !context->res_ctx.pipe_ctx[i].top_pipe
1470 				&& !context->res_ctx.pipe_ctx[i].prev_odm_pipe
1471 				&& context->res_ctx.pipe_ctx[i].stream)
1472 			dc->hwss.blank_pixel_data(dc, &context->res_ctx.pipe_ctx[i], true);
1473 
1474 	/* Disconnect mpcc */
1475 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1476 		if (context->res_ctx.pipe_ctx[i].update_flags.bits.disable
1477 				|| context->res_ctx.pipe_ctx[i].update_flags.bits.opp_changed) {
1478 			dc->hwss.plane_atomic_disconnect(dc, &dc->current_state->res_ctx.pipe_ctx[i]);
1479 			DC_LOG_DC("Reset mpcc for pipe %d\n", dc->current_state->res_ctx.pipe_ctx[i].pipe_idx);
1480 		}
1481 
1482 	/*
1483 	 * Program all updated pipes, order matters for mpcc setup. Start with
1484 	 * top pipe and program all pipes that follow in order
1485 	 */
1486 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1487 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1488 
1489 		if (pipe->plane_state && !pipe->top_pipe) {
1490 			while (pipe) {
1491 				dcn20_program_pipe(dc, pipe, context);
1492 				pipe = pipe->bottom_pipe;
1493 			}
1494 			/* Program secondary blending tree and writeback pipes */
1495 			pipe = &context->res_ctx.pipe_ctx[i];
1496 			if (!pipe->prev_odm_pipe && pipe->stream->num_wb_info > 0
1497 					&& (pipe->update_flags.raw || pipe->plane_state->update_flags.raw || pipe->stream->update_flags.raw)
1498 					&& dc->hwss.program_all_writeback_pipes_in_tree)
1499 				dc->hwss.program_all_writeback_pipes_in_tree(dc, pipe->stream, context);
1500 		}
1501 	}
1502 
1503 	/* Unlock all locked pipes */
1504 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1505 		if (pipe_locked[i]) {
1506 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1507 
1508 			if (pipe_ctx->update_flags.bits.tg_changed || pipe_ctx->update_flags.bits.enable)
1509 				dc->hwss.pipe_control_lock(dc, pipe_ctx, false);
1510 			if (!pipe_ctx->update_flags.bits.enable)
1511 				dc->hwss.pipe_control_lock(dc, &dc->current_state->res_ctx.pipe_ctx[i], false);
1512 		}
1513 
1514 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1515 		if (context->res_ctx.pipe_ctx[i].update_flags.bits.disable)
1516 			dc->hwss.disable_plane(dc, &dc->current_state->res_ctx.pipe_ctx[i]);
1517 
1518 	/*
1519 	 * If we are enabling a pipe, we need to wait for pending clear as this is a critical
1520 	 * part of the enable operation otherwise, DM may request an immediate flip which
1521 	 * will cause HW to perform an "immediate enable" (as opposed to "vsync enable") which
1522 	 * is unsupported on DCN.
1523 	 */
1524 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1525 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1526 
1527 		if (pipe->plane_state && !pipe->top_pipe && pipe->update_flags.bits.enable) {
1528 			struct hubp *hubp = pipe->plane_res.hubp;
1529 			int j = 0;
1530 
1531 			for (j = 0; j < TIMEOUT_FOR_PIPE_ENABLE_MS
1532 					&& hubp->funcs->hubp_is_flip_pending(hubp); j++)
1533 				msleep(1);
1534 		}
1535 	}
1536 
1537 	/* WA to apply WM setting*/
1538 	if (dc->hwseq->wa.DEGVIDCN21)
1539 		dc->res_pool->hubbub->funcs->apply_DEDCN21_147_wa(dc->res_pool->hubbub);
1540 }
1541 
1542 
1543 void dcn20_prepare_bandwidth(
1544 		struct dc *dc,
1545 		struct dc_state *context)
1546 {
1547 	struct hubbub *hubbub = dc->res_pool->hubbub;
1548 
1549 	dc->clk_mgr->funcs->update_clocks(
1550 			dc->clk_mgr,
1551 			context,
1552 			false);
1553 
1554 	/* program dchubbub watermarks */
1555 	hubbub->funcs->program_watermarks(hubbub,
1556 					&context->bw_ctx.bw.dcn.watermarks,
1557 					dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000,
1558 					false);
1559 }
1560 
1561 void dcn20_optimize_bandwidth(
1562 		struct dc *dc,
1563 		struct dc_state *context)
1564 {
1565 	struct hubbub *hubbub = dc->res_pool->hubbub;
1566 
1567 	/* program dchubbub watermarks */
1568 	hubbub->funcs->program_watermarks(hubbub,
1569 					&context->bw_ctx.bw.dcn.watermarks,
1570 					dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000,
1571 					true);
1572 
1573 	dc->clk_mgr->funcs->update_clocks(
1574 			dc->clk_mgr,
1575 			context,
1576 			true);
1577 }
1578 
1579 bool dcn20_update_bandwidth(
1580 		struct dc *dc,
1581 		struct dc_state *context)
1582 {
1583 	int i;
1584 
1585 	/* recalculate DML parameters */
1586 	if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false))
1587 		return false;
1588 
1589 	/* apply updated bandwidth parameters */
1590 	dc->hwss.prepare_bandwidth(dc, context);
1591 
1592 	/* update hubp configs for all pipes */
1593 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1594 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1595 
1596 		if (pipe_ctx->plane_state == NULL)
1597 			continue;
1598 
1599 		if (pipe_ctx->top_pipe == NULL) {
1600 			bool blank = !is_pipe_tree_visible(pipe_ctx);
1601 
1602 			pipe_ctx->stream_res.tg->funcs->program_global_sync(
1603 					pipe_ctx->stream_res.tg,
1604 					pipe_ctx->pipe_dlg_param.vready_offset,
1605 					pipe_ctx->pipe_dlg_param.vstartup_start,
1606 					pipe_ctx->pipe_dlg_param.vupdate_offset,
1607 					pipe_ctx->pipe_dlg_param.vupdate_width);
1608 
1609 			pipe_ctx->stream_res.tg->funcs->set_vtg_params(
1610 					pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1611 
1612 			if (pipe_ctx->prev_odm_pipe == NULL)
1613 				dc->hwss.blank_pixel_data(dc, pipe_ctx, blank);
1614 
1615 			if (dc->hwss.setup_vupdate_interrupt)
1616 				dc->hwss.setup_vupdate_interrupt(pipe_ctx);
1617 		}
1618 
1619 		pipe_ctx->plane_res.hubp->funcs->hubp_setup(
1620 				pipe_ctx->plane_res.hubp,
1621 					&pipe_ctx->dlg_regs,
1622 					&pipe_ctx->ttu_regs,
1623 					&pipe_ctx->rq_regs,
1624 					&pipe_ctx->pipe_dlg_param);
1625 	}
1626 
1627 	return true;
1628 }
1629 
1630 static void dcn20_enable_writeback(
1631 		struct dc *dc,
1632 		const struct dc_stream_status *stream_status,
1633 		struct dc_writeback_info *wb_info,
1634 		struct dc_state *context)
1635 {
1636 	struct dwbc *dwb;
1637 	struct mcif_wb *mcif_wb;
1638 	struct timing_generator *optc;
1639 
1640 	ASSERT(wb_info->dwb_pipe_inst < MAX_DWB_PIPES);
1641 	ASSERT(wb_info->wb_enabled);
1642 	dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
1643 	mcif_wb = dc->res_pool->mcif_wb[wb_info->dwb_pipe_inst];
1644 
1645 	/* set the OPTC source mux */
1646 	ASSERT(stream_status->primary_otg_inst < MAX_PIPES);
1647 	optc = dc->res_pool->timing_generators[stream_status->primary_otg_inst];
1648 	optc->funcs->set_dwb_source(optc, wb_info->dwb_pipe_inst);
1649 	/* set MCIF_WB buffer and arbitration configuration */
1650 	mcif_wb->funcs->config_mcif_buf(mcif_wb, &wb_info->mcif_buf_params, wb_info->dwb_params.dest_height);
1651 	mcif_wb->funcs->config_mcif_arb(mcif_wb, &context->bw_ctx.bw.dcn.bw_writeback.mcif_wb_arb[wb_info->dwb_pipe_inst]);
1652 	/* Enable MCIF_WB */
1653 	mcif_wb->funcs->enable_mcif(mcif_wb);
1654 	/* Enable DWB */
1655 	dwb->funcs->enable(dwb, &wb_info->dwb_params);
1656 	/* TODO: add sequence to enable/disable warmup */
1657 }
1658 
1659 void dcn20_disable_writeback(
1660 		struct dc *dc,
1661 		unsigned int dwb_pipe_inst)
1662 {
1663 	struct dwbc *dwb;
1664 	struct mcif_wb *mcif_wb;
1665 
1666 	ASSERT(dwb_pipe_inst < MAX_DWB_PIPES);
1667 	dwb = dc->res_pool->dwbc[dwb_pipe_inst];
1668 	mcif_wb = dc->res_pool->mcif_wb[dwb_pipe_inst];
1669 
1670 	dwb->funcs->disable(dwb);
1671 	mcif_wb->funcs->disable_mcif(mcif_wb);
1672 }
1673 
1674 bool dcn20_hwss_wait_for_blank_complete(
1675 		struct output_pixel_processor *opp)
1676 {
1677 	int counter;
1678 
1679 	for (counter = 0; counter < 1000; counter++) {
1680 		if (opp->funcs->dpg_is_blanked(opp))
1681 			break;
1682 
1683 		udelay(100);
1684 	}
1685 
1686 	if (counter == 1000) {
1687 		dm_error("DC: failed to blank crtc!\n");
1688 		return false;
1689 	}
1690 
1691 	return true;
1692 }
1693 
1694 bool dcn20_dmdata_status_done(struct pipe_ctx *pipe_ctx)
1695 {
1696 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
1697 
1698 	if (!hubp)
1699 		return false;
1700 	return hubp->funcs->dmdata_status_done(hubp);
1701 }
1702 
1703 static void dcn20_disable_stream_gating(struct dc *dc, struct pipe_ctx *pipe_ctx)
1704 {
1705 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
1706 	struct dce_hwseq *hws = dc->hwseq;
1707 
1708 	if (pipe_ctx->stream_res.dsc) {
1709 		struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
1710 
1711 		dcn20_dsc_pg_control(hws, pipe_ctx->stream_res.dsc->inst, true);
1712 		while (odm_pipe) {
1713 			dcn20_dsc_pg_control(hws, odm_pipe->stream_res.dsc->inst, true);
1714 			odm_pipe = odm_pipe->next_odm_pipe;
1715 		}
1716 	}
1717 #endif
1718 }
1719 
1720 static void dcn20_enable_stream_gating(struct dc *dc, struct pipe_ctx *pipe_ctx)
1721 {
1722 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
1723 	struct dce_hwseq *hws = dc->hwseq;
1724 
1725 	if (pipe_ctx->stream_res.dsc) {
1726 		struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
1727 
1728 		dcn20_dsc_pg_control(hws, pipe_ctx->stream_res.dsc->inst, false);
1729 		while (odm_pipe) {
1730 			dcn20_dsc_pg_control(hws, odm_pipe->stream_res.dsc->inst, false);
1731 			odm_pipe = odm_pipe->next_odm_pipe;
1732 		}
1733 	}
1734 #endif
1735 }
1736 
1737 void dcn20_set_dmdata_attributes(struct pipe_ctx *pipe_ctx)
1738 {
1739 	struct dc_dmdata_attributes attr = { 0 };
1740 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
1741 
1742 	attr.dmdata_mode = DMDATA_HW_MODE;
1743 	attr.dmdata_size =
1744 		dc_is_hdmi_signal(pipe_ctx->stream->signal) ? 32 : 36;
1745 	attr.address.quad_part =
1746 			pipe_ctx->stream->dmdata_address.quad_part;
1747 	attr.dmdata_dl_delta = 0;
1748 	attr.dmdata_qos_mode = 0;
1749 	attr.dmdata_qos_level = 0;
1750 	attr.dmdata_repeat = 1; /* always repeat */
1751 	attr.dmdata_updated = 1;
1752 	attr.dmdata_sw_data = NULL;
1753 
1754 	hubp->funcs->dmdata_set_attributes(hubp, &attr);
1755 }
1756 
1757 void dcn20_disable_stream(struct pipe_ctx *pipe_ctx)
1758 {
1759 	dce110_disable_stream(pipe_ctx);
1760 }
1761 
1762 static void dcn20_init_vm_ctx(
1763 		struct dce_hwseq *hws,
1764 		struct dc *dc,
1765 		struct dc_virtual_addr_space_config *va_config,
1766 		int vmid)
1767 {
1768 	struct dcn_hubbub_virt_addr_config config;
1769 
1770 	if (vmid == 0) {
1771 		ASSERT(0); /* VMID cannot be 0 for vm context */
1772 		return;
1773 	}
1774 
1775 	config.page_table_start_addr = va_config->page_table_start_addr;
1776 	config.page_table_end_addr = va_config->page_table_end_addr;
1777 	config.page_table_block_size = va_config->page_table_block_size_in_bytes;
1778 	config.page_table_depth = va_config->page_table_depth;
1779 	config.page_table_base_addr = va_config->page_table_base_addr;
1780 
1781 	dc->res_pool->hubbub->funcs->init_vm_ctx(dc->res_pool->hubbub, &config, vmid);
1782 }
1783 
1784 static int dcn20_init_sys_ctx(struct dce_hwseq *hws, struct dc *dc, struct dc_phy_addr_space_config *pa_config)
1785 {
1786 	struct dcn_hubbub_phys_addr_config config;
1787 
1788 	config.system_aperture.fb_top = pa_config->system_aperture.fb_top;
1789 	config.system_aperture.fb_offset = pa_config->system_aperture.fb_offset;
1790 	config.system_aperture.fb_base = pa_config->system_aperture.fb_base;
1791 	config.system_aperture.agp_top = pa_config->system_aperture.agp_top;
1792 	config.system_aperture.agp_bot = pa_config->system_aperture.agp_bot;
1793 	config.system_aperture.agp_base = pa_config->system_aperture.agp_base;
1794 	config.gart_config.page_table_start_addr = pa_config->gart_config.page_table_start_addr;
1795 	config.gart_config.page_table_end_addr = pa_config->gart_config.page_table_end_addr;
1796 	config.gart_config.page_table_base_addr = pa_config->gart_config.page_table_base_addr;
1797 	config.page_table_default_page_addr = pa_config->page_table_default_page_addr;
1798 
1799 	return dc->res_pool->hubbub->funcs->init_dchub_sys_ctx(dc->res_pool->hubbub, &config);
1800 }
1801 
1802 static bool patch_address_for_sbs_tb_stereo(
1803 		struct pipe_ctx *pipe_ctx, PHYSICAL_ADDRESS_LOC *addr)
1804 {
1805 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1806 	bool sec_split = pipe_ctx->top_pipe &&
1807 			pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state;
1808 	if (sec_split && plane_state->address.type == PLN_ADDR_TYPE_GRPH_STEREO &&
1809 			(pipe_ctx->stream->timing.timing_3d_format ==
1810 			TIMING_3D_FORMAT_SIDE_BY_SIDE ||
1811 			pipe_ctx->stream->timing.timing_3d_format ==
1812 			TIMING_3D_FORMAT_TOP_AND_BOTTOM)) {
1813 		*addr = plane_state->address.grph_stereo.left_addr;
1814 		plane_state->address.grph_stereo.left_addr =
1815 				plane_state->address.grph_stereo.right_addr;
1816 		return true;
1817 	}
1818 
1819 	if (pipe_ctx->stream->view_format != VIEW_3D_FORMAT_NONE &&
1820 			plane_state->address.type != PLN_ADDR_TYPE_GRPH_STEREO) {
1821 		plane_state->address.type = PLN_ADDR_TYPE_GRPH_STEREO;
1822 		plane_state->address.grph_stereo.right_addr =
1823 				plane_state->address.grph_stereo.left_addr;
1824 	}
1825 	return false;
1826 }
1827 
1828 
1829 static void dcn20_update_plane_addr(const struct dc *dc, struct pipe_ctx *pipe_ctx)
1830 {
1831 	bool addr_patched = false;
1832 	PHYSICAL_ADDRESS_LOC addr;
1833 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1834 
1835 	if (plane_state == NULL)
1836 		return;
1837 
1838 	addr_patched = patch_address_for_sbs_tb_stereo(pipe_ctx, &addr);
1839 
1840 	// Call Helper to track VMID use
1841 	vm_helper_mark_vmid_used(dc->vm_helper, plane_state->address.vmid, pipe_ctx->plane_res.hubp->inst);
1842 
1843 	pipe_ctx->plane_res.hubp->funcs->hubp_program_surface_flip_and_addr(
1844 			pipe_ctx->plane_res.hubp,
1845 			&plane_state->address,
1846 			plane_state->flip_immediate);
1847 
1848 	plane_state->status.requested_address = plane_state->address;
1849 
1850 	if (plane_state->flip_immediate)
1851 		plane_state->status.current_address = plane_state->address;
1852 
1853 	if (addr_patched)
1854 		pipe_ctx->plane_state->address.grph_stereo.left_addr = addr;
1855 }
1856 
1857 void dcn20_unblank_stream(struct pipe_ctx *pipe_ctx,
1858 		struct dc_link_settings *link_settings)
1859 {
1860 	struct encoder_unblank_param params = { { 0 } };
1861 	struct dc_stream_state *stream = pipe_ctx->stream;
1862 	struct dc_link *link = stream->link;
1863 	struct pipe_ctx *odm_pipe;
1864 
1865 	params.opp_cnt = 1;
1866 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1867 		params.opp_cnt++;
1868 	}
1869 	/* only 3 items below are used by unblank */
1870 	params.timing = pipe_ctx->stream->timing;
1871 
1872 	params.link_settings.link_rate = link_settings->link_rate;
1873 
1874 	if (dc_is_dp_signal(pipe_ctx->stream->signal)) {
1875 		if (optc1_is_two_pixels_per_containter(&stream->timing) || params.opp_cnt > 1)
1876 			params.timing.pix_clk_100hz /= 2;
1877 		pipe_ctx->stream_res.stream_enc->funcs->dp_set_odm_combine(
1878 				pipe_ctx->stream_res.stream_enc, params.opp_cnt > 1);
1879 		pipe_ctx->stream_res.stream_enc->funcs->dp_unblank(pipe_ctx->stream_res.stream_enc, &params);
1880 	}
1881 
1882 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1883 		link->dc->hwss.edp_backlight_control(link, true);
1884 	}
1885 }
1886 
1887 void dcn20_setup_vupdate_interrupt(struct pipe_ctx *pipe_ctx)
1888 {
1889 	struct timing_generator *tg = pipe_ctx->stream_res.tg;
1890 	int start_line = get_vupdate_offset_from_vsync(pipe_ctx);
1891 
1892 	if (start_line < 0)
1893 		start_line = 0;
1894 
1895 	if (tg->funcs->setup_vertical_interrupt2)
1896 		tg->funcs->setup_vertical_interrupt2(tg, start_line);
1897 }
1898 
1899 static void dcn20_reset_back_end_for_pipe(
1900 		struct dc *dc,
1901 		struct pipe_ctx *pipe_ctx,
1902 		struct dc_state *context)
1903 {
1904 	int i;
1905 	DC_LOGGER_INIT(dc->ctx->logger);
1906 	if (pipe_ctx->stream_res.stream_enc == NULL) {
1907 		pipe_ctx->stream = NULL;
1908 		return;
1909 	}
1910 
1911 	if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
1912 		/* DPMS may already disable */
1913 		if (!pipe_ctx->stream->dpms_off)
1914 			core_link_disable_stream(pipe_ctx);
1915 		else if (pipe_ctx->stream_res.audio)
1916 			dc->hwss.disable_audio_stream(pipe_ctx);
1917 
1918 		/* free acquired resources */
1919 		if (pipe_ctx->stream_res.audio) {
1920 			/*disable az_endpoint*/
1921 			pipe_ctx->stream_res.audio->funcs->az_disable(pipe_ctx->stream_res.audio);
1922 
1923 			/*free audio*/
1924 			if (dc->caps.dynamic_audio == true) {
1925 				/*we have to dynamic arbitrate the audio endpoints*/
1926 				/*we free the resource, need reset is_audio_acquired*/
1927 				update_audio_usage(&dc->current_state->res_ctx, dc->res_pool,
1928 						pipe_ctx->stream_res.audio, false);
1929 				pipe_ctx->stream_res.audio = NULL;
1930 			}
1931 		}
1932 	}
1933 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
1934 	else if (pipe_ctx->stream_res.dsc) {
1935 		dp_set_dsc_enable(pipe_ctx, false);
1936 	}
1937 #endif
1938 
1939 	/* by upper caller loop, parent pipe: pipe0, will be reset last.
1940 	 * back end share by all pipes and will be disable only when disable
1941 	 * parent pipe.
1942 	 */
1943 	if (pipe_ctx->top_pipe == NULL) {
1944 		pipe_ctx->stream_res.tg->funcs->disable_crtc(pipe_ctx->stream_res.tg);
1945 
1946 		pipe_ctx->stream_res.tg->funcs->enable_optc_clock(pipe_ctx->stream_res.tg, false);
1947 		if (pipe_ctx->stream_res.tg->funcs->set_odm_bypass)
1948 			pipe_ctx->stream_res.tg->funcs->set_odm_bypass(
1949 					pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1950 
1951 		if (pipe_ctx->stream_res.tg->funcs->set_drr)
1952 			pipe_ctx->stream_res.tg->funcs->set_drr(
1953 					pipe_ctx->stream_res.tg, NULL);
1954 	}
1955 
1956 	for (i = 0; i < dc->res_pool->pipe_count; i++)
1957 		if (&dc->current_state->res_ctx.pipe_ctx[i] == pipe_ctx)
1958 			break;
1959 
1960 	if (i == dc->res_pool->pipe_count)
1961 		return;
1962 
1963 	pipe_ctx->stream = NULL;
1964 	DC_LOG_DEBUG("Reset back end for pipe %d, tg:%d\n",
1965 					pipe_ctx->pipe_idx, pipe_ctx->stream_res.tg->inst);
1966 }
1967 
1968 static void dcn20_reset_hw_ctx_wrap(
1969 		struct dc *dc,
1970 		struct dc_state *context)
1971 {
1972 	int i;
1973 
1974 	/* Reset Back End*/
1975 	for (i = dc->res_pool->pipe_count - 1; i >= 0 ; i--) {
1976 		struct pipe_ctx *pipe_ctx_old =
1977 			&dc->current_state->res_ctx.pipe_ctx[i];
1978 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1979 
1980 		if (!pipe_ctx_old->stream)
1981 			continue;
1982 
1983 		if (pipe_ctx_old->top_pipe || pipe_ctx_old->prev_odm_pipe)
1984 			continue;
1985 
1986 		if (!pipe_ctx->stream ||
1987 				pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
1988 			struct clock_source *old_clk = pipe_ctx_old->clock_source;
1989 
1990 			dcn20_reset_back_end_for_pipe(dc, pipe_ctx_old, dc->current_state);
1991 			if (dc->hwss.enable_stream_gating)
1992 				dc->hwss.enable_stream_gating(dc, pipe_ctx);
1993 			if (old_clk)
1994 				old_clk->funcs->cs_power_down(old_clk);
1995 		}
1996 	}
1997 }
1998 
1999 void dcn20_get_mpctree_visual_confirm_color(
2000 		struct pipe_ctx *pipe_ctx,
2001 		struct tg_color *color)
2002 {
2003 	const struct tg_color pipe_colors[6] = {
2004 			{MAX_TG_COLOR_VALUE, 0, 0}, // red
2005 			{MAX_TG_COLOR_VALUE, 0, MAX_TG_COLOR_VALUE}, // yellow
2006 			{0, MAX_TG_COLOR_VALUE, 0}, // blue
2007 			{MAX_TG_COLOR_VALUE / 2, 0, MAX_TG_COLOR_VALUE / 2}, // purple
2008 			{0, 0, MAX_TG_COLOR_VALUE}, // green
2009 			{MAX_TG_COLOR_VALUE, MAX_TG_COLOR_VALUE * 2 / 3, 0}, // orange
2010 	};
2011 
2012 	struct pipe_ctx *top_pipe = pipe_ctx;
2013 
2014 	while (top_pipe->top_pipe) {
2015 		top_pipe = top_pipe->top_pipe;
2016 	}
2017 
2018 	*color = pipe_colors[top_pipe->pipe_idx];
2019 }
2020 
2021 static void dcn20_update_mpcc(struct dc *dc, struct pipe_ctx *pipe_ctx)
2022 {
2023 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
2024 	struct mpcc_blnd_cfg blnd_cfg = { {0} };
2025 	bool per_pixel_alpha = pipe_ctx->plane_state->per_pixel_alpha;
2026 	int mpcc_id;
2027 	struct mpcc *new_mpcc;
2028 	struct mpc *mpc = dc->res_pool->mpc;
2029 	struct mpc_tree *mpc_tree_params = &(pipe_ctx->stream_res.opp->mpc_tree_params);
2030 
2031 	// input to MPCC is always RGB, by default leave black_color at 0
2032 	if (dc->debug.visual_confirm == VISUAL_CONFIRM_HDR) {
2033 		dcn10_get_hdr_visual_confirm_color(
2034 				pipe_ctx, &blnd_cfg.black_color);
2035 	} else if (dc->debug.visual_confirm == VISUAL_CONFIRM_SURFACE) {
2036 		dcn10_get_surface_visual_confirm_color(
2037 				pipe_ctx, &blnd_cfg.black_color);
2038 	} else if (dc->debug.visual_confirm == VISUAL_CONFIRM_MPCTREE) {
2039 		dcn20_get_mpctree_visual_confirm_color(
2040 				pipe_ctx, &blnd_cfg.black_color);
2041 	}
2042 
2043 	if (per_pixel_alpha)
2044 		blnd_cfg.alpha_mode = MPCC_ALPHA_BLEND_MODE_PER_PIXEL_ALPHA;
2045 	else
2046 		blnd_cfg.alpha_mode = MPCC_ALPHA_BLEND_MODE_GLOBAL_ALPHA;
2047 
2048 	blnd_cfg.overlap_only = false;
2049 	blnd_cfg.global_gain = 0xff;
2050 
2051 	if (pipe_ctx->plane_state->global_alpha)
2052 		blnd_cfg.global_alpha = pipe_ctx->plane_state->global_alpha_value;
2053 	else
2054 		blnd_cfg.global_alpha = 0xff;
2055 
2056 	blnd_cfg.background_color_bpc = 4;
2057 	blnd_cfg.bottom_gain_mode = 0;
2058 	blnd_cfg.top_gain = 0x1f000;
2059 	blnd_cfg.bottom_inside_gain = 0x1f000;
2060 	blnd_cfg.bottom_outside_gain = 0x1f000;
2061 	blnd_cfg.pre_multiplied_alpha = per_pixel_alpha;
2062 
2063 	/*
2064 	 * TODO: remove hack
2065 	 * Note: currently there is a bug in init_hw such that
2066 	 * on resume from hibernate, BIOS sets up MPCC0, and
2067 	 * we do mpcc_remove but the mpcc cannot go to idle
2068 	 * after remove. This cause us to pick mpcc1 here,
2069 	 * which causes a pstate hang for yet unknown reason.
2070 	 */
2071 	mpcc_id = hubp->inst;
2072 
2073 	/* If there is no full update, don't need to touch MPC tree*/
2074 	if (!pipe_ctx->plane_state->update_flags.bits.full_update) {
2075 		mpc->funcs->update_blending(mpc, &blnd_cfg, mpcc_id);
2076 		return;
2077 	}
2078 
2079 	/* check if this MPCC is already being used */
2080 	new_mpcc = mpc->funcs->get_mpcc_for_dpp(mpc_tree_params, mpcc_id);
2081 	/* remove MPCC if being used */
2082 	if (new_mpcc != NULL)
2083 		mpc->funcs->remove_mpcc(mpc, mpc_tree_params, new_mpcc);
2084 	else
2085 		if (dc->debug.sanity_checks)
2086 			mpc->funcs->assert_mpcc_idle_before_connect(
2087 					dc->res_pool->mpc, mpcc_id);
2088 
2089 	/* Call MPC to insert new plane */
2090 	new_mpcc = mpc->funcs->insert_plane(dc->res_pool->mpc,
2091 			mpc_tree_params,
2092 			&blnd_cfg,
2093 			NULL,
2094 			NULL,
2095 			hubp->inst,
2096 			mpcc_id);
2097 
2098 	ASSERT(new_mpcc != NULL);
2099 	hubp->opp_id = pipe_ctx->stream_res.opp->inst;
2100 	hubp->mpcc_id = mpcc_id;
2101 }
2102 
2103 static int find_free_gsl_group(const struct dc *dc)
2104 {
2105 	if (dc->res_pool->gsl_groups.gsl_0 == 0)
2106 		return 1;
2107 	if (dc->res_pool->gsl_groups.gsl_1 == 0)
2108 		return 2;
2109 	if (dc->res_pool->gsl_groups.gsl_2 == 0)
2110 		return 3;
2111 
2112 	return 0;
2113 }
2114 
2115 /* NOTE: This is not a generic setup_gsl function (hence the suffix as_lock)
2116  * This is only used to lock pipes in pipe splitting case with immediate flip
2117  * Ordinary MPC/OTG locks suppress VUPDATE which doesn't help with immediate,
2118  * so we get tearing with freesync since we cannot flip multiple pipes
2119  * atomically.
2120  * We use GSL for this:
2121  * - immediate flip: find first available GSL group if not already assigned
2122  *                   program gsl with that group, set current OTG as master
2123  *                   and always us 0x4 = AND of flip_ready from all pipes
2124  * - vsync flip: disable GSL if used
2125  *
2126  * Groups in stream_res are stored as +1 from HW registers, i.e.
2127  * gsl_0 <=> pipe_ctx->stream_res.gsl_group == 1
2128  * Using a magic value like -1 would require tracking all inits/resets
2129  */
2130 void dcn20_setup_gsl_group_as_lock(
2131 		const struct dc *dc,
2132 		struct pipe_ctx *pipe_ctx,
2133 		bool enable)
2134 {
2135 	struct gsl_params gsl;
2136 	int group_idx;
2137 
2138 	memset(&gsl, 0, sizeof(struct gsl_params));
2139 
2140 	if (enable) {
2141 		/* return if group already assigned since GSL was set up
2142 		 * for vsync flip, we would unassign so it can't be "left over"
2143 		 */
2144 		if (pipe_ctx->stream_res.gsl_group > 0)
2145 			return;
2146 
2147 		group_idx = find_free_gsl_group(dc);
2148 		ASSERT(group_idx != 0);
2149 		pipe_ctx->stream_res.gsl_group = group_idx;
2150 
2151 		/* set gsl group reg field and mark resource used */
2152 		switch (group_idx) {
2153 		case 1:
2154 			gsl.gsl0_en = 1;
2155 			dc->res_pool->gsl_groups.gsl_0 = 1;
2156 			break;
2157 		case 2:
2158 			gsl.gsl1_en = 1;
2159 			dc->res_pool->gsl_groups.gsl_1 = 1;
2160 			break;
2161 		case 3:
2162 			gsl.gsl2_en = 1;
2163 			dc->res_pool->gsl_groups.gsl_2 = 1;
2164 			break;
2165 		default:
2166 			BREAK_TO_DEBUGGER();
2167 			return; // invalid case
2168 		}
2169 		gsl.gsl_master_en = 1;
2170 	} else {
2171 		group_idx = pipe_ctx->stream_res.gsl_group;
2172 		if (group_idx == 0)
2173 			return; // if not in use, just return
2174 
2175 		pipe_ctx->stream_res.gsl_group = 0;
2176 
2177 		/* unset gsl group reg field and mark resource free */
2178 		switch (group_idx) {
2179 		case 1:
2180 			gsl.gsl0_en = 0;
2181 			dc->res_pool->gsl_groups.gsl_0 = 0;
2182 			break;
2183 		case 2:
2184 			gsl.gsl1_en = 0;
2185 			dc->res_pool->gsl_groups.gsl_1 = 0;
2186 			break;
2187 		case 3:
2188 			gsl.gsl2_en = 0;
2189 			dc->res_pool->gsl_groups.gsl_2 = 0;
2190 			break;
2191 		default:
2192 			BREAK_TO_DEBUGGER();
2193 			return;
2194 		}
2195 		gsl.gsl_master_en = 0;
2196 	}
2197 
2198 	/* at this point we want to program whether it's to enable or disable */
2199 	if (pipe_ctx->stream_res.tg->funcs->set_gsl != NULL &&
2200 		pipe_ctx->stream_res.tg->funcs->set_gsl_source_select != NULL) {
2201 		pipe_ctx->stream_res.tg->funcs->set_gsl(
2202 			pipe_ctx->stream_res.tg,
2203 			&gsl);
2204 
2205 		pipe_ctx->stream_res.tg->funcs->set_gsl_source_select(
2206 			pipe_ctx->stream_res.tg, group_idx,	enable ? 4 : 0);
2207 	} else
2208 		BREAK_TO_DEBUGGER();
2209 }
2210 
2211 static void dcn20_set_flip_control_gsl(
2212 		struct pipe_ctx *pipe_ctx,
2213 		bool flip_immediate)
2214 {
2215 	if (pipe_ctx && pipe_ctx->plane_res.hubp->funcs->hubp_set_flip_control_surface_gsl)
2216 		pipe_ctx->plane_res.hubp->funcs->hubp_set_flip_control_surface_gsl(
2217 				pipe_ctx->plane_res.hubp, flip_immediate);
2218 
2219 }
2220 
2221 static void dcn20_enable_stream(struct pipe_ctx *pipe_ctx)
2222 {
2223 	enum dc_lane_count lane_count =
2224 		pipe_ctx->stream->link->cur_link_settings.lane_count;
2225 
2226 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
2227 	struct dc_link *link = pipe_ctx->stream->link;
2228 
2229 	uint32_t active_total_with_borders;
2230 	uint32_t early_control = 0;
2231 	struct timing_generator *tg = pipe_ctx->stream_res.tg;
2232 
2233 	/* For MST, there are multiply stream go to only one link.
2234 	 * connect DIG back_end to front_end while enable_stream and
2235 	 * disconnect them during disable_stream
2236 	 * BY this, it is logic clean to separate stream and link
2237 	 */
2238 	link->link_enc->funcs->connect_dig_be_to_fe(link->link_enc,
2239 						    pipe_ctx->stream_res.stream_enc->id, true);
2240 
2241 	if (pipe_ctx->plane_state && pipe_ctx->plane_state->flip_immediate != 1) {
2242 		if (link->dc->hwss.program_dmdata_engine)
2243 			link->dc->hwss.program_dmdata_engine(pipe_ctx);
2244 	}
2245 
2246 	link->dc->hwss.update_info_frame(pipe_ctx);
2247 
2248 	/* enable early control to avoid corruption on DP monitor*/
2249 	active_total_with_borders =
2250 			timing->h_addressable
2251 				+ timing->h_border_left
2252 				+ timing->h_border_right;
2253 
2254 	if (lane_count != 0)
2255 		early_control = active_total_with_borders % lane_count;
2256 
2257 	if (early_control == 0)
2258 		early_control = lane_count;
2259 
2260 	tg->funcs->set_early_control(tg, early_control);
2261 
2262 	/* enable audio only within mode set */
2263 	if (pipe_ctx->stream_res.audio != NULL) {
2264 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
2265 			pipe_ctx->stream_res.stream_enc->funcs->dp_audio_enable(pipe_ctx->stream_res.stream_enc);
2266 	}
2267 }
2268 
2269 static void dcn20_program_dmdata_engine(struct pipe_ctx *pipe_ctx)
2270 {
2271 	struct dc_stream_state    *stream     = pipe_ctx->stream;
2272 	struct hubp               *hubp       = pipe_ctx->plane_res.hubp;
2273 	bool                       enable     = false;
2274 	struct stream_encoder     *stream_enc = pipe_ctx->stream_res.stream_enc;
2275 	enum dynamic_metadata_mode mode       = dc_is_dp_signal(stream->signal)
2276 							? dmdata_dp
2277 							: dmdata_hdmi;
2278 
2279 	/* if using dynamic meta, don't set up generic infopackets */
2280 	if (pipe_ctx->stream->dmdata_address.quad_part != 0) {
2281 		pipe_ctx->stream_res.encoder_info_frame.hdrsmd.valid = false;
2282 		enable = true;
2283 	}
2284 
2285 	if (!hubp)
2286 		return;
2287 
2288 	if (!stream_enc || !stream_enc->funcs->set_dynamic_metadata)
2289 		return;
2290 
2291 	stream_enc->funcs->set_dynamic_metadata(stream_enc, enable,
2292 						hubp->inst, mode);
2293 }
2294 
2295 static void dcn20_fpga_init_hw(struct dc *dc)
2296 {
2297 	int i, j;
2298 	struct dce_hwseq *hws = dc->hwseq;
2299 	struct resource_pool *res_pool = dc->res_pool;
2300 	struct dc_state  *context = dc->current_state;
2301 
2302 	if (dc->clk_mgr && dc->clk_mgr->funcs->init_clocks)
2303 		dc->clk_mgr->funcs->init_clocks(dc->clk_mgr);
2304 
2305 	// Initialize the dccg
2306 	if (res_pool->dccg->funcs->dccg_init)
2307 		res_pool->dccg->funcs->dccg_init(res_pool->dccg);
2308 
2309 	//Enable ability to power gate / don't force power on permanently
2310 	dc->hwss.enable_power_gating_plane(hws, true);
2311 
2312 	// Specific to FPGA dccg and registers
2313 	REG_WRITE(RBBMIF_TIMEOUT_DIS, 0xFFFFFFFF);
2314 	REG_WRITE(RBBMIF_TIMEOUT_DIS_2, 0xFFFFFFFF);
2315 
2316 	dcn20_dccg_init(hws);
2317 
2318 	REG_UPDATE(DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_REFDIV, 2);
2319 	REG_UPDATE(DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_ENABLE, 1);
2320 	REG_WRITE(REFCLK_CNTL, 0);
2321 	//
2322 
2323 
2324 	/* Blank pixel data with OPP DPG */
2325 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
2326 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
2327 
2328 		if (tg->funcs->is_tg_enabled(tg))
2329 			dcn20_init_blank(dc, tg);
2330 	}
2331 
2332 	for (i = 0; i < res_pool->timing_generator_count; i++) {
2333 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
2334 
2335 		if (tg->funcs->is_tg_enabled(tg))
2336 			tg->funcs->lock(tg);
2337 	}
2338 
2339 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2340 		struct dpp *dpp = res_pool->dpps[i];
2341 
2342 		dpp->funcs->dpp_reset(dpp);
2343 	}
2344 
2345 	/* Reset all MPCC muxes */
2346 	res_pool->mpc->funcs->mpc_init(res_pool->mpc);
2347 
2348 	/* initialize OPP mpc_tree parameter */
2349 	for (i = 0; i < dc->res_pool->res_cap->num_opp; i++) {
2350 		res_pool->opps[i]->mpc_tree_params.opp_id = res_pool->opps[i]->inst;
2351 		res_pool->opps[i]->mpc_tree_params.opp_list = NULL;
2352 		for (j = 0; j < MAX_PIPES; j++)
2353 			res_pool->opps[i]->mpcc_disconnect_pending[j] = false;
2354 	}
2355 
2356 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2357 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
2358 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2359 		struct hubp *hubp = dc->res_pool->hubps[i];
2360 		struct dpp *dpp = dc->res_pool->dpps[i];
2361 
2362 		pipe_ctx->stream_res.tg = tg;
2363 		pipe_ctx->pipe_idx = i;
2364 
2365 		pipe_ctx->plane_res.hubp = hubp;
2366 		pipe_ctx->plane_res.dpp = dpp;
2367 		pipe_ctx->plane_res.mpcc_inst = dpp->inst;
2368 		hubp->mpcc_id = dpp->inst;
2369 		hubp->opp_id = OPP_ID_INVALID;
2370 		hubp->power_gated = false;
2371 		pipe_ctx->stream_res.opp = NULL;
2372 
2373 		hubp->funcs->hubp_init(hubp);
2374 
2375 		//dc->res_pool->opps[i]->mpc_tree_params.opp_id = dc->res_pool->opps[i]->inst;
2376 		//dc->res_pool->opps[i]->mpc_tree_params.opp_list = NULL;
2377 		dc->res_pool->opps[i]->mpcc_disconnect_pending[pipe_ctx->plane_res.mpcc_inst] = true;
2378 		pipe_ctx->stream_res.opp = dc->res_pool->opps[i];
2379 		/*to do*/
2380 		hwss1_plane_atomic_disconnect(dc, pipe_ctx);
2381 	}
2382 
2383 	/* initialize DWB pointer to MCIF_WB */
2384 	for (i = 0; i < res_pool->res_cap->num_dwb; i++)
2385 		res_pool->dwbc[i]->mcif = res_pool->mcif_wb[i];
2386 
2387 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
2388 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
2389 
2390 		if (tg->funcs->is_tg_enabled(tg))
2391 			tg->funcs->unlock(tg);
2392 	}
2393 
2394 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2395 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2396 
2397 		dc->hwss.disable_plane(dc, pipe_ctx);
2398 
2399 		pipe_ctx->stream_res.tg = NULL;
2400 		pipe_ctx->plane_res.hubp = NULL;
2401 	}
2402 
2403 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
2404 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
2405 
2406 		tg->funcs->tg_init(tg);
2407 	}
2408 }
2409 
2410 void dcn20_hw_sequencer_construct(struct dc *dc)
2411 {
2412 	dcn10_hw_sequencer_construct(dc);
2413 	dc->hwss.unblank_stream = dcn20_unblank_stream;
2414 	dc->hwss.update_plane_addr = dcn20_update_plane_addr;
2415 	dc->hwss.enable_stream_timing = dcn20_enable_stream_timing;
2416 	dc->hwss.program_triplebuffer = dcn20_program_tripleBuffer;
2417 	dc->hwss.set_input_transfer_func = dcn20_set_input_transfer_func;
2418 	dc->hwss.set_output_transfer_func = dcn20_set_output_transfer_func;
2419 	dc->hwss.apply_ctx_for_surface = NULL;
2420 	dc->hwss.program_front_end_for_ctx = dcn20_program_front_end_for_ctx;
2421 	dc->hwss.pipe_control_lock = dcn20_pipe_control_lock;
2422 	dc->hwss.pipe_control_lock_global = dcn20_pipe_control_lock_global;
2423 	dc->hwss.optimize_bandwidth = dcn20_optimize_bandwidth;
2424 	dc->hwss.prepare_bandwidth = dcn20_prepare_bandwidth;
2425 	dc->hwss.update_bandwidth = dcn20_update_bandwidth;
2426 	dc->hwss.enable_writeback = dcn20_enable_writeback;
2427 	dc->hwss.disable_writeback = dcn20_disable_writeback;
2428 	dc->hwss.program_output_csc = dcn20_program_output_csc;
2429 	dc->hwss.update_odm = dcn20_update_odm;
2430 	dc->hwss.blank_pixel_data = dcn20_blank_pixel_data;
2431 	dc->hwss.dmdata_status_done = dcn20_dmdata_status_done;
2432 	dc->hwss.program_dmdata_engine = dcn20_program_dmdata_engine;
2433 	dc->hwss.enable_stream = dcn20_enable_stream;
2434 	dc->hwss.disable_stream = dcn20_disable_stream;
2435 	dc->hwss.init_sys_ctx = dcn20_init_sys_ctx;
2436 	dc->hwss.init_vm_ctx = dcn20_init_vm_ctx;
2437 	dc->hwss.disable_stream_gating = dcn20_disable_stream_gating;
2438 	dc->hwss.enable_stream_gating = dcn20_enable_stream_gating;
2439 	dc->hwss.setup_vupdate_interrupt = dcn20_setup_vupdate_interrupt;
2440 	dc->hwss.reset_hw_ctx_wrap = dcn20_reset_hw_ctx_wrap;
2441 	dc->hwss.update_mpcc = dcn20_update_mpcc;
2442 	dc->hwss.set_flip_control_gsl = dcn20_set_flip_control_gsl;
2443 	dc->hwss.init_blank = dcn20_init_blank;
2444 	dc->hwss.disable_plane = dcn20_disable_plane;
2445 	dc->hwss.plane_atomic_disable = dcn20_plane_atomic_disable;
2446 	dc->hwss.enable_power_gating_plane = dcn20_enable_power_gating_plane;
2447 	dc->hwss.dpp_pg_control = dcn20_dpp_pg_control;
2448 	dc->hwss.hubp_pg_control = dcn20_hubp_pg_control;
2449 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
2450 	dc->hwss.dsc_pg_control = dcn20_dsc_pg_control;
2451 #else
2452 	dc->hwss.dsc_pg_control = NULL;
2453 #endif
2454 	dc->hwss.disable_vga = dcn20_disable_vga;
2455 
2456 	if (IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
2457 		dc->hwss.init_hw = dcn20_fpga_init_hw;
2458 		dc->hwss.init_pipes = NULL;
2459 	}
2460 
2461 
2462 }
2463