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