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