xref: /openbmc/linux/drivers/gpu/drm/amd/display/dc/dcn32/dcn32_hwseq.c (revision e65e175b07bef5974045cc42238de99057669ca7)
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 static void dcn32_initialize_min_clocks(struct dc *dc)
689 {
690 	struct dc_clocks *clocks = &dc->current_state->bw_ctx.bw.dcn.clk;
691 
692 	clocks->dcfclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].dcfclk_mhz * 1000;
693 	clocks->socclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].socclk_mhz * 1000;
694 	clocks->dramclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].memclk_mhz * 1000;
695 	clocks->dppclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].dppclk_mhz * 1000;
696 	clocks->dispclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].dispclk_mhz * 1000;
697 	clocks->ref_dtbclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].dtbclk_mhz * 1000;
698 	clocks->fclk_p_state_change_support = true;
699 	clocks->p_state_change_support = true;
700 
701 	dc->clk_mgr->funcs->update_clocks(
702 			dc->clk_mgr,
703 			dc->current_state,
704 			true);
705 }
706 
707 void dcn32_init_hw(struct dc *dc)
708 {
709 	struct abm **abms = dc->res_pool->multiple_abms;
710 	struct dce_hwseq *hws = dc->hwseq;
711 	struct dc_bios *dcb = dc->ctx->dc_bios;
712 	struct resource_pool *res_pool = dc->res_pool;
713 	int i;
714 	int edp_num;
715 	uint32_t backlight = MAX_BACKLIGHT_LEVEL;
716 
717 	if (dc->clk_mgr && dc->clk_mgr->funcs->init_clocks)
718 		dc->clk_mgr->funcs->init_clocks(dc->clk_mgr);
719 
720 	// Initialize the dccg
721 	if (res_pool->dccg->funcs->dccg_init)
722 		res_pool->dccg->funcs->dccg_init(res_pool->dccg);
723 
724 	if (!dcb->funcs->is_accelerated_mode(dcb)) {
725 		hws->funcs.bios_golden_init(dc);
726 		hws->funcs.disable_vga(dc->hwseq);
727 	}
728 
729 	// Set default OPTC memory power states
730 	if (dc->debug.enable_mem_low_power.bits.optc) {
731 		// Shutdown when unassigned and light sleep in VBLANK
732 		REG_SET_2(ODM_MEM_PWR_CTRL3, 0, ODM_MEM_UNASSIGNED_PWR_MODE, 3, ODM_MEM_VBLANK_PWR_MODE, 1);
733 	}
734 
735 	if (dc->debug.enable_mem_low_power.bits.vga) {
736 		// Power down VGA memory
737 		REG_UPDATE(MMHUBBUB_MEM_PWR_CNTL, VGA_MEM_PWR_FORCE, 1);
738 	}
739 
740 	if (dc->ctx->dc_bios->fw_info_valid) {
741 		res_pool->ref_clocks.xtalin_clock_inKhz =
742 				dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency;
743 
744 		if (res_pool->dccg && res_pool->hubbub) {
745 			(res_pool->dccg->funcs->get_dccg_ref_freq)(res_pool->dccg,
746 					dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency,
747 					&res_pool->ref_clocks.dccg_ref_clock_inKhz);
748 
749 			(res_pool->hubbub->funcs->get_dchub_ref_freq)(res_pool->hubbub,
750 					res_pool->ref_clocks.dccg_ref_clock_inKhz,
751 					&res_pool->ref_clocks.dchub_ref_clock_inKhz);
752 		} else {
753 			// Not all ASICs have DCCG sw component
754 			res_pool->ref_clocks.dccg_ref_clock_inKhz =
755 					res_pool->ref_clocks.xtalin_clock_inKhz;
756 			res_pool->ref_clocks.dchub_ref_clock_inKhz =
757 					res_pool->ref_clocks.xtalin_clock_inKhz;
758 		}
759 	} else
760 		ASSERT_CRITICAL(false);
761 
762 	for (i = 0; i < dc->link_count; i++) {
763 		/* Power up AND update implementation according to the
764 		 * required signal (which may be different from the
765 		 * default signal on connector).
766 		 */
767 		struct dc_link *link = dc->links[i];
768 
769 		link->link_enc->funcs->hw_init(link->link_enc);
770 
771 		/* Check for enabled DIG to identify enabled display */
772 		if (link->link_enc->funcs->is_dig_enabled &&
773 			link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
774 			link->link_status.link_active = true;
775 			link->phy_state.symclk_state = SYMCLK_ON_TX_ON;
776 			if (link->link_enc->funcs->fec_is_active &&
777 					link->link_enc->funcs->fec_is_active(link->link_enc))
778 				link->fec_state = dc_link_fec_enabled;
779 		}
780 	}
781 
782 	/* Power gate DSCs */
783 	for (i = 0; i < res_pool->res_cap->num_dsc; i++)
784 		if (hws->funcs.dsc_pg_control != NULL)
785 			hws->funcs.dsc_pg_control(hws, res_pool->dscs[i]->inst, false);
786 
787 	/* we want to turn off all dp displays before doing detection */
788 	dc_link_blank_all_dp_displays(dc);
789 
790 	/* If taking control over from VBIOS, we may want to optimize our first
791 	 * mode set, so we need to skip powering down pipes until we know which
792 	 * pipes we want to use.
793 	 * Otherwise, if taking control is not possible, we need to power
794 	 * everything down.
795 	 */
796 	if (dcb->funcs->is_accelerated_mode(dcb) || !dc->config.seamless_boot_edp_requested) {
797 		hws->funcs.init_pipes(dc, dc->current_state);
798 		if (dc->res_pool->hubbub->funcs->allow_self_refresh_control)
799 			dc->res_pool->hubbub->funcs->allow_self_refresh_control(dc->res_pool->hubbub,
800 					!dc->res_pool->hubbub->ctx->dc->debug.disable_stutter);
801 
802 		dcn32_initialize_min_clocks(dc);
803 	}
804 
805 	/* In headless boot cases, DIG may be turned
806 	 * on which causes HW/SW discrepancies.
807 	 * To avoid this, power down hardware on boot
808 	 * if DIG is turned on and seamless boot not enabled
809 	 */
810 	if (!dc->config.seamless_boot_edp_requested) {
811 		struct dc_link *edp_links[MAX_NUM_EDP];
812 		struct dc_link *edp_link;
813 
814 		get_edp_links(dc, edp_links, &edp_num);
815 		if (edp_num) {
816 			for (i = 0; i < edp_num; i++) {
817 				edp_link = edp_links[i];
818 				if (edp_link->link_enc->funcs->is_dig_enabled &&
819 						edp_link->link_enc->funcs->is_dig_enabled(edp_link->link_enc) &&
820 						dc->hwss.edp_backlight_control &&
821 						dc->hwss.power_down &&
822 						dc->hwss.edp_power_control) {
823 					dc->hwss.edp_backlight_control(edp_link, false);
824 					dc->hwss.power_down(dc);
825 					dc->hwss.edp_power_control(edp_link, false);
826 				}
827 			}
828 		} else {
829 			for (i = 0; i < dc->link_count; i++) {
830 				struct dc_link *link = dc->links[i];
831 
832 				if (link->link_enc->funcs->is_dig_enabled &&
833 						link->link_enc->funcs->is_dig_enabled(link->link_enc) &&
834 						dc->hwss.power_down) {
835 					dc->hwss.power_down(dc);
836 					break;
837 				}
838 
839 			}
840 		}
841 	}
842 
843 	for (i = 0; i < res_pool->audio_count; i++) {
844 		struct audio *audio = res_pool->audios[i];
845 
846 		audio->funcs->hw_init(audio);
847 	}
848 
849 	for (i = 0; i < dc->link_count; i++) {
850 		struct dc_link *link = dc->links[i];
851 
852 		if (link->panel_cntl)
853 			backlight = link->panel_cntl->funcs->hw_init(link->panel_cntl);
854 	}
855 
856 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
857 		if (abms[i] != NULL && abms[i]->funcs != NULL)
858 			abms[i]->funcs->abm_init(abms[i], backlight);
859 	}
860 
861 	/* power AFMT HDMI memory TODO: may move to dis/en output save power*/
862 	REG_WRITE(DIO_MEM_PWR_CTRL, 0);
863 
864 	if (!dc->debug.disable_clock_gate) {
865 		/* enable all DCN clock gating */
866 		REG_WRITE(DCCG_GATE_DISABLE_CNTL, 0);
867 
868 		REG_WRITE(DCCG_GATE_DISABLE_CNTL2, 0);
869 
870 		REG_UPDATE(DCFCLK_CNTL, DCFCLK_GATE_DIS, 0);
871 	}
872 	if (hws->funcs.enable_power_gating_plane)
873 		hws->funcs.enable_power_gating_plane(dc->hwseq, true);
874 
875 	if (!dcb->funcs->is_accelerated_mode(dcb) && dc->res_pool->hubbub->funcs->init_watermarks)
876 		dc->res_pool->hubbub->funcs->init_watermarks(dc->res_pool->hubbub);
877 
878 	if (dc->clk_mgr->funcs->notify_wm_ranges)
879 		dc->clk_mgr->funcs->notify_wm_ranges(dc->clk_mgr);
880 
881 	if (dc->clk_mgr->funcs->set_hard_max_memclk)
882 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
883 
884 	if (dc->res_pool->hubbub->funcs->force_pstate_change_control)
885 		dc->res_pool->hubbub->funcs->force_pstate_change_control(
886 				dc->res_pool->hubbub, false, false);
887 
888 	if (dc->res_pool->hubbub->funcs->init_crb)
889 		dc->res_pool->hubbub->funcs->init_crb(dc->res_pool->hubbub);
890 
891 	if (dc->res_pool->hubbub->funcs->set_request_limit && dc->config.sdpif_request_limit_words_per_umc > 0)
892 		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);
893 
894 	// Get DMCUB capabilities
895 	if (dc->ctx->dmub_srv) {
896 		dc_dmub_srv_query_caps_cmd(dc->ctx->dmub_srv->dmub);
897 		dc->caps.dmub_caps.psr = dc->ctx->dmub_srv->dmub->feature_caps.psr;
898 	}
899 }
900 
901 static int calc_mpc_flow_ctrl_cnt(const struct dc_stream_state *stream,
902 		int opp_cnt)
903 {
904 	bool hblank_halved = optc2_is_two_pixels_per_containter(&stream->timing);
905 	int flow_ctrl_cnt;
906 
907 	if (opp_cnt >= 2)
908 		hblank_halved = true;
909 
910 	flow_ctrl_cnt = stream->timing.h_total - stream->timing.h_addressable -
911 			stream->timing.h_border_left -
912 			stream->timing.h_border_right;
913 
914 	if (hblank_halved)
915 		flow_ctrl_cnt /= 2;
916 
917 	/* ODM combine 4:1 case */
918 	if (opp_cnt == 4)
919 		flow_ctrl_cnt /= 2;
920 
921 	return flow_ctrl_cnt;
922 }
923 
924 static void update_dsc_on_stream(struct pipe_ctx *pipe_ctx, bool enable)
925 {
926 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
927 	struct dc_stream_state *stream = pipe_ctx->stream;
928 	struct pipe_ctx *odm_pipe;
929 	int opp_cnt = 1;
930 
931 	ASSERT(dsc);
932 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
933 		opp_cnt++;
934 
935 	if (enable) {
936 		struct dsc_config dsc_cfg;
937 		struct dsc_optc_config dsc_optc_cfg;
938 		enum optc_dsc_mode optc_dsc_mode;
939 
940 		/* Enable DSC hw block */
941 		dsc_cfg.pic_width = (stream->timing.h_addressable + stream->timing.h_border_left + stream->timing.h_border_right) / opp_cnt;
942 		dsc_cfg.pic_height = stream->timing.v_addressable + stream->timing.v_border_top + stream->timing.v_border_bottom;
943 		dsc_cfg.pixel_encoding = stream->timing.pixel_encoding;
944 		dsc_cfg.color_depth = stream->timing.display_color_depth;
945 		dsc_cfg.is_odm = pipe_ctx->next_odm_pipe ? true : false;
946 		dsc_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
947 		ASSERT(dsc_cfg.dc_dsc_cfg.num_slices_h % opp_cnt == 0);
948 		dsc_cfg.dc_dsc_cfg.num_slices_h /= opp_cnt;
949 
950 		dsc->funcs->dsc_set_config(dsc, &dsc_cfg, &dsc_optc_cfg);
951 		dsc->funcs->dsc_enable(dsc, pipe_ctx->stream_res.opp->inst);
952 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
953 			struct display_stream_compressor *odm_dsc = odm_pipe->stream_res.dsc;
954 
955 			ASSERT(odm_dsc);
956 			odm_dsc->funcs->dsc_set_config(odm_dsc, &dsc_cfg, &dsc_optc_cfg);
957 			odm_dsc->funcs->dsc_enable(odm_dsc, odm_pipe->stream_res.opp->inst);
958 		}
959 		dsc_cfg.dc_dsc_cfg.num_slices_h *= opp_cnt;
960 		dsc_cfg.pic_width *= opp_cnt;
961 
962 		optc_dsc_mode = dsc_optc_cfg.is_pixel_format_444 ? OPTC_DSC_ENABLED_444 : OPTC_DSC_ENABLED_NATIVE_SUBSAMPLED;
963 
964 		/* Enable DSC in OPTC */
965 		DC_LOG_DSC("Setting optc DSC config for tg instance %d:", pipe_ctx->stream_res.tg->inst);
966 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(pipe_ctx->stream_res.tg,
967 							optc_dsc_mode,
968 							dsc_optc_cfg.bytes_per_pixel,
969 							dsc_optc_cfg.slice_width);
970 	} else {
971 		/* disable DSC in OPTC */
972 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(
973 				pipe_ctx->stream_res.tg,
974 				OPTC_DSC_DISABLED, 0, 0);
975 
976 		/* disable DSC block */
977 		dsc->funcs->dsc_disable(pipe_ctx->stream_res.dsc);
978 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
979 			ASSERT(odm_pipe->stream_res.dsc);
980 			odm_pipe->stream_res.dsc->funcs->dsc_disable(odm_pipe->stream_res.dsc);
981 		}
982 	}
983 }
984 
985 /*
986 * Given any pipe_ctx, return the total ODM combine factor, and optionally return
987 * the OPPids which are used
988 * */
989 static unsigned int get_odm_config(struct pipe_ctx *pipe_ctx, unsigned int *opp_instances)
990 {
991 	unsigned int opp_count = 1;
992 	struct pipe_ctx *odm_pipe;
993 
994 	/* First get to the top pipe */
995 	for (odm_pipe = pipe_ctx; odm_pipe->prev_odm_pipe; odm_pipe = odm_pipe->prev_odm_pipe)
996 		;
997 
998 	/* First pipe is always used */
999 	if (opp_instances)
1000 		opp_instances[0] = odm_pipe->stream_res.opp->inst;
1001 
1002 	/* Find and count odm pipes, if any */
1003 	for (odm_pipe = odm_pipe->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1004 		if (opp_instances)
1005 			opp_instances[opp_count] = odm_pipe->stream_res.opp->inst;
1006 		opp_count++;
1007 	}
1008 
1009 	return opp_count;
1010 }
1011 
1012 void dcn32_update_odm(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe_ctx)
1013 {
1014 	struct pipe_ctx *odm_pipe;
1015 	int opp_cnt = 0;
1016 	int opp_inst[MAX_PIPES] = {0};
1017 	bool rate_control_2x_pclk = (pipe_ctx->stream->timing.flags.INTERLACE || optc2_is_two_pixels_per_containter(&pipe_ctx->stream->timing));
1018 	struct mpc_dwb_flow_control flow_control;
1019 	struct mpc *mpc = dc->res_pool->mpc;
1020 	int i;
1021 
1022 	opp_cnt = get_odm_config(pipe_ctx, opp_inst);
1023 
1024 	if (opp_cnt > 1)
1025 		pipe_ctx->stream_res.tg->funcs->set_odm_combine(
1026 				pipe_ctx->stream_res.tg,
1027 				opp_inst, opp_cnt,
1028 				&pipe_ctx->stream->timing);
1029 	else
1030 		pipe_ctx->stream_res.tg->funcs->set_odm_bypass(
1031 				pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1032 
1033 	rate_control_2x_pclk = rate_control_2x_pclk || opp_cnt > 1;
1034 	flow_control.flow_ctrl_mode = 0;
1035 	flow_control.flow_ctrl_cnt0 = 0x80;
1036 	flow_control.flow_ctrl_cnt1 = calc_mpc_flow_ctrl_cnt(pipe_ctx->stream, opp_cnt);
1037 	if (mpc->funcs->set_out_rate_control) {
1038 		for (i = 0; i < opp_cnt; ++i) {
1039 			mpc->funcs->set_out_rate_control(
1040 					mpc, opp_inst[i],
1041 					true,
1042 					rate_control_2x_pclk,
1043 					&flow_control);
1044 		}
1045 	}
1046 
1047 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1048 		odm_pipe->stream_res.opp->funcs->opp_pipe_clock_control(
1049 				odm_pipe->stream_res.opp,
1050 				true);
1051 	}
1052 
1053 	if (pipe_ctx->stream_res.dsc) {
1054 		struct pipe_ctx *current_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[pipe_ctx->pipe_idx];
1055 
1056 		update_dsc_on_stream(pipe_ctx, pipe_ctx->stream->timing.flags.DSC);
1057 
1058 		/* Check if no longer using pipe for ODM, then need to disconnect DSC for that pipe */
1059 		if (!pipe_ctx->next_odm_pipe && current_pipe_ctx->next_odm_pipe &&
1060 				current_pipe_ctx->next_odm_pipe->stream_res.dsc) {
1061 			struct display_stream_compressor *dsc = current_pipe_ctx->next_odm_pipe->stream_res.dsc;
1062 			/* disconnect DSC block from stream */
1063 			dsc->funcs->dsc_disconnect(dsc);
1064 		}
1065 	}
1066 }
1067 
1068 unsigned int dcn32_calculate_dccg_k1_k2_values(struct pipe_ctx *pipe_ctx, unsigned int *k1_div, unsigned int *k2_div)
1069 {
1070 	struct dc_stream_state *stream = pipe_ctx->stream;
1071 	unsigned int odm_combine_factor = 0;
1072 	bool two_pix_per_container = false;
1073 
1074 	// For phantom pipes, use the same programming as the main pipes
1075 	if (pipe_ctx->stream->mall_stream_config.type == SUBVP_PHANTOM) {
1076 		stream = pipe_ctx->stream->mall_stream_config.paired_stream;
1077 	}
1078 	two_pix_per_container = optc2_is_two_pixels_per_containter(&stream->timing);
1079 	odm_combine_factor = get_odm_config(pipe_ctx, NULL);
1080 
1081 	if (link_is_dp_128b_132b_signal(pipe_ctx)) {
1082 		*k1_div = PIXEL_RATE_DIV_BY_1;
1083 		*k2_div = PIXEL_RATE_DIV_BY_1;
1084 	} else if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal) || dc_is_dvi_signal(pipe_ctx->stream->signal)) {
1085 		*k1_div = PIXEL_RATE_DIV_BY_1;
1086 		if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420)
1087 			*k2_div = PIXEL_RATE_DIV_BY_2;
1088 		else
1089 			*k2_div = PIXEL_RATE_DIV_BY_4;
1090 	} else if (dc_is_dp_signal(pipe_ctx->stream->signal) || dc_is_virtual_signal(pipe_ctx->stream->signal)) {
1091 		if (two_pix_per_container) {
1092 			*k1_div = PIXEL_RATE_DIV_BY_1;
1093 			*k2_div = PIXEL_RATE_DIV_BY_2;
1094 		} else {
1095 			*k1_div = PIXEL_RATE_DIV_BY_1;
1096 			*k2_div = PIXEL_RATE_DIV_BY_4;
1097 			if ((odm_combine_factor == 2) || dcn32_is_dp_dig_pixel_rate_div_policy(pipe_ctx))
1098 				*k2_div = PIXEL_RATE_DIV_BY_2;
1099 		}
1100 	}
1101 
1102 	if ((*k1_div == PIXEL_RATE_DIV_NA) && (*k2_div == PIXEL_RATE_DIV_NA))
1103 		ASSERT(false);
1104 
1105 	return odm_combine_factor;
1106 }
1107 
1108 void dcn32_set_pixels_per_cycle(struct pipe_ctx *pipe_ctx)
1109 {
1110 	uint32_t pix_per_cycle = 1;
1111 	uint32_t odm_combine_factor = 1;
1112 
1113 	if (!pipe_ctx || !pipe_ctx->stream || !pipe_ctx->stream_res.stream_enc)
1114 		return;
1115 
1116 	odm_combine_factor = get_odm_config(pipe_ctx, NULL);
1117 	if (optc2_is_two_pixels_per_containter(&pipe_ctx->stream->timing) || odm_combine_factor > 1
1118 		|| dcn32_is_dp_dig_pixel_rate_div_policy(pipe_ctx))
1119 		pix_per_cycle = 2;
1120 
1121 	if (pipe_ctx->stream_res.stream_enc->funcs->set_input_mode)
1122 		pipe_ctx->stream_res.stream_enc->funcs->set_input_mode(pipe_ctx->stream_res.stream_enc,
1123 				pix_per_cycle);
1124 }
1125 
1126 void dcn32_unblank_stream(struct pipe_ctx *pipe_ctx,
1127 		struct dc_link_settings *link_settings)
1128 {
1129 	struct encoder_unblank_param params = {0};
1130 	struct dc_stream_state *stream = pipe_ctx->stream;
1131 	struct dc_link *link = stream->link;
1132 	struct dce_hwseq *hws = link->dc->hwseq;
1133 	struct pipe_ctx *odm_pipe;
1134 	uint32_t pix_per_cycle = 1;
1135 
1136 	params.opp_cnt = 1;
1137 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
1138 		params.opp_cnt++;
1139 
1140 	/* only 3 items below are used by unblank */
1141 	params.timing = pipe_ctx->stream->timing;
1142 
1143 	params.link_settings.link_rate = link_settings->link_rate;
1144 
1145 	if (link_is_dp_128b_132b_signal(pipe_ctx)) {
1146 		/* TODO - DP2.0 HW: Set ODM mode in dp hpo encoder here */
1147 		pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_unblank(
1148 				pipe_ctx->stream_res.hpo_dp_stream_enc,
1149 				pipe_ctx->stream_res.tg->inst);
1150 	} else if (dc_is_dp_signal(pipe_ctx->stream->signal)) {
1151 		if (optc2_is_two_pixels_per_containter(&stream->timing) || params.opp_cnt > 1
1152 			|| dcn32_is_dp_dig_pixel_rate_div_policy(pipe_ctx)) {
1153 			params.timing.pix_clk_100hz /= 2;
1154 			pix_per_cycle = 2;
1155 		}
1156 		pipe_ctx->stream_res.stream_enc->funcs->dp_set_odm_combine(
1157 				pipe_ctx->stream_res.stream_enc, pix_per_cycle > 1);
1158 		pipe_ctx->stream_res.stream_enc->funcs->dp_unblank(link, pipe_ctx->stream_res.stream_enc, &params);
1159 	}
1160 
1161 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP)
1162 		hws->funcs.edp_backlight_control(link, true);
1163 }
1164 
1165 bool dcn32_is_dp_dig_pixel_rate_div_policy(struct pipe_ctx *pipe_ctx)
1166 {
1167 	struct dc *dc = pipe_ctx->stream->ctx->dc;
1168 
1169 	if (!is_h_timing_divisible_by_2(pipe_ctx->stream))
1170 		return false;
1171 
1172 	if (dc_is_dp_signal(pipe_ctx->stream->signal) && !link_is_dp_128b_132b_signal(pipe_ctx) &&
1173 		dc->debug.enable_dp_dig_pixel_rate_div_policy)
1174 		return true;
1175 	return false;
1176 }
1177 
1178 static void apply_symclk_on_tx_off_wa(struct dc_link *link)
1179 {
1180 	/* There are use cases where SYMCLK is referenced by OTG. For instance
1181 	 * for TMDS signal, OTG relies SYMCLK even if TX video output is off.
1182 	 * However current link interface will power off PHY when disabling link
1183 	 * output. This will turn off SYMCLK generated by PHY. The workaround is
1184 	 * to identify such case where SYMCLK is still in use by OTG when we
1185 	 * power off PHY. When this is detected, we will temporarily power PHY
1186 	 * back on and move PHY's SYMCLK state to SYMCLK_ON_TX_OFF by calling
1187 	 * program_pix_clk interface. When OTG is disabled, we will then power
1188 	 * off PHY by calling disable link output again.
1189 	 *
1190 	 * In future dcn generations, we plan to rework transmitter control
1191 	 * interface so that we could have an option to set SYMCLK ON TX OFF
1192 	 * state in one step without this workaround
1193 	 */
1194 
1195 	struct dc *dc = link->ctx->dc;
1196 	struct pipe_ctx *pipe_ctx = NULL;
1197 	uint8_t i;
1198 
1199 	if (link->phy_state.symclk_ref_cnts.otg > 0) {
1200 		for (i = 0; i < MAX_PIPES; i++) {
1201 			pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
1202 			if (pipe_ctx->stream && pipe_ctx->stream->link == link && pipe_ctx->top_pipe == NULL) {
1203 				pipe_ctx->clock_source->funcs->program_pix_clk(
1204 						pipe_ctx->clock_source,
1205 						&pipe_ctx->stream_res.pix_clk_params,
1206 						link_dp_get_encoding_format(&pipe_ctx->link_config.dp_link_settings),
1207 						&pipe_ctx->pll_settings);
1208 				link->phy_state.symclk_state = SYMCLK_ON_TX_OFF;
1209 				break;
1210 			}
1211 		}
1212 	}
1213 }
1214 
1215 void dcn32_disable_link_output(struct dc_link *link,
1216 		const struct link_resource *link_res,
1217 		enum signal_type signal)
1218 {
1219 	struct dc *dc = link->ctx->dc;
1220 	const struct link_hwss *link_hwss = get_link_hwss(link, link_res);
1221 	struct dmcu *dmcu = dc->res_pool->dmcu;
1222 
1223 	if (signal == SIGNAL_TYPE_EDP &&
1224 			link->dc->hwss.edp_backlight_control)
1225 		link->dc->hwss.edp_backlight_control(link, false);
1226 	else if (dmcu != NULL && dmcu->funcs->lock_phy)
1227 		dmcu->funcs->lock_phy(dmcu);
1228 
1229 	link_hwss->disable_link_output(link, link_res, signal);
1230 	link->phy_state.symclk_state = SYMCLK_OFF_TX_OFF;
1231 
1232 	if (signal == SIGNAL_TYPE_EDP &&
1233 			link->dc->hwss.edp_backlight_control)
1234 		link->dc->hwss.edp_power_control(link, false);
1235 	else if (dmcu != NULL && dmcu->funcs->lock_phy)
1236 		dmcu->funcs->unlock_phy(dmcu);
1237 
1238 	dp_source_sequence_trace(link, DPCD_SOURCE_SEQ_AFTER_DISABLE_LINK_PHY);
1239 
1240 	apply_symclk_on_tx_off_wa(link);
1241 }
1242 
1243 /* For SubVP the main pipe can have a viewport position change
1244  * without a full update. In this case we must also update the
1245  * viewport positions for the phantom pipe accordingly.
1246  */
1247 void dcn32_update_phantom_vp_position(struct dc *dc,
1248 		struct dc_state *context,
1249 		struct pipe_ctx *phantom_pipe)
1250 {
1251 	uint32_t i;
1252 	struct dc_plane_state *phantom_plane = phantom_pipe->plane_state;
1253 
1254 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1255 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1256 
1257 		if (pipe->stream && pipe->stream->mall_stream_config.type == SUBVP_MAIN &&
1258 				pipe->stream->mall_stream_config.paired_stream == phantom_pipe->stream) {
1259 			if (pipe->plane_state && pipe->plane_state->update_flags.bits.position_change) {
1260 
1261 				phantom_plane->src_rect.x = pipe->plane_state->src_rect.x;
1262 				phantom_plane->src_rect.y = pipe->plane_state->src_rect.y;
1263 				phantom_plane->clip_rect.x = pipe->plane_state->clip_rect.x;
1264 				phantom_plane->dst_rect.x = pipe->plane_state->dst_rect.x;
1265 				phantom_plane->dst_rect.y = pipe->plane_state->dst_rect.y;
1266 
1267 				phantom_pipe->plane_state->update_flags.bits.position_change = 1;
1268 				resource_build_scaling_params(phantom_pipe);
1269 				return;
1270 			}
1271 		}
1272 	}
1273 }
1274 
1275 /* Treat the phantom pipe as if it needs to be fully enabled.
1276  * If the pipe was previously in use but not phantom, it would
1277  * have been disabled earlier in the sequence so we need to run
1278  * the full enable sequence.
1279  */
1280 void dcn32_apply_update_flags_for_phantom(struct pipe_ctx *phantom_pipe)
1281 {
1282 	phantom_pipe->update_flags.raw = 0;
1283 	if (phantom_pipe->stream && phantom_pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
1284 		if (phantom_pipe->stream && phantom_pipe->plane_state) {
1285 			phantom_pipe->update_flags.bits.enable = 1;
1286 			phantom_pipe->update_flags.bits.mpcc = 1;
1287 			phantom_pipe->update_flags.bits.dppclk = 1;
1288 			phantom_pipe->update_flags.bits.hubp_interdependent = 1;
1289 			phantom_pipe->update_flags.bits.hubp_rq_dlg_ttu = 1;
1290 			phantom_pipe->update_flags.bits.gamut_remap = 1;
1291 			phantom_pipe->update_flags.bits.scaler = 1;
1292 			phantom_pipe->update_flags.bits.viewport = 1;
1293 			phantom_pipe->update_flags.bits.det_size = 1;
1294 			if (!phantom_pipe->top_pipe && !phantom_pipe->prev_odm_pipe) {
1295 				phantom_pipe->update_flags.bits.odm = 1;
1296 				phantom_pipe->update_flags.bits.global_sync = 1;
1297 			}
1298 		}
1299 	}
1300 }
1301 
1302 bool dcn32_dsc_pg_status(
1303 		struct dce_hwseq *hws,
1304 		unsigned int dsc_inst)
1305 {
1306 	uint32_t pwr_status = 0;
1307 
1308 	switch (dsc_inst) {
1309 	case 0: /* DSC0 */
1310 		REG_GET(DOMAIN16_PG_STATUS,
1311 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1312 		break;
1313 	case 1: /* DSC1 */
1314 
1315 		REG_GET(DOMAIN17_PG_STATUS,
1316 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1317 		break;
1318 	case 2: /* DSC2 */
1319 		REG_GET(DOMAIN18_PG_STATUS,
1320 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1321 		break;
1322 	case 3: /* DSC3 */
1323 		REG_GET(DOMAIN19_PG_STATUS,
1324 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1325 		break;
1326 	default:
1327 		BREAK_TO_DEBUGGER();
1328 		break;
1329 	}
1330 
1331 	return pwr_status == 0;
1332 }
1333 
1334 void dcn32_update_dsc_pg(struct dc *dc,
1335 		struct dc_state *context,
1336 		bool safe_to_disable)
1337 {
1338 	struct dce_hwseq *hws = dc->hwseq;
1339 	int i;
1340 
1341 	for (i = 0; i < dc->res_pool->res_cap->num_dsc; i++) {
1342 		struct display_stream_compressor *dsc = dc->res_pool->dscs[i];
1343 		bool is_dsc_ungated = hws->funcs.dsc_pg_status(hws, dsc->inst);
1344 
1345 		if (context->res_ctx.is_dsc_acquired[i]) {
1346 			if (!is_dsc_ungated) {
1347 				hws->funcs.dsc_pg_control(hws, dsc->inst, true);
1348 			}
1349 		} else if (safe_to_disable) {
1350 			if (is_dsc_ungated) {
1351 				hws->funcs.dsc_pg_control(hws, dsc->inst, false);
1352 			}
1353 		}
1354 	}
1355 }
1356 
1357 void dcn32_enable_phantom_streams(struct dc *dc, struct dc_state *context)
1358 {
1359 	unsigned int i;
1360 
1361 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1362 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1363 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1364 
1365 		/* If an active, non-phantom pipe is being transitioned into a phantom
1366 		 * pipe, wait for the double buffer update to complete first before we do
1367 		 * ANY phantom pipe programming.
1368 		 */
1369 		if (pipe->stream && pipe->stream->mall_stream_config.type == SUBVP_PHANTOM &&
1370 				old_pipe->stream && old_pipe->stream->mall_stream_config.type != SUBVP_PHANTOM) {
1371 			old_pipe->stream_res.tg->funcs->wait_for_state(
1372 					old_pipe->stream_res.tg,
1373 					CRTC_STATE_VBLANK);
1374 			old_pipe->stream_res.tg->funcs->wait_for_state(
1375 					old_pipe->stream_res.tg,
1376 					CRTC_STATE_VACTIVE);
1377 		}
1378 	}
1379 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1380 		struct pipe_ctx *new_pipe = &context->res_ctx.pipe_ctx[i];
1381 
1382 		if (new_pipe->stream && new_pipe->stream->mall_stream_config.type == SUBVP_PHANTOM) {
1383 			// If old context or new context has phantom pipes, apply
1384 			// the phantom timings now. We can't change the phantom
1385 			// pipe configuration safely without driver acquiring
1386 			// the DMCUB lock first.
1387 			dc->hwss.apply_ctx_to_hw(dc, context);
1388 			break;
1389 		}
1390 	}
1391 }
1392