xref: /linux/drivers/gpu/drm/amd/display/dc/hwss/dcn32/dcn32_hwseq.c (revision d4a292c5f8e65d2784b703c67179f4f7d0c7846c)
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 "dce/dmub_hw_lock_mgr.h"
51 #include "dcn32/dcn32_resource.h"
52 #include "link_service.h"
53 #include "../dcn20/dcn20_hwseq.h"
54 #include "dc_state_priv.h"
55 #include "dio/dcn10/dcn10_dio.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 #undef FN
67 #define FN(reg_name, field_name) \
68 	hws->shifts->field_name, hws->masks->field_name
69 
dcn32_dsc_pg_control(struct dce_hwseq * hws,unsigned int dsc_inst,bool power_on)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 	struct dc *dc = hws->ctx->dc;
79 
80 	if (dc->debug.disable_dsc_power_gate)
81 		return;
82 
83 	if (!dc->debug.enable_double_buffered_dsc_pg_support)
84 		return;
85 
86 	if (dc->debug.ignore_pg)
87 		return;
88 
89 	REG_GET(DC_IP_REQUEST_CNTL, IP_REQUEST_EN, &org_ip_request_cntl);
90 	if (org_ip_request_cntl == 0)
91 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 1);
92 
93 	DC_LOG_DSC("%s DSC power gate for inst %d", power_gate ? "enable" : "disable", dsc_inst);
94 	switch (dsc_inst) {
95 	case 0: /* DSC0 */
96 		REG_UPDATE(DOMAIN16_PG_CONFIG,
97 				DOMAIN_POWER_GATE, power_gate);
98 
99 		REG_WAIT(DOMAIN16_PG_STATUS,
100 				DOMAIN_PGFSM_PWR_STATUS, pwr_status,
101 				1, 1000);
102 		break;
103 	case 1: /* DSC1 */
104 		REG_UPDATE(DOMAIN17_PG_CONFIG,
105 				DOMAIN_POWER_GATE, power_gate);
106 
107 		REG_WAIT(DOMAIN17_PG_STATUS,
108 				DOMAIN_PGFSM_PWR_STATUS, pwr_status,
109 				1, 1000);
110 		break;
111 	case 2: /* DSC2 */
112 		REG_UPDATE(DOMAIN18_PG_CONFIG,
113 				DOMAIN_POWER_GATE, power_gate);
114 
115 		REG_WAIT(DOMAIN18_PG_STATUS,
116 				DOMAIN_PGFSM_PWR_STATUS, pwr_status,
117 				1, 1000);
118 		break;
119 	case 3: /* DSC3 */
120 		REG_UPDATE(DOMAIN19_PG_CONFIG,
121 				DOMAIN_POWER_GATE, power_gate);
122 
123 		REG_WAIT(DOMAIN19_PG_STATUS,
124 				DOMAIN_PGFSM_PWR_STATUS, pwr_status,
125 				1, 1000);
126 		break;
127 	default:
128 		BREAK_TO_DEBUGGER();
129 		break;
130 	}
131 
132 	if (org_ip_request_cntl == 0)
133 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 0);
134 }
135 
136 
dcn32_enable_power_gating_plane(struct dce_hwseq * hws,bool enable)137 void dcn32_enable_power_gating_plane(
138 	struct dce_hwseq *hws,
139 	bool enable)
140 {
141 	bool force_on = true; /* disable power gating */
142 	uint32_t org_ip_request_cntl = 0;
143 
144 	if (enable)
145 		force_on = false;
146 
147 	REG_GET(DC_IP_REQUEST_CNTL, IP_REQUEST_EN, &org_ip_request_cntl);
148 	if (org_ip_request_cntl == 0)
149 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 1);
150 
151 	/* DCHUBP0/1/2/3 */
152 	REG_UPDATE(DOMAIN0_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
153 	REG_UPDATE(DOMAIN1_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
154 	REG_UPDATE(DOMAIN2_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
155 	REG_UPDATE(DOMAIN3_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
156 
157 	/* DCS0/1/2/3 */
158 	REG_UPDATE(DOMAIN16_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
159 	REG_UPDATE(DOMAIN17_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
160 	REG_UPDATE(DOMAIN18_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
161 	REG_UPDATE(DOMAIN19_PG_CONFIG, DOMAIN_POWER_FORCEON, force_on);
162 
163 	if (org_ip_request_cntl == 0)
164 		REG_SET(DC_IP_REQUEST_CNTL, 0, IP_REQUEST_EN, 0);
165 }
166 
dcn32_hubp_pg_control(struct dce_hwseq * hws,unsigned int hubp_inst,bool power_on)167 void dcn32_hubp_pg_control(struct dce_hwseq *hws, unsigned int hubp_inst, bool power_on)
168 {
169 	uint32_t power_gate = power_on ? 0 : 1;
170 	uint32_t pwr_status = power_on ? 0 : 2;
171 
172 	if (hws->ctx->dc->debug.disable_hubp_power_gate)
173 		return;
174 
175 	if (hws->ctx->dc->debug.ignore_pg)
176 		return;
177 
178 	if (REG(DOMAIN0_PG_CONFIG) == 0)
179 		return;
180 
181 	switch (hubp_inst) {
182 	case 0:
183 		REG_SET(DOMAIN0_PG_CONFIG, 0, DOMAIN_POWER_GATE, power_gate);
184 		REG_WAIT(DOMAIN0_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, pwr_status, 1, 1000);
185 		break;
186 	case 1:
187 		REG_SET(DOMAIN1_PG_CONFIG, 0, DOMAIN_POWER_GATE, power_gate);
188 		REG_WAIT(DOMAIN1_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, pwr_status, 1, 1000);
189 		break;
190 	case 2:
191 		REG_SET(DOMAIN2_PG_CONFIG, 0, DOMAIN_POWER_GATE, power_gate);
192 		REG_WAIT(DOMAIN2_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, pwr_status, 1, 1000);
193 		break;
194 	case 3:
195 		REG_SET(DOMAIN3_PG_CONFIG, 0, DOMAIN_POWER_GATE, power_gate);
196 		REG_WAIT(DOMAIN3_PG_STATUS, DOMAIN_PGFSM_PWR_STATUS, pwr_status, 1, 1000);
197 		break;
198 	default:
199 		BREAK_TO_DEBUGGER();
200 		break;
201 	}
202 }
203 
dcn32_check_no_memory_request_for_cab(struct dc * dc)204 static bool dcn32_check_no_memory_request_for_cab(struct dc *dc)
205 {
206 	int i;
207 
208     /* First, check no-memory-request case */
209 	for (i = 0; i < dc->current_state->stream_count; i++) {
210 		if ((dc->current_state->stream_status[i].plane_count) &&
211 			(dc->current_state->streams[i]->link->psr_settings.psr_version == DC_PSR_VERSION_UNSUPPORTED))
212 			/* Fail eligibility on a visible stream */
213 			break;
214 	}
215 
216 	if (i == dc->current_state->stream_count)
217 		return true;
218 
219 	return false;
220 }
221 
222 
223 /* This function loops through every surface that needs to be cached in CAB for SS,
224  * and calculates the total number of ways required to store all surfaces (primary,
225  * meta, cursor).
226  */
dcn32_calculate_cab_allocation(struct dc * dc,struct dc_state * ctx)227 static uint32_t dcn32_calculate_cab_allocation(struct dc *dc, struct dc_state *ctx)
228 {
229 	int i;
230 	uint32_t num_ways = 0;
231 	uint32_t mall_ss_size_bytes = 0;
232 
233 	mall_ss_size_bytes = ctx->bw_ctx.bw.dcn.mall_ss_size_bytes;
234 	// TODO add additional logic for PSR active stream exclusion optimization
235 	// mall_ss_psr_active_size_bytes = ctx->bw_ctx.bw.dcn.mall_ss_psr_active_size_bytes;
236 
237 	// Include cursor size for CAB allocation
238 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
239 		struct pipe_ctx *pipe = &ctx->res_ctx.pipe_ctx[i];
240 
241 		if (!pipe->stream || !pipe->plane_state)
242 			continue;
243 
244 		mall_ss_size_bytes += dcn32_helper_calculate_mall_bytes_for_cursor(dc, pipe, false);
245 	}
246 
247 	// Convert number of cache lines required to number of ways
248 	if (dc->debug.force_mall_ss_num_ways > 0) {
249 		num_ways = dc->debug.force_mall_ss_num_ways;
250 	} else if (dc->res_pool->funcs->calculate_mall_ways_from_bytes) {
251 		num_ways = dc->res_pool->funcs->calculate_mall_ways_from_bytes(dc, mall_ss_size_bytes);
252 	} else {
253 		num_ways = 0;
254 	}
255 
256 	return num_ways;
257 }
258 
dcn32_apply_idle_power_optimizations(struct dc * dc,bool enable)259 bool dcn32_apply_idle_power_optimizations(struct dc *dc, bool enable)
260 {
261 	union dmub_rb_cmd cmd;
262 	uint8_t i;
263 	uint32_t ways;
264 	int j;
265 	bool mall_ss_unsupported = false;
266 	struct dc_plane_state *plane = NULL;
267 
268 	if (!dc->ctx->dmub_srv)
269 		return false;
270 
271 	for (i = 0; i < dc->current_state->stream_count; i++) {
272 		/* MALL SS messaging is not supported with PSR at this time */
273 		if (dc->current_state->streams[i] != NULL &&
274 				dc->current_state->streams[i]->link->psr_settings.psr_version != DC_PSR_VERSION_UNSUPPORTED &&
275 				(dc->current_state->stream_count > 1 || (!dc->current_state->streams[i]->dpms_off &&
276 						dc->current_state->stream_status[i].plane_count > 0)))
277 			return false;
278 	}
279 
280 	if (enable) {
281 		/* 1. Check no memory request case for CAB.
282 		 * If no memory request case, send CAB_ACTION NO_DF_REQ DMUB message
283 		 */
284 		if (dcn32_check_no_memory_request_for_cab(dc)) {
285 			/* Enable no-memory-requests case */
286 			memset(&cmd, 0, sizeof(cmd));
287 			cmd.cab.header.type = DMUB_CMD__CAB_FOR_SS;
288 			cmd.cab.header.sub_type = DMUB_CMD__CAB_NO_DCN_REQ;
289 			cmd.cab.header.payload_bytes = sizeof(cmd.cab) - sizeof(cmd.cab.header);
290 
291 			dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
292 
293 			return true;
294 		}
295 
296 		/* 2. Check if all surfaces can fit in CAB.
297 		 * If surfaces can fit into CAB, send CAB_ACTION_ALLOW DMUB message
298 		 * and configure HUBP's to fetch from MALL
299 		 */
300 		ways = dcn32_calculate_cab_allocation(dc, dc->current_state);
301 
302 		/* MALL not supported with Stereo3D or TMZ surface. If any plane is using stereo,
303 		 * or TMZ surface, don't try to enter MALL.
304 		 */
305 		for (i = 0; i < dc->current_state->stream_count; i++) {
306 			for (j = 0; j < dc->current_state->stream_status[i].plane_count; j++) {
307 				plane = dc->current_state->stream_status[i].plane_states[j];
308 
309 				if (plane->address.type == PLN_ADDR_TYPE_GRPH_STEREO ||
310 						plane->address.tmz_surface) {
311 					mall_ss_unsupported = true;
312 					break;
313 				}
314 			}
315 			if (mall_ss_unsupported)
316 				break;
317 		}
318 		if (ways <= dc->caps.cache_num_ways && !mall_ss_unsupported) {
319 			memset(&cmd, 0, sizeof(cmd));
320 			cmd.cab.header.type = DMUB_CMD__CAB_FOR_SS;
321 			cmd.cab.header.sub_type = DMUB_CMD__CAB_DCN_SS_FIT_IN_CAB;
322 			cmd.cab.header.payload_bytes = sizeof(cmd.cab) - sizeof(cmd.cab.header);
323 			cmd.cab.cab_alloc_ways = (uint8_t)ways;
324 
325 			dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_NO_WAIT);
326 			DC_LOG_MALL("enable scanout from MALL");
327 
328 			return true;
329 		}
330 
331 		DC_LOG_MALL("surface cannot fit in CAB, disabling scanout from MALL\n");
332 		return false;
333 	}
334 
335 	/* Disable CAB */
336 	memset(&cmd, 0, sizeof(cmd));
337 	cmd.cab.header.type = DMUB_CMD__CAB_FOR_SS;
338 	cmd.cab.header.sub_type = DMUB_CMD__CAB_NO_IDLE_OPTIMIZATION;
339 	cmd.cab.header.payload_bytes =
340 			sizeof(cmd.cab) - sizeof(cmd.cab.header);
341 
342 	dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT);
343 
344 	return true;
345 }
346 
347 /* Send DMCUB message with SubVP pipe info
348  * - For each pipe in context, populate payload with required SubVP information
349  *   if the pipe is using SubVP for MCLK switch
350  * - This function must be called while the DMUB HW lock is acquired by driver
351  */
dcn32_commit_subvp_config(struct dc * dc,struct dc_state * context)352 void dcn32_commit_subvp_config(struct dc *dc, struct dc_state *context)
353 {
354 	int i;
355 	bool enable_subvp = false;
356 
357 	if (!dc->ctx || !dc->ctx->dmub_srv)
358 		return;
359 
360 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
361 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
362 
363 		if (pipe_ctx->stream && dc_state_get_pipe_subvp_type(context, pipe_ctx) == SUBVP_MAIN) {
364 			// There is at least 1 SubVP pipe, so enable SubVP
365 			enable_subvp = true;
366 			break;
367 		}
368 	}
369 	dc_dmub_setup_subvp_dmub_command(dc, context, enable_subvp);
370 }
371 
372 /* Sub-Viewport DMUB lock needs to be acquired by driver whenever SubVP is active and:
373  * 1. Any full update for any SubVP main pipe
374  * 2. Any immediate flip for any SubVP pipe
375  * 3. Any flip for DRR pipe
376  * 4. If SubVP was previously in use (i.e. in old context)
377  */
dcn32_subvp_pipe_control_lock(struct dc * dc,struct dc_state * context,bool lock,bool should_lock_all_pipes,struct pipe_ctx * top_pipe_to_program,bool subvp_prev_use)378 void dcn32_subvp_pipe_control_lock(struct dc *dc,
379 		struct dc_state *context,
380 		bool lock,
381 		bool should_lock_all_pipes,
382 		struct pipe_ctx *top_pipe_to_program,
383 		bool subvp_prev_use)
384 {
385 	unsigned int i = 0;
386 	bool subvp_immediate_flip = false;
387 	bool subvp_in_use = false;
388 	struct pipe_ctx *pipe;
389 	enum mall_stream_type pipe_mall_type = SUBVP_NONE;
390 
391 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
392 		pipe = &context->res_ctx.pipe_ctx[i];
393 		pipe_mall_type = dc_state_get_pipe_subvp_type(context, pipe);
394 
395 		if (pipe->stream && pipe->plane_state && pipe_mall_type == SUBVP_MAIN) {
396 			subvp_in_use = true;
397 			break;
398 		}
399 	}
400 
401 	if (top_pipe_to_program && top_pipe_to_program->stream && top_pipe_to_program->plane_state) {
402 		if (dc_state_get_pipe_subvp_type(context, top_pipe_to_program) == SUBVP_MAIN &&
403 				top_pipe_to_program->plane_state->flip_immediate)
404 			subvp_immediate_flip = true;
405 	}
406 
407 	// Don't need to lock for DRR VSYNC flips -- FW will wait for DRR pending update cleared.
408 	if ((subvp_in_use && (should_lock_all_pipes || subvp_immediate_flip)) || (!subvp_in_use && subvp_prev_use)) {
409 		union dmub_inbox0_cmd_lock_hw hw_lock_cmd = { 0 };
410 
411 		if (!lock) {
412 			for (i = 0; i < dc->res_pool->pipe_count; i++) {
413 				pipe = &context->res_ctx.pipe_ctx[i];
414 				if (pipe->stream && pipe->plane_state && pipe_mall_type == SUBVP_MAIN &&
415 						should_lock_all_pipes)
416 					pipe->stream_res.tg->funcs->wait_for_state(pipe->stream_res.tg, CRTC_STATE_VBLANK);
417 			}
418 		}
419 
420 		hw_lock_cmd.bits.command_code = DMUB_INBOX0_CMD__HW_LOCK;
421 		hw_lock_cmd.bits.hw_lock_client = HW_LOCK_CLIENT_DRIVER;
422 		hw_lock_cmd.bits.lock = lock;
423 		hw_lock_cmd.bits.should_release = !lock;
424 		dmub_hw_lock_mgr_inbox0_cmd(dc->ctx->dmub_srv, hw_lock_cmd);
425 	}
426 }
427 
dcn32_subvp_pipe_control_lock_fast(union block_sequence_params * params)428 void dcn32_subvp_pipe_control_lock_fast(union block_sequence_params *params)
429 {
430 	struct dc *dc = params->subvp_pipe_control_lock_fast_params.dc;
431 	bool lock = params->subvp_pipe_control_lock_fast_params.lock;
432 	bool subvp_immediate_flip = params->subvp_pipe_control_lock_fast_params.subvp_immediate_flip;
433 
434 	// Don't need to lock for DRR VSYNC flips -- FW will wait for DRR pending update cleared.
435 	if (subvp_immediate_flip) {
436 		union dmub_inbox0_cmd_lock_hw hw_lock_cmd = { 0 };
437 
438 		hw_lock_cmd.bits.command_code = DMUB_INBOX0_CMD__HW_LOCK;
439 		hw_lock_cmd.bits.hw_lock_client = HW_LOCK_CLIENT_DRIVER;
440 		hw_lock_cmd.bits.lock = lock;
441 		hw_lock_cmd.bits.should_release = !lock;
442 		dmub_hw_lock_mgr_inbox0_cmd(dc->ctx->dmub_srv, hw_lock_cmd);
443 	}
444 }
445 
dcn32_set_mpc_shaper_3dlut(struct pipe_ctx * pipe_ctx,const struct dc_stream_state * stream)446 bool dcn32_set_mpc_shaper_3dlut(
447 	struct pipe_ctx *pipe_ctx, const struct dc_stream_state *stream)
448 {
449 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
450 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
451 	struct dc *dc = pipe_ctx->stream->ctx->dc;
452 	struct mpc *mpc = pipe_ctx->stream_res.opp->ctx->dc->res_pool->mpc;
453 	bool result = false;
454 
455 	const struct pwl_params *shaper_lut = NULL;
456 	//get the shaper lut params
457 	if (stream->func_shaper) {
458 		if (stream->func_shaper->type == TF_TYPE_HWPWL)
459 			shaper_lut = &stream->func_shaper->pwl;
460 		else if (stream->func_shaper->type == TF_TYPE_DISTRIBUTED_POINTS) {
461 			cm_helper_translate_curve_to_hw_format(stream->ctx,
462 					stream->func_shaper,
463 					&dpp_base->shaper_params, true);
464 			shaper_lut = &dpp_base->shaper_params;
465 		}
466 	}
467 
468 	if (stream->lut3d_func &&
469 		stream->lut3d_func->state.bits.initialized == 1) {
470 
471 		result = mpc->funcs->program_3dlut(mpc,	&stream->lut3d_func->lut_3d, mpcc_id);
472 		if (!result)
473 			DC_LOG_ERROR("%s: program_3dlut failed\n", __func__);
474 
475 		result = mpc->funcs->program_shaper(mpc, shaper_lut, mpcc_id);
476 		if (!result)
477 			DC_LOG_ERROR("%s: program_shaper failed\n", __func__);
478 	}
479 
480 	return result;
481 }
482 
dcn32_set_mcm_luts(struct pipe_ctx * pipe_ctx,const struct dc_plane_state * plane_state)483 bool dcn32_set_mcm_luts(
484 	struct pipe_ctx *pipe_ctx, const struct dc_plane_state *plane_state)
485 {
486 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
487 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
488 	struct mpc *mpc = pipe_ctx->stream_res.opp->ctx->dc->res_pool->mpc;
489 	bool rval, result;
490 	const struct pwl_params *lut_params = NULL;
491 
492 	// 1D LUT
493 	if (plane_state->blend_tf.type == TF_TYPE_HWPWL)
494 		lut_params = &plane_state->blend_tf.pwl;
495 	else if (plane_state->blend_tf.type == TF_TYPE_DISTRIBUTED_POINTS) {
496 		result = cm3_helper_translate_curve_to_hw_format(plane_state->ctx,
497 								 &plane_state->blend_tf,
498 								 &dpp_base->regamma_params, false);
499 		if (!result)
500 			return result;
501 
502 		lut_params = &dpp_base->regamma_params;
503 	}
504 	mpc->funcs->program_1dlut(mpc, lut_params, mpcc_id);
505 	lut_params = NULL;
506 
507 	// Shaper
508 	if (plane_state->in_shaper_func.type == TF_TYPE_HWPWL)
509 		lut_params = &plane_state->in_shaper_func.pwl;
510 	else if (plane_state->in_shaper_func.type == TF_TYPE_DISTRIBUTED_POINTS) {
511 		// TODO: dpp_base replace
512 		rval = cm3_helper_translate_curve_to_hw_format(plane_state->ctx,
513 							&plane_state->in_shaper_func,
514 							&dpp_base->shaper_params, true);
515 		lut_params = rval ? &dpp_base->shaper_params : NULL;
516 	}
517 
518 	mpc->funcs->program_shaper(mpc, lut_params, mpcc_id);
519 
520 	// 3D
521 	if (plane_state->lut3d_func.state.bits.initialized == 1)
522 		result = mpc->funcs->program_3dlut(mpc, &plane_state->lut3d_func.lut_3d, mpcc_id);
523 	else
524 		result = mpc->funcs->program_3dlut(mpc, NULL, mpcc_id);
525 
526 	return result;
527 }
528 
dcn32_set_input_transfer_func(struct dc * dc,struct pipe_ctx * pipe_ctx,const struct dc_plane_state * plane_state)529 bool dcn32_set_input_transfer_func(struct dc *dc,
530 				struct pipe_ctx *pipe_ctx,
531 				const struct dc_plane_state *plane_state)
532 {
533 	struct dce_hwseq *hws = dc->hwseq;
534 	struct mpc *mpc = dc->res_pool->mpc;
535 	struct dpp *dpp_base = pipe_ctx->plane_res.dpp;
536 
537 	enum dc_transfer_func_predefined tf;
538 	bool result = true;
539 	const struct pwl_params *params = NULL;
540 
541 	if (mpc == NULL || plane_state == NULL)
542 		return false;
543 
544 	tf = TRANSFER_FUNCTION_UNITY;
545 
546 	if (plane_state->in_transfer_func.type == TF_TYPE_PREDEFINED)
547 		tf = plane_state->in_transfer_func.tf;
548 
549 	dpp_base->funcs->dpp_set_pre_degam(dpp_base, tf);
550 
551 	if (plane_state->in_transfer_func.type == TF_TYPE_HWPWL)
552 		params = &plane_state->in_transfer_func.pwl;
553 	else if (plane_state->in_transfer_func.type == TF_TYPE_DISTRIBUTED_POINTS &&
554 		cm3_helper_translate_curve_to_hw_format(plane_state->ctx,
555 							&plane_state->in_transfer_func,
556 							&dpp_base->degamma_params, false))
557 		params = &dpp_base->degamma_params;
558 
559 	dpp_base->funcs->dpp_program_gamcor_lut(dpp_base, params);
560 
561 	if (pipe_ctx->stream_res.opp &&
562 			pipe_ctx->stream_res.opp->ctx &&
563 			hws->funcs.set_mcm_luts)
564 		result = hws->funcs.set_mcm_luts(pipe_ctx, plane_state);
565 
566 	return result;
567 }
568 
dcn32_set_output_transfer_func(struct dc * dc,struct pipe_ctx * pipe_ctx,const struct dc_stream_state * stream)569 bool dcn32_set_output_transfer_func(struct dc *dc,
570 				struct pipe_ctx *pipe_ctx,
571 				const struct dc_stream_state *stream)
572 {
573 	int mpcc_id = pipe_ctx->plane_res.hubp->inst;
574 	struct mpc *mpc = pipe_ctx->stream_res.opp->ctx->dc->res_pool->mpc;
575 	const struct pwl_params *params = NULL;
576 	bool ret = false;
577 
578 	/* program OGAM or 3DLUT only for the top pipe*/
579 	if (resource_is_pipe_type(pipe_ctx, OPP_HEAD)) {
580 		/*program shaper and 3dlut in MPC*/
581 		ret = dcn32_set_mpc_shaper_3dlut(pipe_ctx, stream);
582 		if (ret == false && mpc->funcs->set_output_gamma) {
583 			if (stream->out_transfer_func.type == TF_TYPE_HWPWL)
584 				params = &stream->out_transfer_func.pwl;
585 			else if (pipe_ctx->stream->out_transfer_func.type ==
586 					TF_TYPE_DISTRIBUTED_POINTS &&
587 					cm3_helper_translate_curve_to_hw_format(stream->ctx,
588 					&stream->out_transfer_func,
589 					&mpc->blender_params, false))
590 				params = &mpc->blender_params;
591 			/* there are no ROM LUTs in OUTGAM */
592 			if (stream->out_transfer_func.type == TF_TYPE_PREDEFINED)
593 				BREAK_TO_DEBUGGER();
594 		}
595 	}
596 
597 	if (mpc->funcs->set_output_gamma)
598 		mpc->funcs->set_output_gamma(mpc, mpcc_id, params);
599 
600 	return ret;
601 }
602 
603 /* Program P-State force value according to if pipe is using SubVP / FPO or not:
604  * 1. Reset P-State force on all pipes first
605  * 2. For each main pipe, force P-State disallow (P-State allow moderated by DMUB)
606  */
dcn32_update_force_pstate(struct dc * dc,struct dc_state * context)607 void dcn32_update_force_pstate(struct dc *dc, struct dc_state *context)
608 {
609 	int i;
610 
611 	/* Unforce p-state for each pipe if it is not FPO or SubVP.
612 	 * For FPO and SubVP, if it's already forced disallow, leave
613 	 * it as disallow.
614 	 */
615 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
616 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
617 		struct hubp *hubp = pipe->plane_res.hubp;
618 		struct dc_stream_status *stream_status = NULL;
619 
620 		if (pipe->stream)
621 			stream_status = dc_state_get_stream_status(context, pipe->stream);
622 
623 		if (!pipe->stream || !(dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_MAIN ||
624 		    (stream_status && stream_status->fpo_in_use))) {
625 			if (hubp && hubp->funcs->hubp_update_force_pstate_disallow)
626 				hubp->funcs->hubp_update_force_pstate_disallow(hubp, false);
627 			if (hubp && hubp->funcs->hubp_update_force_cursor_pstate_disallow)
628 				hubp->funcs->hubp_update_force_cursor_pstate_disallow(hubp, false);
629 		}
630 	}
631 
632 	/* Loop through each pipe -- for each subvp main pipe force p-state allow equal to false.
633 	 */
634 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
635 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
636 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
637 		struct hubp *hubp = pipe->plane_res.hubp;
638 		struct dc_stream_status *stream_status = NULL;
639 		struct dc_stream_status *old_stream_status = NULL;
640 
641 		/* Today for MED update type we do not call update clocks. However, for FPO
642 		 * the assumption is that update clocks should be called to disable P-State
643 		 * switch before any HW programming since FPO in FW and driver are not
644 		 * synchronized. This causes an issue where on a MED update, an FPO P-State
645 		 * switch could be taking place, then driver forces P-State disallow in the below
646 		 * code and prevents FPO from completing the sequence. In this case we add a check
647 		 * to avoid re-programming (and thus re-setting) the P-State force register by
648 		 * only reprogramming if the pipe was not previously Subvp or FPO. The assumption
649 		 * is that the P-State force register should be programmed correctly the first
650 		 * time SubVP / FPO was enabled, so there's no need to update / reset it if the
651 		 * pipe config has never exited SubVP / FPO.
652 		 */
653 		if (pipe->stream)
654 			stream_status = dc_state_get_stream_status(context, pipe->stream);
655 		if (old_pipe->stream)
656 			old_stream_status = dc_state_get_stream_status(dc->current_state, old_pipe->stream);
657 
658 		if (pipe->stream && (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_MAIN ||
659 				(stream_status && stream_status->fpo_in_use)) &&
660 				(!old_pipe->stream || (dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) != SUBVP_MAIN &&
661 				(old_stream_status && !old_stream_status->fpo_in_use)))) {
662 			if (hubp && hubp->funcs->hubp_update_force_pstate_disallow)
663 				hubp->funcs->hubp_update_force_pstate_disallow(hubp, true);
664 			if (hubp && hubp->funcs->hubp_update_force_cursor_pstate_disallow)
665 				hubp->funcs->hubp_update_force_cursor_pstate_disallow(hubp, true);
666 		}
667 	}
668 }
669 
670 /* Update MALL_SEL register based on if pipe / plane
671  * is a phantom pipe, main pipe, and if using MALL
672  * for SS.
673  */
dcn32_update_mall_sel(struct dc * dc,struct dc_state * context)674 void dcn32_update_mall_sel(struct dc *dc, struct dc_state *context)
675 {
676 	int i;
677 	unsigned int num_ways = dcn32_calculate_cab_allocation(dc, context);
678 	bool cache_cursor = false;
679 
680 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
681 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
682 		struct hubp *hubp = pipe->plane_res.hubp;
683 
684 		if (pipe->stream && pipe->plane_state && hubp && hubp->funcs->hubp_update_mall_sel) {
685 			int cursor_size = hubp->curs_attr.pitch * hubp->curs_attr.height;
686 
687 			switch (hubp->curs_attr.color_format) {
688 			case CURSOR_MODE_MONO:
689 				cursor_size /= 2;
690 				break;
691 			case CURSOR_MODE_COLOR_1BIT_AND:
692 			case CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA:
693 			case CURSOR_MODE_COLOR_UN_PRE_MULTIPLIED_ALPHA:
694 				cursor_size *= 4;
695 				break;
696 
697 			case CURSOR_MODE_COLOR_64BIT_FP_PRE_MULTIPLIED:
698 			case CURSOR_MODE_COLOR_64BIT_FP_UN_PRE_MULTIPLIED:
699 			default:
700 				cursor_size *= 8;
701 				break;
702 			}
703 
704 			if (cursor_size > 16384)
705 				cache_cursor = true;
706 
707 			if (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM) {
708 				hubp->funcs->hubp_update_mall_sel(hubp, 1, false);
709 			} else {
710 				// MALL not supported with Stereo3D
711 				hubp->funcs->hubp_update_mall_sel(hubp,
712 					num_ways <= dc->caps.cache_num_ways &&
713 					pipe->stream->link->psr_settings.psr_version == DC_PSR_VERSION_UNSUPPORTED &&
714 					pipe->plane_state->address.type !=  PLN_ADDR_TYPE_GRPH_STEREO &&
715 					!pipe->plane_state->address.tmz_surface ? 2 : 0,
716 							cache_cursor);
717 			}
718 		}
719 	}
720 }
721 
722 /* Program the sub-viewport pipe configuration after the main / phantom pipes
723  * have been programmed in hardware.
724  * 1. Update force P-State for all the main pipes (disallow P-state)
725  * 2. Update MALL_SEL register
726  * 3. Program FORCE_ONE_ROW_FOR_FRAME for main subvp pipes
727  */
dcn32_program_mall_pipe_config(struct dc * dc,struct dc_state * context)728 void dcn32_program_mall_pipe_config(struct dc *dc, struct dc_state *context)
729 {
730 	int i;
731 	struct dce_hwseq *hws = dc->hwseq;
732 
733 	// Don't force p-state disallow -- can't block dummy p-state
734 
735 	// Update MALL_SEL register for each pipe
736 	if (hws && hws->funcs.update_mall_sel)
737 		hws->funcs.update_mall_sel(dc, context);
738 
739 	// Program FORCE_ONE_ROW_FOR_FRAME and CURSOR_REQ_MODE for main subvp pipes
740 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
741 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
742 		struct hubp *hubp = pipe->plane_res.hubp;
743 
744 		if (pipe->stream && hubp && hubp->funcs->hubp_prepare_subvp_buffering) {
745 			/* TODO - remove setting CURSOR_REQ_MODE to 0 for legacy cases
746 			 *      - need to investigate single pipe MPO + SubVP case to
747 			 *        see if CURSOR_REQ_MODE will be back to 1 for SubVP
748 			 *        when it should be 0 for MPO
749 			 */
750 			if (dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_MAIN)
751 				hubp->funcs->hubp_prepare_subvp_buffering(hubp, true);
752 		}
753 	}
754 }
755 
dcn32_initialize_min_clocks(struct dc * dc)756 static void dcn32_initialize_min_clocks(struct dc *dc)
757 {
758 	struct dc_clocks *clocks = &dc->current_state->bw_ctx.bw.dcn.clk;
759 
760 	clocks->dcfclk_deep_sleep_khz = DCN3_2_DCFCLK_DS_INIT_KHZ;
761 	clocks->dcfclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].dcfclk_mhz * 1000;
762 	clocks->socclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].socclk_mhz * 1000;
763 	clocks->dramclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].memclk_mhz * 1000;
764 	clocks->dppclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].dppclk_mhz * 1000;
765 	clocks->ref_dtbclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].dtbclk_mhz * 1000;
766 	clocks->fclk_p_state_change_support = true;
767 	clocks->p_state_change_support = true;
768 	if (dc->debug.disable_boot_optimizations) {
769 		clocks->dispclk_khz = dc->clk_mgr->bw_params->clk_table.entries[0].dispclk_mhz * 1000;
770 	} else {
771 		/* Even though DPG_EN = 1 for the connected display, it still requires the
772 		 * correct timing so we cannot set DISPCLK to min freq or it could cause
773 		 * audio corruption. Read current DISPCLK from DENTIST and request the same
774 		 * freq to ensure that the timing is valid and unchanged.
775 		 */
776 		clocks->dispclk_khz = dc->clk_mgr->funcs->get_dispclk_from_dentist(dc->clk_mgr);
777 	}
778 
779 	dc->clk_mgr->funcs->update_clocks(
780 			dc->clk_mgr,
781 			dc->current_state,
782 			true);
783 }
784 
dcn32_init_hw(struct dc * dc)785 void dcn32_init_hw(struct dc *dc)
786 {
787 	struct abm **abms = dc->res_pool->multiple_abms;
788 	struct dce_hwseq *hws = dc->hwseq;
789 	struct dc_bios *dcb = dc->ctx->dc_bios;
790 	struct resource_pool *res_pool = dc->res_pool;
791 	int i;
792 	int edp_num;
793 	uint32_t backlight = MAX_BACKLIGHT_LEVEL;
794 	uint32_t user_level = MAX_BACKLIGHT_LEVEL;
795 
796 	if (dc->clk_mgr && dc->clk_mgr->funcs && dc->clk_mgr->funcs->init_clocks)
797 		dc->clk_mgr->funcs->init_clocks(dc->clk_mgr);
798 
799 	// Initialize the dccg
800 	if (res_pool->dccg->funcs->dccg_init)
801 		res_pool->dccg->funcs->dccg_init(res_pool->dccg);
802 
803 	if (!dcb->funcs->is_accelerated_mode(dcb)) {
804 		hws->funcs.bios_golden_init(dc);
805 		hws->funcs.disable_vga(dc->hwseq);
806 	}
807 
808 	// Set default OPTC memory power states
809 	if (dc->debug.enable_mem_low_power.bits.optc) {
810 		// Shutdown when unassigned and light sleep in VBLANK
811 		REG_SET_2(ODM_MEM_PWR_CTRL3, 0, ODM_MEM_UNASSIGNED_PWR_MODE, 3, ODM_MEM_VBLANK_PWR_MODE, 1);
812 	}
813 
814 	if (dc->debug.enable_mem_low_power.bits.vga) {
815 		// Power down VGA memory
816 		REG_UPDATE(MMHUBBUB_MEM_PWR_CNTL, VGA_MEM_PWR_FORCE, 1);
817 	}
818 
819 	if (dc->ctx->dc_bios->fw_info_valid) {
820 		res_pool->ref_clocks.xtalin_clock_inKhz =
821 				dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency;
822 
823 		if (res_pool->hubbub) {
824 			(res_pool->dccg->funcs->get_dccg_ref_freq)(res_pool->dccg,
825 					dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency,
826 					&res_pool->ref_clocks.dccg_ref_clock_inKhz);
827 
828 			(res_pool->hubbub->funcs->get_dchub_ref_freq)(res_pool->hubbub,
829 					res_pool->ref_clocks.dccg_ref_clock_inKhz,
830 					&res_pool->ref_clocks.dchub_ref_clock_inKhz);
831 		} else {
832 			// Not all ASICs have DCCG sw component
833 			res_pool->ref_clocks.dccg_ref_clock_inKhz =
834 					res_pool->ref_clocks.xtalin_clock_inKhz;
835 			res_pool->ref_clocks.dchub_ref_clock_inKhz =
836 					res_pool->ref_clocks.xtalin_clock_inKhz;
837 		}
838 	} else
839 		ASSERT_CRITICAL(false);
840 
841 	for (i = 0; i < dc->link_count; i++) {
842 		/* Power up AND update implementation according to the
843 		 * required signal (which may be different from the
844 		 * default signal on connector).
845 		 */
846 		struct dc_link *link = dc->links[i];
847 
848 		link->link_enc->funcs->hw_init(link->link_enc);
849 
850 		/* Check for enabled DIG to identify enabled display */
851 		if (link->link_enc->funcs->is_dig_enabled &&
852 			link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
853 			link->link_status.link_active = true;
854 			link->phy_state.symclk_state = SYMCLK_ON_TX_ON;
855 			if (link->link_enc->funcs->fec_is_active &&
856 					link->link_enc->funcs->fec_is_active(link->link_enc))
857 				link->fec_state = dc_link_fec_enabled;
858 		}
859 	}
860 
861 	/* enable_power_gating_plane before dsc_pg_control because
862 	 * FORCEON = 1 with hw default value on bootup, resume from s3
863 	 */
864 	if (hws->funcs.enable_power_gating_plane)
865 		hws->funcs.enable_power_gating_plane(dc->hwseq, true);
866 
867 	/* we want to turn off all dp displays before doing detection */
868 	dc->link_srv->blank_all_dp_displays(dc);
869 
870 	/* If taking control over from VBIOS, we may want to optimize our first
871 	 * mode set, so we need to skip powering down pipes until we know which
872 	 * pipes we want to use.
873 	 * Otherwise, if taking control is not possible, we need to power
874 	 * everything down.
875 	 */
876 	if (dcb->funcs->is_accelerated_mode(dcb) || !dc->config.seamless_boot_edp_requested) {
877 		/* Disable boot optimizations means power down everything including PHY, DIG,
878 		 * and OTG (i.e. the boot is not optimized because we do a full power down).
879 		 */
880 		if (dc->hwss.enable_accelerated_mode && dc->debug.disable_boot_optimizations)
881 			dc->hwss.enable_accelerated_mode(dc, dc->current_state);
882 		else
883 			hws->funcs.init_pipes(dc, dc->current_state);
884 
885 		if (dc->res_pool->hubbub->funcs->allow_self_refresh_control)
886 			dc->res_pool->hubbub->funcs->allow_self_refresh_control(dc->res_pool->hubbub,
887 					!dc->res_pool->hubbub->ctx->dc->debug.disable_stutter);
888 
889 		dcn32_initialize_min_clocks(dc);
890 
891 		/* On HW init, allow idle optimizations after pipes have been turned off.
892 		 *
893 		 * In certain D3 cases (i.e. BOCO / BOMACO) it's possible that hardware state
894 		 * is reset (i.e. not in idle at the time hw init is called), but software state
895 		 * still has idle_optimizations = true, so we must disable idle optimizations first
896 		 * (i.e. set false), then re-enable (set true).
897 		 */
898 		dc_allow_idle_optimizations(dc, false);
899 		dc_allow_idle_optimizations(dc, true);
900 	}
901 
902 	/* In headless boot cases, DIG may be turned
903 	 * on which causes HW/SW discrepancies.
904 	 * To avoid this, power down hardware on boot
905 	 * if DIG is turned on and seamless boot not enabled
906 	 */
907 	if (!dc->config.seamless_boot_edp_requested) {
908 		struct dc_link *edp_links[MAX_NUM_EDP];
909 		struct dc_link *edp_link;
910 
911 		dc_get_edp_links(dc, edp_links, &edp_num);
912 		if (edp_num) {
913 			for (i = 0; i < edp_num; i++) {
914 				edp_link = edp_links[i];
915 				if (edp_link->link_enc->funcs->is_dig_enabled &&
916 						edp_link->link_enc->funcs->is_dig_enabled(edp_link->link_enc) &&
917 						dc->hwss.edp_backlight_control &&
918 						hws->funcs.power_down &&
919 						dc->hwss.edp_power_control) {
920 					dc->hwss.edp_backlight_control(edp_link, false);
921 					hws->funcs.power_down(dc);
922 					dc->hwss.edp_power_control(edp_link, false);
923 				}
924 			}
925 		} else {
926 			for (i = 0; i < dc->link_count; i++) {
927 				struct dc_link *link = dc->links[i];
928 
929 				if (link->link_enc->funcs->is_dig_enabled &&
930 						link->link_enc->funcs->is_dig_enabled(link->link_enc) &&
931 						hws->funcs.power_down) {
932 					hws->funcs.power_down(dc);
933 					break;
934 				}
935 
936 			}
937 		}
938 	}
939 
940 	for (i = 0; i < res_pool->audio_count; i++) {
941 		struct audio *audio = res_pool->audios[i];
942 
943 		audio->funcs->hw_init(audio);
944 	}
945 
946 	for (i = 0; i < dc->link_count; i++) {
947 		struct dc_link *link = dc->links[i];
948 
949 		if (link->panel_cntl) {
950 			backlight = link->panel_cntl->funcs->hw_init(link->panel_cntl);
951 			user_level = link->panel_cntl->stored_backlight_registers.USER_LEVEL;
952 		}
953 	}
954 
955 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
956 		if (abms[i] != NULL && abms[i]->funcs != NULL)
957 			abms[i]->funcs->abm_init(abms[i], backlight, user_level);
958 	}
959 
960 	/* power AFMT HDMI memory TODO: may move to dis/en output save power*/
961 	if (dc->res_pool->dio && dc->res_pool->dio->funcs->mem_pwr_ctrl)
962 		dc->res_pool->dio->funcs->mem_pwr_ctrl(dc->res_pool->dio, false);
963 
964 	if (!dc->debug.disable_clock_gate) {
965 		/* enable all DCN clock gating */
966 		if (dc->res_pool->dccg && dc->res_pool->dccg->funcs && dc->res_pool->dccg->funcs->allow_clock_gating)
967 			dc->res_pool->dccg->funcs->allow_clock_gating(dc->res_pool->dccg, true);
968 
969 		REG_UPDATE(DCFCLK_CNTL, DCFCLK_GATE_DIS, 0);
970 	}
971 
972 	if (!dcb->funcs->is_accelerated_mode(dcb) && dc->res_pool->hubbub->funcs->init_watermarks)
973 		dc->res_pool->hubbub->funcs->init_watermarks(dc->res_pool->hubbub);
974 
975 	if (dc->clk_mgr && dc->clk_mgr->funcs && dc->clk_mgr->funcs->notify_wm_ranges)
976 		dc->clk_mgr->funcs->notify_wm_ranges(dc->clk_mgr);
977 
978 	if (dc->clk_mgr && dc->clk_mgr->funcs && dc->clk_mgr->funcs->set_hard_max_memclk &&
979 	    !dc->clk_mgr->dc_mode_softmax_enabled)
980 		dc->clk_mgr->funcs->set_hard_max_memclk(dc->clk_mgr);
981 
982 	if (dc->res_pool->hubbub->funcs->force_pstate_change_control)
983 		dc->res_pool->hubbub->funcs->force_pstate_change_control(
984 				dc->res_pool->hubbub, false, false);
985 
986 	if (dc->res_pool->hubbub->funcs->init_crb)
987 		dc->res_pool->hubbub->funcs->init_crb(dc->res_pool->hubbub);
988 
989 	if (dc->res_pool->hubbub->funcs->set_request_limit && dc->config.sdpif_request_limit_words_per_umc > 0)
990 		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);
991 
992 	// Get DMCUB capabilities
993 	if (dc->ctx->dmub_srv) {
994 		dc_dmub_srv_query_caps_cmd(dc->ctx->dmub_srv);
995 		dc->caps.dmub_caps.psr = dc->ctx->dmub_srv->dmub->feature_caps.psr;
996 		dc->caps.dmub_caps.subvp_psr = dc->ctx->dmub_srv->dmub->feature_caps.subvp_psr_support;
997 		dc->caps.dmub_caps.gecc_enable = dc->ctx->dmub_srv->dmub->feature_caps.gecc_enable;
998 		dc->caps.dmub_caps.mclk_sw = dc->ctx->dmub_srv->dmub->feature_caps.fw_assisted_mclk_switch_ver;
999 		dc->caps.dmub_caps.aux_backlight_support = dc->ctx->dmub_srv->dmub->feature_caps.abm_aux_backlight_support;
1000 
1001 		/* for DCN401 testing only */
1002 		dc->caps.dmub_caps.fams_ver = dc->ctx->dmub_srv->dmub->feature_caps.fw_assisted_mclk_switch_ver;
1003 		if (dc->caps.dmub_caps.fams_ver == 2) {
1004 			/* FAMS2 is enabled */
1005 			dc->debug.fams2_config.bits.enable &= true;
1006 		} else if (dc->ctx->dmub_srv->dmub->fw_version <
1007 				DMUB_FW_VERSION(7, 0, 35)) {
1008 			/* FAMS2 is disabled */
1009 			dc->debug.fams2_config.bits.enable = false;
1010 			if (dc->debug.using_dml2 && dc->res_pool->funcs->update_bw_bounding_box) {
1011 				/* update bounding box if FAMS2 disabled */
1012 				dc->res_pool->funcs->update_bw_bounding_box(dc, dc->clk_mgr->bw_params);
1013 			}
1014 			dc->debug.force_disable_subvp = true;
1015 			dc->debug.disable_fpo_optimizations = true;
1016 		}
1017 	}
1018 }
1019 
dcn32_update_dsc_on_stream(struct pipe_ctx * pipe_ctx,bool enable)1020 void dcn32_update_dsc_on_stream(struct pipe_ctx *pipe_ctx, bool enable)
1021 {
1022 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
1023 	struct dc *dc = pipe_ctx->stream->ctx->dc;
1024 	struct dc_stream_state *stream = pipe_ctx->stream;
1025 	struct pipe_ctx *odm_pipe;
1026 	int opp_cnt = 1;
1027 	struct dccg *dccg = dc->res_pool->dccg;
1028 	/* It has been found that when DSCCLK is lower than 16Mhz, we will get DCN
1029 	 * register access hung. When DSCCLk is based on refclk, DSCCLk is always a
1030 	 * fixed value higher than 16Mhz so the issue doesn't occur. When DSCCLK is
1031 	 * generated by DTO, DSCCLK would be based on 1/3 dispclk. For small timings
1032 	 * with DSC such as 480p60Hz, the dispclk could be low enough to trigger
1033 	 * this problem. We are implementing a workaround here to keep using dscclk
1034 	 * based on fixed value refclk when timing is smaller than 3x16Mhz (i.e
1035 	 * 48Mhz) pixel clock to avoid hitting this problem.
1036 	 */
1037 	bool should_use_dto_dscclk = (dccg->funcs->set_dto_dscclk != NULL) &&
1038 			stream->timing.pix_clk_100hz > 480000;
1039 
1040 	ASSERT(dsc);
1041 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
1042 		opp_cnt++;
1043 
1044 	if (enable) {
1045 		struct dsc_config dsc_cfg;
1046 		struct dsc_optc_config dsc_optc_cfg = {0};
1047 		enum optc_dsc_mode optc_dsc_mode;
1048 		struct dcn_dsc_state dsc_state = {0};
1049 
1050 		if (!dsc) {
1051 			DC_LOG_DSC("DSC is NULL for tg instance %d:", pipe_ctx->stream_res.tg->inst);
1052 			return;
1053 		}
1054 
1055 		if (dsc->funcs->dsc_read_state) {
1056 			dsc->funcs->dsc_read_state(dsc, &dsc_state);
1057 			if (!dsc_state.dsc_fw_en) {
1058 				DC_LOG_DSC("DSC has been disabled for tg instance %d:", pipe_ctx->stream_res.tg->inst);
1059 				return;
1060 			}
1061 		}
1062 
1063 		/* Enable DSC hw block */
1064 		dsc_cfg.pic_width = (stream->timing.h_addressable + pipe_ctx->dsc_padding_params.dsc_hactive_padding +
1065 				stream->timing.h_border_left + stream->timing.h_border_right) / opp_cnt;
1066 		dsc_cfg.pic_height = stream->timing.v_addressable + stream->timing.v_border_top + stream->timing.v_border_bottom;
1067 		dsc_cfg.pixel_encoding = stream->timing.pixel_encoding;
1068 		dsc_cfg.color_depth = stream->timing.display_color_depth;
1069 		dsc_cfg.is_odm = pipe_ctx->next_odm_pipe ? true : false;
1070 		dsc_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
1071 		ASSERT(dsc_cfg.dc_dsc_cfg.num_slices_h % opp_cnt == 0);
1072 		dsc_cfg.dc_dsc_cfg.num_slices_h /= opp_cnt;
1073 		dsc_cfg.dsc_padding = 0;
1074 
1075 		if (should_use_dto_dscclk)
1076 			dccg->funcs->set_dto_dscclk(dccg, dsc->inst, dsc_cfg.dc_dsc_cfg.num_slices_h);
1077 		dsc->funcs->dsc_set_config(dsc, &dsc_cfg, &dsc_optc_cfg);
1078 		dsc->funcs->dsc_enable(dsc, pipe_ctx->stream_res.opp->inst);
1079 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1080 			struct display_stream_compressor *odm_dsc = odm_pipe->stream_res.dsc;
1081 
1082 			ASSERT(odm_dsc);
1083 			if (!odm_dsc)
1084 				continue;
1085 			if (should_use_dto_dscclk)
1086 				dccg->funcs->set_dto_dscclk(dccg, odm_dsc->inst, dsc_cfg.dc_dsc_cfg.num_slices_h);
1087 			odm_dsc->funcs->dsc_set_config(odm_dsc, &dsc_cfg, &dsc_optc_cfg);
1088 			odm_dsc->funcs->dsc_enable(odm_dsc, odm_pipe->stream_res.opp->inst);
1089 		}
1090 		optc_dsc_mode = dsc_optc_cfg.is_pixel_format_444 ? OPTC_DSC_ENABLED_444 : OPTC_DSC_ENABLED_NATIVE_SUBSAMPLED;
1091 		/* Enable DSC in OPTC */
1092 		DC_LOG_DSC("Setting optc DSC config for tg instance %d:", pipe_ctx->stream_res.tg->inst);
1093 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(pipe_ctx->stream_res.tg,
1094 							optc_dsc_mode,
1095 							dsc_optc_cfg.bytes_per_pixel,
1096 							dsc_optc_cfg.slice_width);
1097 	} else {
1098 		/* disable DSC in OPTC */
1099 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(
1100 				pipe_ctx->stream_res.tg,
1101 				OPTC_DSC_DISABLED, 0, 0);
1102 
1103 		/* only disconnect DSC block, DSC is disabled when OPP head pipe is reset */
1104 		dsc->funcs->dsc_disconnect(pipe_ctx->stream_res.dsc);
1105 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1106 			ASSERT(odm_pipe->stream_res.dsc);
1107 			odm_pipe->stream_res.dsc->funcs->dsc_disconnect(odm_pipe->stream_res.dsc);
1108 		}
1109 	}
1110 }
1111 
1112 /*
1113 * Given any pipe_ctx, return the total ODM combine factor, and optionally return
1114 * the OPPids which are used
1115 * */
get_odm_config(struct pipe_ctx * pipe_ctx,unsigned int * opp_instances)1116 static unsigned int get_odm_config(struct pipe_ctx *pipe_ctx, unsigned int *opp_instances)
1117 {
1118 	unsigned int opp_count = 1;
1119 	struct pipe_ctx *odm_pipe;
1120 
1121 	/* First get to the top pipe */
1122 	for (odm_pipe = pipe_ctx; odm_pipe->prev_odm_pipe; odm_pipe = odm_pipe->prev_odm_pipe)
1123 		;
1124 
1125 	/* First pipe is always used */
1126 	if (opp_instances)
1127 		opp_instances[0] = odm_pipe->stream_res.opp->inst;
1128 
1129 	/* Find and count odm pipes, if any */
1130 	for (odm_pipe = odm_pipe->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1131 		if (opp_instances)
1132 			opp_instances[opp_count] = odm_pipe->stream_res.opp->inst;
1133 		opp_count++;
1134 	}
1135 
1136 	return opp_count;
1137 }
1138 
dcn32_update_odm(struct dc * dc,struct dc_state * context,struct pipe_ctx * pipe_ctx)1139 void dcn32_update_odm(struct dc *dc, struct dc_state *context, struct pipe_ctx *pipe_ctx)
1140 {
1141 	struct pipe_ctx *odm_pipe;
1142 	int opp_cnt = 0;
1143 	int opp_inst[MAX_PIPES] = {0};
1144 	int odm_slice_width = resource_get_odm_slice_dst_width(pipe_ctx, false);
1145 	int last_odm_slice_width = resource_get_odm_slice_dst_width(pipe_ctx, true);
1146 
1147 	opp_cnt = get_odm_config(pipe_ctx, opp_inst);
1148 
1149 	if (opp_cnt > 1)
1150 		pipe_ctx->stream_res.tg->funcs->set_odm_combine(
1151 				pipe_ctx->stream_res.tg,
1152 				opp_inst, opp_cnt,
1153 				odm_slice_width, last_odm_slice_width);
1154 	else
1155 		pipe_ctx->stream_res.tg->funcs->set_odm_bypass(
1156 				pipe_ctx->stream_res.tg, &pipe_ctx->stream->timing);
1157 
1158 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1159 		odm_pipe->stream_res.opp->funcs->opp_pipe_clock_control(
1160 				odm_pipe->stream_res.opp,
1161 				true);
1162 		odm_pipe->stream_res.opp->funcs->opp_program_left_edge_extra_pixel(
1163 				odm_pipe->stream_res.opp,
1164 				pipe_ctx->stream->timing.pixel_encoding,
1165 				resource_is_pipe_type(odm_pipe, OTG_MASTER));
1166 	}
1167 
1168 	if (pipe_ctx->stream_res.dsc) {
1169 		struct pipe_ctx *current_pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[pipe_ctx->pipe_idx];
1170 
1171 		dcn32_update_dsc_on_stream(pipe_ctx, pipe_ctx->stream->timing.flags.DSC);
1172 
1173 		/* Check if no longer using pipe for ODM, then need to disconnect DSC for that pipe */
1174 		if (!pipe_ctx->next_odm_pipe && current_pipe_ctx->next_odm_pipe &&
1175 				current_pipe_ctx->next_odm_pipe->stream_res.dsc) {
1176 			struct display_stream_compressor *dsc = current_pipe_ctx->next_odm_pipe->stream_res.dsc;
1177 
1178 			/* disconnect DSC block from stream */
1179 			dsc->funcs->dsc_disconnect(dsc);
1180 		}
1181 	}
1182 
1183 	if (!resource_is_pipe_type(pipe_ctx, DPP_PIPE))
1184 		/*
1185 		 * blank pattern is generated by OPP, reprogram blank pattern
1186 		 * due to OPP count change
1187 		 */
1188 		dc->hwseq->funcs.blank_pixel_data(dc, pipe_ctx, true);
1189 }
1190 
dcn32_calculate_dccg_k1_k2_values(struct pipe_ctx * pipe_ctx,unsigned int * k1_div,unsigned int * k2_div)1191 unsigned int dcn32_calculate_dccg_k1_k2_values(struct pipe_ctx *pipe_ctx, unsigned int *k1_div, unsigned int *k2_div)
1192 {
1193 	struct dc_stream_state *stream = pipe_ctx->stream;
1194 	unsigned int odm_combine_factor = 0;
1195 	bool two_pix_per_container = false;
1196 	struct dce_hwseq *hws = stream->ctx->dc->hwseq;
1197 
1198 	two_pix_per_container = pipe_ctx->stream_res.tg->funcs->is_two_pixels_per_container(&stream->timing);
1199 	odm_combine_factor = get_odm_config(pipe_ctx, NULL);
1200 
1201 	if (stream->ctx->dc->link_srv->dp_is_128b_132b_signal(pipe_ctx)) {
1202 		*k1_div = PIXEL_RATE_DIV_BY_1;
1203 		*k2_div = PIXEL_RATE_DIV_BY_1;
1204 	} else if (dc_is_hdmi_tmds_signal(stream->signal) || dc_is_dvi_signal(stream->signal)) {
1205 		*k1_div = PIXEL_RATE_DIV_BY_1;
1206 		if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420)
1207 			*k2_div = PIXEL_RATE_DIV_BY_2;
1208 		else
1209 			*k2_div = PIXEL_RATE_DIV_BY_4;
1210 	} else if (dc_is_dp_signal(stream->signal) || dc_is_virtual_signal(stream->signal)) {
1211 		if (two_pix_per_container) {
1212 			*k1_div = PIXEL_RATE_DIV_BY_1;
1213 			*k2_div = PIXEL_RATE_DIV_BY_2;
1214 		} else {
1215 			*k1_div = PIXEL_RATE_DIV_BY_1;
1216 			*k2_div = PIXEL_RATE_DIV_BY_4;
1217 			if ((odm_combine_factor == 2) || (hws->funcs.is_dp_dig_pixel_rate_div_policy &&
1218 				hws->funcs.is_dp_dig_pixel_rate_div_policy(pipe_ctx)))
1219 				*k2_div = PIXEL_RATE_DIV_BY_2;
1220 		}
1221 	}
1222 
1223 	if ((*k1_div == PIXEL_RATE_DIV_NA) && (*k2_div == PIXEL_RATE_DIV_NA))
1224 		ASSERT(false);
1225 
1226 	return odm_combine_factor;
1227 }
1228 
dcn32_calculate_pix_rate_divider(struct dc * dc,struct dc_state * context,const struct dc_stream_state * stream)1229 void dcn32_calculate_pix_rate_divider(
1230 		struct dc *dc,
1231 		struct dc_state *context,
1232 		const struct dc_stream_state *stream)
1233 {
1234 	struct dce_hwseq *hws = dc->hwseq;
1235 	struct pipe_ctx *pipe_ctx = NULL;
1236 	unsigned int k1_div = PIXEL_RATE_DIV_NA;
1237 	unsigned int k2_div = PIXEL_RATE_DIV_NA;
1238 
1239 	pipe_ctx = resource_get_otg_master_for_stream(&context->res_ctx, stream);
1240 
1241 	if (pipe_ctx) {
1242 
1243 		if (hws->funcs.calculate_dccg_k1_k2_values)
1244 			hws->funcs.calculate_dccg_k1_k2_values(pipe_ctx, &k1_div, &k2_div);
1245 
1246 		pipe_ctx->pixel_rate_divider.div_factor1 = k1_div;
1247 		pipe_ctx->pixel_rate_divider.div_factor2 = k2_div;
1248 	}
1249 }
1250 
dcn32_resync_fifo_dccg_dio(struct dce_hwseq * hws,struct dc * dc,struct dc_state * context,unsigned int current_pipe_idx)1251 void dcn32_resync_fifo_dccg_dio(struct dce_hwseq *hws, struct dc *dc, struct dc_state *context, unsigned int current_pipe_idx)
1252 {
1253 	unsigned int i;
1254 	struct pipe_ctx *pipe = NULL;
1255 	bool otg_disabled[MAX_PIPES] = {false};
1256 	struct dc_state *dc_state = NULL;
1257 
1258 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1259 		if (i <= current_pipe_idx) {
1260 			pipe = &context->res_ctx.pipe_ctx[i];
1261 			dc_state = context;
1262 		} else {
1263 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1264 			dc_state = dc->current_state;
1265 		}
1266 
1267 		if (!resource_is_pipe_type(pipe, OTG_MASTER))
1268 			continue;
1269 
1270 		if ((pipe->stream->dpms_off || dc_is_virtual_signal(pipe->stream->signal))
1271 			&& dc_state_get_pipe_subvp_type(dc_state, pipe) != SUBVP_PHANTOM) {
1272 			pipe->stream_res.tg->funcs->disable_crtc(pipe->stream_res.tg);
1273 			reset_sync_context_for_pipe(dc, context, i);
1274 			otg_disabled[i] = true;
1275 		}
1276 	}
1277 
1278 	hws->ctx->dc->res_pool->dccg->funcs->trigger_dio_fifo_resync(hws->ctx->dc->res_pool->dccg);
1279 
1280 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1281 		if (i <= current_pipe_idx)
1282 			pipe = &context->res_ctx.pipe_ctx[i];
1283 		else
1284 			pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1285 
1286 		if (otg_disabled[i]) {
1287 			int opp_inst[MAX_PIPES] = { pipe->stream_res.opp->inst };
1288 			int opp_cnt = 1;
1289 			int last_odm_slice_width = resource_get_odm_slice_dst_width(pipe, true);
1290 			int odm_slice_width = resource_get_odm_slice_dst_width(pipe, false);
1291 			struct pipe_ctx *odm_pipe;
1292 
1293 			for (odm_pipe = pipe->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
1294 				opp_inst[opp_cnt] = odm_pipe->stream_res.opp->inst;
1295 				opp_cnt++;
1296 			}
1297 			if (opp_cnt > 1)
1298 				pipe->stream_res.tg->funcs->set_odm_combine(
1299 						pipe->stream_res.tg,
1300 						opp_inst, opp_cnt,
1301 						odm_slice_width,
1302 						last_odm_slice_width);
1303 			pipe->stream_res.tg->funcs->enable_crtc(pipe->stream_res.tg);
1304 		}
1305 	}
1306 
1307 	dc_trigger_sync(dc, dc->current_state);
1308 }
1309 
dcn32_unblank_stream(struct pipe_ctx * pipe_ctx,struct dc_link_settings * link_settings)1310 void dcn32_unblank_stream(struct pipe_ctx *pipe_ctx,
1311 		struct dc_link_settings *link_settings)
1312 {
1313 	struct encoder_unblank_param params = {0};
1314 	struct dc_stream_state *stream = pipe_ctx->stream;
1315 	struct dc_link *link = stream->link;
1316 	struct dce_hwseq *hws = link->dc->hwseq;
1317 	struct pipe_ctx *odm_pipe;
1318 
1319 	params.opp_cnt = 1;
1320 	params.pix_per_cycle = pipe_ctx->stream_res.pix_clk_params.dio_se_pix_per_cycle;
1321 
1322 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
1323 		params.opp_cnt++;
1324 
1325 	/* only 3 items below are used by unblank */
1326 	params.timing = pipe_ctx->stream->timing;
1327 
1328 	params.link_settings.link_rate = link_settings->link_rate;
1329 
1330 	if (link->dc->link_srv->dp_is_128b_132b_signal(pipe_ctx)) {
1331 		/* TODO - DP2.0 HW: Set ODM mode in dp hpo encoder here */
1332 		pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_unblank(
1333 				pipe_ctx->stream_res.hpo_dp_stream_enc,
1334 				pipe_ctx->stream_res.tg->inst);
1335 	} else if (dc_is_dp_signal(pipe_ctx->stream->signal)) {
1336 		if (pipe_ctx->stream_res.tg->funcs->is_two_pixels_per_container(&stream->timing) ||
1337 			params.opp_cnt > 1) {
1338 			params.timing.pix_clk_100hz /= 2;
1339 			params.pix_per_cycle = 2;
1340 		}
1341 		if (pipe_ctx->stream_res.stream_enc->funcs->dp_set_odm_combine)
1342 			pipe_ctx->stream_res.stream_enc->funcs->dp_set_odm_combine(
1343 				pipe_ctx->stream_res.stream_enc, params.pix_per_cycle > 1);
1344 		pipe_ctx->stream_res.stream_enc->funcs->dp_unblank(link, pipe_ctx->stream_res.stream_enc, &params);
1345 	}
1346 
1347 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP)
1348 		hws->funcs.edp_backlight_control(link, true);
1349 }
1350 
dcn32_is_dp_dig_pixel_rate_div_policy(struct pipe_ctx * pipe_ctx)1351 bool dcn32_is_dp_dig_pixel_rate_div_policy(struct pipe_ctx *pipe_ctx)
1352 {
1353 	struct dc *dc = pipe_ctx->stream->ctx->dc;
1354 
1355 	if (!is_h_timing_divisible_by_2(pipe_ctx->stream))
1356 		return false;
1357 
1358 	if (dc_is_dp_signal(pipe_ctx->stream->signal) && !dc->link_srv->dp_is_128b_132b_signal(pipe_ctx) &&
1359 		dc->debug.enable_dp_dig_pixel_rate_div_policy)
1360 		return true;
1361 	return false;
1362 }
1363 
apply_symclk_on_tx_off_wa(struct dc_link * link)1364 static void apply_symclk_on_tx_off_wa(struct dc_link *link)
1365 {
1366 	/* There are use cases where SYMCLK is referenced by OTG. For instance
1367 	 * for TMDS signal, OTG relies SYMCLK even if TX video output is off.
1368 	 * However current link interface will power off PHY when disabling link
1369 	 * output. This will turn off SYMCLK generated by PHY. The workaround is
1370 	 * to identify such case where SYMCLK is still in use by OTG when we
1371 	 * power off PHY. When this is detected, we will temporarily power PHY
1372 	 * back on and move PHY's SYMCLK state to SYMCLK_ON_TX_OFF by calling
1373 	 * program_pix_clk interface. When OTG is disabled, we will then power
1374 	 * off PHY by calling disable link output again.
1375 	 *
1376 	 * In future dcn generations, we plan to rework transmitter control
1377 	 * interface so that we could have an option to set SYMCLK ON TX OFF
1378 	 * state in one step without this workaround
1379 	 */
1380 
1381 	struct dc *dc = link->ctx->dc;
1382 	struct pipe_ctx *pipe_ctx = NULL;
1383 	uint8_t i;
1384 
1385 	if (link->phy_state.symclk_ref_cnts.otg > 0) {
1386 		for (i = 0; i < MAX_PIPES; i++) {
1387 			pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
1388 			if (resource_is_pipe_type(pipe_ctx, OPP_HEAD) && pipe_ctx->stream->link == link) {
1389 				pipe_ctx->clock_source->funcs->program_pix_clk(
1390 						pipe_ctx->clock_source,
1391 						&pipe_ctx->stream_res.pix_clk_params,
1392 						dc->link_srv->dp_get_encoding_format(
1393 								&pipe_ctx->link_config.dp_link_settings),
1394 						&pipe_ctx->pll_settings);
1395 				link->phy_state.symclk_state = SYMCLK_ON_TX_OFF;
1396 				break;
1397 			}
1398 		}
1399 	}
1400 }
1401 
dcn32_disable_link_output(struct dc_link * link,const struct link_resource * link_res,enum signal_type signal)1402 void dcn32_disable_link_output(struct dc_link *link,
1403 		const struct link_resource *link_res,
1404 		enum signal_type signal)
1405 {
1406 	struct dc *dc = link->ctx->dc;
1407 	const struct link_hwss *link_hwss = get_link_hwss(link, link_res);
1408 	struct dmcu *dmcu = dc->res_pool->dmcu;
1409 
1410 	if (signal == SIGNAL_TYPE_EDP &&
1411 			link->dc->hwss.edp_backlight_control &&
1412 			!link->skip_implict_edp_power_control)
1413 		link->dc->hwss.edp_backlight_control(link, false);
1414 	else if (dmcu != NULL && dmcu->funcs->lock_phy)
1415 		dmcu->funcs->lock_phy(dmcu);
1416 
1417 	link_hwss->disable_link_output(link, link_res, signal);
1418 	link->phy_state.symclk_state = SYMCLK_OFF_TX_OFF;
1419 	/*
1420 	 * Add the logic to extract BOTH power up and power down sequences
1421 	 * from enable/disable link output and only call edp panel control
1422 	 * in enable_link_dp and disable_link_dp once.
1423 	 */
1424 	if (dmcu != NULL && dmcu->funcs->unlock_phy)
1425 		dmcu->funcs->unlock_phy(dmcu);
1426 
1427 	dc->link_srv->dp_trace_source_sequence(link, DPCD_SOURCE_SEQ_AFTER_DISABLE_LINK_PHY);
1428 
1429 	apply_symclk_on_tx_off_wa(link);
1430 }
1431 
1432 /* For SubVP the main pipe can have a viewport position change
1433  * without a full update. In this case we must also update the
1434  * viewport positions for the phantom pipe accordingly.
1435  */
dcn32_update_phantom_vp_position(struct dc * dc,struct dc_state * context,struct pipe_ctx * phantom_pipe)1436 void dcn32_update_phantom_vp_position(struct dc *dc,
1437 		struct dc_state *context,
1438 		struct pipe_ctx *phantom_pipe)
1439 {
1440 	uint32_t i;
1441 	struct dc_plane_state *phantom_plane = phantom_pipe->plane_state;
1442 
1443 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1444 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1445 
1446 		if (pipe->stream && dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_MAIN &&
1447 				dc_state_get_paired_subvp_stream(context, pipe->stream) == phantom_pipe->stream) {
1448 			if (pipe->plane_state && pipe->plane_state->update_flags.bits.position_change) {
1449 
1450 				phantom_plane->src_rect.x = pipe->plane_state->src_rect.x;
1451 				phantom_plane->src_rect.y = pipe->plane_state->src_rect.y;
1452 				phantom_plane->clip_rect.x = pipe->plane_state->clip_rect.x;
1453 				phantom_plane->dst_rect.x = pipe->plane_state->dst_rect.x;
1454 				phantom_plane->dst_rect.y = pipe->plane_state->dst_rect.y;
1455 
1456 				phantom_pipe->plane_state->update_flags.bits.position_change = 1;
1457 				resource_build_scaling_params(phantom_pipe);
1458 				return;
1459 			}
1460 		}
1461 	}
1462 }
1463 
1464 /* Treat the phantom pipe as if it needs to be fully enabled.
1465  * If the pipe was previously in use but not phantom, it would
1466  * have been disabled earlier in the sequence so we need to run
1467  * the full enable sequence.
1468  */
dcn32_apply_update_flags_for_phantom(struct pipe_ctx * phantom_pipe)1469 void dcn32_apply_update_flags_for_phantom(struct pipe_ctx *phantom_pipe)
1470 {
1471 	phantom_pipe->update_flags.raw = 0;
1472 	if (resource_is_pipe_type(phantom_pipe, DPP_PIPE)) {
1473 		phantom_pipe->update_flags.bits.enable = 1;
1474 		phantom_pipe->update_flags.bits.mpcc = 1;
1475 		phantom_pipe->update_flags.bits.dppclk = 1;
1476 		phantom_pipe->update_flags.bits.hubp_interdependent = 1;
1477 		phantom_pipe->update_flags.bits.hubp_rq_dlg_ttu = 1;
1478 		phantom_pipe->update_flags.bits.gamut_remap = 1;
1479 		phantom_pipe->update_flags.bits.scaler = 1;
1480 		phantom_pipe->update_flags.bits.viewport = 1;
1481 		phantom_pipe->update_flags.bits.det_size = 1;
1482 		if (resource_is_pipe_type(phantom_pipe, OTG_MASTER)) {
1483 			phantom_pipe->update_flags.bits.odm = 1;
1484 			phantom_pipe->update_flags.bits.global_sync = 1;
1485 		}
1486 	}
1487 }
1488 
dcn32_dsc_pg_status(struct dce_hwseq * hws,unsigned int dsc_inst)1489 bool dcn32_dsc_pg_status(
1490 		struct dce_hwseq *hws,
1491 		unsigned int dsc_inst)
1492 {
1493 	uint32_t pwr_status = 0;
1494 
1495 	switch (dsc_inst) {
1496 	case 0: /* DSC0 */
1497 		REG_GET(DOMAIN16_PG_STATUS,
1498 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1499 		break;
1500 	case 1: /* DSC1 */
1501 
1502 		REG_GET(DOMAIN17_PG_STATUS,
1503 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1504 		break;
1505 	case 2: /* DSC2 */
1506 		REG_GET(DOMAIN18_PG_STATUS,
1507 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1508 		break;
1509 	case 3: /* DSC3 */
1510 		REG_GET(DOMAIN19_PG_STATUS,
1511 				DOMAIN_PGFSM_PWR_STATUS, &pwr_status);
1512 		break;
1513 	default:
1514 		BREAK_TO_DEBUGGER();
1515 		break;
1516 	}
1517 
1518 	return pwr_status == 0;
1519 }
1520 
dcn32_update_dsc_pg(struct dc * dc,struct dc_state * context,bool safe_to_disable)1521 void dcn32_update_dsc_pg(struct dc *dc,
1522 		struct dc_state *context,
1523 		bool safe_to_disable)
1524 {
1525 	struct dce_hwseq *hws = dc->hwseq;
1526 	int i;
1527 
1528 	for (i = 0; i < dc->res_pool->res_cap->num_dsc; i++) {
1529 		struct display_stream_compressor *dsc = dc->res_pool->dscs[i];
1530 		bool is_dsc_ungated = hws->funcs.dsc_pg_status(hws, dsc->inst);
1531 
1532 		if (context->res_ctx.is_dsc_acquired[i]) {
1533 			if (!is_dsc_ungated) {
1534 				hws->funcs.dsc_pg_control(hws, dsc->inst, true);
1535 			}
1536 		} else if (safe_to_disable) {
1537 			if (is_dsc_ungated) {
1538 				hws->funcs.dsc_pg_control(hws, dsc->inst, false);
1539 			}
1540 		}
1541 	}
1542 }
1543 
dcn32_disable_phantom_streams(struct dc * dc,struct dc_state * context)1544 void dcn32_disable_phantom_streams(struct dc *dc, struct dc_state *context)
1545 {
1546 	struct dce_hwseq *hws = dc->hwseq;
1547 	int i;
1548 
1549 	for (i = dc->res_pool->pipe_count - 1; i >= 0 ; i--) {
1550 		struct pipe_ctx *pipe_ctx_old =
1551 			&dc->current_state->res_ctx.pipe_ctx[i];
1552 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1553 
1554 		if (!pipe_ctx_old->stream)
1555 			continue;
1556 
1557 		if (dc_state_get_pipe_subvp_type(dc->current_state, pipe_ctx_old) != SUBVP_PHANTOM)
1558 			continue;
1559 
1560 		if (pipe_ctx_old->top_pipe || pipe_ctx_old->prev_odm_pipe)
1561 			continue;
1562 
1563 		if (!pipe_ctx->stream || pipe_need_reprogram(pipe_ctx_old, pipe_ctx) ||
1564 				(pipe_ctx->stream && dc_state_get_pipe_subvp_type(context, pipe_ctx) != SUBVP_PHANTOM)) {
1565 			struct clock_source *old_clk = pipe_ctx_old->clock_source;
1566 
1567 			if (hws->funcs.reset_back_end_for_pipe)
1568 				hws->funcs.reset_back_end_for_pipe(dc, pipe_ctx_old, dc->current_state);
1569 			if (hws->funcs.enable_stream_gating)
1570 				hws->funcs.enable_stream_gating(dc, pipe_ctx_old);
1571 			if (old_clk)
1572 				old_clk->funcs->cs_power_down(old_clk);
1573 		}
1574 	}
1575 }
1576 
dcn32_enable_phantom_streams(struct dc * dc,struct dc_state * context)1577 void dcn32_enable_phantom_streams(struct dc *dc, struct dc_state *context)
1578 {
1579 	unsigned int i;
1580 	enum dc_status status = DC_OK;
1581 	struct dce_hwseq *hws = dc->hwseq;
1582 
1583 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1584 		struct pipe_ctx *pipe = &context->res_ctx.pipe_ctx[i];
1585 		struct pipe_ctx *old_pipe = &dc->current_state->res_ctx.pipe_ctx[i];
1586 
1587 		/* If an active, non-phantom pipe is being transitioned into a phantom
1588 		 * pipe, wait for the double buffer update to complete first before we do
1589 		 * ANY phantom pipe programming.
1590 		 */
1591 		if (pipe->stream && dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM &&
1592 				old_pipe->stream && dc_state_get_pipe_subvp_type(dc->current_state, old_pipe) != SUBVP_PHANTOM) {
1593 			old_pipe->stream_res.tg->funcs->wait_for_state(
1594 					old_pipe->stream_res.tg,
1595 					CRTC_STATE_VBLANK);
1596 			old_pipe->stream_res.tg->funcs->wait_for_state(
1597 					old_pipe->stream_res.tg,
1598 					CRTC_STATE_VACTIVE);
1599 		}
1600 	}
1601 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1602 		struct pipe_ctx *pipe_ctx_old =
1603 					&dc->current_state->res_ctx.pipe_ctx[i];
1604 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1605 
1606 		if (pipe_ctx->stream == NULL)
1607 			continue;
1608 
1609 		if (dc_state_get_pipe_subvp_type(context, pipe_ctx) != SUBVP_PHANTOM)
1610 			continue;
1611 
1612 		if (pipe_ctx->stream == pipe_ctx_old->stream &&
1613 			pipe_ctx->stream->link->link_state_valid) {
1614 			continue;
1615 		}
1616 
1617 		if (pipe_ctx_old->stream && !pipe_need_reprogram(pipe_ctx_old, pipe_ctx))
1618 			continue;
1619 
1620 		if (pipe_ctx->top_pipe || pipe_ctx->prev_odm_pipe)
1621 			continue;
1622 
1623 		if (hws->funcs.apply_single_controller_ctx_to_hw)
1624 			status = hws->funcs.apply_single_controller_ctx_to_hw(
1625 					pipe_ctx,
1626 					context,
1627 					dc);
1628 
1629 		ASSERT(status == DC_OK);
1630 
1631 #ifdef CONFIG_DRM_AMD_DC_FP
1632 		if (hws->funcs.resync_fifo_dccg_dio)
1633 			hws->funcs.resync_fifo_dccg_dio(hws, dc, context, i);
1634 #endif
1635 	}
1636 }
1637 
1638 /* Blank pixel data during initialization */
dcn32_init_blank(struct dc * dc,struct timing_generator * tg)1639 void dcn32_init_blank(
1640 		struct dc *dc,
1641 		struct timing_generator *tg)
1642 {
1643 	struct dce_hwseq *hws = dc->hwseq;
1644 	enum dc_color_space color_space;
1645 	struct tg_color black_color = {0};
1646 	struct output_pixel_processor *opp = NULL;
1647 	struct output_pixel_processor *bottom_opp = NULL;
1648 	uint32_t num_opps, opp_id_src0, opp_id_src1;
1649 	uint32_t otg_active_width = 0, otg_active_height = 0;
1650 	uint32_t i;
1651 
1652 	/* program opp dpg blank color */
1653 	color_space = COLOR_SPACE_SRGB;
1654 	color_space_to_black_color(dc, color_space, &black_color);
1655 
1656 	/* get the OTG active size */
1657 	tg->funcs->get_otg_active_size(tg,
1658 			&otg_active_width,
1659 			&otg_active_height);
1660 
1661 	/* get the OPTC source */
1662 	tg->funcs->get_optc_source(tg, &num_opps, &opp_id_src0, &opp_id_src1);
1663 
1664 	if (opp_id_src0 >= dc->res_pool->res_cap->num_opp) {
1665 		ASSERT(false);
1666 		return;
1667 	}
1668 
1669 	for (i = 0; i < dc->res_pool->res_cap->num_opp; i++) {
1670 		if (dc->res_pool->opps[i] != NULL && dc->res_pool->opps[i]->inst == opp_id_src0) {
1671 			opp = dc->res_pool->opps[i];
1672 			break;
1673 		}
1674 	}
1675 
1676 	if (num_opps == 2) {
1677 		otg_active_width = otg_active_width / 2;
1678 
1679 		if (opp_id_src1 >= dc->res_pool->res_cap->num_opp) {
1680 			ASSERT(false);
1681 			return;
1682 		}
1683 		for (i = 0; i < dc->res_pool->res_cap->num_opp; i++) {
1684 			if (dc->res_pool->opps[i] != NULL && dc->res_pool->opps[i]->inst == opp_id_src1) {
1685 				bottom_opp = dc->res_pool->opps[i];
1686 				break;
1687 			}
1688 		}
1689 	}
1690 
1691 	if (opp && opp->funcs->opp_set_disp_pattern_generator)
1692 		opp->funcs->opp_set_disp_pattern_generator(
1693 				opp,
1694 				CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR,
1695 				CONTROLLER_DP_COLOR_SPACE_UDEFINED,
1696 				COLOR_DEPTH_UNDEFINED,
1697 				&black_color,
1698 				otg_active_width,
1699 				otg_active_height,
1700 				0);
1701 
1702 	if (num_opps == 2) {
1703 		if (bottom_opp && bottom_opp->funcs->opp_set_disp_pattern_generator) {
1704 			bottom_opp->funcs->opp_set_disp_pattern_generator(
1705 					bottom_opp,
1706 					CONTROLLER_DP_TEST_PATTERN_SOLID_COLOR,
1707 					CONTROLLER_DP_COLOR_SPACE_UDEFINED,
1708 					COLOR_DEPTH_UNDEFINED,
1709 					&black_color,
1710 					otg_active_width,
1711 					otg_active_height,
1712 					0);
1713 			hws->funcs.wait_for_blank_complete(bottom_opp);
1714 		}
1715 	}
1716 
1717 	if (opp)
1718 		hws->funcs.wait_for_blank_complete(opp);
1719 }
1720 
1721 /* phantom stream id's can change often, but can be identical between contexts.
1722 *  This function checks for the condition the streams are identical to avoid
1723 *  redundant pipe transitions.
1724 */
is_subvp_phantom_topology_transition_seamless(const struct dc_state * cur_ctx,const struct dc_state * new_ctx,const struct pipe_ctx * cur_pipe,const struct pipe_ctx * new_pipe)1725 static bool is_subvp_phantom_topology_transition_seamless(
1726 	const struct dc_state *cur_ctx,
1727 	const struct dc_state *new_ctx,
1728 	const struct pipe_ctx *cur_pipe,
1729 	const struct pipe_ctx *new_pipe)
1730 {
1731 	enum mall_stream_type cur_pipe_type = dc_state_get_pipe_subvp_type(cur_ctx, cur_pipe);
1732 	enum mall_stream_type new_pipe_type = dc_state_get_pipe_subvp_type(new_ctx, new_pipe);
1733 
1734 	const struct dc_stream_state *cur_paired_stream = dc_state_get_paired_subvp_stream(cur_ctx, cur_pipe->stream);
1735 	const struct dc_stream_state *new_paired_stream = dc_state_get_paired_subvp_stream(new_ctx, new_pipe->stream);
1736 
1737 	return cur_pipe_type == SUBVP_PHANTOM &&
1738 			cur_pipe_type == new_pipe_type &&
1739 			cur_paired_stream && new_paired_stream &&
1740 			cur_paired_stream->stream_id == new_paired_stream->stream_id;
1741 }
1742 
dcn32_is_pipe_topology_transition_seamless(struct dc * dc,const struct dc_state * cur_ctx,const struct dc_state * new_ctx)1743 bool dcn32_is_pipe_topology_transition_seamless(struct dc *dc,
1744 		const struct dc_state *cur_ctx,
1745 		const struct dc_state *new_ctx)
1746 {
1747 	int i;
1748 	const struct pipe_ctx *cur_pipe, *new_pipe;
1749 	bool is_seamless = true;
1750 
1751 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1752 		cur_pipe = &cur_ctx->res_ctx.pipe_ctx[i];
1753 		new_pipe = &new_ctx->res_ctx.pipe_ctx[i];
1754 
1755 		if (resource_is_pipe_type(cur_pipe, FREE_PIPE) ||
1756 				resource_is_pipe_type(new_pipe, FREE_PIPE))
1757 			/* adding or removing free pipes is always seamless */
1758 			continue;
1759 		else if (resource_is_pipe_type(cur_pipe, OTG_MASTER)) {
1760 			if (resource_is_pipe_type(new_pipe, OTG_MASTER))
1761 				if (cur_pipe->stream->stream_id == new_pipe->stream->stream_id ||
1762 						is_subvp_phantom_topology_transition_seamless(cur_ctx, new_ctx, cur_pipe, new_pipe))
1763 				/* OTG master with the same stream is seamless */
1764 					continue;
1765 		} else if (resource_is_pipe_type(cur_pipe, OPP_HEAD)) {
1766 			if (resource_is_pipe_type(new_pipe, OPP_HEAD)) {
1767 				if (cur_pipe->stream_res.tg == new_pipe->stream_res.tg)
1768 					/*
1769 					 * OPP heads sharing the same timing
1770 					 * generator is seamless
1771 					 */
1772 					continue;
1773 			}
1774 		} else if (resource_is_pipe_type(cur_pipe, DPP_PIPE)) {
1775 			if (resource_is_pipe_type(new_pipe, DPP_PIPE)) {
1776 				if (cur_pipe->stream_res.opp == new_pipe->stream_res.opp)
1777 					/*
1778 					 * DPP pipes sharing the same OPP head is
1779 					 * seamless
1780 					 */
1781 					continue;
1782 			}
1783 		}
1784 
1785 		/*
1786 		 * This pipe's transition doesn't fall under any seamless
1787 		 * conditions
1788 		 */
1789 		is_seamless = false;
1790 		break;
1791 	}
1792 
1793 	return is_seamless;
1794 }
1795 
dcn32_prepare_bandwidth(struct dc * dc,struct dc_state * context)1796 void dcn32_prepare_bandwidth(struct dc *dc,
1797 	struct dc_state *context)
1798 {
1799 	bool p_state_change_support = context->bw_ctx.bw.dcn.clk.p_state_change_support;
1800 	/* Any transition into an FPO config should disable MCLK switching first to avoid
1801 	 * driver and FW P-State synchronization issues.
1802 	 */
1803 	if (context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching || dc->clk_mgr->clks.fw_based_mclk_switching) {
1804 		dc->optimized_required = true;
1805 		context->bw_ctx.bw.dcn.clk.p_state_change_support = false;
1806 	}
1807 
1808 	if (dc->clk_mgr->dc_mode_softmax_enabled)
1809 		if (dc->clk_mgr->clks.dramclk_khz <= dc->clk_mgr->bw_params->dc_mode_softmax_memclk * 1000 &&
1810 				context->bw_ctx.bw.dcn.clk.dramclk_khz > dc->clk_mgr->bw_params->dc_mode_softmax_memclk * 1000)
1811 			dc->clk_mgr->funcs->set_max_memclk(dc->clk_mgr, dc->clk_mgr->bw_params->clk_table.entries[dc->clk_mgr->bw_params->clk_table.num_entries - 1].memclk_mhz);
1812 
1813 	dcn20_prepare_bandwidth(dc, context);
1814 
1815 	if (!context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching)
1816 		dc_dmub_srv_p_state_delegate(dc, false, context);
1817 
1818 	if (context->bw_ctx.bw.dcn.clk.fw_based_mclk_switching || dc->clk_mgr->clks.fw_based_mclk_switching) {
1819 		/* After disabling P-State, restore the original value to ensure we get the correct P-State
1820 		 * on the next optimize.
1821 		 */
1822 		context->bw_ctx.bw.dcn.clk.p_state_change_support = p_state_change_support;
1823 	}
1824 }
1825 
dcn32_interdependent_update_lock(struct dc * dc,struct dc_state * context,bool lock)1826 void dcn32_interdependent_update_lock(struct dc *dc,
1827 		struct dc_state *context, bool lock)
1828 {
1829 	unsigned int i;
1830 	struct pipe_ctx *pipe;
1831 	struct timing_generator *tg;
1832 
1833 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1834 		pipe = &context->res_ctx.pipe_ctx[i];
1835 		tg = pipe->stream_res.tg;
1836 
1837 		if (!resource_is_pipe_type(pipe, OTG_MASTER) ||
1838 				!tg->funcs->is_tg_enabled(tg) ||
1839 				dc_state_get_pipe_subvp_type(context, pipe) == SUBVP_PHANTOM)
1840 			continue;
1841 
1842 		if (lock)
1843 			dc->hwss.pipe_control_lock(dc, pipe, true);
1844 		else
1845 			dc->hwss.pipe_control_lock(dc, pipe, false);
1846 	}
1847 }
1848 
dcn32_program_outstanding_updates(struct dc * dc,struct dc_state * context)1849 void dcn32_program_outstanding_updates(struct dc *dc,
1850 		struct dc_state *context)
1851 {
1852 	struct hubbub *hubbub = dc->res_pool->hubbub;
1853 
1854 	/* update compbuf if required */
1855 	if (hubbub->funcs->program_compbuf_size)
1856 		hubbub->funcs->program_compbuf_size(hubbub, context->bw_ctx.bw.dcn.compbuf_size_kb, true);
1857 }
1858