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