xref: /linux/drivers/gpu/drm/amd/display/dc/hwss/dce110/dce110_hwseq.c (revision e9ef810dfee7a2227da9d423aecb0ced35faddbe)
1 /*
2  * Copyright 2015 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 #include "dm_services.h"
27 #include "dc.h"
28 #include "dc_bios_types.h"
29 #include "core_types.h"
30 #include "core_status.h"
31 #include "resource.h"
32 #include "dm_helpers.h"
33 #include "dce110_hwseq.h"
34 #include "dce110/dce110_timing_generator.h"
35 #include "dce/dce_hwseq.h"
36 #include "dce100/dce100_hwseq.h"
37 #include "gpio_service_interface.h"
38 
39 #include "dce110/dce110_compressor.h"
40 
41 #include "bios/bios_parser_helper.h"
42 #include "timing_generator.h"
43 #include "mem_input.h"
44 #include "opp.h"
45 #include "ipp.h"
46 #include "transform.h"
47 #include "stream_encoder.h"
48 #include "link_encoder.h"
49 #include "link_enc_cfg.h"
50 #include "link_hwss.h"
51 #include "link.h"
52 #include "dccg.h"
53 #include "clock_source.h"
54 #include "clk_mgr.h"
55 #include "abm.h"
56 #include "audio.h"
57 #include "reg_helper.h"
58 #include "panel_cntl.h"
59 #include "dc_state_priv.h"
60 #include "dpcd_defs.h"
61 #include "dsc.h"
62 /* include DCE11 register header files */
63 #include "dce/dce_11_0_d.h"
64 #include "dce/dce_11_0_sh_mask.h"
65 #include "custom_float.h"
66 
67 #include "atomfirmware.h"
68 
69 #include "dcn10/dcn10_hwseq.h"
70 
71 #define GAMMA_HW_POINTS_NUM 256
72 
73 /*
74  * All values are in milliseconds;
75  * For eDP, after power-up/power/down,
76  * 300/500 msec max. delay from LCDVCC to black video generation
77  */
78 #define PANEL_POWER_UP_TIMEOUT 300
79 #define PANEL_POWER_DOWN_TIMEOUT 500
80 #define HPD_CHECK_INTERVAL 10
81 #define OLED_POST_T7_DELAY 100
82 #define OLED_PRE_T11_DELAY 150
83 
84 #define CTX \
85 	hws->ctx
86 
87 #define DC_LOGGER \
88 	ctx->logger
89 #define DC_LOGGER_INIT() \
90 	struct dc_context *ctx = dc->ctx
91 
92 #define REG(reg)\
93 	hws->regs->reg
94 
95 #undef FN
96 #define FN(reg_name, field_name) \
97 	hws->shifts->field_name, hws->masks->field_name
98 
99 struct dce110_hw_seq_reg_offsets {
100 	uint32_t crtc;
101 };
102 
103 static const struct dce110_hw_seq_reg_offsets reg_offsets[] = {
104 {
105 	.crtc = (mmCRTC0_CRTC_GSL_CONTROL - mmCRTC_GSL_CONTROL),
106 },
107 {
108 	.crtc = (mmCRTC1_CRTC_GSL_CONTROL - mmCRTC_GSL_CONTROL),
109 },
110 {
111 	.crtc = (mmCRTC2_CRTC_GSL_CONTROL - mmCRTC_GSL_CONTROL),
112 },
113 {
114 	.crtc = (mmCRTCV_GSL_CONTROL - mmCRTC_GSL_CONTROL),
115 }
116 };
117 
118 #define HW_REG_BLND(reg, id)\
119 	(reg + reg_offsets[id].blnd)
120 
121 #define HW_REG_CRTC(reg, id)\
122 	(reg + reg_offsets[id].crtc)
123 
124 #define MAX_WATERMARK 0xFFFF
125 #define SAFE_NBP_MARK 0x7FFF
126 
127 /*******************************************************************************
128  * Private definitions
129  ******************************************************************************/
130 /***************************PIPE_CONTROL***********************************/
dce110_init_pte(struct dc_context * ctx)131 static void dce110_init_pte(struct dc_context *ctx)
132 {
133 	uint32_t addr;
134 	uint32_t value = 0;
135 	uint32_t chunk_int = 0;
136 	uint32_t chunk_mul = 0;
137 
138 	addr = mmUNP_DVMM_PTE_CONTROL;
139 	value = dm_read_reg(ctx, addr);
140 
141 	set_reg_field_value(
142 		value,
143 		0,
144 		DVMM_PTE_CONTROL,
145 		DVMM_USE_SINGLE_PTE);
146 
147 	set_reg_field_value(
148 		value,
149 		1,
150 		DVMM_PTE_CONTROL,
151 		DVMM_PTE_BUFFER_MODE0);
152 
153 	set_reg_field_value(
154 		value,
155 		1,
156 		DVMM_PTE_CONTROL,
157 		DVMM_PTE_BUFFER_MODE1);
158 
159 	dm_write_reg(ctx, addr, value);
160 
161 	addr = mmDVMM_PTE_REQ;
162 	value = dm_read_reg(ctx, addr);
163 
164 	chunk_int = get_reg_field_value(
165 		value,
166 		DVMM_PTE_REQ,
167 		HFLIP_PTEREQ_PER_CHUNK_INT);
168 
169 	chunk_mul = get_reg_field_value(
170 		value,
171 		DVMM_PTE_REQ,
172 		HFLIP_PTEREQ_PER_CHUNK_MULTIPLIER);
173 
174 	if (chunk_int != 0x4 || chunk_mul != 0x4) {
175 
176 		set_reg_field_value(
177 			value,
178 			255,
179 			DVMM_PTE_REQ,
180 			MAX_PTEREQ_TO_ISSUE);
181 
182 		set_reg_field_value(
183 			value,
184 			4,
185 			DVMM_PTE_REQ,
186 			HFLIP_PTEREQ_PER_CHUNK_INT);
187 
188 		set_reg_field_value(
189 			value,
190 			4,
191 			DVMM_PTE_REQ,
192 			HFLIP_PTEREQ_PER_CHUNK_MULTIPLIER);
193 
194 		dm_write_reg(ctx, addr, value);
195 	}
196 }
197 /**************************************************************************/
198 
enable_display_pipe_clock_gating(struct dc_context * ctx,bool clock_gating)199 static void enable_display_pipe_clock_gating(
200 	struct dc_context *ctx,
201 	bool clock_gating)
202 {
203 	/*TODO*/
204 }
205 
dce110_enable_display_power_gating(struct dc * dc,uint8_t controller_id,struct dc_bios * dcb,enum pipe_gating_control power_gating)206 static bool dce110_enable_display_power_gating(
207 	struct dc *dc,
208 	uint8_t controller_id,
209 	struct dc_bios *dcb,
210 	enum pipe_gating_control power_gating)
211 {
212 	enum bp_result bp_result = BP_RESULT_OK;
213 	enum bp_pipe_control_action cntl;
214 	struct dc_context *ctx = dc->ctx;
215 	unsigned int underlay_idx = dc->res_pool->underlay_pipe_index;
216 
217 	if (power_gating == PIPE_GATING_CONTROL_INIT)
218 		cntl = ASIC_PIPE_INIT;
219 	else if (power_gating == PIPE_GATING_CONTROL_ENABLE)
220 		cntl = ASIC_PIPE_ENABLE;
221 	else
222 		cntl = ASIC_PIPE_DISABLE;
223 
224 	if (controller_id == underlay_idx)
225 		controller_id = CONTROLLER_ID_UNDERLAY0 - 1;
226 
227 	if (power_gating != PIPE_GATING_CONTROL_INIT || controller_id == 0) {
228 
229 		bp_result = dcb->funcs->enable_disp_power_gating(
230 						dcb, controller_id + 1, cntl);
231 
232 		/* Revert MASTER_UPDATE_MODE to 0 because bios sets it 2
233 		 * by default when command table is called
234 		 *
235 		 * Bios parser accepts controller_id = 6 as indicative of
236 		 * underlay pipe in dce110. But we do not support more
237 		 * than 3.
238 		 */
239 		if (controller_id < CONTROLLER_ID_MAX - 1)
240 			dm_write_reg(ctx,
241 				HW_REG_CRTC(mmCRTC_MASTER_UPDATE_MODE, controller_id),
242 				0);
243 	}
244 
245 	if (power_gating != PIPE_GATING_CONTROL_ENABLE)
246 		dce110_init_pte(ctx);
247 
248 	if (bp_result == BP_RESULT_OK)
249 		return true;
250 	else
251 		return false;
252 }
253 
dce110_prescale_params(struct ipp_prescale_params * prescale_params,const struct dc_plane_state * plane_state)254 static void dce110_prescale_params(struct ipp_prescale_params *prescale_params,
255 		const struct dc_plane_state *plane_state)
256 {
257 	prescale_params->mode = IPP_PRESCALE_MODE_FIXED_UNSIGNED;
258 
259 	switch (plane_state->format) {
260 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
261 		prescale_params->scale = 0x2082;
262 		break;
263 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
264 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
265 		prescale_params->scale = 0x2020;
266 		break;
267 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
268 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
269 		prescale_params->scale = 0x2008;
270 		break;
271 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
272 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616:
273 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
274 		prescale_params->scale = 0x2000;
275 		break;
276 	default:
277 		ASSERT(false);
278 		break;
279 	}
280 }
281 
282 static bool
dce110_set_input_transfer_func(struct dc * dc,struct pipe_ctx * pipe_ctx,const struct dc_plane_state * plane_state)283 dce110_set_input_transfer_func(struct dc *dc, struct pipe_ctx *pipe_ctx,
284 			       const struct dc_plane_state *plane_state)
285 {
286 	struct input_pixel_processor *ipp = pipe_ctx->plane_res.ipp;
287 	const struct dc_transfer_func *tf = NULL;
288 	struct ipp_prescale_params prescale_params = { 0 };
289 	bool result = true;
290 
291 	if (ipp == NULL)
292 		return false;
293 
294 	tf = &plane_state->in_transfer_func;
295 
296 	dce110_prescale_params(&prescale_params, plane_state);
297 	ipp->funcs->ipp_program_prescale(ipp, &prescale_params);
298 
299 	if (!plane_state->gamma_correction.is_identity &&
300 			dce_use_lut(plane_state->format))
301 		ipp->funcs->ipp_program_input_lut(ipp, &plane_state->gamma_correction);
302 
303 	if (tf->type == TF_TYPE_PREDEFINED) {
304 		switch (tf->tf) {
305 		case TRANSFER_FUNCTION_SRGB:
306 			ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_HW_sRGB);
307 			break;
308 		case TRANSFER_FUNCTION_BT709:
309 			ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_HW_xvYCC);
310 			break;
311 		case TRANSFER_FUNCTION_LINEAR:
312 			ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_BYPASS);
313 			break;
314 		case TRANSFER_FUNCTION_PQ:
315 		default:
316 			result = false;
317 			break;
318 		}
319 	} else if (tf->type == TF_TYPE_BYPASS) {
320 		ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_BYPASS);
321 	} else {
322 		/*TF_TYPE_DISTRIBUTED_POINTS - Not supported in DCE 11*/
323 		result = false;
324 	}
325 
326 	return result;
327 }
328 
convert_to_custom_float(struct pwl_result_data * rgb_resulted,struct curve_points * arr_points,uint32_t hw_points_num)329 static bool convert_to_custom_float(struct pwl_result_data *rgb_resulted,
330 				    struct curve_points *arr_points,
331 				    uint32_t hw_points_num)
332 {
333 	struct custom_float_format fmt;
334 
335 	struct pwl_result_data *rgb = rgb_resulted;
336 
337 	uint32_t i = 0;
338 
339 	fmt.exponenta_bits = 6;
340 	fmt.mantissa_bits = 12;
341 	fmt.sign = true;
342 
343 	if (!convert_to_custom_float_format(arr_points[0].x, &fmt,
344 					    &arr_points[0].custom_float_x)) {
345 		BREAK_TO_DEBUGGER();
346 		return false;
347 	}
348 
349 	if (!convert_to_custom_float_format(arr_points[0].offset, &fmt,
350 					    &arr_points[0].custom_float_offset)) {
351 		BREAK_TO_DEBUGGER();
352 		return false;
353 	}
354 
355 	if (!convert_to_custom_float_format(arr_points[0].slope, &fmt,
356 					    &arr_points[0].custom_float_slope)) {
357 		BREAK_TO_DEBUGGER();
358 		return false;
359 	}
360 
361 	fmt.mantissa_bits = 10;
362 	fmt.sign = false;
363 
364 	if (!convert_to_custom_float_format(arr_points[1].x, &fmt,
365 					    &arr_points[1].custom_float_x)) {
366 		BREAK_TO_DEBUGGER();
367 		return false;
368 	}
369 
370 	if (!convert_to_custom_float_format(arr_points[1].y, &fmt,
371 					    &arr_points[1].custom_float_y)) {
372 		BREAK_TO_DEBUGGER();
373 		return false;
374 	}
375 
376 	if (!convert_to_custom_float_format(arr_points[1].slope, &fmt,
377 					    &arr_points[1].custom_float_slope)) {
378 		BREAK_TO_DEBUGGER();
379 		return false;
380 	}
381 
382 	fmt.mantissa_bits = 12;
383 	fmt.sign = true;
384 
385 	while (i != hw_points_num) {
386 		if (!convert_to_custom_float_format(rgb->red, &fmt,
387 						    &rgb->red_reg)) {
388 			BREAK_TO_DEBUGGER();
389 			return false;
390 		}
391 
392 		if (!convert_to_custom_float_format(rgb->green, &fmt,
393 						    &rgb->green_reg)) {
394 			BREAK_TO_DEBUGGER();
395 			return false;
396 		}
397 
398 		if (!convert_to_custom_float_format(rgb->blue, &fmt,
399 						    &rgb->blue_reg)) {
400 			BREAK_TO_DEBUGGER();
401 			return false;
402 		}
403 
404 		if (!convert_to_custom_float_format(rgb->delta_red, &fmt,
405 						    &rgb->delta_red_reg)) {
406 			BREAK_TO_DEBUGGER();
407 			return false;
408 		}
409 
410 		if (!convert_to_custom_float_format(rgb->delta_green, &fmt,
411 						    &rgb->delta_green_reg)) {
412 			BREAK_TO_DEBUGGER();
413 			return false;
414 		}
415 
416 		if (!convert_to_custom_float_format(rgb->delta_blue, &fmt,
417 						    &rgb->delta_blue_reg)) {
418 			BREAK_TO_DEBUGGER();
419 			return false;
420 		}
421 
422 		++rgb;
423 		++i;
424 	}
425 
426 	return true;
427 }
428 
429 #define MAX_LOW_POINT      25
430 #define NUMBER_REGIONS     16
431 #define NUMBER_SW_SEGMENTS 16
432 
433 static bool
dce110_translate_regamma_to_hw_format(const struct dc_transfer_func * output_tf,struct pwl_params * regamma_params)434 dce110_translate_regamma_to_hw_format(const struct dc_transfer_func *output_tf,
435 				      struct pwl_params *regamma_params)
436 {
437 	struct curve_points *arr_points;
438 	struct pwl_result_data *rgb_resulted;
439 	struct pwl_result_data *rgb;
440 	struct pwl_result_data *rgb_plus_1;
441 	struct fixed31_32 y_r;
442 	struct fixed31_32 y_g;
443 	struct fixed31_32 y_b;
444 	struct fixed31_32 y1_min;
445 	struct fixed31_32 y3_max;
446 
447 	int32_t region_start, region_end;
448 	uint32_t i, j, k, seg_distr[NUMBER_REGIONS], increment, start_index, hw_points;
449 
450 	if (output_tf == NULL || regamma_params == NULL || output_tf->type == TF_TYPE_BYPASS)
451 		return false;
452 
453 	arr_points = regamma_params->arr_points;
454 	rgb_resulted = regamma_params->rgb_resulted;
455 	hw_points = 0;
456 
457 	memset(regamma_params, 0, sizeof(struct pwl_params));
458 
459 	if (output_tf->tf == TRANSFER_FUNCTION_PQ) {
460 		/* 16 segments
461 		 * segments are from 2^-11 to 2^5
462 		 */
463 		region_start = -11;
464 		region_end = region_start + NUMBER_REGIONS;
465 
466 		for (i = 0; i < NUMBER_REGIONS; i++)
467 			seg_distr[i] = 4;
468 
469 	} else {
470 		/* 10 segments
471 		 * segment is from 2^-10 to 2^1
472 		 * We include an extra segment for range [2^0, 2^1). This is to
473 		 * ensure that colors with normalized values of 1 don't miss the
474 		 * LUT.
475 		 */
476 		region_start = -10;
477 		region_end = 1;
478 
479 		seg_distr[0] = 4;
480 		seg_distr[1] = 4;
481 		seg_distr[2] = 4;
482 		seg_distr[3] = 4;
483 		seg_distr[4] = 4;
484 		seg_distr[5] = 4;
485 		seg_distr[6] = 4;
486 		seg_distr[7] = 4;
487 		seg_distr[8] = 4;
488 		seg_distr[9] = 4;
489 		seg_distr[10] = 0;
490 		seg_distr[11] = -1;
491 		seg_distr[12] = -1;
492 		seg_distr[13] = -1;
493 		seg_distr[14] = -1;
494 		seg_distr[15] = -1;
495 	}
496 
497 	for (k = 0; k < 16; k++) {
498 		if (seg_distr[k] != -1)
499 			hw_points += (1 << seg_distr[k]);
500 	}
501 
502 	j = 0;
503 	for (k = 0; k < (region_end - region_start); k++) {
504 		increment = NUMBER_SW_SEGMENTS / (1 << seg_distr[k]);
505 		start_index = (region_start + k + MAX_LOW_POINT) *
506 				NUMBER_SW_SEGMENTS;
507 		for (i = start_index; i < start_index + NUMBER_SW_SEGMENTS;
508 				i += increment) {
509 			if (j == hw_points - 1)
510 				break;
511 			rgb_resulted[j].red = output_tf->tf_pts.red[i];
512 			rgb_resulted[j].green = output_tf->tf_pts.green[i];
513 			rgb_resulted[j].blue = output_tf->tf_pts.blue[i];
514 			j++;
515 		}
516 	}
517 
518 	/* last point */
519 	start_index = (region_end + MAX_LOW_POINT) * NUMBER_SW_SEGMENTS;
520 	rgb_resulted[hw_points - 1].red = output_tf->tf_pts.red[start_index];
521 	rgb_resulted[hw_points - 1].green = output_tf->tf_pts.green[start_index];
522 	rgb_resulted[hw_points - 1].blue = output_tf->tf_pts.blue[start_index];
523 
524 	arr_points[0].x = dc_fixpt_pow(dc_fixpt_from_int(2),
525 					     dc_fixpt_from_int(region_start));
526 	arr_points[1].x = dc_fixpt_pow(dc_fixpt_from_int(2),
527 					     dc_fixpt_from_int(region_end));
528 
529 	y_r = rgb_resulted[0].red;
530 	y_g = rgb_resulted[0].green;
531 	y_b = rgb_resulted[0].blue;
532 
533 	y1_min = dc_fixpt_min(y_r, dc_fixpt_min(y_g, y_b));
534 
535 	arr_points[0].y = y1_min;
536 	arr_points[0].slope = dc_fixpt_div(arr_points[0].y,
537 						 arr_points[0].x);
538 
539 	y_r = rgb_resulted[hw_points - 1].red;
540 	y_g = rgb_resulted[hw_points - 1].green;
541 	y_b = rgb_resulted[hw_points - 1].blue;
542 
543 	/* see comment above, m_arrPoints[1].y should be the Y value for the
544 	 * region end (m_numOfHwPoints), not last HW point(m_numOfHwPoints - 1)
545 	 */
546 	y3_max = dc_fixpt_max(y_r, dc_fixpt_max(y_g, y_b));
547 
548 	arr_points[1].y = y3_max;
549 
550 	arr_points[1].slope = dc_fixpt_zero;
551 
552 	if (output_tf->tf == TRANSFER_FUNCTION_PQ) {
553 		/* for PQ, we want to have a straight line from last HW X point,
554 		 * and the slope to be such that we hit 1.0 at 10000 nits.
555 		 */
556 		const struct fixed31_32 end_value = dc_fixpt_from_int(125);
557 
558 		arr_points[1].slope = dc_fixpt_div(
559 				dc_fixpt_sub(dc_fixpt_one, arr_points[1].y),
560 				dc_fixpt_sub(end_value, arr_points[1].x));
561 	}
562 
563 	regamma_params->hw_points_num = hw_points;
564 
565 	k = 0;
566 	for (i = 1; i < 16; i++) {
567 		if (seg_distr[k] != -1) {
568 			regamma_params->arr_curve_points[k].segments_num = seg_distr[k];
569 			regamma_params->arr_curve_points[i].offset =
570 					regamma_params->arr_curve_points[k].offset + (1 << seg_distr[k]);
571 		}
572 		k++;
573 	}
574 
575 	if (seg_distr[k] != -1)
576 		regamma_params->arr_curve_points[k].segments_num = seg_distr[k];
577 
578 	rgb = rgb_resulted;
579 	rgb_plus_1 = rgb_resulted + 1;
580 
581 	i = 1;
582 
583 	while (i != hw_points + 1) {
584 		if (dc_fixpt_lt(rgb_plus_1->red, rgb->red))
585 			rgb_plus_1->red = rgb->red;
586 		if (dc_fixpt_lt(rgb_plus_1->green, rgb->green))
587 			rgb_plus_1->green = rgb->green;
588 		if (dc_fixpt_lt(rgb_plus_1->blue, rgb->blue))
589 			rgb_plus_1->blue = rgb->blue;
590 
591 		rgb->delta_red = dc_fixpt_sub(rgb_plus_1->red, rgb->red);
592 		rgb->delta_green = dc_fixpt_sub(rgb_plus_1->green, rgb->green);
593 		rgb->delta_blue = dc_fixpt_sub(rgb_plus_1->blue, rgb->blue);
594 
595 		++rgb_plus_1;
596 		++rgb;
597 		++i;
598 	}
599 
600 	convert_to_custom_float(rgb_resulted, arr_points, hw_points);
601 
602 	return true;
603 }
604 
605 static bool
dce110_set_output_transfer_func(struct dc * dc,struct pipe_ctx * pipe_ctx,const struct dc_stream_state * stream)606 dce110_set_output_transfer_func(struct dc *dc, struct pipe_ctx *pipe_ctx,
607 				const struct dc_stream_state *stream)
608 {
609 	struct transform *xfm = pipe_ctx->plane_res.xfm;
610 
611 	xfm->funcs->opp_power_on_regamma_lut(xfm, true);
612 	xfm->regamma_params.hw_points_num = GAMMA_HW_POINTS_NUM;
613 
614 	if (stream->out_transfer_func.type == TF_TYPE_PREDEFINED &&
615 	    stream->out_transfer_func.tf == TRANSFER_FUNCTION_SRGB) {
616 		xfm->funcs->opp_set_regamma_mode(xfm, OPP_REGAMMA_SRGB);
617 	} else if (dce110_translate_regamma_to_hw_format(&stream->out_transfer_func,
618 							 &xfm->regamma_params)) {
619 		xfm->funcs->opp_program_regamma_pwl(xfm, &xfm->regamma_params);
620 		xfm->funcs->opp_set_regamma_mode(xfm, OPP_REGAMMA_USER);
621 	} else {
622 		xfm->funcs->opp_set_regamma_mode(xfm, OPP_REGAMMA_BYPASS);
623 	}
624 
625 	xfm->funcs->opp_power_on_regamma_lut(xfm, false);
626 
627 	return true;
628 }
629 
dce110_update_info_frame(struct pipe_ctx * pipe_ctx)630 void dce110_update_info_frame(struct pipe_ctx *pipe_ctx)
631 {
632 	bool is_hdmi_tmds;
633 	bool is_dp;
634 
635 	ASSERT(pipe_ctx->stream);
636 
637 	if (pipe_ctx->stream_res.stream_enc == NULL)
638 		return;  /* this is not root pipe */
639 
640 	is_hdmi_tmds = dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal);
641 	is_dp = dc_is_dp_signal(pipe_ctx->stream->signal);
642 
643 	if (!is_hdmi_tmds && !is_dp)
644 		return;
645 
646 	if (is_hdmi_tmds)
647 		pipe_ctx->stream_res.stream_enc->funcs->update_hdmi_info_packets(
648 			pipe_ctx->stream_res.stream_enc,
649 			&pipe_ctx->stream_res.encoder_info_frame);
650 	else {
651 		if (pipe_ctx->stream_res.stream_enc->funcs->update_dp_info_packets_sdp_line_num)
652 			pipe_ctx->stream_res.stream_enc->funcs->update_dp_info_packets_sdp_line_num(
653 				pipe_ctx->stream_res.stream_enc,
654 				&pipe_ctx->stream_res.encoder_info_frame);
655 
656 		pipe_ctx->stream_res.stream_enc->funcs->update_dp_info_packets(
657 			pipe_ctx->stream_res.stream_enc,
658 			&pipe_ctx->stream_res.encoder_info_frame);
659 	}
660 }
661 
dce110_enable_stream(struct pipe_ctx * pipe_ctx)662 void dce110_enable_stream(struct pipe_ctx *pipe_ctx)
663 {
664 	enum dc_lane_count lane_count =
665 		pipe_ctx->stream->link->cur_link_settings.lane_count;
666 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
667 	struct dc_link *link = pipe_ctx->stream->link;
668 	const struct dc *dc = link->dc;
669 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
670 	uint32_t active_total_with_borders;
671 	uint32_t early_control = 0;
672 	struct timing_generator *tg = pipe_ctx->stream_res.tg;
673 
674 	link_hwss->setup_stream_encoder(pipe_ctx);
675 
676 	dc->hwss.update_info_frame(pipe_ctx);
677 
678 	/* enable early control to avoid corruption on DP monitor*/
679 	active_total_with_borders =
680 			timing->h_addressable
681 				+ timing->h_border_left
682 				+ timing->h_border_right;
683 
684 	if (lane_count != 0)
685 		early_control = active_total_with_borders % lane_count;
686 
687 	if (early_control == 0)
688 		early_control = lane_count;
689 
690 	tg->funcs->set_early_control(tg, early_control);
691 }
692 
link_transmitter_control(struct dc_bios * bios,struct bp_transmitter_control * cntl)693 static enum bp_result link_transmitter_control(
694 		struct dc_bios *bios,
695 	struct bp_transmitter_control *cntl)
696 {
697 	enum bp_result result;
698 
699 	result = bios->funcs->transmitter_control(bios, cntl);
700 
701 	return result;
702 }
703 
704 /*
705  * @brief
706  * eDP only.
707  */
dce110_edp_wait_for_hpd_ready(struct dc_link * link,bool power_up)708 void dce110_edp_wait_for_hpd_ready(
709 		struct dc_link *link,
710 		bool power_up)
711 {
712 	struct dc_context *ctx = link->ctx;
713 	struct graphics_object_id connector = link->link_enc->connector;
714 	struct gpio *hpd;
715 	bool edp_hpd_high = false;
716 	uint32_t time_elapsed = 0;
717 	uint32_t timeout = power_up ?
718 		PANEL_POWER_UP_TIMEOUT : PANEL_POWER_DOWN_TIMEOUT;
719 
720 	if (dal_graphics_object_id_get_connector_id(connector)
721 			!= CONNECTOR_ID_EDP) {
722 		BREAK_TO_DEBUGGER();
723 		return;
724 	}
725 
726 	if (!power_up)
727 		/*
728 		 * From KV, we will not HPD low after turning off VCC -
729 		 * instead, we will check the SW timer in power_up().
730 		 */
731 		return;
732 
733 	/*
734 	 * When we power on/off the eDP panel,
735 	 * we need to wait until SENSE bit is high/low.
736 	 */
737 
738 	/* obtain HPD */
739 	/* TODO what to do with this? */
740 	hpd = ctx->dc->link_srv->get_hpd_gpio(ctx->dc_bios, connector, ctx->gpio_service);
741 
742 	if (!hpd) {
743 		BREAK_TO_DEBUGGER();
744 		return;
745 	}
746 
747 	if (link->panel_config.pps.extra_t3_ms > 0) {
748 		int extra_t3_in_ms = link->panel_config.pps.extra_t3_ms;
749 
750 		msleep(extra_t3_in_ms);
751 	}
752 
753 	dal_gpio_open(hpd, GPIO_MODE_INTERRUPT);
754 
755 	/* wait until timeout or panel detected */
756 
757 	do {
758 		uint32_t detected = 0;
759 
760 		dal_gpio_get_value(hpd, &detected);
761 
762 		if (!(detected ^ power_up)) {
763 			edp_hpd_high = true;
764 			break;
765 		}
766 
767 		msleep(HPD_CHECK_INTERVAL);
768 
769 		time_elapsed += HPD_CHECK_INTERVAL;
770 	} while (time_elapsed < timeout);
771 
772 	dal_gpio_close(hpd);
773 
774 	dal_gpio_destroy_irq(&hpd);
775 
776 	/* ensure that the panel is detected */
777 	if (!edp_hpd_high)
778 		DC_LOG_DC("%s: wait timed out!\n", __func__);
779 }
780 
dce110_edp_power_control(struct dc_link * link,bool power_up)781 void dce110_edp_power_control(
782 		struct dc_link *link,
783 		bool power_up)
784 {
785 	struct dc_context *ctx = link->ctx;
786 	struct bp_transmitter_control cntl = { 0 };
787 	enum bp_result bp_result;
788 	uint8_t pwrseq_instance;
789 
790 
791 	if (dal_graphics_object_id_get_connector_id(link->link_enc->connector)
792 			!= CONNECTOR_ID_EDP) {
793 		BREAK_TO_DEBUGGER();
794 		return;
795 	}
796 
797 	if (!link->panel_cntl)
798 		return;
799 	if (power_up !=
800 		link->panel_cntl->funcs->is_panel_powered_on(link->panel_cntl)) {
801 
802 		unsigned long long current_ts = dm_get_timestamp(ctx);
803 		unsigned long long time_since_edp_poweroff_ms =
804 				div64_u64(dm_get_elapse_time_in_ns(
805 						ctx,
806 						current_ts,
807 						ctx->dc->link_srv->dp_trace_get_edp_poweroff_timestamp(link)), 1000000);
808 		unsigned long long time_since_edp_poweron_ms =
809 				div64_u64(dm_get_elapse_time_in_ns(
810 						ctx,
811 						current_ts,
812 						ctx->dc->link_srv->dp_trace_get_edp_poweron_timestamp(link)), 1000000);
813 		DC_LOG_HW_RESUME_S3(
814 				"%s: transition: power_up=%d current_ts=%llu edp_poweroff=%llu edp_poweron=%llu time_since_edp_poweroff_ms=%llu time_since_edp_poweron_ms=%llu",
815 				__func__,
816 				power_up,
817 				current_ts,
818 				ctx->dc->link_srv->dp_trace_get_edp_poweroff_timestamp(link),
819 				ctx->dc->link_srv->dp_trace_get_edp_poweron_timestamp(link),
820 				time_since_edp_poweroff_ms,
821 				time_since_edp_poweron_ms);
822 
823 		/* Send VBIOS command to prompt eDP panel power */
824 		if (power_up) {
825 			/* edp requires a min of 500ms from LCDVDD off to on */
826 			unsigned long long remaining_min_edp_poweroff_time_ms = 500;
827 
828 			/* add time defined by a patch, if any (usually patch extra_t12_ms is 0) */
829 			if (link->local_sink != NULL)
830 				remaining_min_edp_poweroff_time_ms +=
831 					link->panel_config.pps.extra_t12_ms;
832 
833 			/* Adjust remaining_min_edp_poweroff_time_ms if this is not the first time. */
834 			if (ctx->dc->link_srv->dp_trace_get_edp_poweroff_timestamp(link) != 0) {
835 				if (time_since_edp_poweroff_ms < remaining_min_edp_poweroff_time_ms)
836 					remaining_min_edp_poweroff_time_ms =
837 						remaining_min_edp_poweroff_time_ms - time_since_edp_poweroff_ms;
838 				else
839 					remaining_min_edp_poweroff_time_ms = 0;
840 			}
841 
842 			if (remaining_min_edp_poweroff_time_ms) {
843 				DC_LOG_HW_RESUME_S3(
844 						"%s: remaining_min_edp_poweroff_time_ms=%llu: begin wait.\n",
845 						__func__, remaining_min_edp_poweroff_time_ms);
846 				msleep(remaining_min_edp_poweroff_time_ms);
847 				DC_LOG_HW_RESUME_S3(
848 						"%s: remaining_min_edp_poweroff_time_ms=%llu: end wait.\n",
849 						__func__, remaining_min_edp_poweroff_time_ms);
850 				dm_output_to_console("%s: wait %lld ms to power on eDP.\n",
851 						__func__, remaining_min_edp_poweroff_time_ms);
852 			} else {
853 				DC_LOG_HW_RESUME_S3(
854 						"%s: remaining_min_edp_poweroff_time_ms=%llu: no wait required.\n",
855 						__func__, remaining_min_edp_poweroff_time_ms);
856 			}
857 		}
858 
859 		DC_LOG_HW_RESUME_S3(
860 				"%s: BEGIN: Panel Power action: %s\n",
861 				__func__, (power_up ? "On":"Off"));
862 
863 		cntl.action = power_up ?
864 			TRANSMITTER_CONTROL_POWER_ON :
865 			TRANSMITTER_CONTROL_POWER_OFF;
866 		cntl.transmitter = link->link_enc->transmitter;
867 		cntl.connector_obj_id = link->link_enc->connector;
868 		cntl.coherent = false;
869 		cntl.lanes_number = LANE_COUNT_FOUR;
870 		cntl.hpd_sel = link->link_enc->hpd_source;
871 		pwrseq_instance = link->panel_cntl->pwrseq_inst;
872 
873 		if (ctx->dc->ctx->dmub_srv &&
874 				ctx->dc->debug.dmub_command_table) {
875 
876 			if (cntl.action == TRANSMITTER_CONTROL_POWER_ON) {
877 				bp_result = ctx->dc_bios->funcs->enable_lvtma_control(ctx->dc_bios,
878 						LVTMA_CONTROL_POWER_ON,
879 						pwrseq_instance, link->link_powered_externally);
880 			} else {
881 				bp_result = ctx->dc_bios->funcs->enable_lvtma_control(ctx->dc_bios,
882 						LVTMA_CONTROL_POWER_OFF,
883 						pwrseq_instance, link->link_powered_externally);
884 			}
885 		}
886 
887 		bp_result = link_transmitter_control(ctx->dc_bios, &cntl);
888 
889 		DC_LOG_HW_RESUME_S3(
890 				"%s: END: Panel Power action: %s bp_result=%u\n",
891 				__func__, (power_up ? "On":"Off"),
892 				bp_result);
893 
894 		ctx->dc->link_srv->dp_trace_set_edp_power_timestamp(link, power_up);
895 
896 		DC_LOG_HW_RESUME_S3(
897 				"%s: updated values: edp_poweroff=%llu edp_poweron=%llu\n",
898 				__func__,
899 				ctx->dc->link_srv->dp_trace_get_edp_poweroff_timestamp(link),
900 				ctx->dc->link_srv->dp_trace_get_edp_poweron_timestamp(link));
901 
902 		if (bp_result != BP_RESULT_OK)
903 			DC_LOG_ERROR(
904 					"%s: Panel Power bp_result: %d\n",
905 					__func__, bp_result);
906 	} else {
907 		DC_LOG_HW_RESUME_S3(
908 				"%s: Skipping Panel Power action: %s\n",
909 				__func__, (power_up ? "On":"Off"));
910 	}
911 }
912 
dce110_edp_wait_for_T12(struct dc_link * link)913 void dce110_edp_wait_for_T12(
914 		struct dc_link *link)
915 {
916 	struct dc_context *ctx = link->ctx;
917 
918 	if (dal_graphics_object_id_get_connector_id(link->link_enc->connector)
919 			!= CONNECTOR_ID_EDP) {
920 		BREAK_TO_DEBUGGER();
921 		return;
922 	}
923 
924 	if (!link->panel_cntl)
925 		return;
926 
927 	if (!link->panel_cntl->funcs->is_panel_powered_on(link->panel_cntl) &&
928 			ctx->dc->link_srv->dp_trace_get_edp_poweroff_timestamp(link) != 0) {
929 		unsigned int t12_duration = 500; // Default T12 as per spec
930 		unsigned long long current_ts = dm_get_timestamp(ctx);
931 		unsigned long long time_since_edp_poweroff_ms =
932 				div64_u64(dm_get_elapse_time_in_ns(
933 						ctx,
934 						current_ts,
935 						ctx->dc->link_srv->dp_trace_get_edp_poweroff_timestamp(link)), 1000000);
936 
937 		t12_duration += link->panel_config.pps.extra_t12_ms; // Add extra T12
938 
939 		if (time_since_edp_poweroff_ms < t12_duration)
940 			msleep(t12_duration - time_since_edp_poweroff_ms);
941 	}
942 }
943 /*todo: cloned in stream enc, fix*/
944 /*
945  * @brief
946  * eDP only. Control the backlight of the eDP panel
947  */
dce110_edp_backlight_control(struct dc_link * link,bool enable)948 void dce110_edp_backlight_control(
949 		struct dc_link *link,
950 		bool enable)
951 {
952 	struct dc_context *ctx = link->ctx;
953 	struct bp_transmitter_control cntl = { 0 };
954 	uint8_t pwrseq_instance = 0;
955 	unsigned int pre_T11_delay = (link->dpcd_sink_ext_caps.bits.oled ? OLED_PRE_T11_DELAY : 0);
956 	unsigned int post_T7_delay = (link->dpcd_sink_ext_caps.bits.oled ? OLED_POST_T7_DELAY : 0);
957 
958 	if (dal_graphics_object_id_get_connector_id(link->link_enc->connector)
959 		!= CONNECTOR_ID_EDP) {
960 		BREAK_TO_DEBUGGER();
961 		return;
962 	}
963 
964 	if (link->panel_cntl && !(link->dpcd_sink_ext_caps.bits.oled ||
965 		link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1 ||
966 		link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1)) {
967 		bool is_backlight_on = link->panel_cntl->funcs->is_panel_backlight_on(link->panel_cntl);
968 
969 		if ((enable && is_backlight_on) || (!enable && !is_backlight_on)) {
970 			DC_LOG_HW_RESUME_S3(
971 				"%s: panel already powered up/off. Do nothing.\n",
972 				__func__);
973 			return;
974 		}
975 	}
976 
977 	/* Send VBIOS command to control eDP panel backlight */
978 
979 	DC_LOG_HW_RESUME_S3(
980 			"%s: backlight action: %s\n",
981 			__func__, (enable ? "On":"Off"));
982 
983 	cntl.action = enable ?
984 		TRANSMITTER_CONTROL_BACKLIGHT_ON :
985 		TRANSMITTER_CONTROL_BACKLIGHT_OFF;
986 
987 	/*cntl.engine_id = ctx->engine;*/
988 	cntl.transmitter = link->link_enc->transmitter;
989 	cntl.connector_obj_id = link->link_enc->connector;
990 	/*todo: unhardcode*/
991 	cntl.lanes_number = LANE_COUNT_FOUR;
992 	cntl.hpd_sel = link->link_enc->hpd_source;
993 	cntl.signal = SIGNAL_TYPE_EDP;
994 
995 	/* For eDP, the following delays might need to be considered
996 	 * after link training completed:
997 	 * idle period - min. accounts for required BS-Idle pattern,
998 	 * max. allows for source frame synchronization);
999 	 * 50 msec max. delay from valid video data from source
1000 	 * to video on dislpay or backlight enable.
1001 	 *
1002 	 * Disable the delay for now.
1003 	 * Enable it in the future if necessary.
1004 	 */
1005 	/* dc_service_sleep_in_milliseconds(50); */
1006 		/*edp 1.2*/
1007 	if (link->panel_cntl)
1008 		pwrseq_instance = link->panel_cntl->pwrseq_inst;
1009 
1010 	if (cntl.action == TRANSMITTER_CONTROL_BACKLIGHT_ON) {
1011 		if (!link->dc->config.edp_no_power_sequencing)
1012 		/*
1013 		 * Sometimes, DP receiver chip power-controlled externally by an
1014 		 * Embedded Controller could be treated and used as eDP,
1015 		 * if it drives mobile display. In this case,
1016 		 * we shouldn't be doing power-sequencing, hence we can skip
1017 		 * waiting for T7-ready.
1018 		 */
1019 			ctx->dc->link_srv->edp_receiver_ready_T7(link);
1020 		else
1021 			DC_LOG_DC("edp_receiver_ready_T7 skipped\n");
1022 	}
1023 
1024 	/* Setting link_powered_externally will bypass delays in the backlight
1025 	 * as they are not required if the link is being powered by a different
1026 	 * source.
1027 	 */
1028 	if (ctx->dc->ctx->dmub_srv &&
1029 			ctx->dc->debug.dmub_command_table) {
1030 		if (cntl.action == TRANSMITTER_CONTROL_BACKLIGHT_ON)
1031 			ctx->dc_bios->funcs->enable_lvtma_control(ctx->dc_bios,
1032 					LVTMA_CONTROL_LCD_BLON,
1033 					pwrseq_instance, link->link_powered_externally);
1034 		else
1035 			ctx->dc_bios->funcs->enable_lvtma_control(ctx->dc_bios,
1036 					LVTMA_CONTROL_LCD_BLOFF,
1037 					pwrseq_instance, link->link_powered_externally);
1038 	}
1039 
1040 	link_transmitter_control(ctx->dc_bios, &cntl);
1041 
1042 	if (enable && link->dpcd_sink_ext_caps.bits.oled &&
1043 	    !link->dc->config.edp_no_power_sequencing &&
1044 	    !link->local_sink->edid_caps.panel_patch.oled_optimize_display_on) {
1045 		post_T7_delay += link->panel_config.pps.extra_post_t7_ms;
1046 		msleep(post_T7_delay);
1047 	}
1048 
1049 	if (link->dpcd_sink_ext_caps.bits.oled ||
1050 		link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1 ||
1051 		link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1)
1052 		ctx->dc->link_srv->edp_backlight_enable_aux(link, enable);
1053 
1054 	/*edp 1.2*/
1055 	if (cntl.action == TRANSMITTER_CONTROL_BACKLIGHT_OFF) {
1056 		if (!link->dc->config.edp_no_power_sequencing)
1057 		/*
1058 		 * Sometimes, DP receiver chip power-controlled externally by an
1059 		 * Embedded Controller could be treated and used as eDP,
1060 		 * if it drives mobile display. In this case,
1061 		 * we shouldn't be doing power-sequencing, hence we can skip
1062 		 * waiting for T9-ready.
1063 		 */
1064 			ctx->dc->link_srv->edp_add_delay_for_T9(link);
1065 		else
1066 			DC_LOG_DC("edp_receiver_ready_T9 skipped\n");
1067 	}
1068 
1069 	if (!enable) {
1070 		/*follow oem panel config's requirement*/
1071 		pre_T11_delay += link->panel_config.pps.extra_pre_t11_ms;
1072 		if (pre_T11_delay)
1073 			msleep(pre_T11_delay);
1074 	}
1075 }
1076 
dce110_enable_audio_stream(struct pipe_ctx * pipe_ctx)1077 void dce110_enable_audio_stream(struct pipe_ctx *pipe_ctx)
1078 {
1079 	/* notify audio driver for audio modes of monitor */
1080 	struct dc *dc;
1081 	struct clk_mgr *clk_mgr;
1082 	unsigned int i, num_audio = 1;
1083 	const struct link_hwss *link_hwss;
1084 
1085 	if (!pipe_ctx->stream)
1086 		return;
1087 
1088 	dc = pipe_ctx->stream->ctx->dc;
1089 	clk_mgr = dc->clk_mgr;
1090 	link_hwss = get_link_hwss(pipe_ctx->stream->link, &pipe_ctx->link_res);
1091 
1092 	if (pipe_ctx->stream_res.audio && pipe_ctx->stream_res.audio->enabled == true)
1093 		return;
1094 
1095 	if (pipe_ctx->stream_res.audio) {
1096 		for (i = 0; i < MAX_PIPES; i++) {
1097 			/*current_state not updated yet*/
1098 			if (dc->current_state->res_ctx.pipe_ctx[i].stream_res.audio != NULL)
1099 				num_audio++;
1100 		}
1101 
1102 		pipe_ctx->stream_res.audio->funcs->az_enable(pipe_ctx->stream_res.audio);
1103 
1104 		if (num_audio >= 1 && clk_mgr->funcs->enable_pme_wa)
1105 			/*this is the first audio. apply the PME w/a in order to wake AZ from D3*/
1106 			clk_mgr->funcs->enable_pme_wa(clk_mgr);
1107 
1108 		link_hwss->enable_audio_packet(pipe_ctx);
1109 
1110 		if (pipe_ctx->stream_res.audio)
1111 			pipe_ctx->stream_res.audio->enabled = true;
1112 	}
1113 }
1114 
dce110_disable_audio_stream(struct pipe_ctx * pipe_ctx)1115 void dce110_disable_audio_stream(struct pipe_ctx *pipe_ctx)
1116 {
1117 	struct dc *dc;
1118 	struct clk_mgr *clk_mgr;
1119 	const struct link_hwss *link_hwss;
1120 
1121 	if (!pipe_ctx || !pipe_ctx->stream)
1122 		return;
1123 
1124 	dc = pipe_ctx->stream->ctx->dc;
1125 	clk_mgr = dc->clk_mgr;
1126 	link_hwss = get_link_hwss(pipe_ctx->stream->link, &pipe_ctx->link_res);
1127 
1128 	if (pipe_ctx->stream_res.audio && pipe_ctx->stream_res.audio->enabled == false)
1129 		return;
1130 
1131 	link_hwss->disable_audio_packet(pipe_ctx);
1132 
1133 	if (pipe_ctx->stream_res.audio) {
1134 		pipe_ctx->stream_res.audio->enabled = false;
1135 
1136 		if (clk_mgr->funcs->enable_pme_wa)
1137 			/*this is the first audio. apply the PME w/a in order to wake AZ from D3*/
1138 			clk_mgr->funcs->enable_pme_wa(clk_mgr);
1139 
1140 		/* TODO: notify audio driver for if audio modes list changed
1141 		 * add audio mode list change flag */
1142 		/* dal_audio_disable_azalia_audio_jack_presence(stream->audio,
1143 		 * stream->stream_engine_id);
1144 		 */
1145 	}
1146 }
1147 
dce110_disable_stream(struct pipe_ctx * pipe_ctx)1148 void dce110_disable_stream(struct pipe_ctx *pipe_ctx)
1149 {
1150 	struct dc_stream_state *stream = pipe_ctx->stream;
1151 	struct dc_link *link = stream->link;
1152 	struct dc *dc = pipe_ctx->stream->ctx->dc;
1153 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1154 	struct dccg *dccg = dc->res_pool->dccg;
1155 	struct timing_generator *tg = pipe_ctx->stream_res.tg;
1156 	struct dtbclk_dto_params dto_params = {0};
1157 	int dp_hpo_inst;
1158 	struct link_encoder *link_enc = pipe_ctx->link_res.dio_link_enc;
1159 	struct stream_encoder *stream_enc = pipe_ctx->stream_res.stream_enc;
1160 
1161 	if (!dc->config.unify_link_enc_assignment)
1162 		link_enc = link_enc_cfg_get_link_enc(link);
1163 
1164 	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal)) {
1165 		pipe_ctx->stream_res.stream_enc->funcs->stop_hdmi_info_packets(
1166 			pipe_ctx->stream_res.stream_enc);
1167 		pipe_ctx->stream_res.stream_enc->funcs->hdmi_reset_stream_attribute(
1168 			pipe_ctx->stream_res.stream_enc);
1169 	}
1170 
1171 	if (dc->link_srv->dp_is_128b_132b_signal(pipe_ctx)) {
1172 		pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->stop_dp_info_packets(
1173 					pipe_ctx->stream_res.hpo_dp_stream_enc);
1174 	} else if (dc_is_dp_signal(pipe_ctx->stream->signal))
1175 		pipe_ctx->stream_res.stream_enc->funcs->stop_dp_info_packets(
1176 			pipe_ctx->stream_res.stream_enc);
1177 
1178 	dc->hwss.disable_audio_stream(pipe_ctx);
1179 
1180 	link_hwss->reset_stream_encoder(pipe_ctx);
1181 
1182 	if (dc->link_srv->dp_is_128b_132b_signal(pipe_ctx) && dccg) {
1183 		dto_params.otg_inst = tg->inst;
1184 		dto_params.timing = &pipe_ctx->stream->timing;
1185 		dp_hpo_inst = pipe_ctx->stream_res.hpo_dp_stream_enc->inst;
1186 		if (dccg) {
1187 			dccg->funcs->disable_symclk32_se(dccg, dp_hpo_inst);
1188 			dccg->funcs->set_dpstreamclk(dccg, REFCLK, tg->inst, dp_hpo_inst);
1189 			if (!(dc->ctx->dce_version >= DCN_VERSION_3_5)) {
1190 				if (dccg && dccg->funcs->set_dtbclk_dto)
1191 					dccg->funcs->set_dtbclk_dto(dccg, &dto_params);
1192 			}
1193 		}
1194 	} else if (dccg && dccg->funcs->disable_symclk_se) {
1195 		dccg->funcs->disable_symclk_se(dccg, stream_enc->stream_enc_inst,
1196 					       link_enc->transmitter - TRANSMITTER_UNIPHY_A);
1197 	}
1198 }
1199 
dce110_unblank_stream(struct pipe_ctx * pipe_ctx,struct dc_link_settings * link_settings)1200 void dce110_unblank_stream(struct pipe_ctx *pipe_ctx,
1201 		struct dc_link_settings *link_settings)
1202 {
1203 	struct encoder_unblank_param params = { { 0 } };
1204 	struct dc_stream_state *stream = pipe_ctx->stream;
1205 	struct dc_link *link = stream->link;
1206 	struct dce_hwseq *hws = link->dc->hwseq;
1207 
1208 	/* only 3 items below are used by unblank */
1209 	params.timing = pipe_ctx->stream->timing;
1210 	params.link_settings.link_rate = link_settings->link_rate;
1211 
1212 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
1213 		pipe_ctx->stream_res.stream_enc->funcs->dp_unblank(link, pipe_ctx->stream_res.stream_enc, &params);
1214 
1215 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1216 		hws->funcs.edp_backlight_control(link, true);
1217 	}
1218 }
1219 
dce110_blank_stream(struct pipe_ctx * pipe_ctx)1220 void dce110_blank_stream(struct pipe_ctx *pipe_ctx)
1221 {
1222 	struct dc_stream_state *stream = pipe_ctx->stream;
1223 	struct dc_link *link = stream->link;
1224 	struct dce_hwseq *hws = link->dc->hwseq;
1225 
1226 	if (hws && hws->wa_state.skip_blank_stream)
1227 		return;
1228 
1229 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1230 		if (!link->skip_implict_edp_power_control && hws)
1231 			hws->funcs.edp_backlight_control(link, false);
1232 		link->dc->hwss.set_abm_immediate_disable(pipe_ctx);
1233 	}
1234 
1235 	if (link->dc->link_srv->dp_is_128b_132b_signal(pipe_ctx)) {
1236 		/* TODO - DP2.0 HW: Set ODM mode in dp hpo encoder here */
1237 		pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_blank(
1238 				pipe_ctx->stream_res.hpo_dp_stream_enc);
1239 	} else if (dc_is_dp_signal(pipe_ctx->stream->signal)) {
1240 		pipe_ctx->stream_res.stream_enc->funcs->dp_blank(link, pipe_ctx->stream_res.stream_enc);
1241 
1242 		if (!dc_is_embedded_signal(pipe_ctx->stream->signal)) {
1243 			/*
1244 			 * After output is idle pattern some sinks need time to recognize the stream
1245 			 * has changed or they enter protection state and hang.
1246 			 */
1247 			msleep(60);
1248 		}
1249 	}
1250 
1251 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP &&
1252 	    !link->dc->config.edp_no_power_sequencing) {
1253 			/*
1254 			 * Sometimes, DP receiver chip power-controlled externally by an
1255 			 * Embedded Controller could be treated and used as eDP,
1256 			 * if it drives mobile display. In this case,
1257 			 * we shouldn't be doing power-sequencing, hence we can skip
1258 			 * waiting for T9-ready.
1259 			 */
1260 		link->dc->link_srv->edp_receiver_ready_T9(link);
1261 	}
1262 
1263 }
1264 
1265 
dce110_set_avmute(struct pipe_ctx * pipe_ctx,bool enable)1266 void dce110_set_avmute(struct pipe_ctx *pipe_ctx, bool enable)
1267 {
1268 	if (pipe_ctx != NULL && pipe_ctx->stream_res.stream_enc != NULL)
1269 		pipe_ctx->stream_res.stream_enc->funcs->set_avmute(pipe_ctx->stream_res.stream_enc, enable);
1270 }
1271 
translate_to_dto_source(enum controller_id crtc_id)1272 static enum audio_dto_source translate_to_dto_source(enum controller_id crtc_id)
1273 {
1274 	switch (crtc_id) {
1275 	case CONTROLLER_ID_D0:
1276 		return DTO_SOURCE_ID0;
1277 	case CONTROLLER_ID_D1:
1278 		return DTO_SOURCE_ID1;
1279 	case CONTROLLER_ID_D2:
1280 		return DTO_SOURCE_ID2;
1281 	case CONTROLLER_ID_D3:
1282 		return DTO_SOURCE_ID3;
1283 	case CONTROLLER_ID_D4:
1284 		return DTO_SOURCE_ID4;
1285 	case CONTROLLER_ID_D5:
1286 		return DTO_SOURCE_ID5;
1287 	default:
1288 		return DTO_SOURCE_UNKNOWN;
1289 	}
1290 }
1291 
populate_audio_dp_link_info(const struct pipe_ctx * pipe_ctx,struct audio_dp_link_info * dp_link_info)1292 static void populate_audio_dp_link_info(
1293 	const struct pipe_ctx *pipe_ctx,
1294 	struct audio_dp_link_info *dp_link_info)
1295 {
1296 	const struct dc_stream_state *stream = pipe_ctx->stream;
1297 	const struct dc_link *link = stream->link;
1298 	struct fixed31_32 link_bw_kbps;
1299 
1300 	dp_link_info->encoding = link->dc->link_srv->dp_get_encoding_format(
1301 				&pipe_ctx->link_config.dp_link_settings);
1302 	dp_link_info->is_mst = (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST);
1303 	dp_link_info->lane_count = pipe_ctx->link_config.dp_link_settings.lane_count;
1304 	dp_link_info->link_rate = pipe_ctx->link_config.dp_link_settings.link_rate;
1305 
1306 	link_bw_kbps = dc_fixpt_from_int(dc_link_bandwidth_kbps(link,
1307 			&pipe_ctx->link_config.dp_link_settings));
1308 
1309 	/* For audio stream calculations, the video stream should not include FEC or SSC
1310 	 * in order to get the most pessimistic values.
1311 	 */
1312 	if (dp_link_info->encoding == DP_8b_10b_ENCODING &&
1313 			link->dc->link_srv->dp_is_fec_supported(link)) {
1314 		link_bw_kbps = dc_fixpt_mul(link_bw_kbps,
1315 				dc_fixpt_from_fraction(100, DATA_EFFICIENCY_8b_10b_FEC_EFFICIENCY_x100));
1316 	} else if (dp_link_info->encoding == DP_128b_132b_ENCODING) {
1317 		link_bw_kbps = dc_fixpt_mul(link_bw_kbps,
1318 				dc_fixpt_from_fraction(10000, 9975)); /* 99.75% SSC overhead*/
1319 	}
1320 
1321 	dp_link_info->link_bandwidth_kbps = dc_fixpt_floor(link_bw_kbps);
1322 
1323 	/* Calculates hblank_min_symbol_width for 128b/132b
1324 	 * Corresponding HBLANK_MIN_SYMBOL_WIDTH register is calculated as:
1325 	 *   floor(h_blank * bits_per_pixel / 128)
1326 	 */
1327 	if (dp_link_info->encoding == DP_128b_132b_ENCODING) {
1328 		struct dc_crtc_timing *crtc_timing = &pipe_ctx->stream->timing;
1329 
1330 		uint32_t h_active = crtc_timing->h_addressable + crtc_timing->h_border_left
1331 				+ crtc_timing->h_border_right;
1332 		uint32_t h_blank = crtc_timing->h_total - h_active;
1333 
1334 		uint32_t bpp;
1335 
1336 		if (crtc_timing->flags.DSC) {
1337 			bpp = crtc_timing->dsc_cfg.bits_per_pixel;
1338 		} else {
1339 			/* When the timing is using DSC, dsc_cfg.bits_per_pixel is in 16th bits.
1340 			 * The bpp in this path is scaled to 16th bits so the final calculation
1341 			 * is correct for both cases.
1342 			 */
1343 			bpp = 16;
1344 			switch (crtc_timing->display_color_depth) {
1345 			case COLOR_DEPTH_666:
1346 				bpp *= 18;
1347 				break;
1348 			case COLOR_DEPTH_888:
1349 				bpp *= 24;
1350 				break;
1351 			case COLOR_DEPTH_101010:
1352 				bpp *= 30;
1353 				break;
1354 			case COLOR_DEPTH_121212:
1355 				bpp *= 36;
1356 				break;
1357 			default:
1358 				bpp = 0;
1359 				break;
1360 			}
1361 
1362 			switch (crtc_timing->pixel_encoding) {
1363 			case PIXEL_ENCODING_YCBCR422:
1364 				bpp = bpp * 2 / 3;
1365 				break;
1366 			case PIXEL_ENCODING_YCBCR420:
1367 				bpp /= 2;
1368 				break;
1369 			default:
1370 				break;
1371 			}
1372 		}
1373 
1374 		/* Min symbol width = floor(h_blank * (bpp/16) / 128) */
1375 		dp_link_info->hblank_min_symbol_width = dc_fixpt_floor(
1376 				dc_fixpt_div(dc_fixpt_from_int(h_blank * bpp),
1377 						dc_fixpt_from_int(128 / 16)));
1378 
1379 	} else {
1380 		dp_link_info->hblank_min_symbol_width = 0;
1381 	}
1382 }
1383 
build_audio_output(struct dc_state * state,const struct pipe_ctx * pipe_ctx,struct audio_output * audio_output)1384 void build_audio_output(
1385 	struct dc_state *state,
1386 	const struct pipe_ctx *pipe_ctx,
1387 	struct audio_output *audio_output)
1388 {
1389 	const struct dc_stream_state *stream = pipe_ctx->stream;
1390 	audio_output->engine_id = pipe_ctx->stream_res.stream_enc->id;
1391 
1392 	audio_output->signal = pipe_ctx->stream->signal;
1393 
1394 	/* audio_crtc_info  */
1395 
1396 	audio_output->crtc_info.h_total =
1397 		stream->timing.h_total;
1398 
1399 	/*
1400 	 * Audio packets are sent during actual CRTC blank physical signal, we
1401 	 * need to specify actual active signal portion
1402 	 */
1403 	audio_output->crtc_info.h_active =
1404 			stream->timing.h_addressable
1405 			+ stream->timing.h_border_left
1406 			+ stream->timing.h_border_right;
1407 
1408 	audio_output->crtc_info.v_active =
1409 			stream->timing.v_addressable
1410 			+ stream->timing.v_border_top
1411 			+ stream->timing.v_border_bottom;
1412 
1413 	audio_output->crtc_info.pixel_repetition = 1;
1414 
1415 	audio_output->crtc_info.interlaced =
1416 			stream->timing.flags.INTERLACE;
1417 
1418 	audio_output->crtc_info.refresh_rate =
1419 		(stream->timing.pix_clk_100hz*100)/
1420 		(stream->timing.h_total*stream->timing.v_total);
1421 
1422 	audio_output->crtc_info.color_depth =
1423 		stream->timing.display_color_depth;
1424 
1425 	audio_output->crtc_info.requested_pixel_clock_100Hz =
1426 			pipe_ctx->stream_res.pix_clk_params.requested_pix_clk_100hz;
1427 
1428 	audio_output->crtc_info.calculated_pixel_clock_100Hz =
1429 			pipe_ctx->stream_res.pix_clk_params.requested_pix_clk_100hz;
1430 
1431 	audio_output->crtc_info.pixel_encoding =
1432 		stream->timing.pixel_encoding;
1433 
1434 	audio_output->crtc_info.dsc_bits_per_pixel =
1435 			stream->timing.dsc_cfg.bits_per_pixel;
1436 
1437 	audio_output->crtc_info.dsc_num_slices =
1438 			stream->timing.dsc_cfg.num_slices_h;
1439 
1440 /*for HDMI, audio ACR is with deep color ratio factor*/
1441 	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal) &&
1442 		audio_output->crtc_info.requested_pixel_clock_100Hz ==
1443 				(stream->timing.pix_clk_100hz)) {
1444 		if (pipe_ctx->stream_res.pix_clk_params.pixel_encoding == PIXEL_ENCODING_YCBCR420) {
1445 			audio_output->crtc_info.requested_pixel_clock_100Hz =
1446 					audio_output->crtc_info.requested_pixel_clock_100Hz/2;
1447 			audio_output->crtc_info.calculated_pixel_clock_100Hz =
1448 					pipe_ctx->stream_res.pix_clk_params.requested_pix_clk_100hz/2;
1449 
1450 		}
1451 	}
1452 
1453 	if (state->clk_mgr &&
1454 		(pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT ||
1455 			pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)) {
1456 		audio_output->pll_info.audio_dto_source_clock_in_khz =
1457 				state->clk_mgr->funcs->get_dp_ref_clk_frequency(
1458 						state->clk_mgr);
1459 	}
1460 
1461 	audio_output->pll_info.feed_back_divider =
1462 			pipe_ctx->pll_settings.feedback_divider;
1463 
1464 	audio_output->pll_info.dto_source =
1465 		translate_to_dto_source(
1466 			pipe_ctx->stream_res.tg->inst + 1);
1467 
1468 	/* TODO hard code to enable for now. Need get from stream */
1469 	audio_output->pll_info.ss_enabled = true;
1470 
1471 	audio_output->pll_info.ss_percentage =
1472 			pipe_ctx->pll_settings.ss_percentage;
1473 
1474 	if (dc_is_dp_signal(pipe_ctx->stream->signal)) {
1475 		populate_audio_dp_link_info(pipe_ctx, &audio_output->dp_link_info);
1476 	}
1477 }
1478 
program_scaler(const struct dc * dc,const struct pipe_ctx * pipe_ctx)1479 static void program_scaler(const struct dc *dc,
1480 		const struct pipe_ctx *pipe_ctx)
1481 {
1482 	struct tg_color color = {0};
1483 
1484 	/* TOFPGA */
1485 	if (pipe_ctx->plane_res.xfm->funcs->transform_set_pixel_storage_depth == NULL)
1486 		return;
1487 
1488 	if (dc->debug.visual_confirm == VISUAL_CONFIRM_SURFACE)
1489 		get_surface_visual_confirm_color(pipe_ctx, &color);
1490 	else
1491 		color_space_to_black_color(dc,
1492 				pipe_ctx->stream->output_color_space,
1493 				&color);
1494 
1495 	pipe_ctx->plane_res.xfm->funcs->transform_set_pixel_storage_depth(
1496 		pipe_ctx->plane_res.xfm,
1497 		pipe_ctx->plane_res.scl_data.lb_params.depth,
1498 		&pipe_ctx->stream->bit_depth_params);
1499 
1500 	if (pipe_ctx->stream_res.tg->funcs->set_overscan_blank_color) {
1501 		/*
1502 		 * The way 420 is packed, 2 channels carry Y component, 1 channel
1503 		 * alternate between Cb and Cr, so both channels need the pixel
1504 		 * value for Y
1505 		 */
1506 		if (pipe_ctx->stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420)
1507 			color.color_r_cr = color.color_g_y;
1508 
1509 		pipe_ctx->stream_res.tg->funcs->set_overscan_blank_color(
1510 				pipe_ctx->stream_res.tg,
1511 				&color);
1512 	}
1513 
1514 	pipe_ctx->plane_res.xfm->funcs->transform_set_scaler(pipe_ctx->plane_res.xfm,
1515 		&pipe_ctx->plane_res.scl_data);
1516 }
1517 
dce110_enable_stream_timing(struct pipe_ctx * pipe_ctx,struct dc_state * context,struct dc * dc)1518 static enum dc_status dce110_enable_stream_timing(
1519 		struct pipe_ctx *pipe_ctx,
1520 		struct dc_state *context,
1521 		struct dc *dc)
1522 {
1523 	struct dc_stream_state *stream = pipe_ctx->stream;
1524 	struct pipe_ctx *pipe_ctx_old = &dc->current_state->res_ctx.
1525 			pipe_ctx[pipe_ctx->pipe_idx];
1526 	struct tg_color black_color = {0};
1527 
1528 	if (!pipe_ctx_old->stream) {
1529 
1530 		/* program blank color */
1531 		color_space_to_black_color(dc,
1532 				stream->output_color_space, &black_color);
1533 		pipe_ctx->stream_res.tg->funcs->set_blank_color(
1534 				pipe_ctx->stream_res.tg,
1535 				&black_color);
1536 
1537 		/*
1538 		 * Must blank CRTC after disabling power gating and before any
1539 		 * programming, otherwise CRTC will be hung in bad state
1540 		 */
1541 		pipe_ctx->stream_res.tg->funcs->set_blank(pipe_ctx->stream_res.tg, true);
1542 
1543 		if (false == pipe_ctx->clock_source->funcs->program_pix_clk(
1544 				pipe_ctx->clock_source,
1545 				&pipe_ctx->stream_res.pix_clk_params,
1546 				dc->link_srv->dp_get_encoding_format(&pipe_ctx->link_config.dp_link_settings),
1547 				&pipe_ctx->pll_settings)) {
1548 			BREAK_TO_DEBUGGER();
1549 			return DC_ERROR_UNEXPECTED;
1550 		}
1551 
1552 		if (dc_is_hdmi_tmds_signal(stream->signal)) {
1553 			stream->link->phy_state.symclk_ref_cnts.otg = 1;
1554 			if (stream->link->phy_state.symclk_state == SYMCLK_OFF_TX_OFF)
1555 				stream->link->phy_state.symclk_state = SYMCLK_ON_TX_OFF;
1556 			else
1557 				stream->link->phy_state.symclk_state = SYMCLK_ON_TX_ON;
1558 		}
1559 
1560 		pipe_ctx->stream_res.tg->funcs->program_timing(
1561 				pipe_ctx->stream_res.tg,
1562 				&stream->timing,
1563 				0,
1564 				0,
1565 				0,
1566 				0,
1567 				0,
1568 				pipe_ctx->stream->signal,
1569 				true);
1570 	}
1571 
1572 	if (!pipe_ctx_old->stream) {
1573 		if (false == pipe_ctx->stream_res.tg->funcs->enable_crtc(
1574 				pipe_ctx->stream_res.tg)) {
1575 			BREAK_TO_DEBUGGER();
1576 			return DC_ERROR_UNEXPECTED;
1577 		}
1578 	}
1579 
1580 	return DC_OK;
1581 }
1582 
dce110_apply_single_controller_ctx_to_hw(struct pipe_ctx * pipe_ctx,struct dc_state * context,struct dc * dc)1583 enum dc_status dce110_apply_single_controller_ctx_to_hw(
1584 		struct pipe_ctx *pipe_ctx,
1585 		struct dc_state *context,
1586 		struct dc *dc)
1587 {
1588 	struct dc_stream_state *stream = pipe_ctx->stream;
1589 	struct dc_link *link = stream->link;
1590 	struct drr_params params = {0};
1591 	unsigned int event_triggers = 0;
1592 	struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
1593 	struct dce_hwseq *hws = dc->hwseq;
1594 	const struct link_hwss *link_hwss = get_link_hwss(
1595 			link, &pipe_ctx->link_res);
1596 
1597 
1598 	if (hws->funcs.disable_stream_gating) {
1599 		hws->funcs.disable_stream_gating(dc, pipe_ctx);
1600 	}
1601 
1602 	if (pipe_ctx->stream_res.audio != NULL) {
1603 		struct audio_output audio_output = {0};
1604 
1605 		build_audio_output(context, pipe_ctx, &audio_output);
1606 
1607 		link_hwss->setup_audio_output(pipe_ctx, &audio_output,
1608 				pipe_ctx->stream_res.audio->inst);
1609 
1610 		pipe_ctx->stream_res.audio->funcs->az_configure(
1611 				pipe_ctx->stream_res.audio,
1612 				pipe_ctx->stream->signal,
1613 				&audio_output.crtc_info,
1614 				&pipe_ctx->stream->audio_info,
1615 				&audio_output.dp_link_info);
1616 
1617 		if (dc->config.disable_hbr_audio_dp2)
1618 			if (pipe_ctx->stream_res.audio->funcs->az_disable_hbr_audio &&
1619 					dc->link_srv->dp_is_128b_132b_signal(pipe_ctx))
1620 				pipe_ctx->stream_res.audio->funcs->az_disable_hbr_audio(pipe_ctx->stream_res.audio);
1621 	}
1622 
1623 	/* make sure no pipes syncd to the pipe being enabled */
1624 	if (!pipe_ctx->stream->apply_seamless_boot_optimization && dc->config.use_pipe_ctx_sync_logic)
1625 		check_syncd_pipes_for_disabled_master_pipe(dc, context, pipe_ctx->pipe_idx);
1626 
1627 	pipe_ctx->stream_res.opp->funcs->opp_program_fmt(
1628 		pipe_ctx->stream_res.opp,
1629 		&stream->bit_depth_params,
1630 		&stream->clamping);
1631 
1632 	pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
1633 			pipe_ctx->stream_res.opp,
1634 			COLOR_SPACE_YCBCR601,
1635 			stream->timing.display_color_depth,
1636 			stream->signal);
1637 
1638 	while (odm_pipe) {
1639 		odm_pipe->stream_res.opp->funcs->opp_set_dyn_expansion(
1640 				odm_pipe->stream_res.opp,
1641 				COLOR_SPACE_YCBCR601,
1642 				stream->timing.display_color_depth,
1643 				stream->signal);
1644 
1645 		odm_pipe->stream_res.opp->funcs->opp_program_fmt(
1646 				odm_pipe->stream_res.opp,
1647 				&stream->bit_depth_params,
1648 				&stream->clamping);
1649 		odm_pipe = odm_pipe->next_odm_pipe;
1650 	}
1651 
1652 	/* DCN3.1 FPGA Workaround
1653 	 * Need to enable HPO DP Stream Encoder before setting OTG master enable.
1654 	 * To do so, move calling function enable_stream_timing to only be done AFTER calling
1655 	 * function core_link_enable_stream
1656 	 */
1657 	if (!(hws->wa.dp_hpo_and_otg_sequence && dc->link_srv->dp_is_128b_132b_signal(pipe_ctx)))
1658 		/*  */
1659 		/* Do not touch stream timing on seamless boot optimization. */
1660 		if (!pipe_ctx->stream->apply_seamless_boot_optimization)
1661 			hws->funcs.enable_stream_timing(pipe_ctx, context, dc);
1662 
1663 	if (hws->funcs.setup_vupdate_interrupt)
1664 		hws->funcs.setup_vupdate_interrupt(dc, pipe_ctx);
1665 
1666 	params.vertical_total_min = stream->adjust.v_total_min;
1667 	params.vertical_total_max = stream->adjust.v_total_max;
1668 	set_drr_and_clear_adjust_pending(pipe_ctx, stream, &params);
1669 
1670 	// DRR should set trigger event to monitor surface update event
1671 	if (stream->adjust.v_total_min != 0 && stream->adjust.v_total_max != 0)
1672 		event_triggers = 0x80;
1673 	/* Event triggers and num frames initialized for DRR, but can be
1674 	 * later updated for PSR use. Note DRR trigger events are generated
1675 	 * regardless of whether num frames met.
1676 	 */
1677 	if (pipe_ctx->stream_res.tg->funcs->set_static_screen_control)
1678 		pipe_ctx->stream_res.tg->funcs->set_static_screen_control(
1679 				pipe_ctx->stream_res.tg, event_triggers, 2);
1680 
1681 	if (!dc_is_virtual_signal(pipe_ctx->stream->signal))
1682 		pipe_ctx->stream_res.stream_enc->funcs->dig_connect_to_otg(
1683 			pipe_ctx->stream_res.stream_enc,
1684 			pipe_ctx->stream_res.tg->inst);
1685 
1686 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
1687 		dc->link_srv->dp_trace_source_sequence(link, DPCD_SOURCE_SEQ_AFTER_CONNECT_DIG_FE_OTG);
1688 
1689 	/* Temporary workaround to perform DSC programming ahead of stream enablement
1690 	 * for smartmux/SPRS
1691 	 * TODO: Remove SmartMux/SPRS checks once movement of DSC programming is generalized
1692 	 */
1693 	if (pipe_ctx->stream->timing.flags.DSC) {
1694 		if ((pipe_ctx->stream->signal == SIGNAL_TYPE_EDP &&
1695 			((link->dc->config.smart_mux_version && link->dc->is_switch_in_progress_dest)
1696 			|| link->is_dds || link->skip_implict_edp_power_control)) &&
1697 			(dc_is_dp_signal(pipe_ctx->stream->signal) ||
1698 			dc_is_virtual_signal(pipe_ctx->stream->signal)))
1699 			dc->link_srv->set_dsc_enable(pipe_ctx, true);
1700 	}
1701 
1702 	if (!stream->dpms_off)
1703 		dc->link_srv->set_dpms_on(context, pipe_ctx);
1704 
1705 	/* DCN3.1 FPGA Workaround
1706 	 * Need to enable HPO DP Stream Encoder before setting OTG master enable.
1707 	 * To do so, move calling function enable_stream_timing to only be done AFTER calling
1708 	 * function core_link_enable_stream
1709 	 */
1710 	if (hws->wa.dp_hpo_and_otg_sequence && dc->link_srv->dp_is_128b_132b_signal(pipe_ctx)) {
1711 		if (!pipe_ctx->stream->apply_seamless_boot_optimization)
1712 			hws->funcs.enable_stream_timing(pipe_ctx, context, dc);
1713 	}
1714 
1715 	pipe_ctx->plane_res.scl_data.lb_params.alpha_en = pipe_ctx->bottom_pipe != NULL;
1716 
1717 	/* Phantom and main stream share the same link (because the stream
1718 	 * is constructed with the same sink). Make sure not to override
1719 	 * and link programming on the main.
1720 	 */
1721 	if (dc_state_get_pipe_subvp_type(context, pipe_ctx) != SUBVP_PHANTOM) {
1722 		pipe_ctx->stream->link->psr_settings.psr_feature_enabled = false;
1723 		pipe_ctx->stream->link->replay_settings.replay_feature_enabled = false;
1724 	}
1725 	return DC_OK;
1726 }
1727 
1728 /******************************************************************************/
1729 
power_down_encoders(struct dc * dc)1730 static void power_down_encoders(struct dc *dc)
1731 {
1732 	int i;
1733 
1734 	for (i = 0; i < dc->link_count; i++) {
1735 		enum signal_type signal = dc->links[i]->connector_signal;
1736 
1737 		dc->link_srv->blank_dp_stream(dc->links[i], false);
1738 
1739 		if (signal != SIGNAL_TYPE_EDP)
1740 			signal = SIGNAL_TYPE_NONE;
1741 
1742 		if (dc->links[i]->ep_type == DISPLAY_ENDPOINT_PHY)
1743 			dc->links[i]->link_enc->funcs->disable_output(
1744 					dc->links[i]->link_enc, signal);
1745 
1746 		dc->links[i]->link_status.link_active = false;
1747 		memset(&dc->links[i]->cur_link_settings, 0,
1748 				sizeof(dc->links[i]->cur_link_settings));
1749 	}
1750 }
1751 
power_down_controllers(struct dc * dc)1752 static void power_down_controllers(struct dc *dc)
1753 {
1754 	int i;
1755 
1756 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
1757 		dc->res_pool->timing_generators[i]->funcs->disable_crtc(
1758 				dc->res_pool->timing_generators[i]);
1759 	}
1760 }
1761 
power_down_clock_sources(struct dc * dc)1762 static void power_down_clock_sources(struct dc *dc)
1763 {
1764 	int i;
1765 
1766 	if (dc->res_pool->dp_clock_source->funcs->cs_power_down(
1767 		dc->res_pool->dp_clock_source) == false)
1768 		dm_error("Failed to power down pll! (dp clk src)\n");
1769 
1770 	for (i = 0; i < dc->res_pool->clk_src_count; i++) {
1771 		if (dc->res_pool->clock_sources[i]->funcs->cs_power_down(
1772 				dc->res_pool->clock_sources[i]) == false)
1773 			dm_error("Failed to power down pll! (clk src index=%d)\n", i);
1774 	}
1775 }
1776 
power_down_all_hw_blocks(struct dc * dc)1777 static void power_down_all_hw_blocks(struct dc *dc)
1778 {
1779 	power_down_encoders(dc);
1780 
1781 	power_down_controllers(dc);
1782 
1783 	power_down_clock_sources(dc);
1784 
1785 	if (dc->fbc_compressor)
1786 		dc->fbc_compressor->funcs->disable_fbc(dc->fbc_compressor);
1787 }
1788 
disable_vga_and_power_gate_all_controllers(struct dc * dc)1789 static void disable_vga_and_power_gate_all_controllers(
1790 		struct dc *dc)
1791 {
1792 	int i;
1793 	struct timing_generator *tg;
1794 	struct dc_context *ctx = dc->ctx;
1795 
1796 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
1797 		tg = dc->res_pool->timing_generators[i];
1798 
1799 		if (tg->funcs->disable_vga)
1800 			tg->funcs->disable_vga(tg);
1801 	}
1802 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
1803 		/* Enable CLOCK gating for each pipe BEFORE controller
1804 		 * powergating. */
1805 		enable_display_pipe_clock_gating(ctx,
1806 				true);
1807 
1808 		dc->current_state->res_ctx.pipe_ctx[i].pipe_idx = i;
1809 		dc->hwss.disable_plane(dc, dc->current_state,
1810 			&dc->current_state->res_ctx.pipe_ctx[i]);
1811 	}
1812 }
1813 
1814 
get_edp_streams(struct dc_state * context,struct dc_stream_state ** edp_streams,int * edp_stream_num)1815 static void get_edp_streams(struct dc_state *context,
1816 		struct dc_stream_state **edp_streams,
1817 		int *edp_stream_num)
1818 {
1819 	int i;
1820 
1821 	*edp_stream_num = 0;
1822 	for (i = 0; i < context->stream_count; i++) {
1823 		if (context->streams[i]->signal == SIGNAL_TYPE_EDP) {
1824 			edp_streams[*edp_stream_num] = context->streams[i];
1825 			if (++(*edp_stream_num) == MAX_NUM_EDP)
1826 				return;
1827 		}
1828 	}
1829 }
1830 
get_edp_links_with_sink(struct dc * dc,struct dc_link ** edp_links_with_sink,int * edp_with_sink_num)1831 static void get_edp_links_with_sink(
1832 		struct dc *dc,
1833 		struct dc_link **edp_links_with_sink,
1834 		int *edp_with_sink_num)
1835 {
1836 	int i;
1837 
1838 	/* check if there is an eDP panel not in use */
1839 	*edp_with_sink_num = 0;
1840 	for (i = 0; i < dc->link_count; i++) {
1841 		if (dc->links[i]->local_sink &&
1842 			dc->links[i]->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1843 			edp_links_with_sink[*edp_with_sink_num] = dc->links[i];
1844 			if (++(*edp_with_sink_num) == MAX_NUM_EDP)
1845 				return;
1846 		}
1847 	}
1848 }
1849 
clean_up_dsc_blocks(struct dc * dc)1850 static void clean_up_dsc_blocks(struct dc *dc)
1851 {
1852 	struct display_stream_compressor *dsc = NULL;
1853 	struct timing_generator *tg = NULL;
1854 	struct stream_encoder *se = NULL;
1855 	struct dccg *dccg = dc->res_pool->dccg;
1856 	struct pg_cntl *pg_cntl = dc->res_pool->pg_cntl;
1857 	int i;
1858 
1859 	if (!dc->caps.is_apu ||
1860 		dc->ctx->dce_version < DCN_VERSION_3_15)
1861 		return;
1862 	/*VBIOS supports dsc starts from dcn315*/
1863 	for (i = 0; i < dc->res_pool->res_cap->num_dsc; i++) {
1864 		struct dcn_dsc_state s  = {0};
1865 
1866 		dsc = dc->res_pool->dscs[i];
1867 		dsc->funcs->dsc_read_state(dsc, &s);
1868 		if (s.dsc_fw_en) {
1869 			/* disable DSC in OPTC */
1870 			if (i < dc->res_pool->timing_generator_count) {
1871 				tg = dc->res_pool->timing_generators[i];
1872 				tg->funcs->set_dsc_config(tg, OPTC_DSC_DISABLED, 0, 0);
1873 			}
1874 			/* disable DSC in stream encoder */
1875 			if (i < dc->res_pool->stream_enc_count) {
1876 				se = dc->res_pool->stream_enc[i];
1877 				se->funcs->dp_set_dsc_config(se, OPTC_DSC_DISABLED, 0, 0);
1878 				se->funcs->dp_set_dsc_pps_info_packet(se, false, NULL, true);
1879 			}
1880 			/* disable DSC block */
1881 			if (dccg->funcs->set_ref_dscclk)
1882 				dccg->funcs->set_ref_dscclk(dccg, dsc->inst);
1883 			dsc->funcs->dsc_disable(dsc);
1884 
1885 			/* power down DSC */
1886 			if (pg_cntl != NULL)
1887 				pg_cntl->funcs->dsc_pg_control(pg_cntl, dsc->inst, false);
1888 		}
1889 	}
1890 }
1891 
1892 /*
1893  * When ASIC goes from VBIOS/VGA mode to driver/accelerated mode we need:
1894  *  1. Power down all DC HW blocks
1895  *  2. Disable VGA engine on all controllers
1896  *  3. Enable power gating for controller
1897  *  4. Set acc_mode_change bit (VBIOS will clear this bit when going to FSDOS)
1898  */
dce110_enable_accelerated_mode(struct dc * dc,struct dc_state * context)1899 void dce110_enable_accelerated_mode(struct dc *dc, struct dc_state *context)
1900 {
1901 	struct dc_link *edp_links_with_sink[MAX_NUM_EDP];
1902 	struct dc_link *edp_links[MAX_NUM_EDP];
1903 	struct dc_stream_state *edp_streams[MAX_NUM_EDP];
1904 	struct dc_link *edp_link_with_sink = NULL;
1905 	struct dc_link *edp_link = NULL;
1906 	struct pipe_ctx *pipe_ctx = NULL;
1907 	struct dce_hwseq *hws = dc->hwseq;
1908 	int edp_with_sink_num;
1909 	int edp_num;
1910 	int edp_stream_num;
1911 	int i;
1912 	bool can_apply_edp_fast_boot = false;
1913 	bool can_apply_seamless_boot = false;
1914 	bool keep_edp_vdd_on = false;
1915 	struct dc_bios *dcb = dc->ctx->dc_bios;
1916 	DC_LOGGER_INIT();
1917 
1918 
1919 	get_edp_links_with_sink(dc, edp_links_with_sink, &edp_with_sink_num);
1920 	dc_get_edp_links(dc, edp_links, &edp_num);
1921 
1922 	if (hws->funcs.init_pipes)
1923 		hws->funcs.init_pipes(dc, context);
1924 
1925 	get_edp_streams(context, edp_streams, &edp_stream_num);
1926 
1927 	// Check fastboot support, disable on DCE8 because of blank screens
1928 	if (edp_num && edp_stream_num && dc->ctx->dce_version != DCE_VERSION_8_0 &&
1929 		    dc->ctx->dce_version != DCE_VERSION_8_1 &&
1930 		    dc->ctx->dce_version != DCE_VERSION_8_3) {
1931 		for (i = 0; i < edp_num; i++) {
1932 			edp_link = edp_links[i];
1933 			if (edp_link != edp_streams[0]->link)
1934 				continue;
1935 			// enable fastboot if backend is enabled on eDP
1936 			if (edp_link->link_enc->funcs->is_dig_enabled &&
1937 			    edp_link->link_enc->funcs->is_dig_enabled(edp_link->link_enc) &&
1938 			    edp_link->link_status.link_active) {
1939 				struct dc_stream_state *edp_stream = edp_streams[0];
1940 
1941 				can_apply_edp_fast_boot = dc_validate_boot_timing(dc,
1942 					edp_stream->sink, &edp_stream->timing);
1943 
1944 				// For Mux-platform, the default value is false.
1945 				// Disable fast boot during mux switching.
1946 				// The flag would be clean after switching done.
1947 				if (dc->is_switch_in_progress_dest && edp_link->is_dds)
1948 					can_apply_edp_fast_boot = false;
1949 
1950 				edp_stream->apply_edp_fast_boot_optimization = can_apply_edp_fast_boot;
1951 				if (can_apply_edp_fast_boot) {
1952 					DC_LOG_EVENT_LINK_TRAINING("eDP fast boot Enable\n");
1953 
1954 					// Vbios & Driver support different pixel rate div policy.
1955 					pipe_ctx = resource_get_otg_master_for_stream(&context->res_ctx, edp_stream);
1956 					if (pipe_ctx &&
1957 						hws->funcs.is_dp_dig_pixel_rate_div_policy &&
1958 						hws->funcs.is_dp_dig_pixel_rate_div_policy(pipe_ctx)) {
1959 						// Get Vbios div factor from register
1960 						dc->res_pool->dccg->funcs->get_pixel_rate_div(
1961 							dc->res_pool->dccg,
1962 							pipe_ctx->stream_res.tg->inst,
1963 							&pipe_ctx->pixel_rate_divider.div_factor1,
1964 							&pipe_ctx->pixel_rate_divider.div_factor2);
1965 
1966 						// VBios doesn't support pixel rate div, so force it.
1967 						// If VBios supports it, we check it from reigster or other flags.
1968 						pipe_ctx->stream_res.pix_clk_params.dio_se_pix_per_cycle = 1;
1969 					}
1970 				}
1971 				break;
1972 			}
1973 		}
1974 		// We are trying to enable eDP, don't power down VDD
1975 		if (can_apply_edp_fast_boot)
1976 			keep_edp_vdd_on = true;
1977 	}
1978 
1979 	// Check seamless boot support
1980 	for (i = 0; i < context->stream_count; i++) {
1981 		if (context->streams[i]->apply_seamless_boot_optimization) {
1982 			can_apply_seamless_boot = true;
1983 			break;
1984 		}
1985 	}
1986 
1987 	/* eDP should not have stream in resume from S4 and so even with VBios post
1988 	 * it should get turned off
1989 	 */
1990 	if (edp_with_sink_num)
1991 		edp_link_with_sink = edp_links_with_sink[0];
1992 
1993 	// During a mux switch, powering down the HW blocks and then enabling
1994 	// the link via a DPCD SET_POWER write causes a brief flash
1995 	keep_edp_vdd_on |= dc->is_switch_in_progress_dest;
1996 
1997 	if (!can_apply_edp_fast_boot && !can_apply_seamless_boot) {
1998 		if (edp_link_with_sink && !keep_edp_vdd_on) {
1999 			/*turn off backlight before DP_blank and encoder powered down*/
2000 			hws->funcs.edp_backlight_control(edp_link_with_sink, false);
2001 		}
2002 		/*resume from S3, no vbios posting, no need to power down again*/
2003 		if (dcb && dcb->funcs && !dcb->funcs->is_accelerated_mode(dcb))
2004 			clk_mgr_exit_optimized_pwr_state(dc, dc->clk_mgr);
2005 
2006 		power_down_all_hw_blocks(dc);
2007 
2008 		/* DSC could be enabled on eDP during VBIOS post.
2009 		 * To clean up dsc blocks if eDP is in link but not active.
2010 		 */
2011 		if (edp_link_with_sink && (edp_stream_num == 0))
2012 			clean_up_dsc_blocks(dc);
2013 
2014 		disable_vga_and_power_gate_all_controllers(dc);
2015 		if (edp_link_with_sink && !keep_edp_vdd_on)
2016 			dc->hwss.edp_power_control(edp_link_with_sink, false);
2017 		if (dcb && dcb->funcs && !dcb->funcs->is_accelerated_mode(dcb))
2018 			clk_mgr_optimize_pwr_state(dc, dc->clk_mgr);
2019 	}
2020 	bios_set_scratch_acc_mode_change(dc->ctx->dc_bios, 1);
2021 }
2022 
compute_pstate_blackout_duration(struct bw_fixed blackout_duration,const struct dc_stream_state * stream)2023 static uint32_t compute_pstate_blackout_duration(
2024 	struct bw_fixed blackout_duration,
2025 	const struct dc_stream_state *stream)
2026 {
2027 	uint32_t total_dest_line_time_ns;
2028 	uint32_t pstate_blackout_duration_ns;
2029 
2030 	pstate_blackout_duration_ns = 1000 * blackout_duration.value >> 24;
2031 
2032 	total_dest_line_time_ns = 1000000UL *
2033 		(stream->timing.h_total * 10) /
2034 		stream->timing.pix_clk_100hz +
2035 		pstate_blackout_duration_ns;
2036 
2037 	return total_dest_line_time_ns;
2038 }
2039 
dce110_set_displaymarks(const struct dc * dc,struct dc_state * context)2040 static void dce110_set_displaymarks(
2041 	const struct dc *dc,
2042 	struct dc_state *context)
2043 {
2044 	uint8_t i, num_pipes;
2045 	unsigned int underlay_idx = dc->res_pool->underlay_pipe_index;
2046 
2047 	for (i = 0, num_pipes = 0; i < MAX_PIPES; i++) {
2048 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2049 		uint32_t total_dest_line_time_ns;
2050 
2051 		if (pipe_ctx->stream == NULL)
2052 			continue;
2053 
2054 		total_dest_line_time_ns = compute_pstate_blackout_duration(
2055 			dc->bw_vbios->blackout_duration, pipe_ctx->stream);
2056 		pipe_ctx->plane_res.mi->funcs->mem_input_program_display_marks(
2057 			pipe_ctx->plane_res.mi,
2058 			context->bw_ctx.bw.dce.nbp_state_change_wm_ns[num_pipes],
2059 			context->bw_ctx.bw.dce.stutter_exit_wm_ns[num_pipes],
2060 			context->bw_ctx.bw.dce.stutter_entry_wm_ns[num_pipes],
2061 			context->bw_ctx.bw.dce.urgent_wm_ns[num_pipes],
2062 			total_dest_line_time_ns);
2063 		if (i == underlay_idx) {
2064 			num_pipes++;
2065 			pipe_ctx->plane_res.mi->funcs->mem_input_program_chroma_display_marks(
2066 				pipe_ctx->plane_res.mi,
2067 				context->bw_ctx.bw.dce.nbp_state_change_wm_ns[num_pipes],
2068 				context->bw_ctx.bw.dce.stutter_exit_wm_ns[num_pipes],
2069 				context->bw_ctx.bw.dce.urgent_wm_ns[num_pipes],
2070 				total_dest_line_time_ns);
2071 		}
2072 		num_pipes++;
2073 	}
2074 }
2075 
dce110_set_safe_displaymarks(struct resource_context * res_ctx,const struct resource_pool * pool)2076 void dce110_set_safe_displaymarks(
2077 		struct resource_context *res_ctx,
2078 		const struct resource_pool *pool)
2079 {
2080 	int i;
2081 	int underlay_idx = pool->underlay_pipe_index;
2082 	struct dce_watermarks max_marks = {
2083 		MAX_WATERMARK, MAX_WATERMARK, MAX_WATERMARK, MAX_WATERMARK };
2084 	struct dce_watermarks nbp_marks = {
2085 		SAFE_NBP_MARK, SAFE_NBP_MARK, SAFE_NBP_MARK, SAFE_NBP_MARK };
2086 	struct dce_watermarks min_marks = { 0, 0, 0, 0};
2087 
2088 	for (i = 0; i < MAX_PIPES; i++) {
2089 		if (res_ctx->pipe_ctx[i].stream == NULL || res_ctx->pipe_ctx[i].plane_res.mi == NULL)
2090 			continue;
2091 
2092 		res_ctx->pipe_ctx[i].plane_res.mi->funcs->mem_input_program_display_marks(
2093 				res_ctx->pipe_ctx[i].plane_res.mi,
2094 				nbp_marks,
2095 				max_marks,
2096 				min_marks,
2097 				max_marks,
2098 				MAX_WATERMARK);
2099 
2100 		if (i == underlay_idx)
2101 			res_ctx->pipe_ctx[i].plane_res.mi->funcs->mem_input_program_chroma_display_marks(
2102 				res_ctx->pipe_ctx[i].plane_res.mi,
2103 				nbp_marks,
2104 				max_marks,
2105 				max_marks,
2106 				MAX_WATERMARK);
2107 
2108 	}
2109 }
2110 
2111 /*******************************************************************************
2112  * Public functions
2113  ******************************************************************************/
2114 
set_drr(struct pipe_ctx ** pipe_ctx,int num_pipes,struct dc_crtc_timing_adjust adjust)2115 static void set_drr(struct pipe_ctx **pipe_ctx,
2116 		int num_pipes, struct dc_crtc_timing_adjust adjust)
2117 {
2118 	int i = 0;
2119 	struct drr_params params = {0};
2120 	// DRR should set trigger event to monitor surface update event
2121 	unsigned int event_triggers = 0x80;
2122 	// Note DRR trigger events are generated regardless of whether num frames met.
2123 	unsigned int num_frames = 2;
2124 
2125 	params.vertical_total_max = adjust.v_total_max;
2126 	params.vertical_total_min = adjust.v_total_min;
2127 
2128 	/* TODO: If multiple pipes are to be supported, you need
2129 	 * some GSL stuff. Static screen triggers may be programmed differently
2130 	 * as well.
2131 	 */
2132 	for (i = 0; i < num_pipes; i++) {
2133 		/* dc_state_destruct() might null the stream resources, so fetch tg
2134 		 * here first to avoid a race condition. The lifetime of the pointee
2135 		 * itself (the timing_generator object) is not a problem here.
2136 		 */
2137 		struct timing_generator *tg = pipe_ctx[i]->stream_res.tg;
2138 
2139 		if ((tg != NULL) && tg->funcs) {
2140 			set_drr_and_clear_adjust_pending(pipe_ctx[i], pipe_ctx[i]->stream, &params);
2141 			if (adjust.v_total_max != 0 && adjust.v_total_min != 0)
2142 				if (tg->funcs->set_static_screen_control)
2143 					tg->funcs->set_static_screen_control(
2144 						tg, event_triggers, num_frames);
2145 		}
2146 	}
2147 }
2148 
get_position(struct pipe_ctx ** pipe_ctx,int num_pipes,struct crtc_position * position)2149 static void get_position(struct pipe_ctx **pipe_ctx,
2150 		int num_pipes,
2151 		struct crtc_position *position)
2152 {
2153 	int i = 0;
2154 
2155 	/* TODO: handle pipes > 1
2156 	 */
2157 	for (i = 0; i < num_pipes; i++)
2158 		pipe_ctx[i]->stream_res.tg->funcs->get_position(pipe_ctx[i]->stream_res.tg, position);
2159 }
2160 
set_static_screen_control(struct pipe_ctx ** pipe_ctx,int num_pipes,const struct dc_static_screen_params * params)2161 static void set_static_screen_control(struct pipe_ctx **pipe_ctx,
2162 		int num_pipes, const struct dc_static_screen_params *params)
2163 {
2164 	unsigned int i;
2165 	unsigned int triggers = 0;
2166 
2167 	if (params->triggers.overlay_update)
2168 		triggers |= 0x100;
2169 	if (params->triggers.surface_update)
2170 		triggers |= 0x80;
2171 	if (params->triggers.cursor_update)
2172 		triggers |= 0x2;
2173 	if (params->triggers.force_trigger)
2174 		triggers |= 0x1;
2175 
2176 	if (num_pipes) {
2177 		struct dc *dc = pipe_ctx[0]->stream->ctx->dc;
2178 
2179 		if (dc->fbc_compressor)
2180 			triggers |= 0x84;
2181 	}
2182 
2183 	for (i = 0; i < num_pipes; i++)
2184 		pipe_ctx[i]->stream_res.tg->funcs->
2185 			set_static_screen_control(pipe_ctx[i]->stream_res.tg,
2186 					triggers, params->num_frames);
2187 }
2188 
2189 /*
2190  *  Check if FBC can be enabled
2191  */
should_enable_fbc(struct dc * dc,struct dc_state * context,uint32_t * pipe_idx)2192 static bool should_enable_fbc(struct dc *dc,
2193 		struct dc_state *context,
2194 		uint32_t *pipe_idx)
2195 {
2196 	uint32_t i;
2197 	struct pipe_ctx *pipe_ctx = NULL;
2198 	struct resource_context *res_ctx = &context->res_ctx;
2199 	unsigned int underlay_idx = dc->res_pool->underlay_pipe_index;
2200 
2201 
2202 	ASSERT(dc->fbc_compressor);
2203 
2204 	/* FBC memory should be allocated */
2205 	if (!dc->ctx->fbc_gpu_addr)
2206 		return false;
2207 
2208 	/* Only supports single display */
2209 	if (context->stream_count != 1)
2210 		return false;
2211 
2212 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2213 		if (res_ctx->pipe_ctx[i].stream) {
2214 
2215 			pipe_ctx = &res_ctx->pipe_ctx[i];
2216 
2217 			/* fbc not applicable on underlay pipe */
2218 			if (pipe_ctx->pipe_idx != underlay_idx) {
2219 				*pipe_idx = i;
2220 				break;
2221 			}
2222 		}
2223 	}
2224 
2225 	if (i == dc->res_pool->pipe_count)
2226 		return false;
2227 
2228 	if (!pipe_ctx->stream->link)
2229 		return false;
2230 
2231 	/* Only supports eDP */
2232 	if (pipe_ctx->stream->link->connector_signal != SIGNAL_TYPE_EDP)
2233 		return false;
2234 
2235 	/* PSR should not be enabled */
2236 	if (pipe_ctx->stream->link->psr_settings.psr_feature_enabled)
2237 		return false;
2238 
2239 	/* Replay should not be enabled */
2240 	if (pipe_ctx->stream->link->replay_settings.replay_feature_enabled)
2241 		return false;
2242 
2243 	/* Nothing to compress */
2244 	if (!pipe_ctx->plane_state)
2245 		return false;
2246 
2247 	/* Only for non-linear tiling */
2248 	if (pipe_ctx->plane_state->tiling_info.gfx8.array_mode == DC_ARRAY_LINEAR_GENERAL)
2249 		return false;
2250 
2251 	return true;
2252 }
2253 
2254 /*
2255  *  Enable FBC
2256  */
enable_fbc(struct dc * dc,struct dc_state * context)2257 static void enable_fbc(
2258 		struct dc *dc,
2259 		struct dc_state *context)
2260 {
2261 	uint32_t pipe_idx = 0;
2262 
2263 	if (should_enable_fbc(dc, context, &pipe_idx)) {
2264 		/* Program GRPH COMPRESSED ADDRESS and PITCH */
2265 		struct compr_addr_and_pitch_params params = {0, 0, 0};
2266 		struct compressor *compr = dc->fbc_compressor;
2267 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
2268 
2269 		params.source_view_width = pipe_ctx->stream->timing.h_addressable;
2270 		params.source_view_height = pipe_ctx->stream->timing.v_addressable;
2271 		params.inst = pipe_ctx->stream_res.tg->inst;
2272 		compr->compr_surface_address.quad_part = dc->ctx->fbc_gpu_addr;
2273 
2274 		compr->funcs->surface_address_and_pitch(compr, &params);
2275 		compr->funcs->set_fbc_invalidation_triggers(compr, 1);
2276 
2277 		compr->funcs->enable_fbc(compr, &params);
2278 	}
2279 }
2280 
dce110_reset_hw_ctx_wrap(struct dc * dc,struct dc_state * context)2281 static void dce110_reset_hw_ctx_wrap(
2282 		struct dc *dc,
2283 		struct dc_state *context)
2284 {
2285 	int i;
2286 
2287 	/* Reset old context */
2288 	/* look up the targets that have been removed since last commit */
2289 	for (i = 0; i < MAX_PIPES; i++) {
2290 		struct pipe_ctx *pipe_ctx_old =
2291 			&dc->current_state->res_ctx.pipe_ctx[i];
2292 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2293 
2294 		/* Note: We need to disable output if clock sources change,
2295 		 * since bios does optimization and doesn't apply if changing
2296 		 * PHY when not already disabled.
2297 		 */
2298 
2299 		/* Skip underlay pipe since it will be handled in commit surface*/
2300 		if (!pipe_ctx_old->stream || pipe_ctx_old->top_pipe)
2301 			continue;
2302 
2303 		if (!pipe_ctx->stream ||
2304 				pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
2305 			struct clock_source *old_clk = pipe_ctx_old->clock_source;
2306 
2307 			/* Disable if new stream is null. O/w, if stream is
2308 			 * disabled already, no need to disable again.
2309 			 */
2310 			if (!pipe_ctx->stream || !pipe_ctx->stream->dpms_off) {
2311 				dc->link_srv->set_dpms_off(pipe_ctx_old);
2312 
2313 				/* free acquired resources*/
2314 				if (pipe_ctx_old->stream_res.audio) {
2315 					/*disable az_endpoint*/
2316 					pipe_ctx_old->stream_res.audio->funcs->
2317 							az_disable(pipe_ctx_old->stream_res.audio);
2318 
2319 					/*free audio*/
2320 					if (dc->caps.dynamic_audio == true) {
2321 						/*we have to dynamic arbitrate the audio endpoints*/
2322 						/*we free the resource, need reset is_audio_acquired*/
2323 						update_audio_usage(&dc->current_state->res_ctx, dc->res_pool,
2324 								pipe_ctx_old->stream_res.audio, false);
2325 						pipe_ctx_old->stream_res.audio = NULL;
2326 					}
2327 				}
2328 			}
2329 
2330 			pipe_ctx_old->stream_res.tg->funcs->set_blank(pipe_ctx_old->stream_res.tg, true);
2331 			if (!hwss_wait_for_blank_complete(pipe_ctx_old->stream_res.tg)) {
2332 				dm_error("DC: failed to blank crtc!\n");
2333 				BREAK_TO_DEBUGGER();
2334 			}
2335 			pipe_ctx_old->stream_res.tg->funcs->disable_crtc(pipe_ctx_old->stream_res.tg);
2336 			if (dc_is_hdmi_tmds_signal(pipe_ctx_old->stream->signal))
2337 				pipe_ctx_old->stream->link->phy_state.symclk_ref_cnts.otg = 0;
2338 			pipe_ctx_old->plane_res.mi->funcs->free_mem_input(
2339 					pipe_ctx_old->plane_res.mi, dc->current_state->stream_count);
2340 
2341 			if (old_clk && 0 == resource_get_clock_source_reference(&context->res_ctx,
2342 										dc->res_pool,
2343 										old_clk))
2344 				old_clk->funcs->cs_power_down(old_clk);
2345 
2346 			dc->hwss.disable_plane(dc, dc->current_state, pipe_ctx_old);
2347 
2348 			pipe_ctx_old->stream = NULL;
2349 		}
2350 	}
2351 }
2352 
dce110_setup_audio_dto(struct dc * dc,struct dc_state * context)2353 static void dce110_setup_audio_dto(
2354 		struct dc *dc,
2355 		struct dc_state *context)
2356 {
2357 	unsigned int i;
2358 
2359 	/* program audio wall clock. use HDMI as clock source if HDMI
2360 	 * audio active. Otherwise, use DP as clock source
2361 	 * first, loop to find any HDMI audio, if not, loop find DP audio
2362 	 */
2363 	/* Setup audio rate clock source */
2364 	/* Issue:
2365 	* Audio lag happened on DP monitor when unplug a HDMI monitor
2366 	*
2367 	* Cause:
2368 	* In case of DP and HDMI connected or HDMI only, DCCG_AUDIO_DTO_SEL
2369 	* is set to either dto0 or dto1, audio should work fine.
2370 	* In case of DP connected only, DCCG_AUDIO_DTO_SEL should be dto1,
2371 	* set to dto0 will cause audio lag.
2372 	*
2373 	* Solution:
2374 	* Not optimized audio wall dto setup. When mode set, iterate pipe_ctx,
2375 	* find first available pipe with audio, setup audio wall DTO per topology
2376 	* instead of per pipe.
2377 	*/
2378 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2379 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2380 
2381 		if (pipe_ctx->stream == NULL)
2382 			continue;
2383 
2384 		if (pipe_ctx->top_pipe)
2385 			continue;
2386 		if (pipe_ctx->stream->signal != SIGNAL_TYPE_HDMI_TYPE_A)
2387 			continue;
2388 		if (pipe_ctx->stream_res.audio != NULL) {
2389 			struct audio_output audio_output;
2390 
2391 			build_audio_output(context, pipe_ctx, &audio_output);
2392 
2393 			if (dc->res_pool->dccg && dc->res_pool->dccg->funcs->set_audio_dtbclk_dto) {
2394 				struct dtbclk_dto_params dto_params = {0};
2395 
2396 				dc->res_pool->dccg->funcs->set_audio_dtbclk_dto(
2397 					dc->res_pool->dccg, &dto_params);
2398 
2399 				pipe_ctx->stream_res.audio->funcs->wall_dto_setup(
2400 						pipe_ctx->stream_res.audio,
2401 						pipe_ctx->stream->signal,
2402 						&audio_output.crtc_info,
2403 						&audio_output.pll_info);
2404 			} else
2405 				pipe_ctx->stream_res.audio->funcs->wall_dto_setup(
2406 					pipe_ctx->stream_res.audio,
2407 					pipe_ctx->stream->signal,
2408 					&audio_output.crtc_info,
2409 					&audio_output.pll_info);
2410 			break;
2411 		}
2412 	}
2413 
2414 	/* no HDMI audio is found, try DP audio */
2415 	if (i == dc->res_pool->pipe_count) {
2416 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
2417 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2418 
2419 			if (pipe_ctx->stream == NULL)
2420 				continue;
2421 
2422 			if (pipe_ctx->top_pipe)
2423 				continue;
2424 
2425 			if (!dc_is_dp_signal(pipe_ctx->stream->signal))
2426 				continue;
2427 
2428 			if (pipe_ctx->stream_res.audio != NULL) {
2429 				struct audio_output audio_output = {0};
2430 
2431 				build_audio_output(context, pipe_ctx, &audio_output);
2432 
2433 				pipe_ctx->stream_res.audio->funcs->wall_dto_setup(
2434 					pipe_ctx->stream_res.audio,
2435 					pipe_ctx->stream->signal,
2436 					&audio_output.crtc_info,
2437 					&audio_output.pll_info);
2438 				break;
2439 			}
2440 		}
2441 	}
2442 }
2443 
dce110_apply_ctx_to_hw(struct dc * dc,struct dc_state * context)2444 enum dc_status dce110_apply_ctx_to_hw(
2445 		struct dc *dc,
2446 		struct dc_state *context)
2447 {
2448 	struct dce_hwseq *hws = dc->hwseq;
2449 	struct dc_bios *dcb = dc->ctx->dc_bios;
2450 	enum dc_status status;
2451 	int i;
2452 	bool was_hpo_acquired = resource_is_hpo_acquired(dc->current_state);
2453 	bool is_hpo_acquired = resource_is_hpo_acquired(context);
2454 
2455 	/* reset syncd pipes from disabled pipes */
2456 	if (dc->config.use_pipe_ctx_sync_logic)
2457 		reset_syncd_pipes_from_disabled_pipes(dc, context);
2458 
2459 	/* Reset old context */
2460 	/* look up the targets that have been removed since last commit */
2461 	hws->funcs.reset_hw_ctx_wrap(dc, context);
2462 
2463 	/* Skip applying if no targets */
2464 	if (context->stream_count <= 0)
2465 		return DC_OK;
2466 
2467 	/* Apply new context */
2468 	dcb->funcs->set_scratch_critical_state(dcb, true);
2469 
2470 	/* below is for real asic only */
2471 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2472 		struct pipe_ctx *pipe_ctx_old =
2473 					&dc->current_state->res_ctx.pipe_ctx[i];
2474 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2475 
2476 		if (pipe_ctx->stream == NULL || pipe_ctx->top_pipe)
2477 			continue;
2478 
2479 		if (pipe_ctx->stream == pipe_ctx_old->stream) {
2480 			if (pipe_ctx_old->clock_source != pipe_ctx->clock_source)
2481 				dce_crtc_switch_to_clk_src(dc->hwseq,
2482 						pipe_ctx->clock_source, i);
2483 			continue;
2484 		}
2485 
2486 		hws->funcs.enable_display_power_gating(
2487 				dc, i, dc->ctx->dc_bios,
2488 				PIPE_GATING_CONTROL_DISABLE);
2489 	}
2490 
2491 	if (dc->fbc_compressor)
2492 		dc->fbc_compressor->funcs->disable_fbc(dc->fbc_compressor);
2493 
2494 	dce110_setup_audio_dto(dc, context);
2495 
2496 	if (dc->hwseq->funcs.setup_hpo_hw_control && was_hpo_acquired != is_hpo_acquired) {
2497 		dc->hwseq->funcs.setup_hpo_hw_control(dc->hwseq, is_hpo_acquired);
2498 	}
2499 
2500 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2501 		struct pipe_ctx *pipe_ctx_old =
2502 					&dc->current_state->res_ctx.pipe_ctx[i];
2503 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2504 
2505 		if (pipe_ctx->stream == NULL)
2506 			continue;
2507 
2508 		if (pipe_ctx->stream == pipe_ctx_old->stream &&
2509 			pipe_ctx->stream->link->link_state_valid) {
2510 			continue;
2511 		}
2512 
2513 		if (pipe_ctx_old->stream && !pipe_need_reprogram(pipe_ctx_old, pipe_ctx))
2514 			continue;
2515 
2516 		if (pipe_ctx->top_pipe || pipe_ctx->prev_odm_pipe)
2517 			continue;
2518 
2519 		status = dce110_apply_single_controller_ctx_to_hw(
2520 				pipe_ctx,
2521 				context,
2522 				dc);
2523 
2524 		if (DC_OK != status)
2525 			return status;
2526 
2527 #ifdef CONFIG_DRM_AMD_DC_FP
2528 		if (hws->funcs.resync_fifo_dccg_dio)
2529 			hws->funcs.resync_fifo_dccg_dio(hws, dc, context, i);
2530 #endif
2531 	}
2532 
2533 	if (dc->fbc_compressor)
2534 		enable_fbc(dc, dc->current_state);
2535 
2536 	dcb->funcs->set_scratch_critical_state(dcb, false);
2537 
2538 	return DC_OK;
2539 }
2540 
2541 /*******************************************************************************
2542  * Front End programming
2543  ******************************************************************************/
set_default_colors(struct pipe_ctx * pipe_ctx)2544 static void set_default_colors(struct pipe_ctx *pipe_ctx)
2545 {
2546 	struct default_adjustment default_adjust = { 0 };
2547 
2548 	default_adjust.force_hw_default = false;
2549 	default_adjust.in_color_space = pipe_ctx->plane_state->color_space;
2550 	default_adjust.out_color_space = pipe_ctx->stream->output_color_space;
2551 	default_adjust.csc_adjust_type = GRAPHICS_CSC_ADJUST_TYPE_SW;
2552 	default_adjust.surface_pixel_format = pipe_ctx->plane_res.scl_data.format;
2553 
2554 	/* display color depth */
2555 	default_adjust.color_depth =
2556 		pipe_ctx->stream->timing.display_color_depth;
2557 
2558 	/* Lb color depth */
2559 	default_adjust.lb_color_depth = pipe_ctx->plane_res.scl_data.lb_params.depth;
2560 
2561 	pipe_ctx->plane_res.xfm->funcs->opp_set_csc_default(
2562 					pipe_ctx->plane_res.xfm, &default_adjust);
2563 }
2564 
2565 
2566 /*******************************************************************************
2567  * In order to turn on/off specific surface we will program
2568  * Blender + CRTC
2569  *
2570  * In case that we have two surfaces and they have a different visibility
2571  * we can't turn off the CRTC since it will turn off the entire display
2572  *
2573  * |----------------------------------------------- |
2574  * |bottom pipe|curr pipe  |              |         |
2575  * |Surface    |Surface    | Blender      |  CRCT   |
2576  * |visibility |visibility | Configuration|         |
2577  * |------------------------------------------------|
2578  * |   off     |    off    | CURRENT_PIPE | blank   |
2579  * |   off     |    on     | CURRENT_PIPE | unblank |
2580  * |   on      |    off    | OTHER_PIPE   | unblank |
2581  * |   on      |    on     | BLENDING     | unblank |
2582  * -------------------------------------------------|
2583  *
2584  ******************************************************************************/
program_surface_visibility(const struct dc * dc,struct pipe_ctx * pipe_ctx)2585 static void program_surface_visibility(const struct dc *dc,
2586 		struct pipe_ctx *pipe_ctx)
2587 {
2588 	enum blnd_mode blender_mode = BLND_MODE_CURRENT_PIPE;
2589 	bool blank_target = false;
2590 
2591 	if (pipe_ctx->bottom_pipe) {
2592 
2593 		/* For now we are supporting only two pipes */
2594 		ASSERT(pipe_ctx->bottom_pipe->bottom_pipe == NULL);
2595 
2596 		if (pipe_ctx->bottom_pipe->plane_state->visible) {
2597 			if (pipe_ctx->plane_state->visible)
2598 				blender_mode = BLND_MODE_BLENDING;
2599 			else
2600 				blender_mode = BLND_MODE_OTHER_PIPE;
2601 
2602 		} else if (!pipe_ctx->plane_state->visible)
2603 			blank_target = true;
2604 
2605 	} else if (!pipe_ctx->plane_state->visible)
2606 		blank_target = true;
2607 
2608 	dce_set_blender_mode(dc->hwseq, pipe_ctx->stream_res.tg->inst, blender_mode);
2609 	pipe_ctx->stream_res.tg->funcs->set_blank(pipe_ctx->stream_res.tg, blank_target);
2610 
2611 }
2612 
program_gamut_remap(struct pipe_ctx * pipe_ctx)2613 static void program_gamut_remap(struct pipe_ctx *pipe_ctx)
2614 {
2615 	int i = 0;
2616 	struct xfm_grph_csc_adjustment adjust;
2617 	memset(&adjust, 0, sizeof(adjust));
2618 	adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_BYPASS;
2619 
2620 
2621 	if (pipe_ctx->stream->gamut_remap_matrix.enable_remap == true) {
2622 		adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_SW;
2623 
2624 		for (i = 0; i < CSC_TEMPERATURE_MATRIX_SIZE; i++)
2625 			adjust.temperature_matrix[i] =
2626 				pipe_ctx->stream->gamut_remap_matrix.matrix[i];
2627 	}
2628 
2629 	pipe_ctx->plane_res.xfm->funcs->transform_set_gamut_remap(pipe_ctx->plane_res.xfm, &adjust);
2630 }
update_plane_addr(const struct dc * dc,struct pipe_ctx * pipe_ctx)2631 static void update_plane_addr(const struct dc *dc,
2632 		struct pipe_ctx *pipe_ctx)
2633 {
2634 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
2635 
2636 	if (plane_state == NULL)
2637 		return;
2638 
2639 	pipe_ctx->plane_res.mi->funcs->mem_input_program_surface_flip_and_addr(
2640 			pipe_ctx->plane_res.mi,
2641 			&plane_state->address,
2642 			plane_state->flip_immediate);
2643 
2644 	plane_state->status.requested_address = plane_state->address;
2645 }
2646 
dce110_update_pending_status(struct pipe_ctx * pipe_ctx)2647 static void dce110_update_pending_status(struct pipe_ctx *pipe_ctx)
2648 {
2649 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
2650 
2651 	if (plane_state == NULL)
2652 		return;
2653 
2654 	plane_state->status.is_flip_pending =
2655 			pipe_ctx->plane_res.mi->funcs->mem_input_is_flip_pending(
2656 					pipe_ctx->plane_res.mi);
2657 
2658 	if (plane_state->status.is_flip_pending && !plane_state->visible)
2659 		pipe_ctx->plane_res.mi->current_address = pipe_ctx->plane_res.mi->request_address;
2660 
2661 	plane_state->status.current_address = pipe_ctx->plane_res.mi->current_address;
2662 	if (pipe_ctx->plane_res.mi->current_address.type == PLN_ADDR_TYPE_GRPH_STEREO &&
2663 			pipe_ctx->stream_res.tg->funcs->is_stereo_left_eye) {
2664 		plane_state->status.is_right_eye =\
2665 				!pipe_ctx->stream_res.tg->funcs->is_stereo_left_eye(pipe_ctx->stream_res.tg);
2666 	}
2667 }
2668 
dce110_power_down(struct dc * dc)2669 void dce110_power_down(struct dc *dc)
2670 {
2671 	power_down_all_hw_blocks(dc);
2672 	disable_vga_and_power_gate_all_controllers(dc);
2673 }
2674 
wait_for_reset_trigger_to_occur(struct dc_context * dc_ctx,struct timing_generator * tg)2675 static bool wait_for_reset_trigger_to_occur(
2676 	struct dc_context *dc_ctx,
2677 	struct timing_generator *tg)
2678 {
2679 	struct dc_context *ctx = dc_ctx;
2680 	bool rc = false;
2681 
2682 	/* To avoid endless loop we wait at most
2683 	 * frames_to_wait_on_triggered_reset frames for the reset to occur. */
2684 	const uint32_t frames_to_wait_on_triggered_reset = 10;
2685 	uint32_t i;
2686 
2687 	for (i = 0; i < frames_to_wait_on_triggered_reset; i++) {
2688 
2689 		if (!tg->funcs->is_counter_moving(tg)) {
2690 			DC_ERROR("TG counter is not moving!\n");
2691 			break;
2692 		}
2693 
2694 		if (tg->funcs->did_triggered_reset_occur(tg)) {
2695 			rc = true;
2696 			/* usually occurs at i=1 */
2697 			DC_SYNC_INFO("GSL: reset occurred at wait count: %d\n",
2698 					i);
2699 			break;
2700 		}
2701 
2702 		/* Wait for one frame. */
2703 		tg->funcs->wait_for_state(tg, CRTC_STATE_VACTIVE);
2704 		tg->funcs->wait_for_state(tg, CRTC_STATE_VBLANK);
2705 	}
2706 
2707 	if (false == rc)
2708 		DC_ERROR("GSL: Timeout on reset trigger!\n");
2709 
2710 	return rc;
2711 }
2712 
2713 /* Enable timing synchronization for a group of Timing Generators. */
dce110_enable_timing_synchronization(struct dc * dc,struct dc_state * state,int group_index,int group_size,struct pipe_ctx * grouped_pipes[])2714 static void dce110_enable_timing_synchronization(
2715 		struct dc *dc,
2716 		struct dc_state *state,
2717 		int group_index,
2718 		int group_size,
2719 		struct pipe_ctx *grouped_pipes[])
2720 {
2721 	struct dc_context *dc_ctx = dc->ctx;
2722 	struct dcp_gsl_params gsl_params = { 0 };
2723 	int i;
2724 	DC_LOGGER_INIT();
2725 
2726 	DC_SYNC_INFO("GSL: Setting-up...\n");
2727 
2728 	/* Designate a single TG in the group as a master.
2729 	 * Since HW doesn't care which one, we always assign
2730 	 * the 1st one in the group. */
2731 	gsl_params.gsl_group = 0;
2732 	gsl_params.gsl_master = grouped_pipes[0]->stream_res.tg->inst;
2733 
2734 	for (i = 0; i < group_size; i++)
2735 		grouped_pipes[i]->stream_res.tg->funcs->setup_global_swap_lock(
2736 					grouped_pipes[i]->stream_res.tg, &gsl_params);
2737 
2738 	/* Reset slave controllers on master VSync */
2739 	DC_SYNC_INFO("GSL: enabling trigger-reset\n");
2740 
2741 	for (i = 1 /* skip the master */; i < group_size; i++)
2742 		grouped_pipes[i]->stream_res.tg->funcs->enable_reset_trigger(
2743 				grouped_pipes[i]->stream_res.tg,
2744 				gsl_params.gsl_group);
2745 
2746 	for (i = 1 /* skip the master */; i < group_size; i++) {
2747 		DC_SYNC_INFO("GSL: waiting for reset to occur.\n");
2748 		wait_for_reset_trigger_to_occur(dc_ctx, grouped_pipes[i]->stream_res.tg);
2749 		grouped_pipes[i]->stream_res.tg->funcs->disable_reset_trigger(
2750 				grouped_pipes[i]->stream_res.tg);
2751 	}
2752 
2753 	/* GSL Vblank synchronization is a one time sync mechanism, assumption
2754 	 * is that the sync'ed displays will not drift out of sync over time*/
2755 	DC_SYNC_INFO("GSL: Restoring register states.\n");
2756 	for (i = 0; i < group_size; i++)
2757 		grouped_pipes[i]->stream_res.tg->funcs->tear_down_global_swap_lock(grouped_pipes[i]->stream_res.tg);
2758 
2759 	DC_SYNC_INFO("GSL: Set-up complete.\n");
2760 }
2761 
dce110_enable_per_frame_crtc_position_reset(struct dc * dc,int group_size,struct pipe_ctx * grouped_pipes[])2762 static void dce110_enable_per_frame_crtc_position_reset(
2763 		struct dc *dc,
2764 		int group_size,
2765 		struct pipe_ctx *grouped_pipes[])
2766 {
2767 	struct dc_context *dc_ctx = dc->ctx;
2768 	struct dcp_gsl_params gsl_params = { 0 };
2769 	int i;
2770 	DC_LOGGER_INIT();
2771 
2772 	gsl_params.gsl_group = 0;
2773 	gsl_params.gsl_master = 0;
2774 
2775 	for (i = 0; i < group_size; i++)
2776 		grouped_pipes[i]->stream_res.tg->funcs->setup_global_swap_lock(
2777 					grouped_pipes[i]->stream_res.tg, &gsl_params);
2778 
2779 	DC_SYNC_INFO("GSL: enabling trigger-reset\n");
2780 
2781 	for (i = 1; i < group_size; i++)
2782 		grouped_pipes[i]->stream_res.tg->funcs->enable_crtc_reset(
2783 				grouped_pipes[i]->stream_res.tg,
2784 				gsl_params.gsl_master,
2785 				&grouped_pipes[i]->stream->triggered_crtc_reset);
2786 
2787 	DC_SYNC_INFO("GSL: waiting for reset to occur.\n");
2788 	for (i = 1; i < group_size; i++)
2789 		wait_for_reset_trigger_to_occur(dc_ctx, grouped_pipes[i]->stream_res.tg);
2790 
2791 	for (i = 0; i < group_size; i++)
2792 		grouped_pipes[i]->stream_res.tg->funcs->tear_down_global_swap_lock(grouped_pipes[i]->stream_res.tg);
2793 
2794 }
2795 
dce110_init_pipes(struct dc * dc,struct dc_state * context)2796 static void dce110_init_pipes(struct dc *dc, struct dc_state *context)
2797 {
2798 	// Do nothing
2799 }
2800 
dce110_init_hw(struct dc * dc)2801 static void dce110_init_hw(struct dc *dc)
2802 {
2803 	int i;
2804 	struct dc_bios *bp;
2805 	struct transform *xfm;
2806 	struct abm *abm;
2807 	struct dmcu *dmcu;
2808 	struct dce_hwseq *hws = dc->hwseq;
2809 	uint32_t backlight = MAX_BACKLIGHT_LEVEL;
2810 	uint32_t user_level = MAX_BACKLIGHT_LEVEL;
2811 
2812 	bp = dc->ctx->dc_bios;
2813 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2814 		xfm = dc->res_pool->transforms[i];
2815 		xfm->funcs->transform_reset(xfm);
2816 
2817 		hws->funcs.enable_display_power_gating(
2818 				dc, i, bp,
2819 				PIPE_GATING_CONTROL_INIT);
2820 		hws->funcs.enable_display_power_gating(
2821 				dc, i, bp,
2822 				PIPE_GATING_CONTROL_DISABLE);
2823 		hws->funcs.enable_display_pipe_clock_gating(
2824 			dc->ctx,
2825 			true);
2826 	}
2827 
2828 	dce_clock_gating_power_up(dc->hwseq, false);
2829 	/***************************************/
2830 
2831 	for (i = 0; i < dc->link_count; i++) {
2832 		/****************************************/
2833 		/* Power up AND update implementation according to the
2834 		 * required signal (which may be different from the
2835 		 * default signal on connector). */
2836 		struct dc_link *link = dc->links[i];
2837 
2838 		link->link_enc->funcs->hw_init(link->link_enc);
2839 	}
2840 
2841 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2842 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
2843 
2844 		tg->funcs->disable_vga(tg);
2845 
2846 		/* Blank controller using driver code instead of
2847 		 * command table. */
2848 		tg->funcs->set_blank(tg, true);
2849 		hwss_wait_for_blank_complete(tg);
2850 	}
2851 
2852 	for (i = 0; i < dc->res_pool->audio_count; i++) {
2853 		struct audio *audio = dc->res_pool->audios[i];
2854 		audio->funcs->hw_init(audio);
2855 	}
2856 
2857 	for (i = 0; i < dc->link_count; i++) {
2858 		struct dc_link *link = dc->links[i];
2859 
2860 		if (link->panel_cntl) {
2861 			backlight = link->panel_cntl->funcs->hw_init(link->panel_cntl);
2862 			user_level = link->panel_cntl->stored_backlight_registers.USER_LEVEL;
2863 		}
2864 	}
2865 
2866 	abm = dc->res_pool->abm;
2867 	if (abm != NULL)
2868 		abm->funcs->abm_init(abm, backlight, user_level);
2869 
2870 	dmcu = dc->res_pool->dmcu;
2871 	if (dmcu != NULL && abm != NULL)
2872 		abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
2873 
2874 	if (dc->fbc_compressor)
2875 		dc->fbc_compressor->funcs->power_up_fbc(dc->fbc_compressor);
2876 
2877 }
2878 
2879 
dce110_prepare_bandwidth(struct dc * dc,struct dc_state * context)2880 void dce110_prepare_bandwidth(
2881 		struct dc *dc,
2882 		struct dc_state *context)
2883 {
2884 	struct clk_mgr *dccg = dc->clk_mgr;
2885 
2886 	dce110_set_safe_displaymarks(&context->res_ctx, dc->res_pool);
2887 	if (dccg)
2888 		dccg->funcs->update_clocks(
2889 				dccg,
2890 				context,
2891 				false);
2892 }
2893 
dce110_optimize_bandwidth(struct dc * dc,struct dc_state * context)2894 void dce110_optimize_bandwidth(
2895 		struct dc *dc,
2896 		struct dc_state *context)
2897 {
2898 	struct clk_mgr *dccg = dc->clk_mgr;
2899 
2900 	dce110_set_displaymarks(dc, context);
2901 
2902 	if (dccg)
2903 		dccg->funcs->update_clocks(
2904 				dccg,
2905 				context,
2906 				true);
2907 }
2908 
dce110_program_front_end_for_pipe(struct dc * dc,struct pipe_ctx * pipe_ctx)2909 static void dce110_program_front_end_for_pipe(
2910 		struct dc *dc, struct pipe_ctx *pipe_ctx)
2911 {
2912 	struct mem_input *mi = pipe_ctx->plane_res.mi;
2913 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
2914 	struct xfm_grph_csc_adjustment adjust;
2915 	struct out_csc_color_matrix tbl_entry;
2916 	unsigned int i;
2917 	struct dce_hwseq *hws = dc->hwseq;
2918 
2919 	memset(&tbl_entry, 0, sizeof(tbl_entry));
2920 
2921 	memset(&adjust, 0, sizeof(adjust));
2922 	adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_BYPASS;
2923 
2924 	dce_enable_fe_clock(dc->hwseq, mi->inst, true);
2925 
2926 	set_default_colors(pipe_ctx);
2927 	if (pipe_ctx->stream->csc_color_matrix.enable_adjustment
2928 			== true) {
2929 		tbl_entry.color_space =
2930 			pipe_ctx->stream->output_color_space;
2931 
2932 		for (i = 0; i < 12; i++)
2933 			tbl_entry.regval[i] =
2934 			pipe_ctx->stream->csc_color_matrix.matrix[i];
2935 
2936 		pipe_ctx->plane_res.xfm->funcs->opp_set_csc_adjustment
2937 				(pipe_ctx->plane_res.xfm, &tbl_entry);
2938 	}
2939 
2940 	if (pipe_ctx->stream->gamut_remap_matrix.enable_remap == true) {
2941 		adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_SW;
2942 
2943 		for (i = 0; i < CSC_TEMPERATURE_MATRIX_SIZE; i++)
2944 			adjust.temperature_matrix[i] =
2945 				pipe_ctx->stream->gamut_remap_matrix.matrix[i];
2946 	}
2947 
2948 	pipe_ctx->plane_res.xfm->funcs->transform_set_gamut_remap(pipe_ctx->plane_res.xfm, &adjust);
2949 
2950 	pipe_ctx->plane_res.scl_data.lb_params.alpha_en = pipe_ctx->bottom_pipe != NULL;
2951 
2952 	program_scaler(dc, pipe_ctx);
2953 
2954 	mi->funcs->mem_input_program_surface_config(
2955 			mi,
2956 			plane_state->format,
2957 			&plane_state->tiling_info,
2958 			&plane_state->plane_size,
2959 			plane_state->rotation,
2960 			NULL,
2961 			false);
2962 	if (mi->funcs->set_blank)
2963 		mi->funcs->set_blank(mi, pipe_ctx->plane_state->visible);
2964 
2965 	if (dc->config.gpu_vm_support)
2966 		mi->funcs->mem_input_program_pte_vm(
2967 				pipe_ctx->plane_res.mi,
2968 				plane_state->format,
2969 				&plane_state->tiling_info,
2970 				plane_state->rotation);
2971 
2972 	/* Moved programming gamma from dc to hwss */
2973 	if (pipe_ctx->plane_state->update_flags.bits.full_update ||
2974 			pipe_ctx->plane_state->update_flags.bits.in_transfer_func_change ||
2975 			pipe_ctx->plane_state->update_flags.bits.gamma_change)
2976 		hws->funcs.set_input_transfer_func(dc, pipe_ctx, pipe_ctx->plane_state);
2977 
2978 	if (pipe_ctx->plane_state->update_flags.bits.full_update)
2979 		hws->funcs.set_output_transfer_func(dc, pipe_ctx, pipe_ctx->stream);
2980 
2981 	DC_LOG_SURFACE(
2982 			"Pipe:%d %p: addr hi:0x%x, "
2983 			"addr low:0x%x, "
2984 			"src: %d, %d, %d,"
2985 			" %d; dst: %d, %d, %d, %d;"
2986 			"clip: %d, %d, %d, %d\n",
2987 			pipe_ctx->pipe_idx,
2988 			(void *) pipe_ctx->plane_state,
2989 			pipe_ctx->plane_state->address.grph.addr.high_part,
2990 			pipe_ctx->plane_state->address.grph.addr.low_part,
2991 			pipe_ctx->plane_state->src_rect.x,
2992 			pipe_ctx->plane_state->src_rect.y,
2993 			pipe_ctx->plane_state->src_rect.width,
2994 			pipe_ctx->plane_state->src_rect.height,
2995 			pipe_ctx->plane_state->dst_rect.x,
2996 			pipe_ctx->plane_state->dst_rect.y,
2997 			pipe_ctx->plane_state->dst_rect.width,
2998 			pipe_ctx->plane_state->dst_rect.height,
2999 			pipe_ctx->plane_state->clip_rect.x,
3000 			pipe_ctx->plane_state->clip_rect.y,
3001 			pipe_ctx->plane_state->clip_rect.width,
3002 			pipe_ctx->plane_state->clip_rect.height);
3003 
3004 	DC_LOG_SURFACE(
3005 			"Pipe %d: width, height, x, y\n"
3006 			"viewport:%d, %d, %d, %d\n"
3007 			"recout:  %d, %d, %d, %d\n",
3008 			pipe_ctx->pipe_idx,
3009 			pipe_ctx->plane_res.scl_data.viewport.width,
3010 			pipe_ctx->plane_res.scl_data.viewport.height,
3011 			pipe_ctx->plane_res.scl_data.viewport.x,
3012 			pipe_ctx->plane_res.scl_data.viewport.y,
3013 			pipe_ctx->plane_res.scl_data.recout.width,
3014 			pipe_ctx->plane_res.scl_data.recout.height,
3015 			pipe_ctx->plane_res.scl_data.recout.x,
3016 			pipe_ctx->plane_res.scl_data.recout.y);
3017 }
3018 
dce110_apply_ctx_for_surface(struct dc * dc,const struct dc_stream_state * stream,int num_planes,struct dc_state * context)3019 static void dce110_apply_ctx_for_surface(
3020 		struct dc *dc,
3021 		const struct dc_stream_state *stream,
3022 		int num_planes,
3023 		struct dc_state *context)
3024 {
3025 	int i;
3026 
3027 	if (num_planes == 0)
3028 		return;
3029 
3030 	if (dc->fbc_compressor)
3031 		dc->fbc_compressor->funcs->disable_fbc(dc->fbc_compressor);
3032 
3033 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
3034 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
3035 
3036 		if (pipe_ctx->stream != stream)
3037 			continue;
3038 
3039 		/* Need to allocate mem before program front end for Fiji */
3040 		pipe_ctx->plane_res.mi->funcs->allocate_mem_input(
3041 				pipe_ctx->plane_res.mi,
3042 				pipe_ctx->stream->timing.h_total,
3043 				pipe_ctx->stream->timing.v_total,
3044 				pipe_ctx->stream->timing.pix_clk_100hz / 10,
3045 				context->stream_count);
3046 
3047 		dce110_program_front_end_for_pipe(dc, pipe_ctx);
3048 
3049 		dc->hwss.update_plane_addr(dc, pipe_ctx);
3050 
3051 		program_surface_visibility(dc, pipe_ctx);
3052 
3053 	}
3054 
3055 	if (dc->fbc_compressor)
3056 		enable_fbc(dc, context);
3057 }
3058 
dce110_post_unlock_program_front_end(struct dc * dc,struct dc_state * context)3059 static void dce110_post_unlock_program_front_end(
3060 		struct dc *dc,
3061 		struct dc_state *context)
3062 {
3063 }
3064 
dce110_power_down_fe(struct dc * dc,struct dc_state * state,struct pipe_ctx * pipe_ctx)3065 static void dce110_power_down_fe(struct dc *dc, struct dc_state *state, struct pipe_ctx *pipe_ctx)
3066 {
3067 	struct dce_hwseq *hws = dc->hwseq;
3068 	int fe_idx = pipe_ctx->plane_res.mi ?
3069 		pipe_ctx->plane_res.mi->inst : pipe_ctx->pipe_idx;
3070 
3071 	/* Do not power down fe when stream is active on dce*/
3072 	if (dc->current_state->res_ctx.pipe_ctx[fe_idx].stream)
3073 		return;
3074 
3075 	hws->funcs.enable_display_power_gating(
3076 		dc, fe_idx, dc->ctx->dc_bios, PIPE_GATING_CONTROL_ENABLE);
3077 
3078 	dc->res_pool->transforms[fe_idx]->funcs->transform_reset(
3079 				dc->res_pool->transforms[fe_idx]);
3080 }
3081 
dce110_wait_for_mpcc_disconnect(struct dc * dc,struct resource_pool * res_pool,struct pipe_ctx * pipe_ctx)3082 static void dce110_wait_for_mpcc_disconnect(
3083 		struct dc *dc,
3084 		struct resource_pool *res_pool,
3085 		struct pipe_ctx *pipe_ctx)
3086 {
3087 	/* do nothing*/
3088 }
3089 
program_output_csc(struct dc * dc,struct pipe_ctx * pipe_ctx,enum dc_color_space colorspace,uint16_t * matrix,int opp_id)3090 static void program_output_csc(struct dc *dc,
3091 		struct pipe_ctx *pipe_ctx,
3092 		enum dc_color_space colorspace,
3093 		uint16_t *matrix,
3094 		int opp_id)
3095 {
3096 	int i;
3097 	struct out_csc_color_matrix tbl_entry;
3098 
3099 	if (pipe_ctx->stream->csc_color_matrix.enable_adjustment == true) {
3100 		enum dc_color_space color_space = pipe_ctx->stream->output_color_space;
3101 
3102 		for (i = 0; i < 12; i++)
3103 			tbl_entry.regval[i] = pipe_ctx->stream->csc_color_matrix.matrix[i];
3104 
3105 		tbl_entry.color_space = color_space;
3106 
3107 		pipe_ctx->plane_res.xfm->funcs->opp_set_csc_adjustment(
3108 				pipe_ctx->plane_res.xfm, &tbl_entry);
3109 	}
3110 }
3111 
dce110_set_cursor_position(struct pipe_ctx * pipe_ctx)3112 static void dce110_set_cursor_position(struct pipe_ctx *pipe_ctx)
3113 {
3114 	struct dc_cursor_position pos_cpy = pipe_ctx->stream->cursor_position;
3115 	struct input_pixel_processor *ipp = pipe_ctx->plane_res.ipp;
3116 	struct mem_input *mi = pipe_ctx->plane_res.mi;
3117 	struct dc_cursor_mi_param param = {
3118 		.pixel_clk_khz = pipe_ctx->stream->timing.pix_clk_100hz / 10,
3119 		.ref_clk_khz = pipe_ctx->stream->ctx->dc->res_pool->ref_clocks.xtalin_clock_inKhz,
3120 		.viewport = pipe_ctx->plane_res.scl_data.viewport,
3121 		.h_scale_ratio = pipe_ctx->plane_res.scl_data.ratios.horz,
3122 		.v_scale_ratio = pipe_ctx->plane_res.scl_data.ratios.vert,
3123 		.rotation = pipe_ctx->plane_state->rotation,
3124 		.mirror = pipe_ctx->plane_state->horizontal_mirror
3125 	};
3126 
3127 	/**
3128 	 * If the cursor's source viewport is clipped then we need to
3129 	 * translate the cursor to appear in the correct position on
3130 	 * the screen.
3131 	 *
3132 	 * This translation isn't affected by scaling so it needs to be
3133 	 * done *after* we adjust the position for the scale factor.
3134 	 *
3135 	 * This is only done by opt-in for now since there are still
3136 	 * some usecases like tiled display that might enable the
3137 	 * cursor on both streams while expecting dc to clip it.
3138 	 */
3139 	if (pos_cpy.translate_by_source) {
3140 		pos_cpy.x += pipe_ctx->plane_state->src_rect.x;
3141 		pos_cpy.y += pipe_ctx->plane_state->src_rect.y;
3142 	}
3143 
3144 	if (pipe_ctx->plane_state->address.type
3145 			== PLN_ADDR_TYPE_VIDEO_PROGRESSIVE)
3146 		pos_cpy.enable = false;
3147 
3148 	if (pipe_ctx->top_pipe && pipe_ctx->plane_state != pipe_ctx->top_pipe->plane_state)
3149 		pos_cpy.enable = false;
3150 
3151 	if (ipp->funcs->ipp_cursor_set_position)
3152 		ipp->funcs->ipp_cursor_set_position(ipp, &pos_cpy, &param);
3153 	if (mi->funcs->set_cursor_position)
3154 		mi->funcs->set_cursor_position(mi, &pos_cpy, &param);
3155 }
3156 
dce110_set_cursor_attribute(struct pipe_ctx * pipe_ctx)3157 static void dce110_set_cursor_attribute(struct pipe_ctx *pipe_ctx)
3158 {
3159 	struct dc_cursor_attributes *attributes = &pipe_ctx->stream->cursor_attributes;
3160 
3161 	if (pipe_ctx->plane_res.ipp &&
3162 	    pipe_ctx->plane_res.ipp->funcs->ipp_cursor_set_attributes)
3163 		pipe_ctx->plane_res.ipp->funcs->ipp_cursor_set_attributes(
3164 				pipe_ctx->plane_res.ipp, attributes);
3165 
3166 	if (pipe_ctx->plane_res.mi &&
3167 	    pipe_ctx->plane_res.mi->funcs->set_cursor_attributes)
3168 		pipe_ctx->plane_res.mi->funcs->set_cursor_attributes(
3169 				pipe_ctx->plane_res.mi, attributes);
3170 
3171 	if (pipe_ctx->plane_res.xfm &&
3172 	    pipe_ctx->plane_res.xfm->funcs->set_cursor_attributes)
3173 		pipe_ctx->plane_res.xfm->funcs->set_cursor_attributes(
3174 				pipe_ctx->plane_res.xfm, attributes);
3175 }
3176 
dce110_set_backlight_level(struct pipe_ctx * pipe_ctx,struct set_backlight_level_params * backlight_level_params)3177 bool dce110_set_backlight_level(struct pipe_ctx *pipe_ctx,
3178 	struct set_backlight_level_params *backlight_level_params)
3179 {
3180 	uint32_t backlight_pwm_u16_16 = backlight_level_params->backlight_pwm_u16_16;
3181 	uint32_t frame_ramp = backlight_level_params->frame_ramp;
3182 	struct dc_link *link = pipe_ctx->stream->link;
3183 	struct dc  *dc = link->ctx->dc;
3184 	struct abm *abm = pipe_ctx->stream_res.abm;
3185 	struct panel_cntl *panel_cntl = link->panel_cntl;
3186 	struct dmcu *dmcu = dc->res_pool->dmcu;
3187 	bool fw_set_brightness = true;
3188 	/* DMCU -1 for all controller id values,
3189 	 * therefore +1 here
3190 	 */
3191 	uint32_t controller_id = pipe_ctx->stream_res.tg->inst + 1;
3192 
3193 	if (abm == NULL || panel_cntl == NULL || (abm->funcs->set_backlight_level_pwm == NULL))
3194 		return false;
3195 
3196 	if (dmcu)
3197 		fw_set_brightness = dmcu->funcs->is_dmcu_initialized(dmcu);
3198 
3199 	if (!fw_set_brightness && panel_cntl->funcs->driver_set_backlight)
3200 		panel_cntl->funcs->driver_set_backlight(panel_cntl, backlight_pwm_u16_16);
3201 	else
3202 		abm->funcs->set_backlight_level_pwm(
3203 				abm,
3204 				backlight_pwm_u16_16,
3205 				frame_ramp,
3206 				controller_id,
3207 				link->panel_cntl->inst);
3208 
3209 	return true;
3210 }
3211 
dce110_set_abm_immediate_disable(struct pipe_ctx * pipe_ctx)3212 void dce110_set_abm_immediate_disable(struct pipe_ctx *pipe_ctx)
3213 {
3214 	struct abm *abm = pipe_ctx->stream_res.abm;
3215 	struct panel_cntl *panel_cntl = pipe_ctx->stream->link->panel_cntl;
3216 
3217 	if (abm)
3218 		abm->funcs->set_abm_immediate_disable(abm,
3219 				pipe_ctx->stream->link->panel_cntl->inst);
3220 
3221 	if (panel_cntl)
3222 		panel_cntl->funcs->store_backlight_level(panel_cntl);
3223 }
3224 
dce110_set_pipe(struct pipe_ctx * pipe_ctx)3225 void dce110_set_pipe(struct pipe_ctx *pipe_ctx)
3226 {
3227 	struct abm *abm = pipe_ctx->stream_res.abm;
3228 	struct panel_cntl *panel_cntl = pipe_ctx->stream->link->panel_cntl;
3229 	uint32_t otg_inst = pipe_ctx->stream_res.tg->inst + 1;
3230 
3231 	if (abm && panel_cntl)
3232 		abm->funcs->set_pipe(abm, otg_inst, panel_cntl->inst);
3233 }
3234 
dce110_enable_lvds_link_output(struct dc_link * link,const struct link_resource * link_res,enum clock_source_id clock_source,uint32_t pixel_clock)3235 void dce110_enable_lvds_link_output(struct dc_link *link,
3236 		const struct link_resource *link_res,
3237 		enum clock_source_id clock_source,
3238 		uint32_t pixel_clock)
3239 {
3240 	link->link_enc->funcs->enable_lvds_output(
3241 			link->link_enc,
3242 			clock_source,
3243 			pixel_clock);
3244 	link->phy_state.symclk_state = SYMCLK_ON_TX_ON;
3245 }
3246 
dce110_enable_tmds_link_output(struct dc_link * link,const struct link_resource * link_res,enum signal_type signal,enum clock_source_id clock_source,enum dc_color_depth color_depth,uint32_t pixel_clock)3247 void dce110_enable_tmds_link_output(struct dc_link *link,
3248 		const struct link_resource *link_res,
3249 		enum signal_type signal,
3250 		enum clock_source_id clock_source,
3251 		enum dc_color_depth color_depth,
3252 		uint32_t pixel_clock)
3253 {
3254 	link->link_enc->funcs->enable_tmds_output(
3255 			link->link_enc,
3256 			clock_source,
3257 			color_depth,
3258 			signal,
3259 			pixel_clock);
3260 	link->phy_state.symclk_state = SYMCLK_ON_TX_ON;
3261 }
3262 
dce110_enable_dp_link_output(struct dc_link * link,const struct link_resource * link_res,enum signal_type signal,enum clock_source_id clock_source,const struct dc_link_settings * link_settings)3263 void dce110_enable_dp_link_output(
3264 		struct dc_link *link,
3265 		const struct link_resource *link_res,
3266 		enum signal_type signal,
3267 		enum clock_source_id clock_source,
3268 		const struct dc_link_settings *link_settings)
3269 {
3270 	struct dc  *dc = link->ctx->dc;
3271 	struct dmcu *dmcu = dc->res_pool->dmcu;
3272 	struct pipe_ctx *pipes =
3273 			link->dc->current_state->res_ctx.pipe_ctx;
3274 	struct clock_source *dp_cs =
3275 			link->dc->res_pool->dp_clock_source;
3276 	const struct link_hwss *link_hwss = get_link_hwss(link, link_res);
3277 	unsigned int i;
3278 
3279 	/*
3280 	 * Add the logic to extract BOTH power up and power down sequences
3281 	 * from enable/disable link output and only call edp panel control
3282 	 * in enable_link_dp and disable_link_dp once.
3283 	 */
3284 	if (link->connector_signal == SIGNAL_TYPE_EDP) {
3285 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
3286 	}
3287 
3288 	/* If the current pixel clock source is not DTO(happens after
3289 	 * switching from HDMI passive dongle to DP on the same connector),
3290 	 * switch the pixel clock source to DTO.
3291 	 */
3292 
3293 	for (i = 0; i < MAX_PIPES; i++) {
3294 		if (pipes[i].stream != NULL &&
3295 				pipes[i].stream->link == link) {
3296 			if (pipes[i].clock_source != NULL &&
3297 					pipes[i].clock_source->id != CLOCK_SOURCE_ID_DP_DTO) {
3298 				pipes[i].clock_source = dp_cs;
3299 				pipes[i].stream_res.pix_clk_params.requested_pix_clk_100hz =
3300 						pipes[i].stream->timing.pix_clk_100hz;
3301 				pipes[i].clock_source->funcs->program_pix_clk(
3302 						pipes[i].clock_source,
3303 						&pipes[i].stream_res.pix_clk_params,
3304 						dc->link_srv->dp_get_encoding_format(link_settings),
3305 						&pipes[i].pll_settings);
3306 			}
3307 		}
3308 	}
3309 
3310 	if (dc->link_srv->dp_get_encoding_format(link_settings) == DP_8b_10b_ENCODING) {
3311 		if (dc->clk_mgr->funcs->notify_link_rate_change)
3312 			dc->clk_mgr->funcs->notify_link_rate_change(dc->clk_mgr, link);
3313 	}
3314 
3315 	if (dmcu != NULL && dmcu->funcs->lock_phy)
3316 		dmcu->funcs->lock_phy(dmcu);
3317 
3318 	if (link_hwss->ext.enable_dp_link_output)
3319 		link_hwss->ext.enable_dp_link_output(link, link_res, signal,
3320 				clock_source, link_settings);
3321 
3322 	link->phy_state.symclk_state = SYMCLK_ON_TX_ON;
3323 
3324 	if (dmcu != NULL && dmcu->funcs->unlock_phy)
3325 		dmcu->funcs->unlock_phy(dmcu);
3326 
3327 	dc->link_srv->dp_trace_source_sequence(link, DPCD_SOURCE_SEQ_AFTER_ENABLE_LINK_PHY);
3328 }
3329 
dce110_disable_link_output(struct dc_link * link,const struct link_resource * link_res,enum signal_type signal)3330 void dce110_disable_link_output(struct dc_link *link,
3331 		const struct link_resource *link_res,
3332 		enum signal_type signal)
3333 {
3334 	struct dc *dc = link->ctx->dc;
3335 	const struct link_hwss *link_hwss = get_link_hwss(link, link_res);
3336 	struct dmcu *dmcu = dc->res_pool->dmcu;
3337 
3338 	if (signal == SIGNAL_TYPE_EDP &&
3339 			link->dc->hwss.edp_backlight_control &&
3340 			!link->skip_implict_edp_power_control)
3341 		link->dc->hwss.edp_backlight_control(link, false);
3342 	else if (dmcu != NULL && dmcu->funcs->lock_phy)
3343 		dmcu->funcs->lock_phy(dmcu);
3344 
3345 	link_hwss->disable_link_output(link, link_res, signal);
3346 	link->phy_state.symclk_state = SYMCLK_OFF_TX_OFF;
3347 	/*
3348 	 * Add the logic to extract BOTH power up and power down sequences
3349 	 * from enable/disable link output and only call edp panel control
3350 	 * in enable_link_dp and disable_link_dp once.
3351 	 */
3352 	if (dmcu != NULL && dmcu->funcs->unlock_phy)
3353 		dmcu->funcs->unlock_phy(dmcu);
3354 	dc->link_srv->dp_trace_source_sequence(link, DPCD_SOURCE_SEQ_AFTER_DISABLE_LINK_PHY);
3355 }
3356 
3357 static const struct hw_sequencer_funcs dce110_funcs = {
3358 	.program_gamut_remap = program_gamut_remap,
3359 	.program_output_csc = program_output_csc,
3360 	.init_hw = dce110_init_hw,
3361 	.apply_ctx_to_hw = dce110_apply_ctx_to_hw,
3362 	.apply_ctx_for_surface = dce110_apply_ctx_for_surface,
3363 	.post_unlock_program_front_end = dce110_post_unlock_program_front_end,
3364 	.update_plane_addr = update_plane_addr,
3365 	.update_pending_status = dce110_update_pending_status,
3366 	.clear_surface_dcc_and_tiling = dce100_reset_surface_dcc_and_tiling,
3367 	.enable_accelerated_mode = dce110_enable_accelerated_mode,
3368 	.enable_timing_synchronization = dce110_enable_timing_synchronization,
3369 	.enable_per_frame_crtc_position_reset = dce110_enable_per_frame_crtc_position_reset,
3370 	.update_info_frame = dce110_update_info_frame,
3371 	.enable_stream = dce110_enable_stream,
3372 	.disable_stream = dce110_disable_stream,
3373 	.unblank_stream = dce110_unblank_stream,
3374 	.blank_stream = dce110_blank_stream,
3375 	.enable_audio_stream = dce110_enable_audio_stream,
3376 	.disable_audio_stream = dce110_disable_audio_stream,
3377 	.disable_plane = dce110_power_down_fe,
3378 	.pipe_control_lock = dce_pipe_control_lock,
3379 	.interdependent_update_lock = NULL,
3380 	.cursor_lock = dce_pipe_control_lock,
3381 	.prepare_bandwidth = dce110_prepare_bandwidth,
3382 	.optimize_bandwidth = dce110_optimize_bandwidth,
3383 	.set_drr = set_drr,
3384 	.get_position = get_position,
3385 	.set_static_screen_control = set_static_screen_control,
3386 	.setup_stereo = NULL,
3387 	.set_avmute = dce110_set_avmute,
3388 	.wait_for_mpcc_disconnect = dce110_wait_for_mpcc_disconnect,
3389 	.edp_backlight_control = dce110_edp_backlight_control,
3390 	.edp_power_control = dce110_edp_power_control,
3391 	.edp_wait_for_hpd_ready = dce110_edp_wait_for_hpd_ready,
3392 	.set_cursor_position = dce110_set_cursor_position,
3393 	.set_cursor_attribute = dce110_set_cursor_attribute,
3394 	.set_backlight_level = dce110_set_backlight_level,
3395 	.set_abm_immediate_disable = dce110_set_abm_immediate_disable,
3396 	.set_pipe = dce110_set_pipe,
3397 	.enable_lvds_link_output = dce110_enable_lvds_link_output,
3398 	.enable_tmds_link_output = dce110_enable_tmds_link_output,
3399 	.enable_dp_link_output = dce110_enable_dp_link_output,
3400 	.disable_link_output = dce110_disable_link_output,
3401 };
3402 
3403 static const struct hwseq_private_funcs dce110_private_funcs = {
3404 	.init_pipes = dce110_init_pipes,
3405 	.set_input_transfer_func = dce110_set_input_transfer_func,
3406 	.set_output_transfer_func = dce110_set_output_transfer_func,
3407 	.power_down = dce110_power_down,
3408 	.enable_display_pipe_clock_gating = enable_display_pipe_clock_gating,
3409 	.enable_display_power_gating = dce110_enable_display_power_gating,
3410 	.reset_hw_ctx_wrap = dce110_reset_hw_ctx_wrap,
3411 	.enable_stream_timing = dce110_enable_stream_timing,
3412 	.disable_stream_gating = NULL,
3413 	.enable_stream_gating = NULL,
3414 	.edp_backlight_control = dce110_edp_backlight_control,
3415 };
3416 
dce110_hw_sequencer_construct(struct dc * dc)3417 void dce110_hw_sequencer_construct(struct dc *dc)
3418 {
3419 	dc->hwss = dce110_funcs;
3420 	dc->hwseq->funcs = dce110_private_funcs;
3421 }
3422 
3423