xref: /openbmc/linux/drivers/gpu/drm/amd/display/dc/dcn32/dcn32_hwseq.c (revision d5a43956b73bd7835ea8a6ab1516ccdb853c2d34)
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 
26 
27 #include "dm_services.h"
28 #include "dm_helpers.h"
29 #include "core_types.h"
30 #include "resource.h"
31 #include "dccg.h"
32 #include "dce/dce_hwseq.h"
33 #include "dcn30/dcn30_cm_common.h"
34 #include "reg_helper.h"
35 #include "abm.h"
36 #include "hubp.h"
37 #include "dchubbub.h"
38 #include "timing_generator.h"
39 #include "opp.h"
40 #include "ipp.h"
41 #include "mpc.h"
42 #include "mcif_wb.h"
43 #include "dc_dmub_srv.h"
44 #include "link_hwss.h"
45 #include "dpcd_defs.h"
46 #include "dcn32_hwseq.h"
47 #include "clk_mgr.h"
48 #include "dsc.h"
49 #include "dcn20/dcn20_optc.h"
50 #include "dmub_subvp_state.h"
51 #include "dce/dmub_hw_lock_mgr.h"
52 #include "dcn32_resource.h"
53 #include "link.h"
54 #include "dc_link_dp.h"
55 #include "dmub/inc/dmub_subvp_state.h"
56 
57 #define DC_LOGGER_INIT(logger)
58 
59 #define CTX \
60 	hws->ctx
61 #define REG(reg)\
62 	hws->regs->reg
63 #define DC_LOGGER \
64 		dc->ctx->logger
65 
66 
67 #undef FN
68 #define FN(reg_name, field_name) \
69 	hws->shifts->field_name, hws->masks->field_name
70 
71 void dcn32_dsc_pg_control(
72 		struct dce_hwseq *hws,
73 		unsigned int dsc_inst,
74 		bool power_on)
75 {
76 	uint32_t power_gate = power_on ? 0 : 1;
77 	uint32_t pwr_status = power_on ? 0 : 2;
78 	uint32_t org_ip_request_cntl = 0;
79 
80 	if (hws->ctx->dc->debug.disable_dsc_power_gate)
81 		return;
82 
83 	REG_GET(DC_IP_REQUEST_CNTL, IP_REQUEST_EN, &org_ip_request_cntl);
84 	if (org_ip_request_cntl == 0)
85 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 1);
86 
87 	switch (dsc_inst) {
88 	case 0: /* DSC0 */
89 		REG_UPDATE(DOMAIN16_PG_CONFIG,
90 				DOMAIN_POWER_GATE, power_gate);
91 
92 		REG_WAIT(DOMAIN16_PG_STATUS,
93 				DOMAIN_PGFSM_PWR_STATUS, pwr_status,
94 				1, 1000);
95 		break;
96 	case 1: /* DSC1 */
97 		REG_UPDATE(DOMAIN17_PG_CONFIG,
98 				DOMAIN_POWER_GATE, power_gate);
99 
100 		REG_WAIT(DOMAIN17_PG_STATUS,
101 				DOMAIN_PGFSM_PWR_STATUS, pwr_status,
102 				1, 1000);
103 		break;
104 	case 2: /* DSC2 */
105 		REG_UPDATE(DOMAIN18_PG_CONFIG,
106 				DOMAIN_POWER_GATE, power_gate);
107 
108 		REG_WAIT(DOMAIN18_PG_STATUS,
109 				DOMAIN_PGFSM_PWR_STATUS, pwr_status,
110 				1, 1000);
111 		break;
112 	case 3: /* DSC3 */
113 		REG_UPDATE(DOMAIN19_PG_CONFIG,
114 				DOMAIN_POWER_GATE, power_gate);
115 
116 		REG_WAIT(DOMAIN19_PG_STATUS,
117 				DOMAIN_PGFSM_PWR_STATUS, pwr_status,
118 				1, 1000);
119 		break;
120 	default:
121 		BREAK_TO_DEBUGGER();
122 		break;
123 	}
124 
125 	if (org_ip_request_cntl == 0)
126 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 0);
127 }
128 
129 
130 void dcn32_enable_power_gating_plane(
131 	struct dce_hwseq *hws,
132 	bool enable)
133 {
134 	bool force_on = true; /* disable power gating */
135 
136 	if (enable)
137 		force_on = false;
138 
139 	/* DCHUBP0/1/2/3 */
140 	REG_UPDATE(DOMAIN0_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
141 	REG_UPDATE(DOMAIN1_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
142 	REG_UPDATE(DOMAIN2_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
143 	REG_UPDATE(DOMAIN3_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
144 
145 	/* DCS0/1/2/3 */
146 	REG_UPDATE(DOMAIN16_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
147 	REG_UPDATE(DOMAIN17_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
148 	REG_UPDATE(DOMAIN18_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
149 	REG_UPDATE(DOMAIN19_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
150 }
151 
152 void dcn32_hubp_pg_control(struct dce_hwseq *hws, unsigned int hubp_inst, bool power_on)
153 {
154 	uint32_t power_gate = power_on ? 0 : 1;
155 	uint32_t pwr_status = power_on ? 0 : 2;
156 
157 	if (hws->ctx->dc->debug.disable_hubp_power_gate)
158 		return;
159 
160 	if (REG(DOMAIN0_PG_CONFIG) == 0)
161 		return;
162 
163 	switch (hubp_inst) {
164 	case 0:
165 		REG_SET(DOMAIN0_PG_CONFIG, 0, DOMAIN_POWER_GATE, power_gate);
166 		REG_WAIT(DOMAIN0_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, pwr_status, 1, 1000);
167 		break;
168 	case 1:
169 		REG_SET(DOMAIN1_PG_CONFIG, 0, DOMAIN_POWER_GATE, power_gate);
170 		REG_WAIT(DOMAIN1_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, pwr_status, 1, 1000);
171 		break;
172 	case 2:
173 		REG_SET(DOMAIN2_PG_CONFIG, 0, DOMAIN_POWER_GATE, power_gate);
174 		REG_WAIT(DOMAIN2_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, pwr_status, 1, 1000);
175 		break;
176 	case 3:
177 		REG_SET(DOMAIN3_PG_CONFIG, 0, DOMAIN_POWER_GATE, power_gate);
178 		REG_WAIT(DOMAIN3_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, pwr_status, 1, 1000);
179 		break;
180 	default:
181 		BREAK_TO_DEBUGGER();
182 		break;
183 	}
184 }
185 
186 static bool dcn32_check_no_memory_request_for_cab(struct dc *dc)
187 {
188 	int i;
189 
190     /* First, check no-memory-request case */
191 	for (i = 0; i < dc->current_state->stream_count; i++) {
192 		if ((dc->current_state->stream_status[i].plane_count) &&
193 			(dc->current_state->streams[i]->link->psr_settings.psr_version == DC_PSR_VERSION_UNSUPPORTED))
194 			/* Fail eligibility on a visible stream */
195 			break;
196 	}
197 
198 	if (i == dc->current_state->stream_count)
199 		return true;
200 
201 	return false;
202 }
203 
204 
205 /* This function loops through every surface that needs to be cached in CAB for SS,
206  * and calculates the total number of ways required to store all surfaces (primary,
207  * meta, cursor).
208  */
209 static uint32_t dcn32_calculate_cab_allocation(struct dc *dc, struct dc_state *ctx)
210 {
211 	int i;
212 	uint8_t num_ways = 0;
213 	uint32_t mall_ss_size_bytes = 0;
214 
215 	mall_ss_size_bytes = ctx->bw_ctx.bw.dcn.mall_ss_size_bytes;
216 	// TODO add additional logic for PSR active stream exclusion optimization
217 	// mall_ss_psr_active_size_bytes = ctx->bw_ctx.bw.dcn.mall_ss_psr_active_size_bytes;
218 
219 	// Include cursor size for CAB allocation
220 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
221 		struct pipe_ctx *pipe = &ctx->res_ctx.pipe_ctx[i];
222 
223 		if (!pipe->stream || !pipe->plane_state)
224 			continue;
225 
226 		mall_ss_size_bytes += dcn32_helper_calculate_mall_bytes_for_cursor(dc, pipe, false);
227 	}
228 
229 	// Convert number of cache lines required to number of ways
230 	if (dc->debug.force_mall_ss_num_ways > 0) {
231 		num_ways = dc->debug.force_mall_ss_num_ways;
232 	} else {
233 		num_ways = dcn32_helper_mall_bytes_to_ways(dc, mall_ss_size_bytes);
234 	}
235 
236 	return num_ways;
237 }
238 
239 bool dcn32_apply_idle_power_optimizations(struct dc *dc, bool enable)
240 {
241 	union dmub_rb_cmd cmd;
242 	uint8_t ways, i;
243 	int j;
244 	bool mall_ss_unsupported = false;
245 	struct dc_plane_state *plane = NULL;
246 
247 	if (!dc->ctx->dmub_srv)
248 		return false;
249 
250 	if (enable) {
251 		if (dc->current_state) {
252 
253 			/* 1. Check no memory request case for CAB.
254 			 * If no memory request case, send CAB_ACTION NO_DF_REQ DMUB message
255 			 */
256 			if (dcn32_check_no_memory_request_for_cab(dc)) {
257 				/* Enable no-memory-requests case */
258 				memset(&cmd, 0, sizeof(cmd));
259 				cmd.cab.header.type = DMUB_CMD__CAB_FOR_SS;
260 				cmd.cab.header.sub_type = DMUB_CMD__CAB_NO_DCN_REQ;
261 				cmd.cab.header.payload_bytes = sizeof(cmd.cab) - sizeof(cmd.cab.header);
262 
263 				dc_dmub_srv_cmd_queue(dc->ctx->dmub_srv, &cmd);
264 				dc_dmub_srv_cmd_execute(dc->ctx->dmub_srv);
265 
266 				return true;
267 			}
268 
269 			/* 2. Check if all surfaces can fit in CAB.
270 			 * If surfaces can fit into CAB, send CAB_ACTION_ALLOW DMUB message
271 			 * and configure HUBP's to fetch from MALL
272 			 */
273 			ways = dcn32_calculate_cab_allocation(dc, dc->current_state);
274 
275 			/* MALL not supported with Stereo3D or TMZ surface. If any plane is using stereo,
276 			 * or TMZ surface, don't try to enter MALL.
277 			 */
278 			for (i = 0; i < dc->current_state->stream_count; i++) {
279 				for (j = 0; j < dc->current_state->stream_status[i].plane_count; j++) {
280 					plane = dc->current_state->stream_status[i].plane_states[j];
281 
282 					if (plane->address.type == PLN_ADDR_TYPE_GRPH_STEREO ||
283 							plane->address.tmz_surface) {
284 						mall_ss_unsupported = true;
285 						break;
286 					}
287 				}
288 				if (mall_ss_unsupported)
289 					break;
290 			}
291 			if (ways <= dc->caps.cache_num_ways && !mall_ss_unsupported) {
292 				memset(&cmd, 0, sizeof(cmd));
293 				cmd.cab.header.type = DMUB_CMD__CAB_FOR_SS;
294 				cmd.cab.header.sub_type = DMUB_CMD__CAB_DCN_SS_FIT_IN_CAB;
295 				cmd.cab.header.payload_bytes = sizeof(cmd.cab) - sizeof(cmd.cab.header);
296 				cmd.cab.cab_alloc_ways = ways;
297 
298 				dc_dmub_srv_cmd_queue(dc->ctx->dmub_srv, &cmd);
299 				dc_dmub_srv_cmd_execute(dc->ctx->dmub_srv);
300 
301 				return true;
302 			}
303 
304 		}
305 		return false;
306 	}
307 
308 	/* Disable CAB */
309 	memset(&cmd, 0, sizeof(cmd));
310 	cmd.cab.header.type = DMUB_CMD__CAB_FOR_SS;
311 	cmd.cab.header.sub_type = DMUB_CMD__CAB_NO_IDLE_OPTIMIZATION;
312 	cmd.cab.header.payload_bytes =
313 			sizeof(cmd.cab) - sizeof(cmd.cab.header);
314 
315 	dc_dmub_srv_cmd_queue(dc->ctx->dmub_srv, &cmd);
316 	dc_dmub_srv_cmd_execute(dc->ctx->dmub_srv);
317 	dc_dmub_srv_wait_idle(dc->ctx->dmub_srv);
318 
319 	return true;
320 }
321 
322 /* Send DMCUB message with SubVP pipe info
323  * - For each pipe in context, populate payload with required SubVP information
324  *   if the pipe is using SubVP for MCLK switch
325  * - This function must be called while the DMUB HW lock is acquired by driver
326  */
327 void dcn32_commit_subvp_config(struct dc *dc, struct dc_state *context)
328 {
329 	int i;
330 	bool enable_subvp = false;
331 
332 	if (!dc->ctx || !dc->ctx->dmub_srv)
333 		return;
334 
335 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
336 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
337 
338 		if (pipe_ctx->stream && pipe_ctx->stream->mall_stream_config.paired_stream &&
339 				pipe_ctx->stream->mall_stream_config.type == SUBVP_MAIN) {
340 			// There is at least 1 SubVP pipe, so enable SubVP
341 			enable_subvp = true;
342 			break;
343 		}
344 	}
345 	dc_dmub_setup_subvp_dmub_command(dc, context, enable_subvp);
346 }
347 
348 /* Sub-Viewport DMUB lock needs to be acquired by driver whenever SubVP is active and:
349  * 1. Any full update for any SubVP main pipe
350  * 2. Any immediate flip for any SubVP pipe
351  * 3. Any flip for DRR pipe
352  * 4. If SubVP was previously in use (i.e. in old context)
353  */
354 void dcn32_subvp_pipe_control_lock(struct dc *dc,
355 		struct dc_state *context,
356 		bool lock,
357 		bool should_lock_all_pipes,
358 		struct pipe_ctx *top_pipe_to_program,
359 		bool subvp_prev_use)
360 {
361 	unsigned int i = 0;
362 	bool subvp_immediate_flip = false;
363 	bool subvp_in_use = false;
364 	struct pipe_ctx *pipe;
365 
366 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
367 		pipe = &context->res_ctx.pipe_ctx[i];
368 
369 		if (pipe->stream && pipe->plane_state && pipe->stream->mall_stream_config.type == SUBVP_MAIN) {
370 			subvp_in_use = true;
371 			break;
372 		}
373 	}
374 
375 	if (top_pipe_to_program && top_pipe_to_program->stream && top_pipe_to_program->plane_state) {
376 		if (top_pipe_to_program->stream->mall_stream_config.type == SUBVP_MAIN &&
377 				top_pipe_to_program->plane_state->flip_immediate)
378 			subvp_immediate_flip = true;
379 	}
380 
381 	// Don't need to lock for DRR VSYNC flips -- FW will wait for DRR pending update cleared.
382 	if ((subvp_in_use && (should_lock_all_pipes || subvp_immediate_flip)) || (!subvp_in_use && subvp_prev_use)) {
383 		union dmub_inbox0_cmd_lock_hw hw_lock_cmd = { 0 };
384 
385 		if (!lock) {
386 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
387 				pipe = &context->res_ctx.pipe_ctx[i];
388 				if (pipe->stream && pipe->plane_state && pipe->stream->mall_stream_config.type == SUBVP_MAIN &&
389 						should_lock_all_pipes)
390 					pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VBLANK);
391 			}
392 		}
393 
394 		hw_lock_cmd.bits.command_code = DMUB_INBOX0_CMD__HW_LOCK;
395 		hw_lock_cmd.bits.hw_lock_client = HW_LOCK_CLIENT_DRIVER;
396 		hw_lock_cmd.bits.lock = lock;
397 		hw_lock_cmd.bits.should_release = !lock;
398 		dmub_hw_lock_mgr_inbox0_cmd(dc->ctx->dmub_srv, hw_lock_cmd);
399 	}
400 }
401 
402 
403 static bool dcn32_set_mpc_shaper_3dlut(
404 	struct pipe_ctx *pipe_ctx, const struct dc_stream_state *stream)
405 {
406 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
407 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
408 	struct mpc *mpc = pipe_ctx->stream_res.opp->ctx->dc->res_pool->mpc;
409 	bool result = false;
410 
411 	const struct pwl_params *shaper_lut = NULL;
412 	//get the shaper lut params
413 	if (stream->func_shaper) {
414 		if (stream->func_shaper->type == TF_TYPE_HWPWL)
415 			shaper_lut = &stream->func_shaper->pwl;
416 		else if (stream->func_shaper->type == TF_TYPE_DISTRIBUTED_POINTS) {
417 			cm_helper_translate_curve_to_hw_format(
418 					stream->func_shaper,
419 					&dpp_base->shaper_params, true);
420 			shaper_lut = &dpp_base->shaper_params;
421 		}
422 	}
423 
424 	if (stream->lut3d_func &&
425 		stream->lut3d_func->state.bits.initialized == 1) {
426 
427 		result = mpc->funcs->program_3dlut(mpc,
428 								&stream->lut3d_func->lut_3d,
429 								mpcc_id);
430 
431 		result = mpc->funcs->program_shaper(mpc,
432 								shaper_lut,
433 								mpcc_id);
434 	}
435 
436 	return result;
437 }
438 
439 bool dcn32_set_mcm_luts(
440 	struct pipe_ctx *pipe_ctx, const struct dc_plane_state *plane_state)
441 {
442 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
443 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
444 	struct mpc *mpc = pipe_ctx->stream_res.opp->ctx->dc->res_pool->mpc;
445 	bool result = true;
446 	struct pwl_params *lut_params = NULL;
447 
448 	// 1D LUT
449 	if (plane_state->blend_tf) {
450 		if (plane_state->blend_tf->type == TF_TYPE_HWPWL)
451 			lut_params = &plane_state->blend_tf->pwl;
452 		else if (plane_state->blend_tf->type == TF_TYPE_DISTRIBUTED_POINTS) {
453 			cm_helper_translate_curve_to_hw_format(
454 					plane_state->blend_tf,
455 					&dpp_base->regamma_params, false);
456 			lut_params = &dpp_base->regamma_params;
457 		}
458 	}
459 	result = mpc->funcs->program_1dlut(mpc, lut_params, mpcc_id);
460 
461 	// Shaper
462 	if (plane_state->in_shaper_func) {
463 		if (plane_state->in_shaper_func->type == TF_TYPE_HWPWL)
464 			lut_params = &plane_state->in_shaper_func->pwl;
465 		else if (plane_state->in_shaper_func->type == TF_TYPE_DISTRIBUTED_POINTS) {
466 			// TODO: dpp_base replace
467 			ASSERT(false);
468 			cm_helper_translate_curve_to_hw_format(
469 					plane_state->in_shaper_func,
470 					&dpp_base->shaper_params, true);
471 			lut_params = &dpp_base->shaper_params;
472 		}
473 	}
474 
475 	result = mpc->funcs->program_shaper(mpc, lut_params, mpcc_id);
476 
477 	// 3D
478 	if (plane_state->lut3d_func && plane_state->lut3d_func->state.bits.initialized == 1)
479 		result = mpc->funcs->program_3dlut(mpc, &plane_state->lut3d_func->lut_3d, mpcc_id);
480 	else
481 		result = mpc->funcs->program_3dlut(mpc, NULL, mpcc_id);
482 
483 	return result;
484 }
485 
486 bool dcn32_set_input_transfer_func(struct dc *dc,
487 				struct pipe_ctx *pipe_ctx,
488 				const struct dc_plane_state *plane_state)
489 {
490 	struct dce_hwseq *hws = dc->hwseq;
491 	struct mpc *mpc = dc->res_pool->mpc;
492 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
493 
494 	enum dc_transfer_func_predefined tf;
495 	bool result = true;
496 	struct pwl_params *params = NULL;
497 
498 	if (mpc == NULL || plane_state == NULL)
499 		return false;
500 
501 	tf = TRANSFER_FUNCTION_UNITY;
502 
503 	if (plane_state->in_transfer_func &&
504 		plane_state->in_transfer_func->type == TF_TYPE_PREDEFINED)
505 		tf = plane_state->in_transfer_func->tf;
506 
507 	dpp_base->funcs->dpp_set_pre_degam(dpp_base, tf);
508 
509 	if (plane_state->in_transfer_func) {
510 		if (plane_state->in_transfer_func->type == TF_TYPE_HWPWL)
511 			params = &plane_state->in_transfer_func->pwl;
512 		else if (plane_state->in_transfer_func->type == TF_TYPE_DISTRIBUTED_POINTS &&
513 			cm3_helper_translate_curve_to_hw_format(plane_state->in_transfer_func,
514 					&dpp_base->degamma_params, false))
515 			params = &dpp_base->degamma_params;
516 	}
517 
518 	dpp_base->funcs->dpp_program_gamcor_lut(dpp_base, params);
519 
520 	if (pipe_ctx->stream_res.opp &&
521 			pipe_ctx->stream_res.opp->ctx &&
522 			hws->funcs.set_mcm_luts)
523 		result = hws->funcs.set_mcm_luts(pipe_ctx, plane_state);
524 
525 	return result;
526 }
527 
528 bool dcn32_set_output_transfer_func(struct dc *dc,
529 				struct pipe_ctx *pipe_ctx,
530 				const struct dc_stream_state *stream)
531 {
532 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
533 	struct mpc *mpc = pipe_ctx->stream_res.opp->ctx->dc->res_pool->mpc;
534 	struct pwl_params *params = NULL;
535 	bool ret = false;
536 
537 	/* program OGAM or 3DLUT only for the top pipe*/
538 	if (pipe_ctx->top_pipe == NULL) {
539 		/*program shaper and 3dlut in MPC*/
540 		ret = dcn32_set_mpc_shaper_3dlut(pipe_ctx, stream);
541 		if (ret == false && mpc->funcs->set_output_gamma && stream->out_transfer_func) {
542 			if (stream->out_transfer_func->type == TF_TYPE_HWPWL)
543 				params = &stream->out_transfer_func->pwl;
544 			else if (pipe_ctx->stream->out_transfer_func->type ==
545 					TF_TYPE_DISTRIBUTED_POINTS &&
546 					cm3_helper_translate_curve_to_hw_format(
547 					stream->out_transfer_func,
548 					&mpc->blender_params, false))
549 				params = &mpc->blender_params;
550 			/* there are no ROM LUTs in OUTGAM */
551 			if (stream->out_transfer_func->type == TF_TYPE_PREDEFINED)
552 				BREAK_TO_DEBUGGER();
553 		}
554 	}
555 
556 	mpc->funcs->set_output_gamma(mpc, mpcc_id, params);
557 	return ret;
558 }
559 
560 /* Program P-State force value according to if pipe is using SubVP or not:
561  * 1. Reset P-State force on all pipes first
562  * 2. For each main pipe, force P-State disallow (P-State allow moderated by DMUB)
563  */
564 void dcn32_subvp_update_force_pstate(struct dc *dc, struct dc_state *context)
565 {
566 	int i;
567 	int num_subvp = 0;
568 	/* Unforce p-state for each pipe
569 	 */
570 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
571 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
572 		struct hubp *hubp = pipe->plane_res.hubp;
573 
574 		if (hubp && hubp->funcs->hubp_update_force_pstate_disallow)
575 			hubp->funcs->hubp_update_force_pstate_disallow(hubp, false);
576 		if (pipe->stream && pipe->stream->mall_stream_config.type == SUBVP_MAIN)
577 			num_subvp++;
578 	}
579 
580 	if (num_subvp == 0)
581 		return;
582 
583 	/* Loop through each pipe -- for each subvp main pipe force p-state allow equal to false.
584 	 */
585 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
586 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
587 
588 		if (pipe->stream && pipe->plane_state && (pipe->stream->mall_stream_config.type == SUBVP_MAIN)) {
589 			struct hubp *hubp = pipe->plane_res.hubp;
590 
591 			if (hubp && hubp->funcs->hubp_update_force_pstate_disallow)
592 				hubp->funcs->hubp_update_force_pstate_disallow(hubp, true);
593 		}
594 	}
595 }
596 
597 /* Update MALL_SEL register based on if pipe / plane
598  * is a phantom pipe, main pipe, and if using MALL
599  * for SS.
600  */
601 void dcn32_update_mall_sel(struct dc *dc, struct dc_state *context)
602 {
603 	int i;
604 	unsigned int num_ways = dcn32_calculate_cab_allocation(dc, context);
605 	bool cache_cursor = false;
606 
607 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
608 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
609 		struct hubp *hubp = pipe->plane_res.hubp;
610 
611 		if (pipe->stream && pipe->plane_state && hubp && hubp->funcs->hubp_update_mall_sel) {
612 			int cursor_size = hubp->curs_attr.pitch * hubp->curs_attr.height;
613 
614 			switch (hubp->curs_attr.color_format) {
615 			case CURSOR_MODE_MONO:
616 				cursor_size /= 2;
617 				break;
618 			case CURSOR_MODE_COLOR_1BIT_AND:
619 			case CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA:
620 			case CURSOR_MODE_COLOR_UN_PRE_MULTIPLIED_ALPHA:
621 				cursor_size *= 4;
622 				break;
623 
624 			case CURSOR_MODE_COLOR_64BIT_FP_PRE_MULTIPLIED:
625 			case CURSOR_MODE_COLOR_64BIT_FP_UN_PRE_MULTIPLIED:
626 			default:
627 				cursor_size *= 8;
628 				break;
629 			}
630 
631 			if (cursor_size > 16384)
632 				cache_cursor = true;
633 
634 			if (pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
635 					hubp->funcs->hubp_update_mall_sel(hubp, 1, false);
636 			} else {
637 				// MALL not supported with Stereo3D
638 				hubp->funcs->hubp_update_mall_sel(hubp,
639 					num_ways <= dc->caps.cache_num_ways &&
640 					pipe->stream->link->psr_settings.psr_version == DC_PSR_VERSION_UNSUPPORTED &&
641 					pipe->plane_state->address.type !=  PLN_ADDR_TYPE_GRPH_STEREO &&
642 					!pipe->plane_state->address.tmz_surface ? 2 : 0,
643 							cache_cursor);
644 			}
645 		}
646 	}
647 }
648 
649 /* Program the sub-viewport pipe configuration after the main / phantom pipes
650  * have been programmed in hardware.
651  * 1. Update force P-State for all the main pipes (disallow P-state)
652  * 2. Update MALL_SEL register
653  * 3. Program FORCE_ONE_ROW_FOR_FRAME for main subvp pipes
654  */
655 void dcn32_program_mall_pipe_config(struct dc *dc, struct dc_state *context)
656 {
657 	int i;
658 	struct dce_hwseq *hws = dc->hwseq;
659 
660 	// Don't force p-state disallow -- can't block dummy p-state
661 
662 	// Update MALL_SEL register for each pipe
663 	if (hws && hws->funcs.update_mall_sel)
664 		hws->funcs.update_mall_sel(dc, context);
665 
666 	//update subvp force pstate
667 	if (hws && hws->funcs.subvp_update_force_pstate)
668 		dc->hwseq->funcs.subvp_update_force_pstate(dc, context);
669 
670 	// Program FORCE_ONE_ROW_FOR_FRAME and CURSOR_REQ_MODE for main subvp pipes
671 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
672 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
673 		struct hubp *hubp = pipe->plane_res.hubp;
674 
675 		if (pipe->stream && hubp && hubp->funcs->hubp_prepare_subvp_buffering) {
676 			/* TODO - remove setting CURSOR_REQ_MODE to 0 for legacy cases
677 			 *      - need to investigate single pipe MPO + SubVP case to
678 			 *        see if CURSOR_REQ_MODE will be back to 1 for SubVP
679 			 *        when it should be 0 for MPO
680 			 */
681 			if (pipe->stream->mall_stream_config.type == SUBVP_MAIN) {
682 				hubp->funcs->hubp_prepare_subvp_buffering(hubp, true);
683 			}
684 		}
685 	}
686 }
687 
688 void dcn32_init_hw(struct dc *dc)
689 {
690 	struct abm **abms = dc->res_pool->multiple_abms;
691 	struct dce_hwseq *hws = dc->hwseq;
692 	struct dc_bios *dcb = dc->ctx->dc_bios;
693 	struct resource_pool *res_pool = dc->res_pool;
694 	int i;
695 	int edp_num;
696 	uint32_t backlight = MAX_BACKLIGHT_LEVEL;
697 
698 	if (dc->clk_mgr && dc->clk_mgr->funcs->init_clocks)
699 		dc->clk_mgr->funcs->init_clocks(dc->clk_mgr);
700 
701 	// Initialize the dccg
702 	if (res_pool->dccg->funcs->dccg_init)
703 		res_pool->dccg->funcs->dccg_init(res_pool->dccg);
704 
705 	if (!dcb->funcs->is_accelerated_mode(dcb)) {
706 		hws->funcs.bios_golden_init(dc);
707 		hws->funcs.disable_vga(dc->hwseq);
708 	}
709 
710 	// Set default OPTC memory power states
711 	if (dc->debug.enable_mem_low_power.bits.optc) {
712 		// Shutdown when unassigned and light sleep in VBLANK
713 		REG_SET_2(ODM_MEM_PWR_CTRL3, 0, ODM_MEM_UNASSIGNED_PWR_MODE, 3, ODM_MEM_VBLANK_PWR_MODE, 1);
714 	}
715 
716 	if (dc->debug.enable_mem_low_power.bits.vga) {
717 		// Power down VGA memory
718 		REG_UPDATE(MMHUBBUB_MEM_PWR_CNTL, VGA_MEM_PWR_FORCE, 1);
719 	}
720 
721 	if (dc->ctx->dc_bios->fw_info_valid) {
722 		res_pool->ref_clocks.xtalin_clock_inKhz =
723 				dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency;
724 
725 		if (res_pool->dccg && res_pool->hubbub) {
726 			(res_pool->dccg->funcs->get_dccg_ref_freq)(res_pool->dccg,
727 					dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency,
728 					&res_pool->ref_clocks.dccg_ref_clock_inKhz);
729 
730 			(res_pool->hubbub->funcs->get_dchub_ref_freq)(res_pool->hubbub,
731 					res_pool->ref_clocks.dccg_ref_clock_inKhz,
732 					&res_pool->ref_clocks.dchub_ref_clock_inKhz);
733 		} else {
734 			// Not all ASICs have DCCG sw component
735 			res_pool->ref_clocks.dccg_ref_clock_inKhz =
736 					res_pool->ref_clocks.xtalin_clock_inKhz;
737 			res_pool->ref_clocks.dchub_ref_clock_inKhz =
738 					res_pool->ref_clocks.xtalin_clock_inKhz;
739 		}
740 	} else
741 		ASSERT_CRITICAL(false);
742 
743 	for (i = 0; i < dc->link_count; i++) {
744 		/* Power up AND update implementation according to the
745 		 * required signal (which may be different from the
746 		 * default signal on connector).
747 		 */
748 		struct dc_link *link = dc->links[i];
749 
750 		link->link_enc->funcs->hw_init(link->link_enc);
751 
752 		/* Check for enabled DIG to identify enabled display */
753 		if (link->link_enc->funcs->is_dig_enabled &&
754 			link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
755 			link->link_status.link_active = true;
756 			link->phy_state.symclk_state = SYMCLK_ON_TX_ON;
757 			if (link->link_enc->funcs->fec_is_active &&
758 					link->link_enc->funcs->fec_is_active(link->link_enc))
759 				link->fec_state = dc_link_fec_enabled;
760 		}
761 	}
762 
763 	/* Power gate DSCs */
764 	for (i = 0; i < res_pool->res_cap->num_dsc; i++)
765 		if (hws->funcs.dsc_pg_control != NULL)
766 			hws->funcs.dsc_pg_control(hws, res_pool->dscs[i]->inst, false);
767 
768 	/* we want to turn off all dp displays before doing detection */
769 	dc_link_blank_all_dp_displays(dc);
770 
771 	/* If taking control over from VBIOS, we may want to optimize our first
772 	 * mode set, so we need to skip powering down pipes until we know which
773 	 * pipes we want to use.
774 	 * Otherwise, if taking control is not possible, we need to power
775 	 * everything down.
776 	 */
777 	if (dcb->funcs->is_accelerated_mode(dcb) || !dc->config.seamless_boot_edp_requested) {
778 		hws->funcs.init_pipes(dc, dc->current_state);
779 		if (dc->res_pool->hubbub->funcs->allow_self_refresh_control)
780 			dc->res_pool->hubbub->funcs->allow_self_refresh_control(dc->res_pool->hubbub,
781 					!dc->res_pool->hubbub->ctx->dc->debug.disable_stutter);
782 	}
783 
784 	/* In headless boot cases, DIG may be turned
785 	 * on which causes HW/SW discrepancies.
786 	 * To avoid this, power down hardware on boot
787 	 * if DIG is turned on and seamless boot not enabled
788 	 */
789 	if (!dc->config.seamless_boot_edp_requested) {
790 		struct dc_link *edp_links[MAX_NUM_EDP];
791 		struct dc_link *edp_link;
792 
793 		get_edp_links(dc, edp_links, &edp_num);
794 		if (edp_num) {
795 			for (i = 0; i < edp_num; i++) {
796 				edp_link = edp_links[i];
797 				if (edp_link->link_enc->funcs->is_dig_enabled &&
798 						edp_link->link_enc->funcs->is_dig_enabled(edp_link->link_enc) &&
799 						dc->hwss.edp_backlight_control &&
800 						dc->hwss.power_down &&
801 						dc->hwss.edp_power_control) {
802 					dc->hwss.edp_backlight_control(edp_link, false);
803 					dc->hwss.power_down(dc);
804 					dc->hwss.edp_power_control(edp_link, false);
805 				}
806 			}
807 		} else {
808 			for (i = 0; i < dc->link_count; i++) {
809 				struct dc_link *link = dc->links[i];
810 
811 				if (link->link_enc->funcs->is_dig_enabled &&
812 						link->link_enc->funcs->is_dig_enabled(link->link_enc) &&
813 						dc->hwss.power_down) {
814 					dc->hwss.power_down(dc);
815 					break;
816 				}
817 
818 			}
819 		}
820 	}
821 
822 	for (i = 0; i < res_pool->audio_count; i++) {
823 		struct audio *audio = res_pool->audios[i];
824 
825 		audio->funcs->hw_init(audio);
826 	}
827 
828 	for (i = 0; i < dc->link_count; i++) {
829 		struct dc_link *link = dc->links[i];
830 
831 		if (link->panel_cntl)
832 			backlight = link->panel_cntl->funcs->hw_init(link->panel_cntl);
833 	}
834 
835 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
836 		if (abms[i] != NULL && abms[i]->funcs != NULL)
837 			abms[i]->funcs->abm_init(abms[i], backlight);
838 	}
839 
840 	/* power AFMT HDMI memory TODO: may move to dis/en output save power*/
841 	REG_WRITE(DIO_MEM_PWR_CTRL, 0);
842 
843 	if (!dc->debug.disable_clock_gate) {
844 		/* enable all DCN clock gating */
845 		REG_WRITE(DCCG_GATE_DISABLE_CNTL, 0);
846 
847 		REG_WRITE(DCCG_GATE_DISABLE_CNTL2, 0);
848 
849 		REG_UPDATE(DCFCLK_CNTL, DCFCLK_GATE_DIS, 0);
850 	}
851 	if (hws->funcs.enable_power_gating_plane)
852 		hws->funcs.enable_power_gating_plane(dc->hwseq, true);
853 
854 	if (!dcb->funcs->is_accelerated_mode(dcb) && dc->res_pool->hubbub->funcs->init_watermarks)
855 		dc->res_pool->hubbub->funcs->init_watermarks(dc->res_pool->hubbub);
856 
857 	if (dc->clk_mgr->funcs->notify_wm_ranges)
858 		dc->clk_mgr->funcs->notify_wm_ranges(dc->clk_mgr);
859 
860 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
861 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
862 
863 	if (dc->res_pool->hubbub->funcs->force_pstate_change_control)
864 		dc->res_pool->hubbub->funcs->force_pstate_change_control(
865 				dc->res_pool->hubbub, false, false);
866 
867 	if (dc->res_pool->hubbub->funcs->init_crb)
868 		dc->res_pool->hubbub->funcs->init_crb(dc->res_pool->hubbub);
869 
870 	if (dc->res_pool->hubbub->funcs->set_request_limit && dc->config.sdpif_request_limit_words_per_umc > 0)
871 		dc->res_pool->hubbub->funcs->set_request_limit(dc->res_pool->hubbub, dc->ctx->dc_bios->vram_info.num_chans, dc->config.sdpif_request_limit_words_per_umc);
872 
873 	// Get DMCUB capabilities
874 	if (dc->ctx->dmub_srv) {
875 		dc_dmub_srv_query_caps_cmd(dc->ctx->dmub_srv->dmub);
876 		dc->caps.dmub_caps.psr = dc->ctx->dmub_srv->dmub->feature_caps.psr;
877 	}
878 }
879 
880 static int calc_mpc_flow_ctrl_cnt(const struct dc_stream_state *stream,
881 		int opp_cnt)
882 {
883 	bool hblank_halved = optc2_is_two_pixels_per_containter(&stream->timing);
884 	int flow_ctrl_cnt;
885 
886 	if (opp_cnt >= 2)
887 		hblank_halved = true;
888 
889 	flow_ctrl_cnt = stream->timing.h_total - stream->timing.h_addressable -
890 			stream->timing.h_border_left -
891 			stream->timing.h_border_right;
892 
893 	if (hblank_halved)
894 		flow_ctrl_cnt /= 2;
895 
896 	/* ODM combine 4:1 case */
897 	if (opp_cnt == 4)
898 		flow_ctrl_cnt /= 2;
899 
900 	return flow_ctrl_cnt;
901 }
902 
903 static void update_dsc_on_stream(struct pipe_ctx *pipe_ctx, bool enable)
904 {
905 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
906 	struct dc_stream_state *stream = pipe_ctx->stream;
907 	struct pipe_ctx *odm_pipe;
908 	int opp_cnt = 1;
909 
910 	ASSERT(dsc);
911 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
912 		opp_cnt++;
913 
914 	if (enable) {
915 		struct dsc_config dsc_cfg;
916 		struct dsc_optc_config dsc_optc_cfg;
917 		enum optc_dsc_mode optc_dsc_mode;
918 
919 		/* Enable DSC hw block */
920 		dsc_cfg.pic_width = (stream->timing.h_addressable + stream->timing.h_border_left + stream->timing.h_border_right) / opp_cnt;
921 		dsc_cfg.pic_height = stream->timing.v_addressable + stream->timing.v_border_top + stream->timing.v_border_bottom;
922 		dsc_cfg.pixel_encoding = stream->timing.pixel_encoding;
923 		dsc_cfg.color_depth = stream->timing.display_color_depth;
924 		dsc_cfg.is_odm = pipe_ctx->next_odm_pipe ? true : false;
925 		dsc_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
926 		ASSERT(dsc_cfg.dc_dsc_cfg.num_slices_h % opp_cnt == 0);
927 		dsc_cfg.dc_dsc_cfg.num_slices_h /= opp_cnt;
928 
929 		dsc->funcs->dsc_set_config(dsc, &dsc_cfg, &dsc_optc_cfg);
930 		dsc->funcs->dsc_enable(dsc, pipe_ctx->stream_res.opp->inst);
931 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
932 			struct display_stream_compressor *odm_dsc = odm_pipe->stream_res.dsc;
933 
934 			ASSERT(odm_dsc);
935 			odm_dsc->funcs->dsc_set_config(odm_dsc, &dsc_cfg, &dsc_optc_cfg);
936 			odm_dsc->funcs->dsc_enable(odm_dsc, odm_pipe->stream_res.opp->inst);
937 		}
938 		dsc_cfg.dc_dsc_cfg.num_slices_h *= opp_cnt;
939 		dsc_cfg.pic_width *= opp_cnt;
940 
941 		optc_dsc_mode = dsc_optc_cfg.is_pixel_format_444 ? OPTC_DSC_ENABLED_444 : OPTC_DSC_ENABLED_NATIVE_SUBSAMPLED;
942 
943 		/* Enable DSC in OPTC */
944 		DC_LOG_DSC("Setting optc DSC config for tg instance %d:", pipe_ctx->stream_res.tg->inst);
945 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(pipe_ctx->stream_res.tg,
946 							optc_dsc_mode,
947 							dsc_optc_cfg.bytes_per_pixel,
948 							dsc_optc_cfg.slice_width);
949 	} else {
950 		/* disable DSC in OPTC */
951 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(
952 				pipe_ctx->stream_res.tg,
953 				OPTC_DSC_DISABLED, 0, 0);
954 
955 		/* disable DSC block */
956 		dsc->funcs->dsc_disable(pipe_ctx->stream_res.dsc);
957 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
958 			ASSERT(odm_pipe->stream_res.dsc);
959 			odm_pipe->stream_res.dsc->funcs->dsc_disable(odm_pipe->stream_res.dsc);
960 		}
961 	}
962 }
963 
964 /*
965 * Given any pipe_ctx, return the total ODM combine factor, and optionally return
966 * the OPPids which are used
967 * */
968 static unsigned int get_odm_config(struct pipe_ctx *pipe_ctx, unsigned int *opp_instances)
969 {
970 	unsigned int opp_count = 1;
971 	struct pipe_ctx *odm_pipe;
972 
973 	/* First get to the top pipe */
974 	for (odm_pipe = pipe_ctx; odm_pipe->prev_odm_pipe; odm_pipe = odm_pipe->prev_odm_pipe)
975 		;
976 
977 	/* First pipe is always used */
978 	if (opp_instances)
979 		opp_instances[0] = odm_pipe->stream_res.opp->inst;
980 
981 	/* Find and count odm pipes, if any */
982 	for (odm_pipe = odm_pipe->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
983 		if (opp_instances)
984 			opp_instances[opp_count] = odm_pipe->stream_res.opp->inst;
985 		opp_count++;
986 	}
987 
988 	return opp_count;
989 }
990 
991 void dcn32_update_odm(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe_ctx)
992 {
993 	struct pipe_ctx *odm_pipe;
994 	int opp_cnt = 0;
995 	int opp_inst[MAX_PIPES] = {0};
996 	bool rate_control_2x_pclk = (pipe_ctx->stream->timing.flags.INTERLACE || optc2_is_two_pixels_per_containter(&pipe_ctx->stream->timing));
997 	struct mpc_dwb_flow_control flow_control;
998 	struct mpc *mpc = dc->res_pool->mpc;
999 	int i;
1000 
1001 	opp_cnt = get_odm_config(pipe_ctx, opp_inst);
1002 
1003 	if (opp_cnt > 1)
1004 		pipe_ctx->stream_res.tg->funcs->set_odm_combine(
1005 				pipe_ctx->stream_res.tg,
1006 				opp_inst, opp_cnt,
1007 				&pipe_ctx->stream->timing);
1008 	else
1009 		pipe_ctx->stream_res.tg->funcs->set_odm_bypass(
1010 				pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1011 
1012 	rate_control_2x_pclk = rate_control_2x_pclk || opp_cnt > 1;
1013 	flow_control.flow_ctrl_mode = 0;
1014 	flow_control.flow_ctrl_cnt0 = 0x80;
1015 	flow_control.flow_ctrl_cnt1 = calc_mpc_flow_ctrl_cnt(pipe_ctx->stream, opp_cnt);
1016 	if (mpc->funcs->set_out_rate_control) {
1017 		for (i = 0; i < opp_cnt; ++i) {
1018 			mpc->funcs->set_out_rate_control(
1019 					mpc, opp_inst[i],
1020 					true,
1021 					rate_control_2x_pclk,
1022 					&flow_control);
1023 		}
1024 	}
1025 
1026 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1027 		odm_pipe->stream_res.opp->funcs->opp_pipe_clock_control(
1028 				odm_pipe->stream_res.opp,
1029 				true);
1030 	}
1031 
1032 	if (pipe_ctx->stream_res.dsc) {
1033 		struct pipe_ctx *current_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[pipe_ctx->pipe_idx];
1034 
1035 		update_dsc_on_stream(pipe_ctx, pipe_ctx->stream->timing.flags.DSC);
1036 
1037 		/* Check if no longer using pipe for ODM, then need to disconnect DSC for that pipe */
1038 		if (!pipe_ctx->next_odm_pipe && current_pipe_ctx->next_odm_pipe &&
1039 				current_pipe_ctx->next_odm_pipe->stream_res.dsc) {
1040 			struct display_stream_compressor *dsc = current_pipe_ctx->next_odm_pipe->stream_res.dsc;
1041 			/* disconnect DSC block from stream */
1042 			dsc->funcs->dsc_disconnect(dsc);
1043 		}
1044 	}
1045 }
1046 
1047 unsigned int dcn32_calculate_dccg_k1_k2_values(struct pipe_ctx *pipe_ctx, unsigned int *k1_div, unsigned int *k2_div)
1048 {
1049 	struct dc_stream_state *stream = pipe_ctx->stream;
1050 	unsigned int odm_combine_factor = 0;
1051 	bool two_pix_per_container = false;
1052 
1053 	// For phantom pipes, use the same programming as the main pipes
1054 	if (pipe_ctx->stream->mall_stream_config.type == SUBVP_PHANTOM) {
1055 		stream = pipe_ctx->stream->mall_stream_config.paired_stream;
1056 	}
1057 	two_pix_per_container = optc2_is_two_pixels_per_containter(&stream->timing);
1058 	odm_combine_factor = get_odm_config(pipe_ctx, NULL);
1059 
1060 	if (link_is_dp_128b_132b_signal(pipe_ctx)) {
1061 		*k1_div = PIXEL_RATE_DIV_BY_1;
1062 		*k2_div = PIXEL_RATE_DIV_BY_1;
1063 	} else if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal) || dc_is_dvi_signal(pipe_ctx->stream->signal)) {
1064 		*k1_div = PIXEL_RATE_DIV_BY_1;
1065 		if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420)
1066 			*k2_div = PIXEL_RATE_DIV_BY_2;
1067 		else
1068 			*k2_div = PIXEL_RATE_DIV_BY_4;
1069 	} else if (dc_is_dp_signal(pipe_ctx->stream->signal) || dc_is_virtual_signal(pipe_ctx->stream->signal)) {
1070 		if (two_pix_per_container) {
1071 			*k1_div = PIXEL_RATE_DIV_BY_1;
1072 			*k2_div = PIXEL_RATE_DIV_BY_2;
1073 		} else {
1074 			*k1_div = PIXEL_RATE_DIV_BY_1;
1075 			*k2_div = PIXEL_RATE_DIV_BY_4;
1076 			if ((odm_combine_factor == 2) || dcn32_is_dp_dig_pixel_rate_div_policy(pipe_ctx))
1077 				*k2_div = PIXEL_RATE_DIV_BY_2;
1078 		}
1079 	}
1080 
1081 	if ((*k1_div == PIXEL_RATE_DIV_NA) && (*k2_div == PIXEL_RATE_DIV_NA))
1082 		ASSERT(false);
1083 
1084 	return odm_combine_factor;
1085 }
1086 
1087 void dcn32_set_pixels_per_cycle(struct pipe_ctx *pipe_ctx)
1088 {
1089 	uint32_t pix_per_cycle = 1;
1090 	uint32_t odm_combine_factor = 1;
1091 
1092 	if (!pipe_ctx || !pipe_ctx->stream || !pipe_ctx->stream_res.stream_enc)
1093 		return;
1094 
1095 	odm_combine_factor = get_odm_config(pipe_ctx, NULL);
1096 	if (optc2_is_two_pixels_per_containter(&pipe_ctx->stream->timing) || odm_combine_factor > 1
1097 		|| dcn32_is_dp_dig_pixel_rate_div_policy(pipe_ctx))
1098 		pix_per_cycle = 2;
1099 
1100 	if (pipe_ctx->stream_res.stream_enc->funcs->set_input_mode)
1101 		pipe_ctx->stream_res.stream_enc->funcs->set_input_mode(pipe_ctx->stream_res.stream_enc,
1102 				pix_per_cycle);
1103 }
1104 
1105 void dcn32_unblank_stream(struct pipe_ctx *pipe_ctx,
1106 		struct dc_link_settings *link_settings)
1107 {
1108 	struct encoder_unblank_param params = {0};
1109 	struct dc_stream_state *stream = pipe_ctx->stream;
1110 	struct dc_link *link = stream->link;
1111 	struct dce_hwseq *hws = link->dc->hwseq;
1112 	struct pipe_ctx *odm_pipe;
1113 	uint32_t pix_per_cycle = 1;
1114 
1115 	params.opp_cnt = 1;
1116 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
1117 		params.opp_cnt++;
1118 
1119 	/* only 3 items below are used by unblank */
1120 	params.timing = pipe_ctx->stream->timing;
1121 
1122 	params.link_settings.link_rate = link_settings->link_rate;
1123 
1124 	if (link_is_dp_128b_132b_signal(pipe_ctx)) {
1125 		/* TODO - DP2.0 HW: Set ODM mode in dp hpo encoder here */
1126 		pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_unblank(
1127 				pipe_ctx->stream_res.hpo_dp_stream_enc,
1128 				pipe_ctx->stream_res.tg->inst);
1129 	} else if (dc_is_dp_signal(pipe_ctx->stream->signal)) {
1130 		if (optc2_is_two_pixels_per_containter(&stream->timing) || params.opp_cnt > 1
1131 			|| dcn32_is_dp_dig_pixel_rate_div_policy(pipe_ctx)) {
1132 			params.timing.pix_clk_100hz /= 2;
1133 			pix_per_cycle = 2;
1134 		}
1135 		pipe_ctx->stream_res.stream_enc->funcs->dp_set_odm_combine(
1136 				pipe_ctx->stream_res.stream_enc, pix_per_cycle > 1);
1137 		pipe_ctx->stream_res.stream_enc->funcs->dp_unblank(link, pipe_ctx->stream_res.stream_enc, &params);
1138 	}
1139 
1140 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP)
1141 		hws->funcs.edp_backlight_control(link, true);
1142 }
1143 
1144 bool dcn32_is_dp_dig_pixel_rate_div_policy(struct pipe_ctx *pipe_ctx)
1145 {
1146 	struct dc *dc = pipe_ctx->stream->ctx->dc;
1147 
1148 	if (!is_h_timing_divisible_by_2(pipe_ctx->stream))
1149 		return false;
1150 
1151 	if (dc_is_dp_signal(pipe_ctx->stream->signal) && !link_is_dp_128b_132b_signal(pipe_ctx) &&
1152 		dc->debug.enable_dp_dig_pixel_rate_div_policy)
1153 		return true;
1154 	return false;
1155 }
1156 
1157 static void apply_symclk_on_tx_off_wa(struct dc_link *link)
1158 {
1159 	/* There are use cases where SYMCLK is referenced by OTG. For instance
1160 	 * for TMDS signal, OTG relies SYMCLK even if TX video output is off.
1161 	 * However current link interface will power off PHY when disabling link
1162 	 * output. This will turn off SYMCLK generated by PHY. The workaround is
1163 	 * to identify such case where SYMCLK is still in use by OTG when we
1164 	 * power off PHY. When this is detected, we will temporarily power PHY
1165 	 * back on and move PHY's SYMCLK state to SYMCLK_ON_TX_OFF by calling
1166 	 * program_pix_clk interface. When OTG is disabled, we will then power
1167 	 * off PHY by calling disable link output again.
1168 	 *
1169 	 * In future dcn generations, we plan to rework transmitter control
1170 	 * interface so that we could have an option to set SYMCLK ON TX OFF
1171 	 * state in one step without this workaround
1172 	 */
1173 
1174 	struct dc *dc = link->ctx->dc;
1175 	struct pipe_ctx *pipe_ctx = NULL;
1176 	uint8_t i;
1177 
1178 	if (link->phy_state.symclk_ref_cnts.otg > 0) {
1179 		for (i = 0; i < MAX_PIPES; i++) {
1180 			pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
1181 			if (pipe_ctx->stream && pipe_ctx->stream->link == link && pipe_ctx->top_pipe == NULL) {
1182 				pipe_ctx->clock_source->funcs->program_pix_clk(
1183 						pipe_ctx->clock_source,
1184 						&pipe_ctx->stream_res.pix_clk_params,
1185 						link_dp_get_encoding_format(&pipe_ctx->link_config.dp_link_settings),
1186 						&pipe_ctx->pll_settings);
1187 				link->phy_state.symclk_state = SYMCLK_ON_TX_OFF;
1188 				break;
1189 			}
1190 		}
1191 	}
1192 }
1193 
1194 void dcn32_disable_link_output(struct dc_link *link,
1195 		const struct link_resource *link_res,
1196 		enum signal_type signal)
1197 {
1198 	struct dc *dc = link->ctx->dc;
1199 	const struct link_hwss *link_hwss = get_link_hwss(link, link_res);
1200 	struct dmcu *dmcu = dc->res_pool->dmcu;
1201 
1202 	if (signal == SIGNAL_TYPE_EDP &&
1203 			link->dc->hwss.edp_backlight_control)
1204 		link->dc->hwss.edp_backlight_control(link, false);
1205 	else if (dmcu != NULL && dmcu->funcs->lock_phy)
1206 		dmcu->funcs->lock_phy(dmcu);
1207 
1208 	link_hwss->disable_link_output(link, link_res, signal);
1209 	link->phy_state.symclk_state = SYMCLK_OFF_TX_OFF;
1210 
1211 	if (signal == SIGNAL_TYPE_EDP &&
1212 			link->dc->hwss.edp_backlight_control)
1213 		link->dc->hwss.edp_power_control(link, false);
1214 	else if (dmcu != NULL && dmcu->funcs->lock_phy)
1215 		dmcu->funcs->unlock_phy(dmcu);
1216 
1217 	dp_source_sequence_trace(link, DPCD_SOURCE_SEQ_AFTER_DISABLE_LINK_PHY);
1218 
1219 	apply_symclk_on_tx_off_wa(link);
1220 }
1221 
1222 /* For SubVP the main pipe can have a viewport position change
1223  * without a full update. In this case we must also update the
1224  * viewport positions for the phantom pipe accordingly.
1225  */
1226 void dcn32_update_phantom_vp_position(struct dc *dc,
1227 		struct dc_state *context,
1228 		struct pipe_ctx *phantom_pipe)
1229 {
1230 	uint32_t i;
1231 	struct dc_plane_state *phantom_plane = phantom_pipe->plane_state;
1232 
1233 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1234 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1235 
1236 		if (pipe->stream && pipe->stream->mall_stream_config.type == SUBVP_MAIN &&
1237 				pipe->stream->mall_stream_config.paired_stream == phantom_pipe->stream) {
1238 			if (pipe->plane_state && pipe->plane_state->update_flags.bits.position_change) {
1239 
1240 				phantom_plane->src_rect.x = pipe->plane_state->src_rect.x;
1241 				phantom_plane->src_rect.y = pipe->plane_state->src_rect.y;
1242 				phantom_plane->clip_rect.x = pipe->plane_state->clip_rect.x;
1243 				phantom_plane->dst_rect.x = pipe->plane_state->dst_rect.x;
1244 				phantom_plane->dst_rect.y = pipe->plane_state->dst_rect.y;
1245 
1246 				phantom_pipe->plane_state->update_flags.bits.position_change = 1;
1247 				resource_build_scaling_params(phantom_pipe);
1248 				return;
1249 			}
1250 		}
1251 	}
1252 }
1253 
1254 /* Treat the phantom pipe as if it needs to be fully enabled.
1255  * If the pipe was previously in use but not phantom, it would
1256  * have been disabled earlier in the sequence so we need to run
1257  * the full enable sequence.
1258  */
1259 void dcn32_apply_update_flags_for_phantom(struct pipe_ctx *phantom_pipe)
1260 {
1261 	phantom_pipe->update_flags.raw = 0;
1262 	if (phantom_pipe->stream && phantom_pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
1263 		if (phantom_pipe->stream && phantom_pipe->plane_state) {
1264 			phantom_pipe->update_flags.bits.enable = 1;
1265 			phantom_pipe->update_flags.bits.mpcc = 1;
1266 			phantom_pipe->update_flags.bits.dppclk = 1;
1267 			phantom_pipe->update_flags.bits.hubp_interdependent = 1;
1268 			phantom_pipe->update_flags.bits.hubp_rq_dlg_ttu = 1;
1269 			phantom_pipe->update_flags.bits.gamut_remap = 1;
1270 			phantom_pipe->update_flags.bits.scaler = 1;
1271 			phantom_pipe->update_flags.bits.viewport = 1;
1272 			phantom_pipe->update_flags.bits.det_size = 1;
1273 			if (!phantom_pipe->top_pipe && !phantom_pipe->prev_odm_pipe) {
1274 				phantom_pipe->update_flags.bits.odm = 1;
1275 				phantom_pipe->update_flags.bits.global_sync = 1;
1276 			}
1277 		}
1278 	}
1279 }
1280 
1281 bool dcn32_dsc_pg_status(
1282 		struct dce_hwseq *hws,
1283 		unsigned int dsc_inst)
1284 {
1285 	uint32_t pwr_status = 0;
1286 
1287 	switch (dsc_inst) {
1288 	case 0: /* DSC0 */
1289 		REG_GET(DOMAIN16_PG_STATUS,
1290 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1291 		break;
1292 	case 1: /* DSC1 */
1293 
1294 		REG_GET(DOMAIN17_PG_STATUS,
1295 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1296 		break;
1297 	case 2: /* DSC2 */
1298 		REG_GET(DOMAIN18_PG_STATUS,
1299 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1300 		break;
1301 	case 3: /* DSC3 */
1302 		REG_GET(DOMAIN19_PG_STATUS,
1303 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1304 		break;
1305 	default:
1306 		BREAK_TO_DEBUGGER();
1307 		break;
1308 	}
1309 
1310 	return pwr_status == 0;
1311 }
1312 
1313 void dcn32_update_dsc_pg(struct dc *dc,
1314 		struct dc_state *context,
1315 		bool safe_to_disable)
1316 {
1317 	struct dce_hwseq *hws = dc->hwseq;
1318 	int i;
1319 
1320 	for (i = 0; i < dc->res_pool->res_cap->num_dsc; i++) {
1321 		struct display_stream_compressor *dsc = dc->res_pool->dscs[i];
1322 		bool is_dsc_ungated = hws->funcs.dsc_pg_status(hws, dsc->inst);
1323 
1324 		if (context->res_ctx.is_dsc_acquired[i]) {
1325 			if (!is_dsc_ungated) {
1326 				hws->funcs.dsc_pg_control(hws, dsc->inst, true);
1327 			}
1328 		} else if (safe_to_disable) {
1329 			if (is_dsc_ungated) {
1330 				hws->funcs.dsc_pg_control(hws, dsc->inst, false);
1331 			}
1332 		}
1333 	}
1334 }
1335 
1336 void dcn32_enable_phantom_streams(struct dc *dc, struct dc_state *context)
1337 {
1338 	unsigned int i;
1339 
1340 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1341 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1342 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1343 
1344 		/* If an active, non-phantom pipe is being transitioned into a phantom
1345 		 * pipe, wait for the double buffer update to complete first before we do
1346 		 * ANY phantom pipe programming.
1347 		 */
1348 		if (pipe->stream && pipe->stream->mall_stream_config.type == SUBVP_PHANTOM &&
1349 				old_pipe->stream && old_pipe->stream->mall_stream_config.type != SUBVP_PHANTOM) {
1350 			old_pipe->stream_res.tg->funcs->wait_for_state(
1351 					old_pipe->stream_res.tg,
1352 					CRTC_STATE_VBLANK);
1353 			old_pipe->stream_res.tg->funcs->wait_for_state(
1354 					old_pipe->stream_res.tg,
1355 					CRTC_STATE_VACTIVE);
1356 		}
1357 	}
1358 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1359 		struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
1360 
1361 		if (new_pipe->stream && new_pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
1362 			// If old context or new context has phantom pipes, apply
1363 			// the phantom timings now. We can't change the phantom
1364 			// pipe configuration safely without driver acquiring
1365 			// the DMCUB lock first.
1366 			dc->hwss.apply_ctx_to_hw(dc, context);
1367 			break;
1368 		}
1369 	}
1370 }
1371