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