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