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 "basics/dc_common.h"
29 #include "dm_helpers.h"
30 #include "core_types.h"
31 #include "resource.h"
32 #include "dcn20/dcn20_resource.h"
33 #include "dcn20_hwseq.h"
34 #include "dce/dce_hwseq.h"
35 #include "dcn20/dcn20_dsc.h"
36 #include "dcn20/dcn20_optc.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 "dchubbub.h"
47 #include "reg_helper.h"
48 #include "dcn10/dcn10_cm_common.h"
49 #include "vm_helper.h"
50 #include "dccg.h"
51 #include "dc_dmub_srv.h"
52 #include "dce/dmub_hw_lock_mgr.h"
53 #include "hw_sequencer.h"
54 #include "dpcd_defs.h"
55 #include "inc/link_enc_cfg.h"
56 #include "link_hwss.h"
57 #include "link.h"
58 #include "dc_state_priv.h"
59 
60 #define DC_LOGGER \
61 	dc_logger
62 #define DC_LOGGER_INIT(logger) \
63 	struct dal_logger *dc_logger = logger
64 
65 #define CTX \
66 	hws->ctx
67 #define REG(reg)\
68 	hws->regs->reg
69 
70 #undef FN
71 #define FN(reg_name, field_name) \
72 	hws->shifts->field_name, hws->masks->field_name
73 
dcn20_log_color_state(struct dc * dc,struct dc_log_buffer_ctx * log_ctx)74 void dcn20_log_color_state(struct dc *dc,
75 			   struct dc_log_buffer_ctx *log_ctx)
76 {
77 	struct dc_context *dc_ctx = dc->ctx;
78 	struct resource_pool *pool = dc->res_pool;
79 	int i;
80 
81 	DTN_INFO("DPP:  DGAM mode  SHAPER mode  3DLUT mode  3DLUT bit depth"
82 		 "  3DLUT size  RGAM mode  GAMUT adjust  "
83 		 "C11        C12        C13        C14        "
84 		 "C21        C22        C23        C24        "
85 		 "C31        C32        C33        C34        \n");
86 
87 	for (i = 0; i < pool->pipe_count; i++) {
88 		struct dpp *dpp = pool->dpps[i];
89 		struct dcn_dpp_state s = {0};
90 
91 		dpp->funcs->dpp_read_state(dpp, &s);
92 		dpp->funcs->dpp_get_gamut_remap(dpp, &s.gamut_remap);
93 
94 		if (!s.is_enabled)
95 			continue;
96 
97 		DTN_INFO("[%2d]:  %8s  %11s  %10s  %15s  %10s  %9s  %12s  "
98 			 "%010lld %010lld %010lld %010lld "
99 			 "%010lld %010lld %010lld %010lld "
100 			 "%010lld %010lld %010lld %010lld",
101 			dpp->inst,
102 			(s.dgam_lut_mode == 0) ? "Bypass" :
103 			 ((s.dgam_lut_mode == 1) ? "sRGB" :
104 			 ((s.dgam_lut_mode == 2) ? "Ycc" :
105 			 ((s.dgam_lut_mode == 3) ? "RAM" :
106 			 ((s.dgam_lut_mode == 4) ? "RAM" :
107 						   "Unknown")))),
108 			(s.shaper_lut_mode == 1) ? "RAM A" :
109 			 ((s.shaper_lut_mode == 2) ? "RAM B" :
110 						     "Bypass"),
111 			(s.lut3d_mode == 1) ? "RAM A" :
112 			 ((s.lut3d_mode == 2) ? "RAM B" :
113 						"Bypass"),
114 			(s.lut3d_bit_depth <= 0) ? "12-bit" : "10-bit",
115 			(s.lut3d_size == 0) ? "17x17x17" : "9x9x9",
116 			(s.rgam_lut_mode == 1) ? "RAM A" :
117 			 ((s.rgam_lut_mode == 1) ? "RAM B" : "Bypass"),
118 			(s.gamut_remap.gamut_adjust_type == 0) ? "Bypass" :
119 			 ((s.gamut_remap.gamut_adjust_type == 1) ? "HW" :
120 								   "SW"),
121 			s.gamut_remap.temperature_matrix[0].value,
122 			s.gamut_remap.temperature_matrix[1].value,
123 			s.gamut_remap.temperature_matrix[2].value,
124 			s.gamut_remap.temperature_matrix[3].value,
125 			s.gamut_remap.temperature_matrix[4].value,
126 			s.gamut_remap.temperature_matrix[5].value,
127 			s.gamut_remap.temperature_matrix[6].value,
128 			s.gamut_remap.temperature_matrix[7].value,
129 			s.gamut_remap.temperature_matrix[8].value,
130 			s.gamut_remap.temperature_matrix[9].value,
131 			s.gamut_remap.temperature_matrix[10].value,
132 			s.gamut_remap.temperature_matrix[11].value);
133 		DTN_INFO("\n");
134 	}
135 	DTN_INFO("\n");
136 	DTN_INFO("DPP Color Caps: input_lut_shared:%d  icsc:%d"
137 		 "  dgam_ram:%d  dgam_rom: srgb:%d,bt2020:%d,gamma2_2:%d,pq:%d,hlg:%d"
138 		 "  post_csc:%d  gamcor:%d  dgam_rom_for_yuv:%d  3d_lut:%d"
139 		 "  blnd_lut:%d  oscs:%d\n\n",
140 		 dc->caps.color.dpp.input_lut_shared,
141 		 dc->caps.color.dpp.icsc,
142 		 dc->caps.color.dpp.dgam_ram,
143 		 dc->caps.color.dpp.dgam_rom_caps.srgb,
144 		 dc->caps.color.dpp.dgam_rom_caps.bt2020,
145 		 dc->caps.color.dpp.dgam_rom_caps.gamma2_2,
146 		 dc->caps.color.dpp.dgam_rom_caps.pq,
147 		 dc->caps.color.dpp.dgam_rom_caps.hlg,
148 		 dc->caps.color.dpp.post_csc,
149 		 dc->caps.color.dpp.gamma_corr,
150 		 dc->caps.color.dpp.dgam_rom_for_yuv,
151 		 dc->caps.color.dpp.hw_3d_lut,
152 		 dc->caps.color.dpp.ogam_ram,
153 		 dc->caps.color.dpp.ocsc);
154 
155 	DTN_INFO("MPCC:  OPP  DPP  MPCCBOT  MODE  ALPHA_MODE  PREMULT  OVERLAP_ONLY  IDLE"
156 		 "  OGAM mode\n");
157 
158 	for (i = 0; i < pool->mpcc_count; i++) {
159 		struct mpcc_state s = {0};
160 
161 		pool->mpc->funcs->read_mpcc_state(pool->mpc, i, &s);
162 		if (s.opp_id != 0xf)
163 			DTN_INFO("[%2d]:  %2xh  %2xh  %6xh  %4d  %10d  %7d  %12d  %4d  %9s\n",
164 				i, s.opp_id, s.dpp_id, s.bot_mpcc_id,
165 				s.mode, s.alpha_mode, s.pre_multiplied_alpha, s.overlap_only,
166 				s.idle,
167 				(s.rgam_mode == 1) ? "RAM A" :
168 				 ((s.rgam_mode == 2) ? "RAM B" :
169 						       "Bypass"));
170 	}
171 	DTN_INFO("\n");
172 	DTN_INFO("MPC Color Caps: gamut_remap:%d, 3dlut:%d, ogam_ram:%d, ocsc:%d\n\n",
173 		 dc->caps.color.mpc.gamut_remap,
174 		 dc->caps.color.mpc.num_3dluts,
175 		 dc->caps.color.mpc.ogam_ram,
176 		 dc->caps.color.mpc.ocsc);
177 }
178 
179 
find_free_gsl_group(const struct dc * dc)180 static int find_free_gsl_group(const struct dc *dc)
181 {
182 	if (dc->res_pool->gsl_groups.gsl_0 == 0)
183 		return 1;
184 	if (dc->res_pool->gsl_groups.gsl_1 == 0)
185 		return 2;
186 	if (dc->res_pool->gsl_groups.gsl_2 == 0)
187 		return 3;
188 
189 	return 0;
190 }
191 
192 /* NOTE: This is not a generic setup_gsl function (hence the suffix as_lock)
193  * This is only used to lock pipes in pipe splitting case with immediate flip
194  * Ordinary MPC/OTG locks suppress VUPDATE which doesn't help with immediate,
195  * so we get tearing with freesync since we cannot flip multiple pipes
196  * atomically.
197  * We use GSL for this:
198  * - immediate flip: find first available GSL group if not already assigned
199  *                   program gsl with that group, set current OTG as master
200  *                   and always us 0x4 = AND of flip_ready from all pipes
201  * - vsync flip: disable GSL if used
202  *
203  * Groups in stream_res are stored as +1 from HW registers, i.e.
204  * gsl_0 <=> pipe_ctx->stream_res.gsl_group == 1
205  * Using a magic value like -1 would require tracking all inits/resets
206  */
dcn20_setup_gsl_group_as_lock(const struct dc * dc,struct pipe_ctx * pipe_ctx,bool enable)207 void dcn20_setup_gsl_group_as_lock(
208 		const struct dc *dc,
209 		struct pipe_ctx *pipe_ctx,
210 		bool enable)
211 {
212 	struct gsl_params gsl;
213 	int group_idx;
214 
215 	memset(&gsl, 0, sizeof(struct gsl_params));
216 
217 	if (enable) {
218 		/* return if group already assigned since GSL was set up
219 		 * for vsync flip, we would unassign so it can't be "left over"
220 		 */
221 		if (pipe_ctx->stream_res.gsl_group > 0)
222 			return;
223 
224 		group_idx = find_free_gsl_group(dc);
225 		ASSERT(group_idx != 0);
226 		pipe_ctx->stream_res.gsl_group = group_idx;
227 
228 		/* set gsl group reg field and mark resource used */
229 		switch (group_idx) {
230 		case 1:
231 			gsl.gsl0_en = 1;
232 			dc->res_pool->gsl_groups.gsl_0 = 1;
233 			break;
234 		case 2:
235 			gsl.gsl1_en = 1;
236 			dc->res_pool->gsl_groups.gsl_1 = 1;
237 			break;
238 		case 3:
239 			gsl.gsl2_en = 1;
240 			dc->res_pool->gsl_groups.gsl_2 = 1;
241 			break;
242 		default:
243 			BREAK_TO_DEBUGGER();
244 			return; // invalid case
245 		}
246 		gsl.gsl_master_en = 1;
247 	} else {
248 		group_idx = pipe_ctx->stream_res.gsl_group;
249 		if (group_idx == 0)
250 			return; // if not in use, just return
251 
252 		pipe_ctx->stream_res.gsl_group = 0;
253 
254 		/* unset gsl group reg field and mark resource free */
255 		switch (group_idx) {
256 		case 1:
257 			gsl.gsl0_en = 0;
258 			dc->res_pool->gsl_groups.gsl_0 = 0;
259 			break;
260 		case 2:
261 			gsl.gsl1_en = 0;
262 			dc->res_pool->gsl_groups.gsl_1 = 0;
263 			break;
264 		case 3:
265 			gsl.gsl2_en = 0;
266 			dc->res_pool->gsl_groups.gsl_2 = 0;
267 			break;
268 		default:
269 			BREAK_TO_DEBUGGER();
270 			return;
271 		}
272 		gsl.gsl_master_en = 0;
273 	}
274 
275 	/* at this point we want to program whether it's to enable or disable */
276 	if (pipe_ctx->stream_res.tg->funcs->set_gsl != NULL &&
277 		pipe_ctx->stream_res.tg->funcs->set_gsl_source_select != NULL) {
278 		pipe_ctx->stream_res.tg->funcs->set_gsl(
279 			pipe_ctx->stream_res.tg,
280 			&gsl);
281 
282 		pipe_ctx->stream_res.tg->funcs->set_gsl_source_select(
283 			pipe_ctx->stream_res.tg, group_idx,	enable ? 4 : 0);
284 	} else
285 		BREAK_TO_DEBUGGER();
286 }
287 
dcn20_set_flip_control_gsl(struct pipe_ctx * pipe_ctx,bool flip_immediate)288 void dcn20_set_flip_control_gsl(
289 		struct pipe_ctx *pipe_ctx,
290 		bool flip_immediate)
291 {
292 	if (pipe_ctx && pipe_ctx->plane_res.hubp->funcs->hubp_set_flip_control_surface_gsl)
293 		pipe_ctx->plane_res.hubp->funcs->hubp_set_flip_control_surface_gsl(
294 				pipe_ctx->plane_res.hubp, flip_immediate);
295 
296 }
297 
dcn20_enable_power_gating_plane(struct dce_hwseq * hws,bool enable)298 void dcn20_enable_power_gating_plane(
299 	struct dce_hwseq *hws,
300 	bool enable)
301 {
302 	bool force_on = true; /* disable power gating */
303 	uint32_t org_ip_request_cntl = 0;
304 
305 	if (enable)
306 		force_on = false;
307 
308 	REG_GET(DC_IP_REQUEST_CNTL, IP_REQUEST_EN, &org_ip_request_cntl);
309 	if (org_ip_request_cntl == 0)
310 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 1);
311 
312 	/* DCHUBP0/1/2/3/4/5 */
313 	REG_UPDATE(DOMAIN0_PG_CONFIG, DOMAIN0_POWER_FORCEON, force_on);
314 	REG_UPDATE(DOMAIN2_PG_CONFIG, DOMAIN2_POWER_FORCEON, force_on);
315 	REG_UPDATE(DOMAIN4_PG_CONFIG, DOMAIN4_POWER_FORCEON, force_on);
316 	REG_UPDATE(DOMAIN6_PG_CONFIG, DOMAIN6_POWER_FORCEON, force_on);
317 	if (REG(DOMAIN8_PG_CONFIG))
318 		REG_UPDATE(DOMAIN8_PG_CONFIG, DOMAIN8_POWER_FORCEON, force_on);
319 	if (REG(DOMAIN10_PG_CONFIG))
320 		REG_UPDATE(DOMAIN10_PG_CONFIG, DOMAIN8_POWER_FORCEON, force_on);
321 
322 	/* DPP0/1/2/3/4/5 */
323 	REG_UPDATE(DOMAIN1_PG_CONFIG, DOMAIN1_POWER_FORCEON, force_on);
324 	REG_UPDATE(DOMAIN3_PG_CONFIG, DOMAIN3_POWER_FORCEON, force_on);
325 	REG_UPDATE(DOMAIN5_PG_CONFIG, DOMAIN5_POWER_FORCEON, force_on);
326 	REG_UPDATE(DOMAIN7_PG_CONFIG, DOMAIN7_POWER_FORCEON, force_on);
327 	if (REG(DOMAIN9_PG_CONFIG))
328 		REG_UPDATE(DOMAIN9_PG_CONFIG, DOMAIN9_POWER_FORCEON, force_on);
329 	if (REG(DOMAIN11_PG_CONFIG))
330 		REG_UPDATE(DOMAIN11_PG_CONFIG, DOMAIN9_POWER_FORCEON, force_on);
331 
332 	/* DCS0/1/2/3/4/5 */
333 	REG_UPDATE(DOMAIN16_PG_CONFIG, DOMAIN16_POWER_FORCEON, force_on);
334 	REG_UPDATE(DOMAIN17_PG_CONFIG, DOMAIN17_POWER_FORCEON, force_on);
335 	REG_UPDATE(DOMAIN18_PG_CONFIG, DOMAIN18_POWER_FORCEON, force_on);
336 	if (REG(DOMAIN19_PG_CONFIG))
337 		REG_UPDATE(DOMAIN19_PG_CONFIG, DOMAIN19_POWER_FORCEON, force_on);
338 	if (REG(DOMAIN20_PG_CONFIG))
339 		REG_UPDATE(DOMAIN20_PG_CONFIG, DOMAIN20_POWER_FORCEON, force_on);
340 	if (REG(DOMAIN21_PG_CONFIG))
341 		REG_UPDATE(DOMAIN21_PG_CONFIG, DOMAIN21_POWER_FORCEON, force_on);
342 
343 	if (org_ip_request_cntl == 0)
344 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 0);
345 
346 }
347 
dcn20_dccg_init(struct dce_hwseq * hws)348 void dcn20_dccg_init(struct dce_hwseq *hws)
349 {
350 	/*
351 	 * set MICROSECOND_TIME_BASE_DIV
352 	 * 100Mhz refclk -> 0x120264
353 	 * 27Mhz refclk -> 0x12021b
354 	 * 48Mhz refclk -> 0x120230
355 	 *
356 	 */
357 	REG_WRITE(MICROSECOND_TIME_BASE_DIV, 0x120264);
358 
359 	/*
360 	 * set MILLISECOND_TIME_BASE_DIV
361 	 * 100Mhz refclk -> 0x1186a0
362 	 * 27Mhz refclk -> 0x106978
363 	 * 48Mhz refclk -> 0x10bb80
364 	 *
365 	 */
366 	REG_WRITE(MILLISECOND_TIME_BASE_DIV, 0x1186a0);
367 
368 	/* This value is dependent on the hardware pipeline delay so set once per SOC */
369 	REG_WRITE(DISPCLK_FREQ_CHANGE_CNTL, 0xe01003c);
370 }
371 
dcn20_disable_vga(struct dce_hwseq * hws)372 void dcn20_disable_vga(
373 	struct dce_hwseq *hws)
374 {
375 	REG_WRITE(D1VGA_CONTROL, 0);
376 	REG_WRITE(D2VGA_CONTROL, 0);
377 	REG_WRITE(D3VGA_CONTROL, 0);
378 	REG_WRITE(D4VGA_CONTROL, 0);
379 	REG_WRITE(D5VGA_CONTROL, 0);
380 	REG_WRITE(D6VGA_CONTROL, 0);
381 }
382 
dcn20_program_triple_buffer(const struct dc * dc,struct pipe_ctx * pipe_ctx,bool enable_triple_buffer)383 void dcn20_program_triple_buffer(
384 	const struct dc *dc,
385 	struct pipe_ctx *pipe_ctx,
386 	bool enable_triple_buffer)
387 {
388 	if (pipe_ctx->plane_res.hubp && pipe_ctx->plane_res.hubp->funcs) {
389 		pipe_ctx->plane_res.hubp->funcs->hubp_enable_tripleBuffer(
390 			pipe_ctx->plane_res.hubp,
391 			enable_triple_buffer);
392 	}
393 }
394 
395 /* Blank pixel data during initialization */
dcn20_init_blank(struct dc * dc,struct timing_generator * tg)396 void dcn20_init_blank(
397 		struct dc *dc,
398 		struct timing_generator *tg)
399 {
400 	struct dce_hwseq *hws = dc->hwseq;
401 	enum dc_color_space color_space;
402 	struct tg_color black_color = {0};
403 	struct output_pixel_processor *opp = NULL;
404 	struct output_pixel_processor *bottom_opp = NULL;
405 	uint32_t num_opps, opp_id_src0, opp_id_src1;
406 	uint32_t otg_active_width = 0, otg_active_height = 0;
407 
408 	/* program opp dpg blank color */
409 	color_space = COLOR_SPACE_SRGB;
410 	color_space_to_black_color(dc, color_space, &black_color);
411 
412 	/* get the OTG active size */
413 	tg->funcs->get_otg_active_size(tg,
414 			&otg_active_width,
415 			&otg_active_height);
416 
417 	/* get the OPTC source */
418 	tg->funcs->get_optc_source(tg, &num_opps, &opp_id_src0, &opp_id_src1);
419 
420 	if (opp_id_src0 >= dc->res_pool->res_cap->num_opp) {
421 		ASSERT(false);
422 		return;
423 	}
424 	opp = dc->res_pool->opps[opp_id_src0];
425 
426 	/* don't override the blank pattern if already enabled with the correct one. */
427 	if (opp->funcs->dpg_is_blanked && opp->funcs->dpg_is_blanked(opp))
428 		return;
429 
430 	if (num_opps == 2) {
431 		otg_active_width = otg_active_width / 2;
432 
433 		if (opp_id_src1 >= dc->res_pool->res_cap->num_opp) {
434 			ASSERT(false);
435 			return;
436 		}
437 		bottom_opp = dc->res_pool->opps[opp_id_src1];
438 	}
439 
440 	opp->funcs->opp_set_disp_pattern_generator(
441 			opp,
442 			CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR,
443 			CONTROLLER_DP_COLOR_SPACE_UDEFINED,
444 			COLOR_DEPTH_UNDEFINED,
445 			&black_color,
446 			otg_active_width,
447 			otg_active_height,
448 			0);
449 
450 	if (num_opps == 2) {
451 		bottom_opp->funcs->opp_set_disp_pattern_generator(
452 				bottom_opp,
453 				CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR,
454 				CONTROLLER_DP_COLOR_SPACE_UDEFINED,
455 				COLOR_DEPTH_UNDEFINED,
456 				&black_color,
457 				otg_active_width,
458 				otg_active_height,
459 				0);
460 	}
461 
462 	hws->funcs.wait_for_blank_complete(opp);
463 }
464 
dcn20_dsc_pg_control(struct dce_hwseq * hws,unsigned int dsc_inst,bool power_on)465 void dcn20_dsc_pg_control(
466 		struct dce_hwseq *hws,
467 		unsigned int dsc_inst,
468 		bool power_on)
469 {
470 	uint32_t power_gate = power_on ? 0 : 1;
471 	uint32_t pwr_status = power_on ? 0 : 2;
472 	uint32_t org_ip_request_cntl = 0;
473 
474 	if (hws->ctx->dc->debug.disable_dsc_power_gate)
475 		return;
476 
477 	if (REG(DOMAIN16_PG_CONFIG) == 0)
478 		return;
479 
480 	REG_GET(DC_IP_REQUEST_CNTL, IP_REQUEST_EN, &org_ip_request_cntl);
481 	if (org_ip_request_cntl == 0)
482 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 1);
483 
484 	switch (dsc_inst) {
485 	case 0: /* DSC0 */
486 		REG_UPDATE(DOMAIN16_PG_CONFIG,
487 				DOMAIN16_POWER_GATE, power_gate);
488 
489 		REG_WAIT(DOMAIN16_PG_STATUS,
490 				DOMAIN16_PGFSM_PWR_STATUS, pwr_status,
491 				1, 1000);
492 		break;
493 	case 1: /* DSC1 */
494 		REG_UPDATE(DOMAIN17_PG_CONFIG,
495 				DOMAIN17_POWER_GATE, power_gate);
496 
497 		REG_WAIT(DOMAIN17_PG_STATUS,
498 				DOMAIN17_PGFSM_PWR_STATUS, pwr_status,
499 				1, 1000);
500 		break;
501 	case 2: /* DSC2 */
502 		REG_UPDATE(DOMAIN18_PG_CONFIG,
503 				DOMAIN18_POWER_GATE, power_gate);
504 
505 		REG_WAIT(DOMAIN18_PG_STATUS,
506 				DOMAIN18_PGFSM_PWR_STATUS, pwr_status,
507 				1, 1000);
508 		break;
509 	case 3: /* DSC3 */
510 		REG_UPDATE(DOMAIN19_PG_CONFIG,
511 				DOMAIN19_POWER_GATE, power_gate);
512 
513 		REG_WAIT(DOMAIN19_PG_STATUS,
514 				DOMAIN19_PGFSM_PWR_STATUS, pwr_status,
515 				1, 1000);
516 		break;
517 	case 4: /* DSC4 */
518 		REG_UPDATE(DOMAIN20_PG_CONFIG,
519 				DOMAIN20_POWER_GATE, power_gate);
520 
521 		REG_WAIT(DOMAIN20_PG_STATUS,
522 				DOMAIN20_PGFSM_PWR_STATUS, pwr_status,
523 				1, 1000);
524 		break;
525 	case 5: /* DSC5 */
526 		REG_UPDATE(DOMAIN21_PG_CONFIG,
527 				DOMAIN21_POWER_GATE, power_gate);
528 
529 		REG_WAIT(DOMAIN21_PG_STATUS,
530 				DOMAIN21_PGFSM_PWR_STATUS, pwr_status,
531 				1, 1000);
532 		break;
533 	default:
534 		BREAK_TO_DEBUGGER();
535 		break;
536 	}
537 
538 	if (org_ip_request_cntl == 0)
539 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 0);
540 }
541 
dcn20_dpp_pg_control(struct dce_hwseq * hws,unsigned int dpp_inst,bool power_on)542 void dcn20_dpp_pg_control(
543 		struct dce_hwseq *hws,
544 		unsigned int dpp_inst,
545 		bool power_on)
546 {
547 	uint32_t power_gate = power_on ? 0 : 1;
548 	uint32_t pwr_status = power_on ? 0 : 2;
549 
550 	if (hws->ctx->dc->debug.disable_dpp_power_gate)
551 		return;
552 	if (REG(DOMAIN1_PG_CONFIG) == 0)
553 		return;
554 
555 	switch (dpp_inst) {
556 	case 0: /* DPP0 */
557 		REG_UPDATE(DOMAIN1_PG_CONFIG,
558 				DOMAIN1_POWER_GATE, power_gate);
559 
560 		REG_WAIT(DOMAIN1_PG_STATUS,
561 				DOMAIN1_PGFSM_PWR_STATUS, pwr_status,
562 				1, 1000);
563 		break;
564 	case 1: /* DPP1 */
565 		REG_UPDATE(DOMAIN3_PG_CONFIG,
566 				DOMAIN3_POWER_GATE, power_gate);
567 
568 		REG_WAIT(DOMAIN3_PG_STATUS,
569 				DOMAIN3_PGFSM_PWR_STATUS, pwr_status,
570 				1, 1000);
571 		break;
572 	case 2: /* DPP2 */
573 		REG_UPDATE(DOMAIN5_PG_CONFIG,
574 				DOMAIN5_POWER_GATE, power_gate);
575 
576 		REG_WAIT(DOMAIN5_PG_STATUS,
577 				DOMAIN5_PGFSM_PWR_STATUS, pwr_status,
578 				1, 1000);
579 		break;
580 	case 3: /* DPP3 */
581 		REG_UPDATE(DOMAIN7_PG_CONFIG,
582 				DOMAIN7_POWER_GATE, power_gate);
583 
584 		REG_WAIT(DOMAIN7_PG_STATUS,
585 				DOMAIN7_PGFSM_PWR_STATUS, pwr_status,
586 				1, 1000);
587 		break;
588 	case 4: /* DPP4 */
589 		REG_UPDATE(DOMAIN9_PG_CONFIG,
590 				DOMAIN9_POWER_GATE, power_gate);
591 
592 		REG_WAIT(DOMAIN9_PG_STATUS,
593 				DOMAIN9_PGFSM_PWR_STATUS, pwr_status,
594 				1, 1000);
595 		break;
596 	case 5: /* DPP5 */
597 		/*
598 		 * Do not power gate DPP5, should be left at HW default, power on permanently.
599 		 * PG on Pipe5 is De-featured, attempting to put it to PG state may result in hard
600 		 * reset.
601 		 * REG_UPDATE(DOMAIN11_PG_CONFIG,
602 		 *		DOMAIN11_POWER_GATE, power_gate);
603 		 *
604 		 * REG_WAIT(DOMAIN11_PG_STATUS,
605 		 *		DOMAIN11_PGFSM_PWR_STATUS, pwr_status,
606 		 * 		1, 1000);
607 		 */
608 		break;
609 	default:
610 		BREAK_TO_DEBUGGER();
611 		break;
612 	}
613 }
614 
615 
dcn20_hubp_pg_control(struct dce_hwseq * hws,unsigned int hubp_inst,bool power_on)616 void dcn20_hubp_pg_control(
617 		struct dce_hwseq *hws,
618 		unsigned int hubp_inst,
619 		bool power_on)
620 {
621 	uint32_t power_gate = power_on ? 0 : 1;
622 	uint32_t pwr_status = power_on ? 0 : 2;
623 
624 	if (hws->ctx->dc->debug.disable_hubp_power_gate)
625 		return;
626 	if (REG(DOMAIN0_PG_CONFIG) == 0)
627 		return;
628 
629 	switch (hubp_inst) {
630 	case 0: /* DCHUBP0 */
631 		REG_UPDATE(DOMAIN0_PG_CONFIG,
632 				DOMAIN0_POWER_GATE, power_gate);
633 
634 		REG_WAIT(DOMAIN0_PG_STATUS,
635 				DOMAIN0_PGFSM_PWR_STATUS, pwr_status,
636 				1, 1000);
637 		break;
638 	case 1: /* DCHUBP1 */
639 		REG_UPDATE(DOMAIN2_PG_CONFIG,
640 				DOMAIN2_POWER_GATE, power_gate);
641 
642 		REG_WAIT(DOMAIN2_PG_STATUS,
643 				DOMAIN2_PGFSM_PWR_STATUS, pwr_status,
644 				1, 1000);
645 		break;
646 	case 2: /* DCHUBP2 */
647 		REG_UPDATE(DOMAIN4_PG_CONFIG,
648 				DOMAIN4_POWER_GATE, power_gate);
649 
650 		REG_WAIT(DOMAIN4_PG_STATUS,
651 				DOMAIN4_PGFSM_PWR_STATUS, pwr_status,
652 				1, 1000);
653 		break;
654 	case 3: /* DCHUBP3 */
655 		REG_UPDATE(DOMAIN6_PG_CONFIG,
656 				DOMAIN6_POWER_GATE, power_gate);
657 
658 		REG_WAIT(DOMAIN6_PG_STATUS,
659 				DOMAIN6_PGFSM_PWR_STATUS, pwr_status,
660 				1, 1000);
661 		break;
662 	case 4: /* DCHUBP4 */
663 		REG_UPDATE(DOMAIN8_PG_CONFIG,
664 				DOMAIN8_POWER_GATE, power_gate);
665 
666 		REG_WAIT(DOMAIN8_PG_STATUS,
667 				DOMAIN8_PGFSM_PWR_STATUS, pwr_status,
668 				1, 1000);
669 		break;
670 	case 5: /* DCHUBP5 */
671 		/*
672 		 * Do not power gate DCHUB5, should be left at HW default, power on permanently.
673 		 * PG on Pipe5 is De-featured, attempting to put it to PG state may result in hard
674 		 * reset.
675 		 * REG_UPDATE(DOMAIN10_PG_CONFIG,
676 		 *		DOMAIN10_POWER_GATE, power_gate);
677 		 *
678 		 * REG_WAIT(DOMAIN10_PG_STATUS,
679 		 *		DOMAIN10_PGFSM_PWR_STATUS, pwr_status,
680 		 *		1, 1000);
681 		 */
682 		break;
683 	default:
684 		BREAK_TO_DEBUGGER();
685 		break;
686 	}
687 }
688 
689 
690 /* disable HW used by plane.
691  * note:  cannot disable until disconnect is complete
692  */
dcn20_plane_atomic_disable(struct dc * dc,struct pipe_ctx * pipe_ctx)693 void dcn20_plane_atomic_disable(struct dc *dc, struct pipe_ctx *pipe_ctx)
694 {
695 	struct dce_hwseq *hws = dc->hwseq;
696 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
697 	struct dpp *dpp = pipe_ctx->plane_res.dpp;
698 
699 	dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, pipe_ctx);
700 
701 	/* In flip immediate with pipe splitting case GSL is used for
702 	 * synchronization so we must disable it when the plane is disabled.
703 	 */
704 	if (pipe_ctx->stream_res.gsl_group != 0)
705 		dcn20_setup_gsl_group_as_lock(dc, pipe_ctx, false);
706 
707 	if (hubp->funcs->hubp_update_mall_sel)
708 		hubp->funcs->hubp_update_mall_sel(hubp, 0, false);
709 
710 	dc->hwss.set_flip_control_gsl(pipe_ctx, false);
711 
712 	hubp->funcs->hubp_clk_cntl(hubp, false);
713 
714 	dpp->funcs->dpp_dppclk_control(dpp, false, false);
715 
716 	hubp->power_gated = true;
717 
718 	hws->funcs.plane_atomic_power_down(dc,
719 			pipe_ctx->plane_res.dpp,
720 			pipe_ctx->plane_res.hubp);
721 
722 	pipe_ctx->stream = NULL;
723 	memset(&pipe_ctx->stream_res, 0, sizeof(pipe_ctx->stream_res));
724 	memset(&pipe_ctx->plane_res, 0, sizeof(pipe_ctx->plane_res));
725 	pipe_ctx->top_pipe = NULL;
726 	pipe_ctx->bottom_pipe = NULL;
727 	pipe_ctx->prev_odm_pipe = NULL;
728 	pipe_ctx->next_odm_pipe = NULL;
729 	pipe_ctx->plane_state = NULL;
730 }
731 
732 
dcn20_disable_plane(struct dc * dc,struct dc_state * state,struct pipe_ctx * pipe_ctx)733 void dcn20_disable_plane(struct dc *dc, struct dc_state *state, struct pipe_ctx *pipe_ctx)
734 {
735 	bool is_phantom = dc_state_get_pipe_subvp_type(state, pipe_ctx) == SUBVP_PHANTOM;
736 	struct timing_generator *tg = is_phantom ? pipe_ctx->stream_res.tg : NULL;
737 
738 	DC_LOGGER_INIT(dc->ctx->logger);
739 
740 	if (!pipe_ctx->plane_res.hubp || pipe_ctx->plane_res.hubp->power_gated)
741 		return;
742 
743 	dcn20_plane_atomic_disable(dc, pipe_ctx);
744 
745 	/* Turn back off the phantom OTG after the phantom plane is fully disabled
746 	 */
747 	if (is_phantom)
748 		if (tg && tg->funcs->disable_phantom_crtc)
749 			tg->funcs->disable_phantom_crtc(tg);
750 
751 	DC_LOG_DC("Power down front end %d\n",
752 					pipe_ctx->pipe_idx);
753 }
754 
dcn20_disable_pixel_data(struct dc * dc,struct pipe_ctx * pipe_ctx,bool blank)755 void dcn20_disable_pixel_data(struct dc *dc, struct pipe_ctx *pipe_ctx, bool blank)
756 {
757 	dcn20_blank_pixel_data(dc, pipe_ctx, blank);
758 }
759 
calc_mpc_flow_ctrl_cnt(const struct dc_stream_state * stream,int opp_cnt,bool is_two_pixels_per_container)760 static int calc_mpc_flow_ctrl_cnt(const struct dc_stream_state *stream,
761 		int opp_cnt, bool is_two_pixels_per_container)
762 {
763 	bool hblank_halved = is_two_pixels_per_container;
764 	int flow_ctrl_cnt;
765 
766 	if (opp_cnt >= 2)
767 		hblank_halved = true;
768 
769 	flow_ctrl_cnt = stream->timing.h_total - stream->timing.h_addressable -
770 			stream->timing.h_border_left -
771 			stream->timing.h_border_right;
772 
773 	if (hblank_halved)
774 		flow_ctrl_cnt /= 2;
775 
776 	/* ODM combine 4:1 case */
777 	if (opp_cnt == 4)
778 		flow_ctrl_cnt /= 2;
779 
780 	return flow_ctrl_cnt;
781 }
782 
get_phyd32clk_src(struct dc_link * link)783 static enum phyd32clk_clock_source get_phyd32clk_src(struct dc_link *link)
784 {
785 	switch (link->link_enc->transmitter) {
786 	case TRANSMITTER_UNIPHY_A:
787 		return PHYD32CLKA;
788 	case TRANSMITTER_UNIPHY_B:
789 		return PHYD32CLKB;
790 	case TRANSMITTER_UNIPHY_C:
791 		return PHYD32CLKC;
792 	case TRANSMITTER_UNIPHY_D:
793 		return PHYD32CLKD;
794 	case TRANSMITTER_UNIPHY_E:
795 		return PHYD32CLKE;
796 	default:
797 		return PHYD32CLKA;
798 	}
799 }
800 
get_odm_segment_count(struct pipe_ctx * pipe_ctx)801 static int get_odm_segment_count(struct pipe_ctx *pipe_ctx)
802 {
803 	struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
804 	int count = 1;
805 
806 	while (odm_pipe != NULL) {
807 		count++;
808 		odm_pipe = odm_pipe->next_odm_pipe;
809 	}
810 
811 	return count;
812 }
813 
dcn20_enable_stream_timing(struct pipe_ctx * pipe_ctx,struct dc_state * context,struct dc * dc)814 enum dc_status dcn20_enable_stream_timing(
815 		struct pipe_ctx *pipe_ctx,
816 		struct dc_state *context,
817 		struct dc *dc)
818 {
819 	struct dce_hwseq *hws = dc->hwseq;
820 	struct dc_stream_state *stream = pipe_ctx->stream;
821 	struct drr_params params = {0};
822 	unsigned int event_triggers = 0;
823 	int opp_cnt = 1;
824 	int opp_inst[MAX_PIPES] = {0};
825 	bool interlace = stream->timing.flags.INTERLACE;
826 	int i;
827 	struct mpc_dwb_flow_control flow_control;
828 	struct mpc *mpc = dc->res_pool->mpc;
829 	bool is_two_pixels_per_container =
830 			pipe_ctx->stream_res.tg->funcs->is_two_pixels_per_container(&stream->timing);
831 	bool rate_control_2x_pclk = (interlace || is_two_pixels_per_container);
832 	int odm_slice_width;
833 	int last_odm_slice_width;
834 	struct pipe_ctx *opp_heads[MAX_PIPES];
835 
836 	if (dc->res_pool->dccg->funcs->set_pixel_rate_div)
837 		dc->res_pool->dccg->funcs->set_pixel_rate_div(
838 			dc->res_pool->dccg,
839 			pipe_ctx->stream_res.tg->inst,
840 			pipe_ctx->pixel_rate_divider.div_factor1,
841 			pipe_ctx->pixel_rate_divider.div_factor2);
842 
843 	/* by upper caller loop, pipe0 is parent pipe and be called first.
844 	 * back end is set up by for pipe0. Other children pipe share back end
845 	 * with pipe 0. No program is needed.
846 	 */
847 	if (pipe_ctx->top_pipe != NULL)
848 		return DC_OK;
849 
850 	/* TODO check if timing_changed, disable stream if timing changed */
851 
852 	opp_cnt = resource_get_opp_heads_for_otg_master(pipe_ctx, &context->res_ctx, opp_heads);
853 	for (i = 0; i < opp_cnt; i++)
854 		opp_inst[i] = opp_heads[i]->stream_res.opp->inst;
855 
856 	odm_slice_width = resource_get_odm_slice_dst_width(pipe_ctx, false);
857 	last_odm_slice_width = resource_get_odm_slice_dst_width(pipe_ctx, true);
858 	if (opp_cnt > 1)
859 		pipe_ctx->stream_res.tg->funcs->set_odm_combine(
860 				pipe_ctx->stream_res.tg,
861 				opp_inst, opp_cnt, odm_slice_width,
862 				last_odm_slice_width);
863 
864 	/* HW program guide assume display already disable
865 	 * by unplug sequence. OTG assume stop.
866 	 */
867 	pipe_ctx->stream_res.tg->funcs->enable_optc_clock(pipe_ctx->stream_res.tg, true);
868 
869 	if (false == pipe_ctx->clock_source->funcs->program_pix_clk(
870 			pipe_ctx->clock_source,
871 			&pipe_ctx->stream_res.pix_clk_params,
872 			dc->link_srv->dp_get_encoding_format(&pipe_ctx->link_config.dp_link_settings),
873 			&pipe_ctx->pll_settings)) {
874 		BREAK_TO_DEBUGGER();
875 		return DC_ERROR_UNEXPECTED;
876 	}
877 
878 	if (dc->link_srv->dp_is_128b_132b_signal(pipe_ctx)) {
879 		struct dccg *dccg = dc->res_pool->dccg;
880 		struct timing_generator *tg = pipe_ctx->stream_res.tg;
881 		struct dtbclk_dto_params dto_params = {0};
882 
883 		if (dccg->funcs->set_dtbclk_p_src)
884 			dccg->funcs->set_dtbclk_p_src(dccg, DTBCLK0, tg->inst);
885 
886 		dto_params.otg_inst = tg->inst;
887 		dto_params.pixclk_khz = pipe_ctx->stream->timing.pix_clk_100hz / 10;
888 		dto_params.num_odm_segments = get_odm_segment_count(pipe_ctx);
889 		dto_params.timing = &pipe_ctx->stream->timing;
890 		dto_params.ref_dtbclk_khz = dc->clk_mgr->funcs->get_dtb_ref_clk_frequency(dc->clk_mgr);
891 		dccg->funcs->set_dtbclk_dto(dccg, &dto_params);
892 	}
893 
894 	if (dc_is_hdmi_tmds_signal(stream->signal)) {
895 		stream->link->phy_state.symclk_ref_cnts.otg = 1;
896 		if (stream->link->phy_state.symclk_state == SYMCLK_OFF_TX_OFF)
897 			stream->link->phy_state.symclk_state = SYMCLK_ON_TX_OFF;
898 		else
899 			stream->link->phy_state.symclk_state = SYMCLK_ON_TX_ON;
900 	}
901 
902 	if (dc->hwseq->funcs.PLAT_58856_wa && (!dc_is_dp_signal(stream->signal)))
903 		dc->hwseq->funcs.PLAT_58856_wa(context, pipe_ctx);
904 
905 	pipe_ctx->stream_res.tg->funcs->program_timing(
906 			pipe_ctx->stream_res.tg,
907 			&stream->timing,
908 			pipe_ctx->pipe_dlg_param.vready_offset,
909 			pipe_ctx->pipe_dlg_param.vstartup_start,
910 			pipe_ctx->pipe_dlg_param.vupdate_offset,
911 			pipe_ctx->pipe_dlg_param.vupdate_width,
912 			pipe_ctx->pipe_dlg_param.pstate_keepout,
913 			pipe_ctx->stream->signal,
914 			true);
915 
916 	rate_control_2x_pclk = rate_control_2x_pclk || opp_cnt > 1;
917 	flow_control.flow_ctrl_mode = 0;
918 	flow_control.flow_ctrl_cnt0 = 0x80;
919 	flow_control.flow_ctrl_cnt1 = calc_mpc_flow_ctrl_cnt(stream, opp_cnt,
920 			is_two_pixels_per_container);
921 	if (mpc->funcs->set_out_rate_control) {
922 		for (i = 0; i < opp_cnt; ++i) {
923 			mpc->funcs->set_out_rate_control(
924 					mpc, opp_inst[i],
925 					true,
926 					rate_control_2x_pclk,
927 					&flow_control);
928 		}
929 	}
930 
931 	for (i = 0; i < opp_cnt; i++) {
932 		opp_heads[i]->stream_res.opp->funcs->opp_pipe_clock_control(
933 				opp_heads[i]->stream_res.opp,
934 				true);
935 		opp_heads[i]->stream_res.opp->funcs->opp_program_left_edge_extra_pixel(
936 				opp_heads[i]->stream_res.opp,
937 				stream->timing.pixel_encoding,
938 				resource_is_pipe_type(opp_heads[i], OTG_MASTER));
939 	}
940 
941 	hws->funcs.blank_pixel_data(dc, pipe_ctx, true);
942 
943 	/* VTG is  within DCHUB command block. DCFCLK is always on */
944 	if (false == pipe_ctx->stream_res.tg->funcs->enable_crtc(pipe_ctx->stream_res.tg)) {
945 		BREAK_TO_DEBUGGER();
946 		return DC_ERROR_UNEXPECTED;
947 	}
948 
949 	hws->funcs.wait_for_blank_complete(pipe_ctx->stream_res.opp);
950 
951 	params.vertical_total_min = stream->adjust.v_total_min;
952 	params.vertical_total_max = stream->adjust.v_total_max;
953 	params.vertical_total_mid = stream->adjust.v_total_mid;
954 	params.vertical_total_mid_frame_num = stream->adjust.v_total_mid_frame_num;
955 	set_drr_and_clear_adjust_pending(pipe_ctx, stream, &params);
956 
957 	// DRR should set trigger event to monitor surface update event
958 	if (stream->adjust.v_total_min != 0 && stream->adjust.v_total_max != 0)
959 		event_triggers = 0x80;
960 	/* Event triggers and num frames initialized for DRR, but can be
961 	 * later updated for PSR use. Note DRR trigger events are generated
962 	 * regardless of whether num frames met.
963 	 */
964 	if (pipe_ctx->stream_res.tg->funcs->set_static_screen_control)
965 		pipe_ctx->stream_res.tg->funcs->set_static_screen_control(
966 				pipe_ctx->stream_res.tg, event_triggers, 2);
967 
968 	/* TODO program crtc source select for non-virtual signal*/
969 	/* TODO program FMT */
970 	/* TODO setup link_enc */
971 	/* TODO set stream attributes */
972 	/* TODO program audio */
973 	/* TODO enable stream if timing changed */
974 	/* TODO unblank stream if DP */
975 
976 	if (dc_state_get_pipe_subvp_type(context, pipe_ctx) == SUBVP_PHANTOM) {
977 		if (pipe_ctx->stream_res.tg->funcs->phantom_crtc_post_enable)
978 			pipe_ctx->stream_res.tg->funcs->phantom_crtc_post_enable(pipe_ctx->stream_res.tg);
979 	}
980 
981 	return DC_OK;
982 }
983 
dcn20_program_output_csc(struct dc * dc,struct pipe_ctx * pipe_ctx,enum dc_color_space colorspace,uint16_t * matrix,int opp_id)984 void dcn20_program_output_csc(struct dc *dc,
985 		struct pipe_ctx *pipe_ctx,
986 		enum dc_color_space colorspace,
987 		uint16_t *matrix,
988 		int opp_id)
989 {
990 	struct mpc *mpc = dc->res_pool->mpc;
991 	enum mpc_output_csc_mode ocsc_mode = MPC_OUTPUT_CSC_COEF_A;
992 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
993 
994 	if (mpc->funcs->power_on_mpc_mem_pwr)
995 		mpc->funcs->power_on_mpc_mem_pwr(mpc, mpcc_id, true);
996 
997 	if (pipe_ctx->stream->csc_color_matrix.enable_adjustment == true) {
998 		if (mpc->funcs->set_output_csc != NULL)
999 			mpc->funcs->set_output_csc(mpc,
1000 					opp_id,
1001 					matrix,
1002 					ocsc_mode);
1003 	} else {
1004 		if (mpc->funcs->set_ocsc_default != NULL)
1005 			mpc->funcs->set_ocsc_default(mpc,
1006 					opp_id,
1007 					colorspace,
1008 					ocsc_mode);
1009 	}
1010 }
1011 
dcn20_set_output_transfer_func(struct dc * dc,struct pipe_ctx * pipe_ctx,const struct dc_stream_state * stream)1012 bool dcn20_set_output_transfer_func(struct dc *dc, struct pipe_ctx *pipe_ctx,
1013 				const struct dc_stream_state *stream)
1014 {
1015 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
1016 	struct mpc *mpc = pipe_ctx->stream_res.opp->ctx->dc->res_pool->mpc;
1017 	const struct pwl_params *params = NULL;
1018 	/*
1019 	 * program OGAM only for the top pipe
1020 	 * if there is a pipe split then fix diagnostic is required:
1021 	 * how to pass OGAM parameter for stream.
1022 	 * if programming for all pipes is required then remove condition
1023 	 * pipe_ctx->top_pipe == NULL ,but then fix the diagnostic.
1024 	 */
1025 	if (mpc->funcs->power_on_mpc_mem_pwr)
1026 		mpc->funcs->power_on_mpc_mem_pwr(mpc, mpcc_id, true);
1027 	if (pipe_ctx->top_pipe == NULL
1028 			&& mpc->funcs->set_output_gamma) {
1029 		if (stream->out_transfer_func.type == TF_TYPE_HWPWL)
1030 			params = &stream->out_transfer_func.pwl;
1031 		else if (pipe_ctx->stream->out_transfer_func.type ==
1032 			TF_TYPE_DISTRIBUTED_POINTS &&
1033 			cm_helper_translate_curve_to_hw_format(dc->ctx,
1034 			&stream->out_transfer_func,
1035 			&mpc->blender_params, false))
1036 			params = &mpc->blender_params;
1037 		/*
1038 		 * there is no ROM
1039 		 */
1040 		if (stream->out_transfer_func.type == TF_TYPE_PREDEFINED)
1041 			BREAK_TO_DEBUGGER();
1042 	}
1043 	/*
1044 	 * if above if is not executed then 'params' equal to 0 and set in bypass
1045 	 */
1046 	if (mpc->funcs->set_output_gamma)
1047 		mpc->funcs->set_output_gamma(mpc, mpcc_id, params);
1048 
1049 	return true;
1050 }
1051 
dcn20_set_blend_lut(struct pipe_ctx * pipe_ctx,const struct dc_plane_state * plane_state)1052 bool dcn20_set_blend_lut(
1053 	struct pipe_ctx *pipe_ctx, const struct dc_plane_state *plane_state)
1054 {
1055 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
1056 	bool result = true;
1057 	const struct pwl_params *blend_lut = NULL;
1058 
1059 	if (plane_state->blend_tf.type == TF_TYPE_HWPWL)
1060 		blend_lut = &plane_state->blend_tf.pwl;
1061 	else if (plane_state->blend_tf.type == TF_TYPE_DISTRIBUTED_POINTS) {
1062 		cm_helper_translate_curve_to_hw_format(plane_state->ctx,
1063 				&plane_state->blend_tf,
1064 				&dpp_base->regamma_params, false);
1065 		blend_lut = &dpp_base->regamma_params;
1066 	}
1067 	result = dpp_base->funcs->dpp_program_blnd_lut(dpp_base, blend_lut);
1068 
1069 	return result;
1070 }
1071 
dcn20_set_shaper_3dlut(struct pipe_ctx * pipe_ctx,const struct dc_plane_state * plane_state)1072 bool dcn20_set_shaper_3dlut(
1073 	struct pipe_ctx *pipe_ctx, const struct dc_plane_state *plane_state)
1074 {
1075 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
1076 	bool result = true;
1077 	const struct pwl_params *shaper_lut = NULL;
1078 
1079 	if (plane_state->in_shaper_func.type == TF_TYPE_HWPWL)
1080 		shaper_lut = &plane_state->in_shaper_func.pwl;
1081 	else if (plane_state->in_shaper_func.type == TF_TYPE_DISTRIBUTED_POINTS) {
1082 		cm_helper_translate_curve_to_hw_format(plane_state->ctx,
1083 				&plane_state->in_shaper_func,
1084 				&dpp_base->shaper_params, true);
1085 		shaper_lut = &dpp_base->shaper_params;
1086 	}
1087 
1088 	result = dpp_base->funcs->dpp_program_shaper_lut(dpp_base, shaper_lut);
1089 	if (plane_state->lut3d_func.state.bits.initialized == 1)
1090 		result = dpp_base->funcs->dpp_program_3dlut(dpp_base,
1091 								&plane_state->lut3d_func.lut_3d);
1092 	else
1093 		result = dpp_base->funcs->dpp_program_3dlut(dpp_base, NULL);
1094 
1095 	return result;
1096 }
1097 
dcn20_set_input_transfer_func(struct dc * dc,struct pipe_ctx * pipe_ctx,const struct dc_plane_state * plane_state)1098 bool dcn20_set_input_transfer_func(struct dc *dc,
1099 				struct pipe_ctx *pipe_ctx,
1100 				const struct dc_plane_state *plane_state)
1101 {
1102 	struct dce_hwseq *hws = dc->hwseq;
1103 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
1104 	const struct dc_transfer_func *tf = NULL;
1105 	bool result = true;
1106 	bool use_degamma_ram = false;
1107 
1108 	if (dpp_base == NULL || plane_state == NULL)
1109 		return false;
1110 
1111 	hws->funcs.set_shaper_3dlut(pipe_ctx, plane_state);
1112 	hws->funcs.set_blend_lut(pipe_ctx, plane_state);
1113 
1114 	tf = &plane_state->in_transfer_func;
1115 
1116 	if (tf->type == TF_TYPE_HWPWL || tf->type == TF_TYPE_DISTRIBUTED_POINTS)
1117 		use_degamma_ram = true;
1118 
1119 	if (use_degamma_ram == true) {
1120 		if (tf->type == TF_TYPE_HWPWL)
1121 			dpp_base->funcs->dpp_program_degamma_pwl(dpp_base,
1122 					&tf->pwl);
1123 		else if (tf->type == TF_TYPE_DISTRIBUTED_POINTS) {
1124 			cm_helper_translate_curve_to_degamma_hw_format(tf,
1125 					&dpp_base->degamma_params);
1126 			dpp_base->funcs->dpp_program_degamma_pwl(dpp_base,
1127 				&dpp_base->degamma_params);
1128 		}
1129 		return true;
1130 	}
1131 	/* handle here the optimized cases when de-gamma ROM could be used.
1132 	 *
1133 	 */
1134 	if (tf->type == TF_TYPE_PREDEFINED) {
1135 		switch (tf->tf) {
1136 		case TRANSFER_FUNCTION_SRGB:
1137 			dpp_base->funcs->dpp_set_degamma(dpp_base,
1138 					IPP_DEGAMMA_MODE_HW_sRGB);
1139 			break;
1140 		case TRANSFER_FUNCTION_BT709:
1141 			dpp_base->funcs->dpp_set_degamma(dpp_base,
1142 					IPP_DEGAMMA_MODE_HW_xvYCC);
1143 			break;
1144 		case TRANSFER_FUNCTION_LINEAR:
1145 			dpp_base->funcs->dpp_set_degamma(dpp_base,
1146 					IPP_DEGAMMA_MODE_BYPASS);
1147 			break;
1148 		case TRANSFER_FUNCTION_PQ:
1149 			dpp_base->funcs->dpp_set_degamma(dpp_base, IPP_DEGAMMA_MODE_USER_PWL);
1150 			cm_helper_translate_curve_to_degamma_hw_format(tf, &dpp_base->degamma_params);
1151 			dpp_base->funcs->dpp_program_degamma_pwl(dpp_base, &dpp_base->degamma_params);
1152 			result = true;
1153 			break;
1154 		default:
1155 			result = false;
1156 			break;
1157 		}
1158 	} else if (tf->type == TF_TYPE_BYPASS)
1159 		dpp_base->funcs->dpp_set_degamma(dpp_base,
1160 				IPP_DEGAMMA_MODE_BYPASS);
1161 	else {
1162 		/*
1163 		 * if we are here, we did not handle correctly.
1164 		 * fix is required for this use case
1165 		 */
1166 		BREAK_TO_DEBUGGER();
1167 		dpp_base->funcs->dpp_set_degamma(dpp_base,
1168 				IPP_DEGAMMA_MODE_BYPASS);
1169 	}
1170 
1171 	return result;
1172 }
1173 
dcn20_update_odm(struct dc * dc,struct dc_state * context,struct pipe_ctx * pipe_ctx)1174 void dcn20_update_odm(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe_ctx)
1175 {
1176 	struct pipe_ctx *odm_pipe;
1177 	int opp_cnt = 1;
1178 	int opp_inst[MAX_PIPES] = { pipe_ctx->stream_res.opp->inst };
1179 	int odm_slice_width = resource_get_odm_slice_dst_width(pipe_ctx, false);
1180 	int last_odm_slice_width = resource_get_odm_slice_dst_width(pipe_ctx, true);
1181 
1182 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1183 		opp_inst[opp_cnt] = odm_pipe->stream_res.opp->inst;
1184 		opp_cnt++;
1185 	}
1186 
1187 	if (opp_cnt > 1)
1188 		pipe_ctx->stream_res.tg->funcs->set_odm_combine(
1189 				pipe_ctx->stream_res.tg,
1190 				opp_inst, opp_cnt,
1191 				odm_slice_width, last_odm_slice_width);
1192 	else
1193 		pipe_ctx->stream_res.tg->funcs->set_odm_bypass(
1194 				pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1195 }
1196 
dcn20_blank_pixel_data(struct dc * dc,struct pipe_ctx * pipe_ctx,bool blank)1197 void dcn20_blank_pixel_data(
1198 		struct dc *dc,
1199 		struct pipe_ctx *pipe_ctx,
1200 		bool blank)
1201 {
1202 	struct tg_color black_color = {0};
1203 	struct stream_resource *stream_res = &pipe_ctx->stream_res;
1204 	struct dc_stream_state *stream = pipe_ctx->stream;
1205 	enum dc_color_space color_space = stream->output_color_space;
1206 	enum controller_dp_test_pattern test_pattern = CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR;
1207 	enum controller_dp_color_space test_pattern_color_space = CONTROLLER_DP_COLOR_SPACE_UDEFINED;
1208 	struct pipe_ctx *odm_pipe;
1209 	struct rect odm_slice_src;
1210 
1211 	if (stream->link->test_pattern_enabled)
1212 		return;
1213 
1214 	/* get opp dpg blank color */
1215 	color_space_to_black_color(dc, color_space, &black_color);
1216 
1217 	if (blank) {
1218 		dc->hwss.set_abm_immediate_disable(pipe_ctx);
1219 
1220 		if (dc->debug.visual_confirm != VISUAL_CONFIRM_DISABLE) {
1221 			test_pattern = CONTROLLER_DP_TEST_PATTERN_COLORSQUARES;
1222 			test_pattern_color_space = CONTROLLER_DP_COLOR_SPACE_RGB;
1223 		}
1224 	} else {
1225 		test_pattern = CONTROLLER_DP_TEST_PATTERN_VIDEOMODE;
1226 	}
1227 
1228 	odm_pipe = pipe_ctx;
1229 
1230 	while (odm_pipe->next_odm_pipe) {
1231 		odm_slice_src = resource_get_odm_slice_src_rect(odm_pipe);
1232 		dc->hwss.set_disp_pattern_generator(dc,
1233 				odm_pipe,
1234 				test_pattern,
1235 				test_pattern_color_space,
1236 				stream->timing.display_color_depth,
1237 				&black_color,
1238 				odm_slice_src.width,
1239 				odm_slice_src.height,
1240 				odm_slice_src.x);
1241 		odm_pipe = odm_pipe->next_odm_pipe;
1242 	}
1243 
1244 	odm_slice_src = resource_get_odm_slice_src_rect(odm_pipe);
1245 	dc->hwss.set_disp_pattern_generator(dc,
1246 			odm_pipe,
1247 			test_pattern,
1248 			test_pattern_color_space,
1249 			stream->timing.display_color_depth,
1250 			&black_color,
1251 			odm_slice_src.width,
1252 			odm_slice_src.height,
1253 			odm_slice_src.x);
1254 
1255 	if (!blank)
1256 		if (stream_res->abm) {
1257 			dc->hwss.set_pipe(pipe_ctx);
1258 			stream_res->abm->funcs->set_abm_level(stream_res->abm, stream->abm_level);
1259 		}
1260 }
1261 
1262 
dcn20_power_on_plane_resources(struct dce_hwseq * hws,struct pipe_ctx * pipe_ctx)1263 static void dcn20_power_on_plane_resources(
1264 	struct dce_hwseq *hws,
1265 	struct pipe_ctx *pipe_ctx)
1266 {
1267 	uint32_t org_ip_request_cntl = 0;
1268 
1269 	DC_LOGGER_INIT(hws->ctx->logger);
1270 
1271 	if (hws->funcs.dpp_root_clock_control)
1272 		hws->funcs.dpp_root_clock_control(hws, pipe_ctx->plane_res.dpp->inst, true);
1273 
1274 	if (REG(DC_IP_REQUEST_CNTL)) {
1275 		REG_GET(DC_IP_REQUEST_CNTL, IP_REQUEST_EN, &org_ip_request_cntl);
1276 		if (org_ip_request_cntl == 0)
1277 			REG_SET(DC_IP_REQUEST_CNTL, 0,
1278 					IP_REQUEST_EN, 1);
1279 
1280 		if (hws->funcs.dpp_pg_control)
1281 			hws->funcs.dpp_pg_control(hws, pipe_ctx->plane_res.dpp->inst, true);
1282 
1283 		if (hws->funcs.hubp_pg_control)
1284 			hws->funcs.hubp_pg_control(hws, pipe_ctx->plane_res.hubp->inst, true);
1285 
1286 		if (org_ip_request_cntl == 0)
1287 			REG_SET(DC_IP_REQUEST_CNTL, 0,
1288 					IP_REQUEST_EN, 0);
1289 
1290 		DC_LOG_DEBUG(
1291 				"Un-gated front end for pipe %d\n", pipe_ctx->plane_res.hubp->inst);
1292 	}
1293 }
1294 
dcn20_enable_plane(struct dc * dc,struct pipe_ctx * pipe_ctx,struct dc_state * context)1295 void dcn20_enable_plane(struct dc *dc, struct pipe_ctx *pipe_ctx,
1296 			       struct dc_state *context)
1297 {
1298 	//if (dc->debug.sanity_checks) {
1299 	//	dcn10_verify_allow_pstate_change_high(dc);
1300 	//}
1301 	dcn20_power_on_plane_resources(dc->hwseq, pipe_ctx);
1302 
1303 	/* enable DCFCLK current DCHUB */
1304 	pipe_ctx->plane_res.hubp->funcs->hubp_clk_cntl(pipe_ctx->plane_res.hubp, true);
1305 
1306 	/* initialize HUBP on power up */
1307 	pipe_ctx->plane_res.hubp->funcs->hubp_init(pipe_ctx->plane_res.hubp);
1308 
1309 	/* make sure OPP_PIPE_CLOCK_EN = 1 */
1310 	pipe_ctx->stream_res.opp->funcs->opp_pipe_clock_control(
1311 			pipe_ctx->stream_res.opp,
1312 			true);
1313 
1314 /* TODO: enable/disable in dm as per update type.
1315 	if (plane_state) {
1316 		DC_LOG_DC(dc->ctx->logger,
1317 				"Pipe:%d 0x%x: addr hi:0x%x, "
1318 				"addr low:0x%x, "
1319 				"src: %d, %d, %d,"
1320 				" %d; dst: %d, %d, %d, %d;\n",
1321 				pipe_ctx->pipe_idx,
1322 				plane_state,
1323 				plane_state->address.grph.addr.high_part,
1324 				plane_state->address.grph.addr.low_part,
1325 				plane_state->src_rect.x,
1326 				plane_state->src_rect.y,
1327 				plane_state->src_rect.width,
1328 				plane_state->src_rect.height,
1329 				plane_state->dst_rect.x,
1330 				plane_state->dst_rect.y,
1331 				plane_state->dst_rect.width,
1332 				plane_state->dst_rect.height);
1333 
1334 		DC_LOG_DC(dc->ctx->logger,
1335 				"Pipe %d: width, height, x, y         format:%d\n"
1336 				"viewport:%d, %d, %d, %d\n"
1337 				"recout:  %d, %d, %d, %d\n",
1338 				pipe_ctx->pipe_idx,
1339 				plane_state->format,
1340 				pipe_ctx->plane_res.scl_data.viewport.width,
1341 				pipe_ctx->plane_res.scl_data.viewport.height,
1342 				pipe_ctx->plane_res.scl_data.viewport.x,
1343 				pipe_ctx->plane_res.scl_data.viewport.y,
1344 				pipe_ctx->plane_res.scl_data.recout.width,
1345 				pipe_ctx->plane_res.scl_data.recout.height,
1346 				pipe_ctx->plane_res.scl_data.recout.x,
1347 				pipe_ctx->plane_res.scl_data.recout.y);
1348 		print_rq_dlg_ttu(dc, pipe_ctx);
1349 	}
1350 */
1351 	if (dc->vm_pa_config.valid) {
1352 		struct vm_system_aperture_param apt;
1353 
1354 		apt.sys_default.quad_part = 0;
1355 
1356 		apt.sys_low.quad_part = dc->vm_pa_config.system_aperture.start_addr;
1357 		apt.sys_high.quad_part = dc->vm_pa_config.system_aperture.end_addr;
1358 
1359 		// Program system aperture settings
1360 		pipe_ctx->plane_res.hubp->funcs->hubp_set_vm_system_aperture_settings(pipe_ctx->plane_res.hubp, &apt);
1361 	}
1362 
1363 	if (!pipe_ctx->top_pipe
1364 		&& pipe_ctx->plane_state
1365 		&& pipe_ctx->plane_state->flip_int_enabled
1366 		&& pipe_ctx->plane_res.hubp->funcs->hubp_set_flip_int)
1367 			pipe_ctx->plane_res.hubp->funcs->hubp_set_flip_int(pipe_ctx->plane_res.hubp);
1368 
1369 //	if (dc->debug.sanity_checks) {
1370 //		dcn10_verify_allow_pstate_change_high(dc);
1371 //	}
1372 }
1373 
dcn20_pipe_control_lock(struct dc * dc,struct pipe_ctx * pipe,bool lock)1374 void dcn20_pipe_control_lock(
1375 	struct dc *dc,
1376 	struct pipe_ctx *pipe,
1377 	bool lock)
1378 {
1379 	struct pipe_ctx *temp_pipe;
1380 	bool flip_immediate = false;
1381 
1382 	/* use TG master update lock to lock everything on the TG
1383 	 * therefore only top pipe need to lock
1384 	 */
1385 	if (!pipe || pipe->top_pipe)
1386 		return;
1387 
1388 	if (pipe->plane_state != NULL)
1389 		flip_immediate = pipe->plane_state->flip_immediate;
1390 
1391 	if  (pipe->stream_res.gsl_group > 0) {
1392 	    temp_pipe = pipe->bottom_pipe;
1393 	    while (!flip_immediate && temp_pipe) {
1394 		    if (temp_pipe->plane_state != NULL)
1395 			    flip_immediate = temp_pipe->plane_state->flip_immediate;
1396 		    temp_pipe = temp_pipe->bottom_pipe;
1397 	    }
1398 	}
1399 
1400 	if (flip_immediate && lock) {
1401 		const int TIMEOUT_FOR_FLIP_PENDING_US = 100000;
1402 		unsigned int polling_interval_us = 1;
1403 		int i;
1404 
1405 		temp_pipe = pipe;
1406 		while (temp_pipe) {
1407 			if (temp_pipe->plane_state && temp_pipe->plane_state->flip_immediate) {
1408 				for (i = 0; i < TIMEOUT_FOR_FLIP_PENDING_US / polling_interval_us; ++i) {
1409 					if (!temp_pipe->plane_res.hubp->funcs->hubp_is_flip_pending(temp_pipe->plane_res.hubp))
1410 						break;
1411 					udelay(polling_interval_us);
1412 				}
1413 
1414 				/* no reason it should take this long for immediate flips */
1415 				ASSERT(i != TIMEOUT_FOR_FLIP_PENDING_US);
1416 			}
1417 			temp_pipe = temp_pipe->bottom_pipe;
1418 		}
1419 	}
1420 
1421 	/* In flip immediate and pipe splitting case, we need to use GSL
1422 	 * for synchronization. Only do setup on locking and on flip type change.
1423 	 */
1424 	if (lock && (pipe->bottom_pipe != NULL || !flip_immediate))
1425 		if ((flip_immediate && pipe->stream_res.gsl_group == 0) ||
1426 		    (!flip_immediate && pipe->stream_res.gsl_group > 0))
1427 			dcn20_setup_gsl_group_as_lock(dc, pipe, flip_immediate);
1428 
1429 	if (pipe->plane_state != NULL)
1430 		flip_immediate = pipe->plane_state->flip_immediate;
1431 
1432 	temp_pipe = pipe->bottom_pipe;
1433 	while (flip_immediate && temp_pipe) {
1434 	    if (temp_pipe->plane_state != NULL)
1435 		flip_immediate = temp_pipe->plane_state->flip_immediate;
1436 	    temp_pipe = temp_pipe->bottom_pipe;
1437 	}
1438 
1439 	if (!lock && pipe->stream_res.gsl_group > 0 && pipe->plane_state &&
1440 		!flip_immediate)
1441 	    dcn20_setup_gsl_group_as_lock(dc, pipe, false);
1442 
1443 	if (pipe->stream && should_use_dmub_lock(pipe->stream->link)) {
1444 		union dmub_hw_lock_flags hw_locks = { 0 };
1445 		struct dmub_hw_lock_inst_flags inst_flags = { 0 };
1446 
1447 		hw_locks.bits.lock_pipe = 1;
1448 		inst_flags.otg_inst =  pipe->stream_res.tg->inst;
1449 
1450 		if (pipe->plane_state != NULL)
1451 			hw_locks.bits.triple_buffer_lock = pipe->plane_state->triplebuffer_flips;
1452 
1453 		dmub_hw_lock_mgr_cmd(dc->ctx->dmub_srv,
1454 					lock,
1455 					&hw_locks,
1456 					&inst_flags);
1457 	} else if (pipe->plane_state != NULL && pipe->plane_state->triplebuffer_flips) {
1458 		if (lock)
1459 			pipe->stream_res.tg->funcs->triplebuffer_lock(pipe->stream_res.tg);
1460 		else
1461 			pipe->stream_res.tg->funcs->triplebuffer_unlock(pipe->stream_res.tg);
1462 	} else {
1463 		if (lock)
1464 			pipe->stream_res.tg->funcs->lock(pipe->stream_res.tg);
1465 		else {
1466 			if (dc->hwseq->funcs.perform_3dlut_wa_unlock)
1467 				dc->hwseq->funcs.perform_3dlut_wa_unlock(pipe);
1468 			else
1469 				pipe->stream_res.tg->funcs->unlock(pipe->stream_res.tg);
1470 		}
1471 	}
1472 }
1473 
dcn20_detect_pipe_changes(struct dc_state * old_state,struct dc_state * new_state,struct pipe_ctx * old_pipe,struct pipe_ctx * new_pipe)1474 void dcn20_detect_pipe_changes(struct dc_state *old_state,
1475 		struct dc_state *new_state,
1476 		struct pipe_ctx *old_pipe,
1477 		struct pipe_ctx *new_pipe)
1478 {
1479 	bool old_is_phantom = dc_state_get_pipe_subvp_type(old_state, old_pipe) == SUBVP_PHANTOM;
1480 	bool new_is_phantom = dc_state_get_pipe_subvp_type(new_state, new_pipe) == SUBVP_PHANTOM;
1481 
1482 	new_pipe->update_flags.raw = 0;
1483 
1484 	/* If non-phantom pipe is being transitioned to a phantom pipe,
1485 	 * set disable and return immediately. This is because the pipe
1486 	 * that was previously in use must be fully disabled before we
1487 	 * can "enable" it as a phantom pipe (since the OTG will certainly
1488 	 * be different). The post_unlock sequence will set the correct
1489 	 * update flags to enable the phantom pipe.
1490 	 */
1491 	if (old_pipe->plane_state && !old_is_phantom &&
1492 			new_pipe->plane_state && new_is_phantom) {
1493 		new_pipe->update_flags.bits.disable = 1;
1494 		return;
1495 	}
1496 
1497 	if (resource_is_pipe_type(new_pipe, OTG_MASTER) &&
1498 			resource_is_odm_topology_changed(new_pipe, old_pipe))
1499 		/* Detect odm changes */
1500 		new_pipe->update_flags.bits.odm = 1;
1501 
1502 	/* Exit on unchanged, unused pipe */
1503 	if (!old_pipe->plane_state && !new_pipe->plane_state)
1504 		return;
1505 	/* Detect pipe enable/disable */
1506 	if (!old_pipe->plane_state && new_pipe->plane_state) {
1507 		new_pipe->update_flags.bits.enable = 1;
1508 		new_pipe->update_flags.bits.mpcc = 1;
1509 		new_pipe->update_flags.bits.dppclk = 1;
1510 		new_pipe->update_flags.bits.hubp_interdependent = 1;
1511 		new_pipe->update_flags.bits.hubp_rq_dlg_ttu = 1;
1512 		new_pipe->update_flags.bits.unbounded_req = 1;
1513 		new_pipe->update_flags.bits.gamut_remap = 1;
1514 		new_pipe->update_flags.bits.scaler = 1;
1515 		new_pipe->update_flags.bits.viewport = 1;
1516 		new_pipe->update_flags.bits.det_size = 1;
1517 		if (new_pipe->stream->test_pattern.type != DP_TEST_PATTERN_VIDEO_MODE &&
1518 				new_pipe->stream_res.test_pattern_params.width != 0 &&
1519 				new_pipe->stream_res.test_pattern_params.height != 0)
1520 			new_pipe->update_flags.bits.test_pattern_changed = 1;
1521 		if (!new_pipe->top_pipe && !new_pipe->prev_odm_pipe) {
1522 			new_pipe->update_flags.bits.odm = 1;
1523 			new_pipe->update_flags.bits.global_sync = 1;
1524 		}
1525 		return;
1526 	}
1527 
1528 	/* For SubVP we need to unconditionally enable because any phantom pipes are
1529 	 * always removed then newly added for every full updates whenever SubVP is in use.
1530 	 * The remove-add sequence of the phantom pipe always results in the pipe
1531 	 * being blanked in enable_stream_timing (DPG).
1532 	 */
1533 	if (new_pipe->stream && dc_state_get_pipe_subvp_type(new_state, new_pipe) == SUBVP_PHANTOM)
1534 		new_pipe->update_flags.bits.enable = 1;
1535 
1536 	/* Phantom pipes are effectively disabled, if the pipe was previously phantom
1537 	 * we have to enable
1538 	 */
1539 	if (old_pipe->plane_state && old_is_phantom &&
1540 			new_pipe->plane_state && !new_is_phantom)
1541 		new_pipe->update_flags.bits.enable = 1;
1542 
1543 	if (old_pipe->plane_state && !new_pipe->plane_state) {
1544 		new_pipe->update_flags.bits.disable = 1;
1545 		return;
1546 	}
1547 
1548 	/* Detect plane change */
1549 	if (old_pipe->plane_state != new_pipe->plane_state) {
1550 		new_pipe->update_flags.bits.plane_changed = true;
1551 	}
1552 
1553 	/* Detect top pipe only changes */
1554 	if (resource_is_pipe_type(new_pipe, OTG_MASTER)) {
1555 		/* Detect global sync changes */
1556 		if (old_pipe->pipe_dlg_param.vready_offset != new_pipe->pipe_dlg_param.vready_offset
1557 				|| old_pipe->pipe_dlg_param.vstartup_start != new_pipe->pipe_dlg_param.vstartup_start
1558 				|| old_pipe->pipe_dlg_param.vupdate_offset != new_pipe->pipe_dlg_param.vupdate_offset
1559 				|| old_pipe->pipe_dlg_param.vupdate_width != new_pipe->pipe_dlg_param.vupdate_width)
1560 			new_pipe->update_flags.bits.global_sync = 1;
1561 	}
1562 
1563 	if (old_pipe->det_buffer_size_kb != new_pipe->det_buffer_size_kb)
1564 		new_pipe->update_flags.bits.det_size = 1;
1565 
1566 	/*
1567 	 * Detect opp / tg change, only set on change, not on enable
1568 	 * Assume mpcc inst = pipe index, if not this code needs to be updated
1569 	 * since mpcc is what is affected by these. In fact all of our sequence
1570 	 * makes this assumption at the moment with how hubp reset is matched to
1571 	 * same index mpcc reset.
1572 	 */
1573 	if (old_pipe->stream_res.opp != new_pipe->stream_res.opp)
1574 		new_pipe->update_flags.bits.opp_changed = 1;
1575 	if (old_pipe->stream_res.tg != new_pipe->stream_res.tg)
1576 		new_pipe->update_flags.bits.tg_changed = 1;
1577 
1578 	/*
1579 	 * Detect mpcc blending changes, only dpp inst and opp matter here,
1580 	 * mpccs getting removed/inserted update connected ones during their own
1581 	 * programming
1582 	 */
1583 	if (old_pipe->plane_res.dpp != new_pipe->plane_res.dpp
1584 			|| old_pipe->stream_res.opp != new_pipe->stream_res.opp)
1585 		new_pipe->update_flags.bits.mpcc = 1;
1586 
1587 	/* Detect dppclk change */
1588 	if (old_pipe->plane_res.bw.dppclk_khz != new_pipe->plane_res.bw.dppclk_khz)
1589 		new_pipe->update_flags.bits.dppclk = 1;
1590 
1591 	/* Check for scl update */
1592 	if (memcmp(&old_pipe->plane_res.scl_data, &new_pipe->plane_res.scl_data, sizeof(struct scaler_data)))
1593 			new_pipe->update_flags.bits.scaler = 1;
1594 	/* Check for vp update */
1595 	if (memcmp(&old_pipe->plane_res.scl_data.viewport, &new_pipe->plane_res.scl_data.viewport, sizeof(struct rect))
1596 			|| memcmp(&old_pipe->plane_res.scl_data.viewport_c,
1597 				&new_pipe->plane_res.scl_data.viewport_c, sizeof(struct rect)))
1598 		new_pipe->update_flags.bits.viewport = 1;
1599 
1600 	/* Detect dlg/ttu/rq updates */
1601 	{
1602 		struct _vcs_dpi_display_dlg_regs_st old_dlg_attr = old_pipe->dlg_regs;
1603 		struct _vcs_dpi_display_ttu_regs_st old_ttu_attr = old_pipe->ttu_regs;
1604 		struct _vcs_dpi_display_dlg_regs_st *new_dlg_attr = &new_pipe->dlg_regs;
1605 		struct _vcs_dpi_display_ttu_regs_st *new_ttu_attr = &new_pipe->ttu_regs;
1606 
1607 		/* Detect pipe interdependent updates */
1608 		if (old_dlg_attr.dst_y_prefetch != new_dlg_attr->dst_y_prefetch ||
1609 				old_dlg_attr.vratio_prefetch != new_dlg_attr->vratio_prefetch ||
1610 				old_dlg_attr.vratio_prefetch_c != new_dlg_attr->vratio_prefetch_c ||
1611 				old_dlg_attr.dst_y_per_vm_vblank != new_dlg_attr->dst_y_per_vm_vblank ||
1612 				old_dlg_attr.dst_y_per_row_vblank != new_dlg_attr->dst_y_per_row_vblank ||
1613 				old_dlg_attr.dst_y_per_vm_flip != new_dlg_attr->dst_y_per_vm_flip ||
1614 				old_dlg_attr.dst_y_per_row_flip != new_dlg_attr->dst_y_per_row_flip ||
1615 				old_dlg_attr.refcyc_per_meta_chunk_vblank_l != new_dlg_attr->refcyc_per_meta_chunk_vblank_l ||
1616 				old_dlg_attr.refcyc_per_meta_chunk_vblank_c != new_dlg_attr->refcyc_per_meta_chunk_vblank_c ||
1617 				old_dlg_attr.refcyc_per_meta_chunk_flip_l != new_dlg_attr->refcyc_per_meta_chunk_flip_l ||
1618 				old_dlg_attr.refcyc_per_line_delivery_pre_l != new_dlg_attr->refcyc_per_line_delivery_pre_l ||
1619 				old_dlg_attr.refcyc_per_line_delivery_pre_c != new_dlg_attr->refcyc_per_line_delivery_pre_c ||
1620 				old_ttu_attr.refcyc_per_req_delivery_pre_l != new_ttu_attr->refcyc_per_req_delivery_pre_l ||
1621 				old_ttu_attr.refcyc_per_req_delivery_pre_c != new_ttu_attr->refcyc_per_req_delivery_pre_c ||
1622 				old_ttu_attr.refcyc_per_req_delivery_pre_cur0 != new_ttu_attr->refcyc_per_req_delivery_pre_cur0 ||
1623 				old_ttu_attr.refcyc_per_req_delivery_pre_cur1 != new_ttu_attr->refcyc_per_req_delivery_pre_cur1 ||
1624 				old_ttu_attr.min_ttu_vblank != new_ttu_attr->min_ttu_vblank ||
1625 				old_ttu_attr.qos_level_flip != new_ttu_attr->qos_level_flip) {
1626 			old_dlg_attr.dst_y_prefetch = new_dlg_attr->dst_y_prefetch;
1627 			old_dlg_attr.vratio_prefetch = new_dlg_attr->vratio_prefetch;
1628 			old_dlg_attr.vratio_prefetch_c = new_dlg_attr->vratio_prefetch_c;
1629 			old_dlg_attr.dst_y_per_vm_vblank = new_dlg_attr->dst_y_per_vm_vblank;
1630 			old_dlg_attr.dst_y_per_row_vblank = new_dlg_attr->dst_y_per_row_vblank;
1631 			old_dlg_attr.dst_y_per_vm_flip = new_dlg_attr->dst_y_per_vm_flip;
1632 			old_dlg_attr.dst_y_per_row_flip = new_dlg_attr->dst_y_per_row_flip;
1633 			old_dlg_attr.refcyc_per_meta_chunk_vblank_l = new_dlg_attr->refcyc_per_meta_chunk_vblank_l;
1634 			old_dlg_attr.refcyc_per_meta_chunk_vblank_c = new_dlg_attr->refcyc_per_meta_chunk_vblank_c;
1635 			old_dlg_attr.refcyc_per_meta_chunk_flip_l = new_dlg_attr->refcyc_per_meta_chunk_flip_l;
1636 			old_dlg_attr.refcyc_per_line_delivery_pre_l = new_dlg_attr->refcyc_per_line_delivery_pre_l;
1637 			old_dlg_attr.refcyc_per_line_delivery_pre_c = new_dlg_attr->refcyc_per_line_delivery_pre_c;
1638 			old_ttu_attr.refcyc_per_req_delivery_pre_l = new_ttu_attr->refcyc_per_req_delivery_pre_l;
1639 			old_ttu_attr.refcyc_per_req_delivery_pre_c = new_ttu_attr->refcyc_per_req_delivery_pre_c;
1640 			old_ttu_attr.refcyc_per_req_delivery_pre_cur0 = new_ttu_attr->refcyc_per_req_delivery_pre_cur0;
1641 			old_ttu_attr.refcyc_per_req_delivery_pre_cur1 = new_ttu_attr->refcyc_per_req_delivery_pre_cur1;
1642 			old_ttu_attr.min_ttu_vblank = new_ttu_attr->min_ttu_vblank;
1643 			old_ttu_attr.qos_level_flip = new_ttu_attr->qos_level_flip;
1644 			new_pipe->update_flags.bits.hubp_interdependent = 1;
1645 		}
1646 		/* Detect any other updates to ttu/rq/dlg */
1647 		if (memcmp(&old_dlg_attr, &new_pipe->dlg_regs, sizeof(old_dlg_attr)) ||
1648 				memcmp(&old_ttu_attr, &new_pipe->ttu_regs, sizeof(old_ttu_attr)) ||
1649 				memcmp(&old_pipe->rq_regs, &new_pipe->rq_regs, sizeof(old_pipe->rq_regs)))
1650 			new_pipe->update_flags.bits.hubp_rq_dlg_ttu = 1;
1651 	}
1652 
1653 	if (old_pipe->unbounded_req != new_pipe->unbounded_req)
1654 		new_pipe->update_flags.bits.unbounded_req = 1;
1655 
1656 	if (memcmp(&old_pipe->stream_res.test_pattern_params,
1657 				&new_pipe->stream_res.test_pattern_params, sizeof(struct test_pattern_params))) {
1658 		new_pipe->update_flags.bits.test_pattern_changed = 1;
1659 	}
1660 }
1661 
dcn20_update_dchubp_dpp(struct dc * dc,struct pipe_ctx * pipe_ctx,struct dc_state * context)1662 void dcn20_update_dchubp_dpp(
1663 	struct dc *dc,
1664 	struct pipe_ctx *pipe_ctx,
1665 	struct dc_state *context)
1666 {
1667 	struct dce_hwseq *hws = dc->hwseq;
1668 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
1669 	struct dpp *dpp = pipe_ctx->plane_res.dpp;
1670 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1671 	struct dccg *dccg = dc->res_pool->dccg;
1672 	bool viewport_changed = false;
1673 	enum mall_stream_type pipe_mall_type = dc_state_get_pipe_subvp_type(context, pipe_ctx);
1674 
1675 	if (pipe_ctx->update_flags.bits.dppclk)
1676 		dpp->funcs->dpp_dppclk_control(dpp, false, true);
1677 
1678 	if (pipe_ctx->update_flags.bits.enable)
1679 		dccg->funcs->update_dpp_dto(dccg, dpp->inst, pipe_ctx->plane_res.bw.dppclk_khz);
1680 
1681 	/* TODO: Need input parameter to tell current DCHUB pipe tie to which OTG
1682 	 * VTG is within DCHUBBUB which is commond block share by each pipe HUBP.
1683 	 * VTG is 1:1 mapping with OTG. Each pipe HUBP will select which VTG
1684 	 */
1685 
1686 	if (pipe_ctx->update_flags.bits.hubp_rq_dlg_ttu) {
1687 		hubp->funcs->hubp_vtg_sel(hubp, pipe_ctx->stream_res.tg->inst);
1688 
1689 		if (hubp->funcs->hubp_setup2) {
1690 			hubp->funcs->hubp_setup2(
1691 				hubp,
1692 				&pipe_ctx->hubp_regs,
1693 				&pipe_ctx->global_sync,
1694 				&pipe_ctx->stream->timing);
1695 		} else {
1696 			hubp->funcs->hubp_setup(
1697 				hubp,
1698 				&pipe_ctx->dlg_regs,
1699 				&pipe_ctx->ttu_regs,
1700 				&pipe_ctx->rq_regs,
1701 				&pipe_ctx->pipe_dlg_param);
1702 		}
1703 	}
1704 
1705 	if (pipe_ctx->update_flags.bits.unbounded_req && hubp->funcs->set_unbounded_requesting)
1706 		hubp->funcs->set_unbounded_requesting(hubp, pipe_ctx->unbounded_req);
1707 
1708 	if (pipe_ctx->update_flags.bits.hubp_interdependent) {
1709 		if (hubp->funcs->hubp_setup_interdependent2) {
1710 			hubp->funcs->hubp_setup_interdependent2(
1711 				hubp,
1712 				&pipe_ctx->hubp_regs);
1713 		} else {
1714 			hubp->funcs->hubp_setup_interdependent(
1715 				hubp,
1716 				&pipe_ctx->dlg_regs,
1717 				&pipe_ctx->ttu_regs);
1718 		}
1719 	}
1720 
1721 	if (pipe_ctx->update_flags.bits.enable ||
1722 			pipe_ctx->update_flags.bits.plane_changed ||
1723 			plane_state->update_flags.bits.bpp_change ||
1724 			plane_state->update_flags.bits.input_csc_change ||
1725 			plane_state->update_flags.bits.color_space_change ||
1726 			plane_state->update_flags.bits.coeff_reduction_change) {
1727 		struct dc_bias_and_scale bns_params = plane_state->bias_and_scale;
1728 
1729 		// program the input csc
1730 		dpp->funcs->dpp_setup(dpp,
1731 				plane_state->format,
1732 				EXPANSION_MODE_ZERO,
1733 				plane_state->input_csc_color_matrix,
1734 				plane_state->color_space,
1735 				NULL);
1736 
1737 		if (dpp->funcs->set_cursor_matrix) {
1738 			dpp->funcs->set_cursor_matrix(dpp,
1739 				plane_state->color_space,
1740 				plane_state->cursor_csc_color_matrix);
1741 		}
1742 		if (dpp->funcs->dpp_program_bias_and_scale) {
1743 			//TODO :for CNVC set scale and bias registers if necessary
1744 			dpp->funcs->dpp_program_bias_and_scale(dpp, &bns_params);
1745 		}
1746 	}
1747 
1748 	if (pipe_ctx->update_flags.bits.mpcc
1749 			|| pipe_ctx->update_flags.bits.plane_changed
1750 			|| plane_state->update_flags.bits.global_alpha_change
1751 			|| plane_state->update_flags.bits.per_pixel_alpha_change) {
1752 		// MPCC inst is equal to pipe index in practice
1753 		hws->funcs.update_mpcc(dc, pipe_ctx);
1754 	}
1755 
1756 	if (pipe_ctx->update_flags.bits.scaler ||
1757 			plane_state->update_flags.bits.scaling_change ||
1758 			plane_state->update_flags.bits.position_change ||
1759 			plane_state->update_flags.bits.per_pixel_alpha_change ||
1760 			pipe_ctx->stream->update_flags.bits.scaling) {
1761 		pipe_ctx->plane_res.scl_data.lb_params.alpha_en = pipe_ctx->plane_state->per_pixel_alpha;
1762 		ASSERT(pipe_ctx->plane_res.scl_data.lb_params.depth == LB_PIXEL_DEPTH_36BPP);
1763 		/* scaler configuration */
1764 		pipe_ctx->plane_res.dpp->funcs->dpp_set_scaler(
1765 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data);
1766 	}
1767 
1768 	if (pipe_ctx->update_flags.bits.viewport ||
1769 			(context == dc->current_state && plane_state->update_flags.bits.position_change) ||
1770 			(context == dc->current_state && plane_state->update_flags.bits.scaling_change) ||
1771 			(context == dc->current_state && pipe_ctx->stream->update_flags.bits.scaling)) {
1772 
1773 		hubp->funcs->mem_program_viewport(
1774 			hubp,
1775 			&pipe_ctx->plane_res.scl_data.viewport,
1776 			&pipe_ctx->plane_res.scl_data.viewport_c);
1777 		viewport_changed = true;
1778 	}
1779 
1780 	if (hubp->funcs->hubp_program_mcache_id_and_split_coordinate)
1781 		hubp->funcs->hubp_program_mcache_id_and_split_coordinate(hubp, &pipe_ctx->mcache_regs);
1782 
1783 	/* Any updates are handled in dc interface, just need to apply existing for plane enable */
1784 	if ((pipe_ctx->update_flags.bits.enable || pipe_ctx->update_flags.bits.opp_changed ||
1785 			pipe_ctx->update_flags.bits.scaler || viewport_changed == true) &&
1786 			pipe_ctx->stream->cursor_attributes.address.quad_part != 0) {
1787 		dc->hwss.set_cursor_attribute(pipe_ctx);
1788 		dc->hwss.set_cursor_position(pipe_ctx);
1789 
1790 		if (dc->hwss.set_cursor_sdr_white_level)
1791 			dc->hwss.set_cursor_sdr_white_level(pipe_ctx);
1792 	}
1793 
1794 	/* Any updates are handled in dc interface, just need
1795 	 * to apply existing for plane enable / opp change */
1796 	if (pipe_ctx->update_flags.bits.enable || pipe_ctx->update_flags.bits.opp_changed
1797 			|| pipe_ctx->update_flags.bits.plane_changed
1798 			|| pipe_ctx->stream->update_flags.bits.gamut_remap
1799 			|| plane_state->update_flags.bits.gamut_remap_change
1800 			|| pipe_ctx->stream->update_flags.bits.out_csc) {
1801 		/* dpp/cm gamut remap*/
1802 		dc->hwss.program_gamut_remap(pipe_ctx);
1803 
1804 		/*call the dcn2 method which uses mpc csc*/
1805 		dc->hwss.program_output_csc(dc,
1806 				pipe_ctx,
1807 				pipe_ctx->stream->output_color_space,
1808 				pipe_ctx->stream->csc_color_matrix.matrix,
1809 				hubp->opp_id);
1810 	}
1811 
1812 	if (pipe_ctx->update_flags.bits.enable ||
1813 			pipe_ctx->update_flags.bits.plane_changed ||
1814 			pipe_ctx->update_flags.bits.opp_changed ||
1815 			plane_state->update_flags.bits.pixel_format_change ||
1816 			plane_state->update_flags.bits.horizontal_mirror_change ||
1817 			plane_state->update_flags.bits.rotation_change ||
1818 			plane_state->update_flags.bits.swizzle_change ||
1819 			plane_state->update_flags.bits.dcc_change ||
1820 			plane_state->update_flags.bits.bpp_change ||
1821 			plane_state->update_flags.bits.scaling_change ||
1822 			plane_state->update_flags.bits.plane_size_change) {
1823 		struct plane_size size = plane_state->plane_size;
1824 
1825 		size.surface_size = pipe_ctx->plane_res.scl_data.viewport;
1826 		hubp->funcs->hubp_program_surface_config(
1827 			hubp,
1828 			plane_state->format,
1829 			&plane_state->tiling_info,
1830 			&size,
1831 			plane_state->rotation,
1832 			&plane_state->dcc,
1833 			plane_state->horizontal_mirror,
1834 			0);
1835 		hubp->power_gated = false;
1836 	}
1837 
1838 	if (pipe_ctx->update_flags.bits.enable ||
1839 		pipe_ctx->update_flags.bits.plane_changed ||
1840 		plane_state->update_flags.bits.addr_update) {
1841 		if (resource_is_pipe_type(pipe_ctx, OTG_MASTER) &&
1842 				pipe_mall_type == SUBVP_MAIN) {
1843 			union block_sequence_params params;
1844 
1845 			params.subvp_save_surf_addr.dc_dmub_srv = dc->ctx->dmub_srv;
1846 			params.subvp_save_surf_addr.addr = &pipe_ctx->plane_state->address;
1847 			params.subvp_save_surf_addr.subvp_index = pipe_ctx->subvp_index;
1848 			hwss_subvp_save_surf_addr(&params);
1849 		}
1850 		dc->hwss.update_plane_addr(dc, pipe_ctx);
1851 	}
1852 
1853 	if (pipe_ctx->update_flags.bits.enable)
1854 		hubp->funcs->set_blank(hubp, false);
1855 	/* If the stream paired with this plane is phantom, the plane is also phantom */
1856 	if (pipe_mall_type == SUBVP_PHANTOM && hubp->funcs->phantom_hubp_post_enable)
1857 		hubp->funcs->phantom_hubp_post_enable(hubp);
1858 }
1859 
dcn20_calculate_vready_offset_for_group(struct pipe_ctx * pipe)1860 static int dcn20_calculate_vready_offset_for_group(struct pipe_ctx *pipe)
1861 {
1862 	struct pipe_ctx *other_pipe;
1863 	int vready_offset = pipe->pipe_dlg_param.vready_offset;
1864 
1865 	/* Always use the largest vready_offset of all connected pipes */
1866 	for (other_pipe = pipe->bottom_pipe; other_pipe != NULL; other_pipe = other_pipe->bottom_pipe) {
1867 		if (other_pipe->pipe_dlg_param.vready_offset > vready_offset)
1868 			vready_offset = other_pipe->pipe_dlg_param.vready_offset;
1869 	}
1870 	for (other_pipe = pipe->top_pipe; other_pipe != NULL; other_pipe = other_pipe->top_pipe) {
1871 		if (other_pipe->pipe_dlg_param.vready_offset > vready_offset)
1872 			vready_offset = other_pipe->pipe_dlg_param.vready_offset;
1873 	}
1874 	for (other_pipe = pipe->next_odm_pipe; other_pipe != NULL; other_pipe = other_pipe->next_odm_pipe) {
1875 		if (other_pipe->pipe_dlg_param.vready_offset > vready_offset)
1876 			vready_offset = other_pipe->pipe_dlg_param.vready_offset;
1877 	}
1878 	for (other_pipe = pipe->prev_odm_pipe; other_pipe != NULL; other_pipe = other_pipe->prev_odm_pipe) {
1879 		if (other_pipe->pipe_dlg_param.vready_offset > vready_offset)
1880 			vready_offset = other_pipe->pipe_dlg_param.vready_offset;
1881 	}
1882 
1883 	return vready_offset;
1884 }
1885 
dcn20_program_tg(struct dc * dc,struct pipe_ctx * pipe_ctx,struct dc_state * context,struct dce_hwseq * hws)1886 static void dcn20_program_tg(
1887 	struct dc *dc,
1888 	struct pipe_ctx *pipe_ctx,
1889 	struct dc_state *context,
1890 	struct dce_hwseq *hws)
1891 {
1892 	pipe_ctx->stream_res.tg->funcs->program_global_sync(
1893 		pipe_ctx->stream_res.tg,
1894 		dcn20_calculate_vready_offset_for_group(pipe_ctx),
1895 		pipe_ctx->pipe_dlg_param.vstartup_start,
1896 		pipe_ctx->pipe_dlg_param.vupdate_offset,
1897 		pipe_ctx->pipe_dlg_param.vupdate_width,
1898 		pipe_ctx->pipe_dlg_param.pstate_keepout);
1899 
1900 	if (dc_state_get_pipe_subvp_type(context, pipe_ctx) != SUBVP_PHANTOM)
1901 		pipe_ctx->stream_res.tg->funcs->wait_for_state(pipe_ctx->stream_res.tg, CRTC_STATE_VACTIVE);
1902 
1903 	pipe_ctx->stream_res.tg->funcs->set_vtg_params(
1904 		pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing, true);
1905 
1906 	if (hws->funcs.setup_vupdate_interrupt)
1907 		hws->funcs.setup_vupdate_interrupt(dc, pipe_ctx);
1908 }
1909 
dcn20_program_pipe(struct dc * dc,struct pipe_ctx * pipe_ctx,struct dc_state * context)1910 static void dcn20_program_pipe(
1911 		struct dc *dc,
1912 		struct pipe_ctx *pipe_ctx,
1913 		struct dc_state *context)
1914 {
1915 	struct dce_hwseq *hws = dc->hwseq;
1916 
1917 	/* Only need to unblank on top pipe */
1918 	if (resource_is_pipe_type(pipe_ctx, OTG_MASTER)) {
1919 		if (pipe_ctx->update_flags.bits.enable ||
1920 			pipe_ctx->update_flags.bits.odm ||
1921 			pipe_ctx->stream->update_flags.bits.abm_level)
1922 			hws->funcs.blank_pixel_data(dc, pipe_ctx,
1923 				!pipe_ctx->plane_state ||
1924 				!pipe_ctx->plane_state->visible);
1925 	}
1926 
1927 	/* Only update TG on top pipe */
1928 	if (pipe_ctx->update_flags.bits.global_sync && !pipe_ctx->top_pipe
1929 		&& !pipe_ctx->prev_odm_pipe)
1930 		dcn20_program_tg(dc, pipe_ctx, context, hws);
1931 
1932 	if (pipe_ctx->update_flags.bits.odm)
1933 		hws->funcs.update_odm(dc, context, pipe_ctx);
1934 
1935 	if (pipe_ctx->update_flags.bits.enable) {
1936 		if (hws->funcs.enable_plane)
1937 			hws->funcs.enable_plane(dc, pipe_ctx, context);
1938 		else
1939 			dcn20_enable_plane(dc, pipe_ctx, context);
1940 
1941 		if (dc->res_pool->hubbub->funcs->force_wm_propagate_to_pipes)
1942 			dc->res_pool->hubbub->funcs->force_wm_propagate_to_pipes(dc->res_pool->hubbub);
1943 	}
1944 
1945 	if (pipe_ctx->update_flags.bits.det_size) {
1946 		if (dc->res_pool->hubbub->funcs->program_det_size)
1947 			dc->res_pool->hubbub->funcs->program_det_size(
1948 				dc->res_pool->hubbub, pipe_ctx->plane_res.hubp->inst, pipe_ctx->det_buffer_size_kb);
1949 
1950 		if (dc->res_pool->hubbub->funcs->program_det_segments)
1951 			dc->res_pool->hubbub->funcs->program_det_segments(
1952 				dc->res_pool->hubbub, pipe_ctx->plane_res.hubp->inst, pipe_ctx->hubp_regs.det_size);
1953 	}
1954 
1955 	if (pipe_ctx->plane_state && (pipe_ctx->update_flags.raw ||
1956 	    pipe_ctx->plane_state->update_flags.raw ||
1957 	    pipe_ctx->stream->update_flags.raw))
1958 		dcn20_update_dchubp_dpp(dc, pipe_ctx, context);
1959 
1960 	if (pipe_ctx->plane_state && (pipe_ctx->update_flags.bits.enable ||
1961 		pipe_ctx->plane_state->update_flags.bits.hdr_mult))
1962 		hws->funcs.set_hdr_multiplier(pipe_ctx);
1963 
1964 	if (hws->funcs.populate_mcm_luts) {
1965 		if (pipe_ctx->plane_state) {
1966 			hws->funcs.populate_mcm_luts(dc, pipe_ctx, pipe_ctx->plane_state->mcm_luts,
1967 				pipe_ctx->plane_state->lut_bank_a);
1968 			pipe_ctx->plane_state->lut_bank_a = !pipe_ctx->plane_state->lut_bank_a;
1969 		}
1970 	}
1971 
1972 	if (pipe_ctx->plane_state &&
1973 		(pipe_ctx->plane_state->update_flags.bits.in_transfer_func_change ||
1974 			pipe_ctx->plane_state->update_flags.bits.gamma_change ||
1975 			pipe_ctx->plane_state->update_flags.bits.lut_3d ||
1976 			pipe_ctx->update_flags.bits.enable))
1977 		hws->funcs.set_input_transfer_func(dc, pipe_ctx, pipe_ctx->plane_state);
1978 
1979 	/* dcn10_translate_regamma_to_hw_format takes 750us to finish
1980 	 * only do gamma programming for powering on, internal memcmp to avoid
1981 	 * updating on slave planes
1982 	 */
1983 	if (pipe_ctx->update_flags.bits.enable ||
1984 		pipe_ctx->update_flags.bits.plane_changed ||
1985 		pipe_ctx->stream->update_flags.bits.out_tf ||
1986 		(pipe_ctx->plane_state &&
1987 			pipe_ctx->plane_state->update_flags.bits.output_tf_change))
1988 		hws->funcs.set_output_transfer_func(dc, pipe_ctx, pipe_ctx->stream);
1989 
1990 	/* If the pipe has been enabled or has a different opp, we
1991 	 * should reprogram the fmt. This deals with cases where
1992 	 * interation between mpc and odm combine on different streams
1993 	 * causes a different pipe to be chosen to odm combine with.
1994 	 */
1995 	if (pipe_ctx->update_flags.bits.enable
1996 		|| pipe_ctx->update_flags.bits.opp_changed) {
1997 
1998 		pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
1999 			pipe_ctx->stream_res.opp,
2000 			COLOR_SPACE_YCBCR601,
2001 			pipe_ctx->stream->timing.display_color_depth,
2002 			pipe_ctx->stream->signal);
2003 
2004 		pipe_ctx->stream_res.opp->funcs->opp_program_fmt(
2005 			pipe_ctx->stream_res.opp,
2006 			&pipe_ctx->stream->bit_depth_params,
2007 			&pipe_ctx->stream->clamping);
2008 	}
2009 
2010 	/* Set ABM pipe after other pipe configurations done */
2011 	if ((pipe_ctx->plane_state && pipe_ctx->plane_state->visible)) {
2012 		if (pipe_ctx->stream_res.abm) {
2013 			dc->hwss.set_pipe(pipe_ctx);
2014 			pipe_ctx->stream_res.abm->funcs->set_abm_level(pipe_ctx->stream_res.abm,
2015 				pipe_ctx->stream->abm_level);
2016 		}
2017 	}
2018 
2019 	if (pipe_ctx->update_flags.bits.test_pattern_changed) {
2020 		struct output_pixel_processor *odm_opp = pipe_ctx->stream_res.opp;
2021 		struct bit_depth_reduction_params params;
2022 
2023 		memset(&params, 0, sizeof(params));
2024 		odm_opp->funcs->opp_program_bit_depth_reduction(odm_opp, &params);
2025 		dc->hwss.set_disp_pattern_generator(dc,
2026 			pipe_ctx,
2027 			pipe_ctx->stream_res.test_pattern_params.test_pattern,
2028 			pipe_ctx->stream_res.test_pattern_params.color_space,
2029 			pipe_ctx->stream_res.test_pattern_params.color_depth,
2030 			NULL,
2031 			pipe_ctx->stream_res.test_pattern_params.width,
2032 			pipe_ctx->stream_res.test_pattern_params.height,
2033 			pipe_ctx->stream_res.test_pattern_params.offset);
2034 	}
2035 }
2036 
dcn20_program_front_end_for_ctx(struct dc * dc,struct dc_state * context)2037 void dcn20_program_front_end_for_ctx(
2038 		struct dc *dc,
2039 		struct dc_state *context)
2040 {
2041 	int i;
2042 	unsigned int prev_hubp_count = 0;
2043 	unsigned int hubp_count = 0;
2044 	struct dce_hwseq *hws = dc->hwseq;
2045 	struct pipe_ctx *pipe = NULL;
2046 
2047 	DC_LOGGER_INIT(dc->ctx->logger);
2048 
2049 	if (resource_is_pipe_topology_changed(dc->current_state, context))
2050 		resource_log_pipe_topology_update(dc, context);
2051 
2052 	if (dc->hwss.program_triplebuffer != NULL && dc->debug.enable_tri_buf) {
2053 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2054 			pipe = &context->res_ctx.pipe_ctx[i];
2055 
2056 			if (!pipe->top_pipe && !pipe->prev_odm_pipe && pipe->plane_state) {
2057 				ASSERT(!pipe->plane_state->triplebuffer_flips);
2058 				/*turn off triple buffer for full update*/
2059 				dc->hwss.program_triplebuffer(
2060 					dc, pipe, pipe->plane_state->triplebuffer_flips);
2061 			}
2062 		}
2063 	}
2064 
2065 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2066 		if (dc->current_state->res_ctx.pipe_ctx[i].plane_state)
2067 			prev_hubp_count++;
2068 		if (context->res_ctx.pipe_ctx[i].plane_state)
2069 			hubp_count++;
2070 	}
2071 
2072 	if (prev_hubp_count == 0 && hubp_count > 0) {
2073 		if (dc->res_pool->hubbub->funcs->force_pstate_change_control)
2074 			dc->res_pool->hubbub->funcs->force_pstate_change_control(
2075 				dc->res_pool->hubbub, true, false);
2076 		udelay(500);
2077 	}
2078 
2079 	/* Set pipe update flags and lock pipes */
2080 	for (i = 0; i < dc->res_pool->pipe_count; i++)
2081 		dcn20_detect_pipe_changes(dc->current_state, context, &dc->current_state->res_ctx.pipe_ctx[i],
2082 			&context->res_ctx.pipe_ctx[i]);
2083 
2084 	/* When disabling phantom pipes, turn on phantom OTG first (so we can get double
2085 	 * buffer updates properly)
2086 	 */
2087 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2088 		struct dc_stream_state *stream = dc->current_state->res_ctx.pipe_ctx[i].stream;
2089 
2090 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2091 
2092 		if (context->res_ctx.pipe_ctx[i].update_flags.bits.disable && stream &&
2093 			dc_state_get_pipe_subvp_type(dc->current_state, pipe) == SUBVP_PHANTOM) {
2094 			struct timing_generator *tg = dc->current_state->res_ctx.pipe_ctx[i].stream_res.tg;
2095 
2096 			if (tg->funcs->enable_crtc) {
2097 				if (dc->hwseq->funcs.blank_pixel_data)
2098 					dc->hwseq->funcs.blank_pixel_data(dc, pipe, true);
2099 
2100 				tg->funcs->enable_crtc(tg);
2101 			}
2102 		}
2103 	}
2104 	/* OTG blank before disabling all front ends */
2105 	for (i = 0; i < dc->res_pool->pipe_count; i++)
2106 		if (context->res_ctx.pipe_ctx[i].update_flags.bits.disable
2107 			&& !context->res_ctx.pipe_ctx[i].top_pipe
2108 			&& !context->res_ctx.pipe_ctx[i].prev_odm_pipe
2109 			&& context->res_ctx.pipe_ctx[i].stream)
2110 			hws->funcs.blank_pixel_data(dc, &context->res_ctx.pipe_ctx[i], true);
2111 
2112 	/* Disconnect mpcc */
2113 	for (i = 0; i < dc->res_pool->pipe_count; i++)
2114 		if (context->res_ctx.pipe_ctx[i].update_flags.bits.disable
2115 			|| context->res_ctx.pipe_ctx[i].update_flags.bits.opp_changed) {
2116 			struct hubbub *hubbub = dc->res_pool->hubbub;
2117 
2118 			/* Phantom pipe DET should be 0, but if a pipe in use is being transitioned to phantom
2119 			 * then we want to do the programming here (effectively it's being disabled). If we do
2120 			 * the programming later the DET won't be updated until the OTG for the phantom pipe is
2121 			 * turned on (i.e. in an MCLK switch) which can come in too late and cause issues with
2122 			 * DET allocation.
2123 			 */
2124 			if ((context->res_ctx.pipe_ctx[i].update_flags.bits.disable ||
2125 				(context->res_ctx.pipe_ctx[i].plane_state &&
2126 				dc_state_get_pipe_subvp_type(context, &context->res_ctx.pipe_ctx[i])
2127 				== SUBVP_PHANTOM))) {
2128 				if (hubbub->funcs->program_det_size)
2129 					hubbub->funcs->program_det_size(hubbub,
2130 						dc->current_state->res_ctx.pipe_ctx[i].plane_res.hubp->inst, 0);
2131 				if (dc->res_pool->hubbub->funcs->program_det_segments)
2132 					dc->res_pool->hubbub->funcs->program_det_segments(
2133 						hubbub, dc->current_state->res_ctx.pipe_ctx[i].plane_res.hubp->inst, 0);
2134 			}
2135 			hws->funcs.plane_atomic_disconnect(dc, dc->current_state,
2136 				&dc->current_state->res_ctx.pipe_ctx[i]);
2137 			DC_LOG_DC("Reset mpcc for pipe %d\n", dc->current_state->res_ctx.pipe_ctx[i].pipe_idx);
2138 		}
2139 
2140 	/* update ODM for blanked OTG master pipes */
2141 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2142 		pipe = &context->res_ctx.pipe_ctx[i];
2143 		if (resource_is_pipe_type(pipe, OTG_MASTER) &&
2144 			!resource_is_pipe_type(pipe, DPP_PIPE) &&
2145 			pipe->update_flags.bits.odm &&
2146 			hws->funcs.update_odm)
2147 			hws->funcs.update_odm(dc, context, pipe);
2148 	}
2149 
2150 	/*
2151 	 * Program all updated pipes, order matters for mpcc setup. Start with
2152 	 * top pipe and program all pipes that follow in order
2153 	 */
2154 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2155 		pipe = &context->res_ctx.pipe_ctx[i];
2156 
2157 		if (pipe->plane_state && !pipe->top_pipe) {
2158 			while (pipe) {
2159 				if (hws->funcs.program_pipe)
2160 					hws->funcs.program_pipe(dc, pipe, context);
2161 				else {
2162 					/* Don't program phantom pipes in the regular front end programming sequence.
2163 					 * There is an MPO transition case where a pipe being used by a video plane is
2164 					 * transitioned directly to be a phantom pipe when closing the MPO video.
2165 					 * However the phantom pipe will program a new HUBP_VTG_SEL (update takes place
2166 					 * right away) but the MPO still exists until the double buffered update of the
2167 					 * main pipe so we will get a frame of underflow if the phantom pipe is
2168 					 * programmed here.
2169 					 */
2170 					if (pipe->stream &&
2171 						dc_state_get_pipe_subvp_type(context, pipe) != SUBVP_PHANTOM)
2172 						dcn20_program_pipe(dc, pipe, context);
2173 				}
2174 
2175 				pipe = pipe->bottom_pipe;
2176 			}
2177 		}
2178 
2179 		/* Program secondary blending tree and writeback pipes */
2180 		pipe = &context->res_ctx.pipe_ctx[i];
2181 		if (!pipe->top_pipe && !pipe->prev_odm_pipe
2182 			&& pipe->stream && pipe->stream->num_wb_info > 0
2183 			&& (pipe->update_flags.raw || (pipe->plane_state && pipe->plane_state->update_flags.raw)
2184 				|| pipe->stream->update_flags.raw)
2185 			&& hws->funcs.program_all_writeback_pipes_in_tree)
2186 			hws->funcs.program_all_writeback_pipes_in_tree(dc, pipe->stream, context);
2187 
2188 		/* Avoid underflow by check of pipe line read when adding 2nd plane. */
2189 		if (hws->wa.wait_hubpret_read_start_during_mpo_transition &&
2190 			!pipe->top_pipe &&
2191 			pipe->stream &&
2192 			pipe->plane_res.hubp->funcs->hubp_wait_pipe_read_start &&
2193 			dc->current_state->stream_status[0].plane_count == 1 &&
2194 			context->stream_status[0].plane_count > 1) {
2195 			pipe->plane_res.hubp->funcs->hubp_wait_pipe_read_start(pipe->plane_res.hubp);
2196 		}
2197 	}
2198 }
2199 
2200 /* post_unlock_reset_opp - the function wait for corresponding double
2201  * buffered pending status clear and reset opp head pipe's none double buffered
2202  * registers to their initial state.
2203  */
dcn20_post_unlock_reset_opp(struct dc * dc,struct pipe_ctx * opp_head)2204 void dcn20_post_unlock_reset_opp(struct dc *dc,
2205 		struct pipe_ctx *opp_head)
2206 {
2207 	struct display_stream_compressor *dsc = opp_head->stream_res.dsc;
2208 	struct dccg *dccg = dc->res_pool->dccg;
2209 
2210 	/*
2211 	 * wait for all DPP pipes in current mpc blending tree completes double
2212 	 * buffered disconnection before resetting OPP
2213 	 */
2214 	dc->hwss.wait_for_mpcc_disconnect(dc, dc->res_pool, opp_head);
2215 
2216 	if (dsc) {
2217 		bool is_dsc_ungated = false;
2218 
2219 		if (dc->hwseq->funcs.dsc_pg_status)
2220 			is_dsc_ungated = dc->hwseq->funcs.dsc_pg_status(dc->hwseq, dsc->inst);
2221 
2222 		if (is_dsc_ungated) {
2223 			/*
2224 			 * seamless update specific where we will postpone non
2225 			 * double buffered DSCCLK disable logic in post unlock
2226 			 * sequence after DSC is disconnected from OPP but not
2227 			 * yet power gated.
2228 			 */
2229 			dsc->funcs->dsc_wait_disconnect_pending_clear(dsc);
2230 			dsc->funcs->dsc_disable(dsc);
2231 			if (dccg->funcs->set_ref_dscclk)
2232 				dccg->funcs->set_ref_dscclk(dccg, dsc->inst);
2233 		}
2234 	}
2235 }
2236 
dcn20_post_unlock_program_front_end(struct dc * dc,struct dc_state * context)2237 void dcn20_post_unlock_program_front_end(
2238 		struct dc *dc,
2239 		struct dc_state *context)
2240 {
2241 	// Timeout for pipe enable
2242 	unsigned int timeout_us = 100000;
2243 	unsigned int polling_interval_us = 1;
2244 	struct dce_hwseq *hwseq = dc->hwseq;
2245 	int i;
2246 
2247 	for (i = 0; i < dc->res_pool->pipe_count; i++)
2248 		if (resource_is_pipe_type(&dc->current_state->res_ctx.pipe_ctx[i], OPP_HEAD) &&
2249 			!resource_is_pipe_type(&context->res_ctx.pipe_ctx[i], OPP_HEAD))
2250 			dcn20_post_unlock_reset_opp(dc,
2251 				&dc->current_state->res_ctx.pipe_ctx[i]);
2252 
2253 	for (i = 0; i < dc->res_pool->pipe_count; i++)
2254 		if (context->res_ctx.pipe_ctx[i].update_flags.bits.disable)
2255 			dc->hwss.disable_plane(dc, dc->current_state, &dc->current_state->res_ctx.pipe_ctx[i]);
2256 
2257 	/*
2258 	 * If we are enabling a pipe, we need to wait for pending clear as this is a critical
2259 	 * part of the enable operation otherwise, DM may request an immediate flip which
2260 	 * will cause HW to perform an "immediate enable" (as opposed to "vsync enable") which
2261 	 * is unsupported on DCN.
2262 	 */
2263 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2264 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2265 		// Don't check flip pending on phantom pipes
2266 		if (pipe->plane_state && !pipe->top_pipe && pipe->update_flags.bits.enable &&
2267 			dc_state_get_pipe_subvp_type(context, pipe) != SUBVP_PHANTOM) {
2268 			struct hubp *hubp = pipe->plane_res.hubp;
2269 			int j = 0;
2270 
2271 			for (j = 0; j < timeout_us / polling_interval_us
2272 				&& hubp->funcs->hubp_is_flip_pending(hubp); j++)
2273 				udelay(polling_interval_us);
2274 		}
2275 	}
2276 
2277 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2278 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2279 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
2280 
2281 		/* When going from a smaller ODM slice count to larger, we must ensure double
2282 		 * buffer update completes before we return to ensure we don't reduce DISPCLK
2283 		 * before we've transitioned to 2:1 or 4:1
2284 		 */
2285 		if (resource_is_pipe_type(old_pipe, OTG_MASTER) && resource_is_pipe_type(pipe, OTG_MASTER) &&
2286 			resource_get_odm_slice_count(old_pipe) < resource_get_odm_slice_count(pipe) &&
2287 			dc_state_get_pipe_subvp_type(context, pipe) != SUBVP_PHANTOM) {
2288 			int j = 0;
2289 			struct timing_generator *tg = pipe->stream_res.tg;
2290 
2291 			if (tg->funcs->get_optc_double_buffer_pending) {
2292 				for (j = 0; j < timeout_us / polling_interval_us
2293 					&& tg->funcs->get_optc_double_buffer_pending(tg); j++)
2294 					udelay(polling_interval_us);
2295 			}
2296 		}
2297 	}
2298 
2299 	if (dc->res_pool->hubbub->funcs->force_pstate_change_control)
2300 		dc->res_pool->hubbub->funcs->force_pstate_change_control(
2301 			dc->res_pool->hubbub, false, false);
2302 
2303 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2304 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2305 
2306 		if (pipe->plane_state && !pipe->top_pipe) {
2307 			/* Program phantom pipe here to prevent a frame of underflow in the MPO transition
2308 			 * case (if a pipe being used for a video plane transitions to a phantom pipe, it
2309 			 * can underflow due to HUBP_VTG_SEL programming if done in the regular front end
2310 			 * programming sequence).
2311 			 */
2312 			while (pipe) {
2313 				if (pipe->stream && dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
2314 					/* When turning on the phantom pipe we want to run through the
2315 					 * entire enable sequence, so apply all the "enable" flags.
2316 					 */
2317 					if (dc->hwss.apply_update_flags_for_phantom)
2318 						dc->hwss.apply_update_flags_for_phantom(pipe);
2319 					if (dc->hwss.update_phantom_vp_position)
2320 						dc->hwss.update_phantom_vp_position(dc, context, pipe);
2321 					dcn20_program_pipe(dc, pipe, context);
2322 				}
2323 				pipe = pipe->bottom_pipe;
2324 			}
2325 		}
2326 	}
2327 
2328 	if (!hwseq)
2329 		return;
2330 
2331 	/* P-State support transitions:
2332 	 * Natural -> FPO:      P-State disabled in prepare, force disallow anytime is safe
2333 	 * FPO -> Natural:      Unforce anytime after FW disable is safe (P-State will assert naturally)
2334 	 * Unsupported -> FPO:  P-State enabled in optimize, force disallow anytime is safe
2335 	 * FPO -> Unsupported:  P-State disabled in prepare, unforce disallow anytime is safe
2336 	 * FPO <-> SubVP:       Force disallow is maintained on the FPO / SubVP pipes
2337 	 */
2338 	if (hwseq->funcs.update_force_pstate)
2339 		dc->hwseq->funcs.update_force_pstate(dc, context);
2340 
2341 	/* Only program the MALL registers after all the main and phantom pipes
2342 	 * are done programming.
2343 	 */
2344 	if (hwseq->funcs.program_mall_pipe_config)
2345 		hwseq->funcs.program_mall_pipe_config(dc, context);
2346 
2347 	/* WA to apply WM setting*/
2348 	if (hwseq->wa.DEGVIDCN21)
2349 		dc->res_pool->hubbub->funcs->apply_DEDCN21_147_wa(dc->res_pool->hubbub);
2350 
2351 	/* WA for stutter underflow during MPO transitions when adding 2nd plane */
2352 	if (hwseq->wa.disallow_self_refresh_during_multi_plane_transition) {
2353 
2354 		if (dc->current_state->stream_status[0].plane_count == 1 &&
2355 			context->stream_status[0].plane_count > 1) {
2356 
2357 			struct timing_generator *tg = dc->res_pool->timing_generators[0];
2358 
2359 			dc->res_pool->hubbub->funcs->allow_self_refresh_control(dc->res_pool->hubbub, false);
2360 
2361 			hwseq->wa_state.disallow_self_refresh_during_multi_plane_transition_applied = true;
2362 			hwseq->wa_state.disallow_self_refresh_during_multi_plane_transition_applied_on_frame = tg->funcs->get_frame_count(tg);
2363 		}
2364 	}
2365 }
2366 
dcn20_prepare_bandwidth(struct dc * dc,struct dc_state * context)2367 void dcn20_prepare_bandwidth(
2368 		struct dc *dc,
2369 		struct dc_state *context)
2370 {
2371 	struct hubbub *hubbub = dc->res_pool->hubbub;
2372 	unsigned int compbuf_size_kb = 0;
2373 	unsigned int cache_wm_a = context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns;
2374 	unsigned int i;
2375 
2376 	dc->clk_mgr->funcs->update_clocks(
2377 			dc->clk_mgr,
2378 			context,
2379 			false);
2380 
2381 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2382 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2383 
2384 		// At optimize don't restore the original watermark value
2385 		if (pipe->stream && dc_state_get_pipe_subvp_type(context, pipe) != SUBVP_NONE) {
2386 			context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns = 4U * 1000U * 1000U * 1000U;
2387 			break;
2388 		}
2389 	}
2390 
2391 	/* program dchubbub watermarks:
2392 	 * For assigning wm_optimized_required, use |= operator since we don't want
2393 	 * to clear the value if the optimize has not happened yet
2394 	 */
2395 	dc->wm_optimized_required |= hubbub->funcs->program_watermarks(hubbub,
2396 					&context->bw_ctx.bw.dcn.watermarks,
2397 					dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000,
2398 					false);
2399 
2400 	// Restore the real watermark so we can commit the value to DMCUB
2401 	// DMCUB uses the "original" watermark value in SubVP MCLK switch
2402 	context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns = cache_wm_a;
2403 
2404 	/* decrease compbuf size */
2405 	if (hubbub->funcs->program_compbuf_size) {
2406 		if (context->bw_ctx.dml.ip.min_comp_buffer_size_kbytes) {
2407 			compbuf_size_kb = context->bw_ctx.dml.ip.min_comp_buffer_size_kbytes;
2408 			dc->wm_optimized_required |= (compbuf_size_kb != dc->current_state->bw_ctx.dml.ip.min_comp_buffer_size_kbytes);
2409 		} else {
2410 			compbuf_size_kb = context->bw_ctx.bw.dcn.compbuf_size_kb;
2411 			dc->wm_optimized_required |= (compbuf_size_kb != dc->current_state->bw_ctx.bw.dcn.compbuf_size_kb);
2412 		}
2413 
2414 		hubbub->funcs->program_compbuf_size(hubbub, compbuf_size_kb, false);
2415 	}
2416 }
2417 
dcn20_optimize_bandwidth(struct dc * dc,struct dc_state * context)2418 void dcn20_optimize_bandwidth(
2419 		struct dc *dc,
2420 		struct dc_state *context)
2421 {
2422 	struct hubbub *hubbub = dc->res_pool->hubbub;
2423 	int i;
2424 
2425 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2426 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
2427 
2428 		// At optimize don't need  to restore the original watermark value
2429 		if (pipe->stream && dc_state_get_pipe_subvp_type(context, pipe) != SUBVP_NONE) {
2430 			context->bw_ctx.bw.dcn.watermarks.a.cstate_pstate.pstate_change_ns = 4U * 1000U * 1000U * 1000U;
2431 			break;
2432 		}
2433 	}
2434 
2435 	/* program dchubbub watermarks */
2436 	hubbub->funcs->program_watermarks(hubbub,
2437 					&context->bw_ctx.bw.dcn.watermarks,
2438 					dc->res_pool->ref_clocks.dchub_ref_clock_inKhz / 1000,
2439 					true);
2440 
2441 	if (dc->clk_mgr->dc_mode_softmax_enabled)
2442 		if (dc->clk_mgr->clks.dramclk_khz > dc->clk_mgr->bw_params->dc_mode_softmax_memclk * 1000 &&
2443 				context->bw_ctx.bw.dcn.clk.dramclk_khz <= dc->clk_mgr->bw_params->dc_mode_softmax_memclk * 1000)
2444 			dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, dc->clk_mgr->bw_params->dc_mode_softmax_memclk);
2445 
2446 	/* increase compbuf size */
2447 	if (hubbub->funcs->program_compbuf_size)
2448 		hubbub->funcs->program_compbuf_size(hubbub, context->bw_ctx.bw.dcn.compbuf_size_kb, true);
2449 
2450 	if (context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching) {
2451 		dc_dmub_srv_p_state_delegate(dc,
2452 			true, context);
2453 		context->bw_ctx.bw.dcn.clk.p_state_change_support = true;
2454 		dc->clk_mgr->clks.fw_based_mclk_switching = true;
2455 	} else {
2456 		dc->clk_mgr->clks.fw_based_mclk_switching = false;
2457 	}
2458 
2459 	dc->clk_mgr->funcs->update_clocks(
2460 			dc->clk_mgr,
2461 			context,
2462 			true);
2463 	if (context->bw_ctx.bw.dcn.clk.zstate_support == DCN_ZSTATE_SUPPORT_ALLOW &&
2464 		!dc->debug.disable_extblankadj) {
2465 		for (i = 0; i < dc->res_pool->pipe_count; ++i) {
2466 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2467 
2468 			if (pipe_ctx->stream && pipe_ctx->plane_res.hubp->funcs->program_extended_blank
2469 				&& pipe_ctx->stream->adjust.v_total_min == pipe_ctx->stream->adjust.v_total_max
2470 				&& pipe_ctx->stream->adjust.v_total_max > pipe_ctx->stream->timing.v_total)
2471 					pipe_ctx->plane_res.hubp->funcs->program_extended_blank(pipe_ctx->plane_res.hubp,
2472 						pipe_ctx->dlg_regs.min_dst_y_next_start);
2473 		}
2474 	}
2475 }
2476 
dcn20_update_bandwidth(struct dc * dc,struct dc_state * context)2477 bool dcn20_update_bandwidth(
2478 		struct dc *dc,
2479 		struct dc_state *context)
2480 {
2481 	int i;
2482 	struct dce_hwseq *hws = dc->hwseq;
2483 
2484 	/* recalculate DML parameters */
2485 	if (!dc->res_pool->funcs->validate_bandwidth(dc, context, false))
2486 		return false;
2487 
2488 	/* apply updated bandwidth parameters */
2489 	dc->hwss.prepare_bandwidth(dc, context);
2490 
2491 	/* update hubp configs for all pipes */
2492 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2493 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2494 
2495 		if (pipe_ctx->plane_state == NULL)
2496 			continue;
2497 
2498 		if (pipe_ctx->top_pipe == NULL) {
2499 			bool blank = !is_pipe_tree_visible(pipe_ctx);
2500 
2501 			pipe_ctx->stream_res.tg->funcs->program_global_sync(
2502 					pipe_ctx->stream_res.tg,
2503 					dcn20_calculate_vready_offset_for_group(pipe_ctx),
2504 					pipe_ctx->pipe_dlg_param.vstartup_start,
2505 					pipe_ctx->pipe_dlg_param.vupdate_offset,
2506 					pipe_ctx->pipe_dlg_param.vupdate_width,
2507 					pipe_ctx->pipe_dlg_param.pstate_keepout);
2508 
2509 			pipe_ctx->stream_res.tg->funcs->set_vtg_params(
2510 					pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing, false);
2511 
2512 			if (pipe_ctx->prev_odm_pipe == NULL)
2513 				hws->funcs.blank_pixel_data(dc, pipe_ctx, blank);
2514 
2515 			if (hws->funcs.setup_vupdate_interrupt)
2516 				hws->funcs.setup_vupdate_interrupt(dc, pipe_ctx);
2517 		}
2518 
2519 		pipe_ctx->plane_res.hubp->funcs->hubp_setup(
2520 				pipe_ctx->plane_res.hubp,
2521 					&pipe_ctx->dlg_regs,
2522 					&pipe_ctx->ttu_regs,
2523 					&pipe_ctx->rq_regs,
2524 					&pipe_ctx->pipe_dlg_param);
2525 	}
2526 
2527 	return true;
2528 }
2529 
dcn20_enable_writeback(struct dc * dc,struct dc_writeback_info * wb_info,struct dc_state * context)2530 void dcn20_enable_writeback(
2531 		struct dc *dc,
2532 		struct dc_writeback_info *wb_info,
2533 		struct dc_state *context)
2534 {
2535 	struct dwbc *dwb;
2536 	struct mcif_wb *mcif_wb;
2537 	struct timing_generator *optc;
2538 
2539 	ASSERT(wb_info->dwb_pipe_inst < MAX_DWB_PIPES);
2540 	ASSERT(wb_info->wb_enabled);
2541 	dwb = dc->res_pool->dwbc[wb_info->dwb_pipe_inst];
2542 	mcif_wb = dc->res_pool->mcif_wb[wb_info->dwb_pipe_inst];
2543 
2544 	/* set the OPTC source mux */
2545 	optc = dc->res_pool->timing_generators[dwb->otg_inst];
2546 	optc->funcs->set_dwb_source(optc, wb_info->dwb_pipe_inst);
2547 	/* set MCIF_WB buffer and arbitration configuration */
2548 	mcif_wb->funcs->config_mcif_buf(mcif_wb, &wb_info->mcif_buf_params, wb_info->dwb_params.dest_height);
2549 	mcif_wb->funcs->config_mcif_arb(mcif_wb, &context->bw_ctx.bw.dcn.bw_writeback.mcif_wb_arb[wb_info->dwb_pipe_inst]);
2550 	/* Enable MCIF_WB */
2551 	mcif_wb->funcs->enable_mcif(mcif_wb);
2552 	/* Enable DWB */
2553 	dwb->funcs->enable(dwb, &wb_info->dwb_params);
2554 	/* TODO: add sequence to enable/disable warmup */
2555 }
2556 
dcn20_disable_writeback(struct dc * dc,unsigned int dwb_pipe_inst)2557 void dcn20_disable_writeback(
2558 		struct dc *dc,
2559 		unsigned int dwb_pipe_inst)
2560 {
2561 	struct dwbc *dwb;
2562 	struct mcif_wb *mcif_wb;
2563 
2564 	ASSERT(dwb_pipe_inst < MAX_DWB_PIPES);
2565 	dwb = dc->res_pool->dwbc[dwb_pipe_inst];
2566 	mcif_wb = dc->res_pool->mcif_wb[dwb_pipe_inst];
2567 
2568 	dwb->funcs->disable(dwb);
2569 	mcif_wb->funcs->disable_mcif(mcif_wb);
2570 }
2571 
dcn20_wait_for_blank_complete(struct output_pixel_processor * opp)2572 bool dcn20_wait_for_blank_complete(
2573 		struct output_pixel_processor *opp)
2574 {
2575 	int counter;
2576 
2577 	if (!opp)
2578 		return false;
2579 
2580 	for (counter = 0; counter < 1000; counter++) {
2581 		if (!opp->funcs->dpg_is_pending(opp))
2582 			break;
2583 
2584 		udelay(100);
2585 	}
2586 
2587 	if (counter == 1000) {
2588 		dm_error("DC: failed to blank crtc!\n");
2589 		return false;
2590 	}
2591 
2592 	return opp->funcs->dpg_is_blanked(opp);
2593 }
2594 
dcn20_dmdata_status_done(struct pipe_ctx * pipe_ctx)2595 bool dcn20_dmdata_status_done(struct pipe_ctx *pipe_ctx)
2596 {
2597 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
2598 
2599 	if (!hubp)
2600 		return false;
2601 	return hubp->funcs->dmdata_status_done(hubp);
2602 }
2603 
dcn20_disable_stream_gating(struct dc * dc,struct pipe_ctx * pipe_ctx)2604 void dcn20_disable_stream_gating(struct dc *dc, struct pipe_ctx *pipe_ctx)
2605 {
2606 	struct dce_hwseq *hws = dc->hwseq;
2607 
2608 	if (pipe_ctx->stream_res.dsc) {
2609 		struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
2610 
2611 		hws->funcs.dsc_pg_control(hws, pipe_ctx->stream_res.dsc->inst, true);
2612 		while (odm_pipe) {
2613 			hws->funcs.dsc_pg_control(hws, odm_pipe->stream_res.dsc->inst, true);
2614 			odm_pipe = odm_pipe->next_odm_pipe;
2615 		}
2616 	}
2617 }
2618 
dcn20_enable_stream_gating(struct dc * dc,struct pipe_ctx * pipe_ctx)2619 void dcn20_enable_stream_gating(struct dc *dc, struct pipe_ctx *pipe_ctx)
2620 {
2621 	struct dce_hwseq *hws = dc->hwseq;
2622 
2623 	if (pipe_ctx->stream_res.dsc) {
2624 		struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
2625 
2626 		hws->funcs.dsc_pg_control(hws, pipe_ctx->stream_res.dsc->inst, false);
2627 		while (odm_pipe) {
2628 			hws->funcs.dsc_pg_control(hws, odm_pipe->stream_res.dsc->inst, false);
2629 			odm_pipe = odm_pipe->next_odm_pipe;
2630 		}
2631 	}
2632 }
2633 
dcn20_set_dmdata_attributes(struct pipe_ctx * pipe_ctx)2634 void dcn20_set_dmdata_attributes(struct pipe_ctx *pipe_ctx)
2635 {
2636 	struct dc_dmdata_attributes attr = { 0 };
2637 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
2638 
2639 	attr.dmdata_mode = DMDATA_HW_MODE;
2640 	attr.dmdata_size =
2641 		dc_is_hdmi_signal(pipe_ctx->stream->signal) ? 32 : 36;
2642 	attr.address.quad_part =
2643 			pipe_ctx->stream->dmdata_address.quad_part;
2644 	attr.dmdata_dl_delta = 0;
2645 	attr.dmdata_qos_mode = 0;
2646 	attr.dmdata_qos_level = 0;
2647 	attr.dmdata_repeat = 1; /* always repeat */
2648 	attr.dmdata_updated = 1;
2649 	attr.dmdata_sw_data = NULL;
2650 
2651 	hubp->funcs->dmdata_set_attributes(hubp, &attr);
2652 }
2653 
dcn20_init_vm_ctx(struct dce_hwseq * hws,struct dc * dc,struct dc_virtual_addr_space_config * va_config,int vmid)2654 void dcn20_init_vm_ctx(
2655 		struct dce_hwseq *hws,
2656 		struct dc *dc,
2657 		struct dc_virtual_addr_space_config *va_config,
2658 		int vmid)
2659 {
2660 	struct dcn_hubbub_virt_addr_config config;
2661 
2662 	if (vmid == 0) {
2663 		ASSERT(0); /* VMID cannot be 0 for vm context */
2664 		return;
2665 	}
2666 
2667 	config.page_table_start_addr = va_config->page_table_start_addr;
2668 	config.page_table_end_addr = va_config->page_table_end_addr;
2669 	config.page_table_block_size = va_config->page_table_block_size_in_bytes;
2670 	config.page_table_depth = va_config->page_table_depth;
2671 	config.page_table_base_addr = va_config->page_table_base_addr;
2672 
2673 	dc->res_pool->hubbub->funcs->init_vm_ctx(dc->res_pool->hubbub, &config, vmid);
2674 }
2675 
dcn20_init_sys_ctx(struct dce_hwseq * hws,struct dc * dc,struct dc_phy_addr_space_config * pa_config)2676 int dcn20_init_sys_ctx(struct dce_hwseq *hws, struct dc *dc, struct dc_phy_addr_space_config *pa_config)
2677 {
2678 	struct dcn_hubbub_phys_addr_config config;
2679 
2680 	config.system_aperture.fb_top = pa_config->system_aperture.fb_top;
2681 	config.system_aperture.fb_offset = pa_config->system_aperture.fb_offset;
2682 	config.system_aperture.fb_base = pa_config->system_aperture.fb_base;
2683 	config.system_aperture.agp_top = pa_config->system_aperture.agp_top;
2684 	config.system_aperture.agp_bot = pa_config->system_aperture.agp_bot;
2685 	config.system_aperture.agp_base = pa_config->system_aperture.agp_base;
2686 	config.gart_config.page_table_start_addr = pa_config->gart_config.page_table_start_addr;
2687 	config.gart_config.page_table_end_addr = pa_config->gart_config.page_table_end_addr;
2688 	config.gart_config.page_table_base_addr = pa_config->gart_config.page_table_base_addr;
2689 	config.page_table_default_page_addr = pa_config->page_table_default_page_addr;
2690 
2691 	return dc->res_pool->hubbub->funcs->init_dchub_sys_ctx(dc->res_pool->hubbub, &config);
2692 }
2693 
patch_address_for_sbs_tb_stereo(struct pipe_ctx * pipe_ctx,PHYSICAL_ADDRESS_LOC * addr)2694 static bool patch_address_for_sbs_tb_stereo(
2695 		struct pipe_ctx *pipe_ctx, PHYSICAL_ADDRESS_LOC *addr)
2696 {
2697 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
2698 	bool sec_split = pipe_ctx->top_pipe &&
2699 			pipe_ctx->top_pipe->plane_state == pipe_ctx->plane_state;
2700 	if (sec_split && plane_state->address.type == PLN_ADDR_TYPE_GRPH_STEREO &&
2701 			(pipe_ctx->stream->timing.timing_3d_format ==
2702 			TIMING_3D_FORMAT_SIDE_BY_SIDE ||
2703 			pipe_ctx->stream->timing.timing_3d_format ==
2704 			TIMING_3D_FORMAT_TOP_AND_BOTTOM)) {
2705 		*addr = plane_state->address.grph_stereo.left_addr;
2706 		plane_state->address.grph_stereo.left_addr =
2707 				plane_state->address.grph_stereo.right_addr;
2708 		return true;
2709 	}
2710 
2711 	if (pipe_ctx->stream->view_format != VIEW_3D_FORMAT_NONE &&
2712 			plane_state->address.type != PLN_ADDR_TYPE_GRPH_STEREO) {
2713 		plane_state->address.type = PLN_ADDR_TYPE_GRPH_STEREO;
2714 		plane_state->address.grph_stereo.right_addr =
2715 				plane_state->address.grph_stereo.left_addr;
2716 		plane_state->address.grph_stereo.right_meta_addr =
2717 				plane_state->address.grph_stereo.left_meta_addr;
2718 	}
2719 	return false;
2720 }
2721 
dcn20_update_plane_addr(const struct dc * dc,struct pipe_ctx * pipe_ctx)2722 void dcn20_update_plane_addr(const struct dc *dc, struct pipe_ctx *pipe_ctx)
2723 {
2724 	bool addr_patched = false;
2725 	PHYSICAL_ADDRESS_LOC addr;
2726 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
2727 
2728 	if (plane_state == NULL)
2729 		return;
2730 
2731 	addr_patched = patch_address_for_sbs_tb_stereo(pipe_ctx, &addr);
2732 
2733 	// Call Helper to track VMID use
2734 	vm_helper_mark_vmid_used(dc->vm_helper, plane_state->address.vmid, pipe_ctx->plane_res.hubp->inst);
2735 
2736 	pipe_ctx->plane_res.hubp->funcs->hubp_program_surface_flip_and_addr(
2737 			pipe_ctx->plane_res.hubp,
2738 			&plane_state->address,
2739 			plane_state->flip_immediate);
2740 
2741 	plane_state->status.requested_address = plane_state->address;
2742 
2743 	if (plane_state->flip_immediate)
2744 		plane_state->status.current_address = plane_state->address;
2745 
2746 	if (addr_patched)
2747 		pipe_ctx->plane_state->address.grph_stereo.left_addr = addr;
2748 }
2749 
dcn20_unblank_stream(struct pipe_ctx * pipe_ctx,struct dc_link_settings * link_settings)2750 void dcn20_unblank_stream(struct pipe_ctx *pipe_ctx,
2751 		struct dc_link_settings *link_settings)
2752 {
2753 	struct encoder_unblank_param params = {0};
2754 	struct dc_stream_state *stream = pipe_ctx->stream;
2755 	struct dc_link *link = stream->link;
2756 	struct dce_hwseq *hws = link->dc->hwseq;
2757 	struct pipe_ctx *odm_pipe;
2758 	bool is_two_pixels_per_container =
2759 			pipe_ctx->stream_res.tg->funcs->is_two_pixels_per_container(&stream->timing);
2760 
2761 	params.opp_cnt = 1;
2762 
2763 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
2764 		params.opp_cnt++;
2765 	}
2766 	/* only 3 items below are used by unblank */
2767 	params.timing = pipe_ctx->stream->timing;
2768 
2769 	params.link_settings.link_rate = link_settings->link_rate;
2770 
2771 	if (link->dc->link_srv->dp_is_128b_132b_signal(pipe_ctx)) {
2772 		/* TODO - DP2.0 HW: Set ODM mode in dp hpo encoder here */
2773 		pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_unblank(
2774 				pipe_ctx->stream_res.hpo_dp_stream_enc,
2775 				pipe_ctx->stream_res.tg->inst);
2776 	} else if (dc_is_dp_signal(pipe_ctx->stream->signal)) {
2777 		if (is_two_pixels_per_container || params.opp_cnt > 1)
2778 			params.timing.pix_clk_100hz /= 2;
2779 		if (pipe_ctx->stream_res.stream_enc->funcs->dp_set_odm_combine)
2780 			pipe_ctx->stream_res.stream_enc->funcs->dp_set_odm_combine(
2781 				pipe_ctx->stream_res.stream_enc, params.opp_cnt > 1);
2782 		pipe_ctx->stream_res.stream_enc->funcs->dp_unblank(link, pipe_ctx->stream_res.stream_enc, &params);
2783 	}
2784 
2785 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
2786 		hws->funcs.edp_backlight_control(link, true);
2787 	}
2788 }
2789 
dcn20_setup_vupdate_interrupt(struct dc * dc,struct pipe_ctx * pipe_ctx)2790 void dcn20_setup_vupdate_interrupt(struct dc *dc, struct pipe_ctx *pipe_ctx)
2791 {
2792 	struct timing_generator *tg = pipe_ctx->stream_res.tg;
2793 	int start_line = dc->hwss.get_vupdate_offset_from_vsync(pipe_ctx);
2794 
2795 	if (start_line < 0)
2796 		start_line = 0;
2797 
2798 	if (tg->funcs->setup_vertical_interrupt2)
2799 		tg->funcs->setup_vertical_interrupt2(tg, start_line);
2800 }
2801 
dcn20_reset_back_end_for_pipe(struct dc * dc,struct pipe_ctx * pipe_ctx,struct dc_state * context)2802 void dcn20_reset_back_end_for_pipe(
2803 		struct dc *dc,
2804 		struct pipe_ctx *pipe_ctx,
2805 		struct dc_state *context)
2806 {
2807 	struct dc_link *link = pipe_ctx->stream->link;
2808 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
2809 
2810 	DC_LOGGER_INIT(dc->ctx->logger);
2811 	if (pipe_ctx->stream_res.stream_enc == NULL) {
2812 		pipe_ctx->stream = NULL;
2813 		return;
2814 	}
2815 
2816 	/* DPMS may already disable or */
2817 	/* dpms_off status is incorrect due to fastboot
2818 	 * feature. When system resume from S4 with second
2819 	 * screen only, the dpms_off would be true but
2820 	 * VBIOS lit up eDP, so check link status too.
2821 	 */
2822 	if (!pipe_ctx->stream->dpms_off || link->link_status.link_active)
2823 		dc->link_srv->set_dpms_off(pipe_ctx);
2824 	else if (pipe_ctx->stream_res.audio)
2825 		dc->hwss.disable_audio_stream(pipe_ctx);
2826 
2827 	/* free acquired resources */
2828 	if (pipe_ctx->stream_res.audio) {
2829 		/*disable az_endpoint*/
2830 		pipe_ctx->stream_res.audio->funcs->az_disable(pipe_ctx->stream_res.audio);
2831 
2832 		/*free audio*/
2833 		if (dc->caps.dynamic_audio == true) {
2834 			/*we have to dynamic arbitrate the audio endpoints*/
2835 			/*we free the resource, need reset is_audio_acquired*/
2836 			update_audio_usage(&dc->current_state->res_ctx, dc->res_pool,
2837 					pipe_ctx->stream_res.audio, false);
2838 			pipe_ctx->stream_res.audio = NULL;
2839 		}
2840 	}
2841 
2842 	/* by upper caller loop, parent pipe: pipe0, will be reset last.
2843 	 * back end share by all pipes and will be disable only when disable
2844 	 * parent pipe.
2845 	 */
2846 	if (pipe_ctx->top_pipe == NULL) {
2847 
2848 		dc->hwss.set_abm_immediate_disable(pipe_ctx);
2849 
2850 		pipe_ctx->stream_res.tg->funcs->disable_crtc(pipe_ctx->stream_res.tg);
2851 
2852 		pipe_ctx->stream_res.tg->funcs->enable_optc_clock(pipe_ctx->stream_res.tg, false);
2853 		if (pipe_ctx->stream_res.tg->funcs->set_odm_bypass)
2854 			pipe_ctx->stream_res.tg->funcs->set_odm_bypass(
2855 					pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
2856 
2857 		set_drr_and_clear_adjust_pending(pipe_ctx, pipe_ctx->stream, NULL);
2858 		/* TODO - convert symclk_ref_cnts for otg to a bit map to solve
2859 		 * the case where the same symclk is shared across multiple otg
2860 		 * instances
2861 		 */
2862 		if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal))
2863 			link->phy_state.symclk_ref_cnts.otg = 0;
2864 		if (link->phy_state.symclk_state == SYMCLK_ON_TX_OFF) {
2865 			link_hwss->disable_link_output(link,
2866 					&pipe_ctx->link_res, pipe_ctx->stream->signal);
2867 			link->phy_state.symclk_state = SYMCLK_OFF_TX_OFF;
2868 		}
2869 	}
2870 
2871 /*
2872  * In case of a dangling plane, setting this to NULL unconditionally
2873  * causes failures during reset hw ctx where, if stream is NULL,
2874  * it is expected that the pipe_ctx pointers to pipes and plane are NULL.
2875  */
2876 	pipe_ctx->stream = NULL;
2877 	pipe_ctx->top_pipe = NULL;
2878 	pipe_ctx->bottom_pipe = NULL;
2879 	pipe_ctx->next_odm_pipe = NULL;
2880 	pipe_ctx->prev_odm_pipe = NULL;
2881 	DC_LOG_DEBUG("Reset back end for pipe %d, tg:%d\n",
2882 					pipe_ctx->pipe_idx, pipe_ctx->stream_res.tg->inst);
2883 }
2884 
dcn20_reset_hw_ctx_wrap(struct dc * dc,struct dc_state * context)2885 void dcn20_reset_hw_ctx_wrap(
2886 		struct dc *dc,
2887 		struct dc_state *context)
2888 {
2889 	int i;
2890 	struct dce_hwseq *hws = dc->hwseq;
2891 
2892 	/* Reset Back End*/
2893 	for (i = dc->res_pool->pipe_count - 1; i >= 0 ; i--) {
2894 		struct pipe_ctx *pipe_ctx_old =
2895 			&dc->current_state->res_ctx.pipe_ctx[i];
2896 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2897 
2898 		if (!pipe_ctx_old->stream)
2899 			continue;
2900 
2901 		if (pipe_ctx_old->top_pipe || pipe_ctx_old->prev_odm_pipe)
2902 			continue;
2903 
2904 		if (!pipe_ctx->stream ||
2905 				pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
2906 			struct clock_source *old_clk = pipe_ctx_old->clock_source;
2907 
2908 			dcn20_reset_back_end_for_pipe(dc, pipe_ctx_old, dc->current_state);
2909 			if (hws->funcs.enable_stream_gating)
2910 				hws->funcs.enable_stream_gating(dc, pipe_ctx_old);
2911 			if (old_clk)
2912 				old_clk->funcs->cs_power_down(old_clk);
2913 		}
2914 	}
2915 }
2916 
dcn20_update_mpcc(struct dc * dc,struct pipe_ctx * pipe_ctx)2917 void dcn20_update_mpcc(struct dc *dc, struct pipe_ctx *pipe_ctx)
2918 {
2919 	struct hubp *hubp = pipe_ctx->plane_res.hubp;
2920 	struct mpcc_blnd_cfg blnd_cfg = {0};
2921 	bool per_pixel_alpha = pipe_ctx->plane_state->per_pixel_alpha;
2922 	int mpcc_id;
2923 	struct mpcc *new_mpcc;
2924 	struct mpc *mpc = dc->res_pool->mpc;
2925 	struct mpc_tree *mpc_tree_params = &(pipe_ctx->stream_res.opp->mpc_tree_params);
2926 
2927 	blnd_cfg.overlap_only = false;
2928 	blnd_cfg.global_gain = 0xff;
2929 
2930 	if (per_pixel_alpha) {
2931 		blnd_cfg.pre_multiplied_alpha = pipe_ctx->plane_state->pre_multiplied_alpha;
2932 		if (pipe_ctx->plane_state->global_alpha) {
2933 			blnd_cfg.alpha_mode = MPCC_ALPHA_BLEND_MODE_PER_PIXEL_ALPHA_COMBINED_GLOBAL_GAIN;
2934 			blnd_cfg.global_gain = pipe_ctx->plane_state->global_alpha_value;
2935 		} else {
2936 			blnd_cfg.alpha_mode = MPCC_ALPHA_BLEND_MODE_PER_PIXEL_ALPHA;
2937 		}
2938 	} else {
2939 		blnd_cfg.pre_multiplied_alpha = false;
2940 		blnd_cfg.alpha_mode = MPCC_ALPHA_BLEND_MODE_GLOBAL_ALPHA;
2941 	}
2942 
2943 	if (pipe_ctx->plane_state->global_alpha)
2944 		blnd_cfg.global_alpha = pipe_ctx->plane_state->global_alpha_value;
2945 	else
2946 		blnd_cfg.global_alpha = 0xff;
2947 
2948 	blnd_cfg.background_color_bpc = 4;
2949 	blnd_cfg.bottom_gain_mode = 0;
2950 	blnd_cfg.top_gain = 0x1f000;
2951 	blnd_cfg.bottom_inside_gain = 0x1f000;
2952 	blnd_cfg.bottom_outside_gain = 0x1f000;
2953 
2954 	if (pipe_ctx->plane_state->format
2955 			== SURFACE_PIXEL_FORMAT_GRPH_RGBE_ALPHA)
2956 		blnd_cfg.pre_multiplied_alpha = false;
2957 
2958 	/*
2959 	 * TODO: remove hack
2960 	 * Note: currently there is a bug in init_hw such that
2961 	 * on resume from hibernate, BIOS sets up MPCC0, and
2962 	 * we do mpcc_remove but the mpcc cannot go to idle
2963 	 * after remove. This cause us to pick mpcc1 here,
2964 	 * which causes a pstate hang for yet unknown reason.
2965 	 */
2966 	mpcc_id = hubp->inst;
2967 
2968 	/* If there is no full update, don't need to touch MPC tree*/
2969 	if (!pipe_ctx->plane_state->update_flags.bits.full_update &&
2970 		!pipe_ctx->update_flags.bits.mpcc) {
2971 		mpc->funcs->update_blending(mpc, &blnd_cfg, mpcc_id);
2972 		dc->hwss.update_visual_confirm_color(dc, pipe_ctx, mpcc_id);
2973 		return;
2974 	}
2975 
2976 	/* check if this MPCC is already being used */
2977 	new_mpcc = mpc->funcs->get_mpcc_for_dpp(mpc_tree_params, mpcc_id);
2978 	/* remove MPCC if being used */
2979 	if (new_mpcc != NULL)
2980 		mpc->funcs->remove_mpcc(mpc, mpc_tree_params, new_mpcc);
2981 	else
2982 		if (dc->debug.sanity_checks)
2983 			mpc->funcs->assert_mpcc_idle_before_connect(
2984 					dc->res_pool->mpc, mpcc_id);
2985 
2986 	/* Call MPC to insert new plane */
2987 	new_mpcc = mpc->funcs->insert_plane(dc->res_pool->mpc,
2988 			mpc_tree_params,
2989 			&blnd_cfg,
2990 			NULL,
2991 			NULL,
2992 			hubp->inst,
2993 			mpcc_id);
2994 	dc->hwss.update_visual_confirm_color(dc, pipe_ctx, mpcc_id);
2995 
2996 	ASSERT(new_mpcc != NULL);
2997 	hubp->opp_id = pipe_ctx->stream_res.opp->inst;
2998 	hubp->mpcc_id = mpcc_id;
2999 }
3000 
dcn20_enable_stream(struct pipe_ctx * pipe_ctx)3001 void dcn20_enable_stream(struct pipe_ctx *pipe_ctx)
3002 {
3003 	enum dc_lane_count lane_count =
3004 		pipe_ctx->stream->link->cur_link_settings.lane_count;
3005 
3006 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
3007 	struct dc_link *link = pipe_ctx->stream->link;
3008 
3009 	uint32_t active_total_with_borders;
3010 	uint32_t early_control = 0;
3011 	struct timing_generator *tg = pipe_ctx->stream_res.tg;
3012 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
3013 	struct dc *dc = pipe_ctx->stream->ctx->dc;
3014 	struct dtbclk_dto_params dto_params = {0};
3015 	struct dccg *dccg = dc->res_pool->dccg;
3016 	enum phyd32clk_clock_source phyd32clk;
3017 	int dp_hpo_inst;
3018 
3019 	struct link_encoder *link_enc = pipe_ctx->link_res.dio_link_enc;
3020 	struct stream_encoder *stream_enc = pipe_ctx->stream_res.stream_enc;
3021 
3022 	if (!dc->config.unify_link_enc_assignment)
3023 		link_enc = link_enc_cfg_get_link_enc(link);
3024 
3025 	if (dc->link_srv->dp_is_128b_132b_signal(pipe_ctx)) {
3026 		dto_params.otg_inst = tg->inst;
3027 		dto_params.pixclk_khz = pipe_ctx->stream->timing.pix_clk_100hz / 10;
3028 		dto_params.num_odm_segments = get_odm_segment_count(pipe_ctx);
3029 		dto_params.timing = &pipe_ctx->stream->timing;
3030 		dto_params.ref_dtbclk_khz = dc->clk_mgr->funcs->get_dtb_ref_clk_frequency(dc->clk_mgr);
3031 		dccg->funcs->set_dtbclk_dto(dccg, &dto_params);
3032 		dp_hpo_inst = pipe_ctx->stream_res.hpo_dp_stream_enc->inst;
3033 		dccg->funcs->set_dpstreamclk(dccg, DTBCLK0, tg->inst, dp_hpo_inst);
3034 
3035 		phyd32clk = get_phyd32clk_src(link);
3036 		if (link->cur_link_settings.link_rate == LINK_RATE_UNKNOWN) {
3037 			dccg->funcs->disable_symclk32_se(dccg, dp_hpo_inst);
3038 		} else {
3039 			dccg->funcs->enable_symclk32_se(dccg, dp_hpo_inst, phyd32clk);
3040 		}
3041 	} else {
3042 		if (dccg->funcs->enable_symclk_se)
3043 			dccg->funcs->enable_symclk_se(dccg, stream_enc->stream_enc_inst,
3044 						      link_enc->transmitter - TRANSMITTER_UNIPHY_A);
3045 	}
3046 
3047 	if (dc->res_pool->dccg->funcs->set_pixel_rate_div)
3048 		dc->res_pool->dccg->funcs->set_pixel_rate_div(
3049 			dc->res_pool->dccg,
3050 			pipe_ctx->stream_res.tg->inst,
3051 			pipe_ctx->pixel_rate_divider.div_factor1,
3052 			pipe_ctx->pixel_rate_divider.div_factor2);
3053 
3054 	link_hwss->setup_stream_encoder(pipe_ctx);
3055 
3056 	if (pipe_ctx->plane_state && pipe_ctx->plane_state->flip_immediate != 1) {
3057 		if (dc->hwss.program_dmdata_engine)
3058 			dc->hwss.program_dmdata_engine(pipe_ctx);
3059 	}
3060 
3061 	dc->hwss.update_info_frame(pipe_ctx);
3062 
3063 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
3064 		dc->link_srv->dp_trace_source_sequence(link, DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
3065 
3066 	/* enable early control to avoid corruption on DP monitor*/
3067 	active_total_with_borders =
3068 			timing->h_addressable
3069 				+ timing->h_border_left
3070 				+ timing->h_border_right;
3071 
3072 	if (lane_count != 0)
3073 		early_control = active_total_with_borders % lane_count;
3074 
3075 	if (early_control == 0)
3076 		early_control = lane_count;
3077 
3078 	tg->funcs->set_early_control(tg, early_control);
3079 }
3080 
dcn20_program_dmdata_engine(struct pipe_ctx * pipe_ctx)3081 void dcn20_program_dmdata_engine(struct pipe_ctx *pipe_ctx)
3082 {
3083 	struct dc_stream_state    *stream     = pipe_ctx->stream;
3084 	struct hubp               *hubp       = pipe_ctx->plane_res.hubp;
3085 	bool                       enable     = false;
3086 	struct stream_encoder     *stream_enc = pipe_ctx->stream_res.stream_enc;
3087 	enum dynamic_metadata_mode mode       = dc_is_dp_signal(stream->signal)
3088 							? dmdata_dp
3089 							: dmdata_hdmi;
3090 
3091 	/* if using dynamic meta, don't set up generic infopackets */
3092 	if (pipe_ctx->stream->dmdata_address.quad_part != 0) {
3093 		pipe_ctx->stream_res.encoder_info_frame.hdrsmd.valid = false;
3094 		enable = true;
3095 	}
3096 
3097 	if (!hubp)
3098 		return;
3099 
3100 	if (!stream_enc || !stream_enc->funcs->set_dynamic_metadata)
3101 		return;
3102 
3103 	stream_enc->funcs->set_dynamic_metadata(stream_enc, enable,
3104 						hubp->inst, mode);
3105 }
3106 
dcn20_fpga_init_hw(struct dc * dc)3107 void dcn20_fpga_init_hw(struct dc *dc)
3108 {
3109 	int i, j;
3110 	struct dce_hwseq *hws = dc->hwseq;
3111 	struct resource_pool *res_pool = dc->res_pool;
3112 	struct dc_state  *context = dc->current_state;
3113 
3114 	if (dc->clk_mgr && dc->clk_mgr->funcs->init_clocks)
3115 		dc->clk_mgr->funcs->init_clocks(dc->clk_mgr);
3116 
3117 	// Initialize the dccg
3118 	if (res_pool->dccg->funcs->dccg_init)
3119 		res_pool->dccg->funcs->dccg_init(res_pool->dccg);
3120 
3121 	//Enable ability to power gate / don't force power on permanently
3122 	hws->funcs.enable_power_gating_plane(hws, true);
3123 
3124 	// Specific to FPGA dccg and registers
3125 	REG_WRITE(RBBMIF_TIMEOUT_DIS, 0xFFFFFFFF);
3126 	REG_WRITE(RBBMIF_TIMEOUT_DIS_2, 0xFFFFFFFF);
3127 
3128 	hws->funcs.dccg_init(hws);
3129 
3130 	REG_UPDATE(DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_REFDIV, 2);
3131 	REG_UPDATE(DCHUBBUB_GLOBAL_TIMER_CNTL, DCHUBBUB_GLOBAL_TIMER_ENABLE, 1);
3132 	if (REG(REFCLK_CNTL))
3133 		REG_WRITE(REFCLK_CNTL, 0);
3134 	//
3135 
3136 
3137 	/* Blank pixel data with OPP DPG */
3138 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
3139 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
3140 
3141 		if (tg->funcs->is_tg_enabled(tg))
3142 			dcn20_init_blank(dc, tg);
3143 	}
3144 
3145 	for (i = 0; i < res_pool->timing_generator_count; i++) {
3146 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
3147 
3148 		if (tg->funcs->is_tg_enabled(tg))
3149 			tg->funcs->lock(tg);
3150 	}
3151 
3152 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3153 		struct dpp *dpp = res_pool->dpps[i];
3154 
3155 		dpp->funcs->dpp_reset(dpp);
3156 	}
3157 
3158 	/* Reset all MPCC muxes */
3159 	res_pool->mpc->funcs->mpc_init(res_pool->mpc);
3160 
3161 	/* initialize OPP mpc_tree parameter */
3162 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3163 		res_pool->opps[i]->mpc_tree_params.opp_id = res_pool->opps[i]->inst;
3164 		res_pool->opps[i]->mpc_tree_params.opp_list = NULL;
3165 		for (j = 0; j < MAX_PIPES; j++)
3166 			res_pool->opps[i]->mpcc_disconnect_pending[j] = false;
3167 	}
3168 
3169 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3170 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
3171 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
3172 		struct hubp *hubp = dc->res_pool->hubps[i];
3173 		struct dpp *dpp = dc->res_pool->dpps[i];
3174 
3175 		pipe_ctx->stream_res.tg = tg;
3176 		pipe_ctx->pipe_idx = i;
3177 
3178 		pipe_ctx->plane_res.hubp = hubp;
3179 		pipe_ctx->plane_res.dpp = dpp;
3180 		pipe_ctx->plane_res.mpcc_inst = dpp->inst;
3181 		hubp->mpcc_id = dpp->inst;
3182 		hubp->opp_id = OPP_ID_INVALID;
3183 		hubp->power_gated = false;
3184 		pipe_ctx->stream_res.opp = NULL;
3185 
3186 		hubp->funcs->hubp_init(hubp);
3187 
3188 		//dc->res_pool->opps[i]->mpc_tree_params.opp_id = dc->res_pool->opps[i]->inst;
3189 		//dc->res_pool->opps[i]->mpc_tree_params.opp_list = NULL;
3190 		dc->res_pool->opps[i]->mpcc_disconnect_pending[pipe_ctx->plane_res.mpcc_inst] = true;
3191 		pipe_ctx->stream_res.opp = dc->res_pool->opps[i];
3192 		/*to do*/
3193 		hws->funcs.plane_atomic_disconnect(dc, context, pipe_ctx);
3194 	}
3195 
3196 	/* initialize DWB pointer to MCIF_WB */
3197 	for (i = 0; i < res_pool->res_cap->num_dwb; i++)
3198 		res_pool->dwbc[i]->mcif = res_pool->mcif_wb[i];
3199 
3200 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
3201 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
3202 
3203 		if (tg->funcs->is_tg_enabled(tg))
3204 			tg->funcs->unlock(tg);
3205 	}
3206 
3207 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3208 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
3209 
3210 		dc->hwss.disable_plane(dc, context, pipe_ctx);
3211 
3212 		pipe_ctx->stream_res.tg = NULL;
3213 		pipe_ctx->plane_res.hubp = NULL;
3214 	}
3215 
3216 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
3217 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
3218 
3219 		tg->funcs->tg_init(tg);
3220 	}
3221 
3222 	if (dc->res_pool->hubbub->funcs->init_crb)
3223 		dc->res_pool->hubbub->funcs->init_crb(dc->res_pool->hubbub);
3224 }
3225 
dcn20_set_disp_pattern_generator(const struct dc * dc,struct pipe_ctx * pipe_ctx,enum controller_dp_test_pattern test_pattern,enum controller_dp_color_space color_space,enum dc_color_depth color_depth,const struct tg_color * solid_color,int width,int height,int offset)3226 void dcn20_set_disp_pattern_generator(const struct dc *dc,
3227 		struct pipe_ctx *pipe_ctx,
3228 		enum controller_dp_test_pattern test_pattern,
3229 		enum controller_dp_color_space color_space,
3230 		enum dc_color_depth color_depth,
3231 		const struct tg_color *solid_color,
3232 		int width, int height, int offset)
3233 {
3234 	pipe_ctx->stream_res.opp->funcs->opp_set_disp_pattern_generator(pipe_ctx->stream_res.opp, test_pattern,
3235 			color_space, color_depth, solid_color, width, height, offset);
3236 }
3237