14562236bSHarry Wentland /*
24562236bSHarry Wentland  * Copyright 2015 Advanced Micro Devices, Inc.
34562236bSHarry Wentland  *
44562236bSHarry Wentland  * Permission is hereby granted, free of charge, to any person obtaining a
54562236bSHarry Wentland  * copy of this software and associated documentation files (the "Software"),
64562236bSHarry Wentland  * to deal in the Software without restriction, including without limitation
74562236bSHarry Wentland  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84562236bSHarry Wentland  * and/or sell copies of the Software, and to permit persons to whom the
94562236bSHarry Wentland  * Software is furnished to do so, subject to the following conditions:
104562236bSHarry Wentland  *
114562236bSHarry Wentland  * The above copyright notice and this permission notice shall be included in
124562236bSHarry Wentland  * all copies or substantial portions of the Software.
134562236bSHarry Wentland  *
144562236bSHarry Wentland  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154562236bSHarry Wentland  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164562236bSHarry Wentland  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174562236bSHarry Wentland  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184562236bSHarry Wentland  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194562236bSHarry Wentland  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204562236bSHarry Wentland  * OTHER DEALINGS IN THE SOFTWARE.
214562236bSHarry Wentland  *
224562236bSHarry Wentland  * Authors: AMD
234562236bSHarry Wentland  *
244562236bSHarry Wentland  */
25c366be54SSam Ravnborg 
26c366be54SSam Ravnborg #include <linux/delay.h>
27c366be54SSam Ravnborg 
284562236bSHarry Wentland #include "dm_services.h"
294562236bSHarry Wentland #include "dc.h"
304562236bSHarry Wentland #include "dc_bios_types.h"
314562236bSHarry Wentland #include "core_types.h"
324562236bSHarry Wentland #include "core_status.h"
334562236bSHarry Wentland #include "resource.h"
344562236bSHarry Wentland #include "dm_helpers.h"
354562236bSHarry Wentland #include "dce110_hw_sequencer.h"
364562236bSHarry Wentland #include "dce110_timing_generator.h"
3798489c02SLeo (Sunpeng) Li #include "dce/dce_hwseq.h"
3887401969SAndrew Jiang #include "gpio_service_interface.h"
394562236bSHarry Wentland 
401663ae1cSBhawanpreet Lakha #include "dce110_compressor.h"
411663ae1cSBhawanpreet Lakha 
424562236bSHarry Wentland #include "bios/bios_parser_helper.h"
434562236bSHarry Wentland #include "timing_generator.h"
444562236bSHarry Wentland #include "mem_input.h"
454562236bSHarry Wentland #include "opp.h"
464562236bSHarry Wentland #include "ipp.h"
474562236bSHarry Wentland #include "transform.h"
484562236bSHarry Wentland #include "stream_encoder.h"
494562236bSHarry Wentland #include "link_encoder.h"
5087401969SAndrew Jiang #include "link_hwss.h"
514562236bSHarry Wentland #include "clock_source.h"
52dc88b4a6SEric Yang #include "clk_mgr.h"
535e7773a2SAnthony Koo #include "abm.h"
544562236bSHarry Wentland #include "audio.h"
5508b16886SZeyu Fan #include "reg_helper.h"
56d4caa72eSAnthony Koo #include "panel_cntl.h"
574562236bSHarry Wentland 
584562236bSHarry Wentland /* include DCE11 register header files */
594562236bSHarry Wentland #include "dce/dce_11_0_d.h"
604562236bSHarry Wentland #include "dce/dce_11_0_sh_mask.h"
61e266fdf6SVitaly Prosyak #include "custom_float.h"
624562236bSHarry Wentland 
634cac1e6dSYongqiang Sun #include "atomfirmware.h"
644cac1e6dSYongqiang Sun 
6578c77382SAnthony Koo #define GAMMA_HW_POINTS_NUM 256
6678c77382SAnthony Koo 
6787401969SAndrew Jiang /*
6887401969SAndrew Jiang  * All values are in milliseconds;
6987401969SAndrew Jiang  * For eDP, after power-up/power/down,
7087401969SAndrew Jiang  * 300/500 msec max. delay from LCDVCC to black video generation
7187401969SAndrew Jiang  */
7287401969SAndrew Jiang #define PANEL_POWER_UP_TIMEOUT 300
7387401969SAndrew Jiang #define PANEL_POWER_DOWN_TIMEOUT 500
7487401969SAndrew Jiang #define HPD_CHECK_INTERVAL 10
7596577cf8SHersen Wu #define OLED_POST_T7_DELAY 100
7696577cf8SHersen Wu #define OLED_PRE_T11_DELAY 150
7787401969SAndrew Jiang 
785eefbc40SYue Hin Lau #define CTX \
795eefbc40SYue Hin Lau 	hws->ctx
805d4b05ddSBhawanpreet Lakha 
815d4b05ddSBhawanpreet Lakha #define DC_LOGGER_INIT()
825d4b05ddSBhawanpreet Lakha 
835eefbc40SYue Hin Lau #define REG(reg)\
845eefbc40SYue Hin Lau 	hws->regs->reg
855eefbc40SYue Hin Lau 
865eefbc40SYue Hin Lau #undef FN
875eefbc40SYue Hin Lau #define FN(reg_name, field_name) \
885eefbc40SYue Hin Lau 	hws->shifts->field_name, hws->masks->field_name
895eefbc40SYue Hin Lau 
904562236bSHarry Wentland struct dce110_hw_seq_reg_offsets {
914562236bSHarry Wentland 	uint32_t crtc;
924562236bSHarry Wentland };
934562236bSHarry Wentland 
944562236bSHarry Wentland static const struct dce110_hw_seq_reg_offsets reg_offsets[] = {
954562236bSHarry Wentland {
964562236bSHarry Wentland 	.crtc = (mmCRTC0_CRTC_GSL_CONTROL - mmCRTC_GSL_CONTROL),
974562236bSHarry Wentland },
984562236bSHarry Wentland {
994562236bSHarry Wentland 	.crtc = (mmCRTC1_CRTC_GSL_CONTROL - mmCRTC_GSL_CONTROL),
1004562236bSHarry Wentland },
1014562236bSHarry Wentland {
1024562236bSHarry Wentland 	.crtc = (mmCRTC2_CRTC_GSL_CONTROL - mmCRTC_GSL_CONTROL),
1034562236bSHarry Wentland },
1044562236bSHarry Wentland {
1054562236bSHarry Wentland 	.crtc = (mmCRTCV_GSL_CONTROL - mmCRTC_GSL_CONTROL),
1064562236bSHarry Wentland }
1074562236bSHarry Wentland };
1084562236bSHarry Wentland 
1094562236bSHarry Wentland #define HW_REG_BLND(reg, id)\
1104562236bSHarry Wentland 	(reg + reg_offsets[id].blnd)
1114562236bSHarry Wentland 
1124562236bSHarry Wentland #define HW_REG_CRTC(reg, id)\
1134562236bSHarry Wentland 	(reg + reg_offsets[id].crtc)
1144562236bSHarry Wentland 
1154562236bSHarry Wentland #define MAX_WATERMARK 0xFFFF
1164562236bSHarry Wentland #define SAFE_NBP_MARK 0x7FFF
1174562236bSHarry Wentland 
1184562236bSHarry Wentland /*******************************************************************************
1194562236bSHarry Wentland  * Private definitions
1204562236bSHarry Wentland  ******************************************************************************/
1214562236bSHarry Wentland /***************************PIPE_CONTROL***********************************/
1224562236bSHarry Wentland static void dce110_init_pte(struct dc_context *ctx)
1234562236bSHarry Wentland {
1244562236bSHarry Wentland 	uint32_t addr;
1254562236bSHarry Wentland 	uint32_t value = 0;
1264562236bSHarry Wentland 	uint32_t chunk_int = 0;
1274562236bSHarry Wentland 	uint32_t chunk_mul = 0;
1284562236bSHarry Wentland 
1294562236bSHarry Wentland 	addr = mmUNP_DVMM_PTE_CONTROL;
1304562236bSHarry Wentland 	value = dm_read_reg(ctx, addr);
1314562236bSHarry Wentland 
1324562236bSHarry Wentland 	set_reg_field_value(
1334562236bSHarry Wentland 		value,
1344562236bSHarry Wentland 		0,
1354562236bSHarry Wentland 		DVMM_PTE_CONTROL,
1364562236bSHarry Wentland 		DVMM_USE_SINGLE_PTE);
1374562236bSHarry Wentland 
1384562236bSHarry Wentland 	set_reg_field_value(
1394562236bSHarry Wentland 		value,
1404562236bSHarry Wentland 		1,
1414562236bSHarry Wentland 		DVMM_PTE_CONTROL,
1424562236bSHarry Wentland 		DVMM_PTE_BUFFER_MODE0);
1434562236bSHarry Wentland 
1444562236bSHarry Wentland 	set_reg_field_value(
1454562236bSHarry Wentland 		value,
1464562236bSHarry Wentland 		1,
1474562236bSHarry Wentland 		DVMM_PTE_CONTROL,
1484562236bSHarry Wentland 		DVMM_PTE_BUFFER_MODE1);
1494562236bSHarry Wentland 
1504562236bSHarry Wentland 	dm_write_reg(ctx, addr, value);
1514562236bSHarry Wentland 
1524562236bSHarry Wentland 	addr = mmDVMM_PTE_REQ;
1534562236bSHarry Wentland 	value = dm_read_reg(ctx, addr);
1544562236bSHarry Wentland 
1554562236bSHarry Wentland 	chunk_int = get_reg_field_value(
1564562236bSHarry Wentland 		value,
1574562236bSHarry Wentland 		DVMM_PTE_REQ,
1584562236bSHarry Wentland 		HFLIP_PTEREQ_PER_CHUNK_INT);
1594562236bSHarry Wentland 
1604562236bSHarry Wentland 	chunk_mul = get_reg_field_value(
1614562236bSHarry Wentland 		value,
1624562236bSHarry Wentland 		DVMM_PTE_REQ,
1634562236bSHarry Wentland 		HFLIP_PTEREQ_PER_CHUNK_MULTIPLIER);
1644562236bSHarry Wentland 
1654562236bSHarry Wentland 	if (chunk_int != 0x4 || chunk_mul != 0x4) {
1664562236bSHarry Wentland 
1674562236bSHarry Wentland 		set_reg_field_value(
1684562236bSHarry Wentland 			value,
1694562236bSHarry Wentland 			255,
1704562236bSHarry Wentland 			DVMM_PTE_REQ,
1714562236bSHarry Wentland 			MAX_PTEREQ_TO_ISSUE);
1724562236bSHarry Wentland 
1734562236bSHarry Wentland 		set_reg_field_value(
1744562236bSHarry Wentland 			value,
1754562236bSHarry Wentland 			4,
1764562236bSHarry Wentland 			DVMM_PTE_REQ,
1774562236bSHarry Wentland 			HFLIP_PTEREQ_PER_CHUNK_INT);
1784562236bSHarry Wentland 
1794562236bSHarry Wentland 		set_reg_field_value(
1804562236bSHarry Wentland 			value,
1814562236bSHarry Wentland 			4,
1824562236bSHarry Wentland 			DVMM_PTE_REQ,
1834562236bSHarry Wentland 			HFLIP_PTEREQ_PER_CHUNK_MULTIPLIER);
1844562236bSHarry Wentland 
1854562236bSHarry Wentland 		dm_write_reg(ctx, addr, value);
1864562236bSHarry Wentland 	}
1874562236bSHarry Wentland }
1884562236bSHarry Wentland /**************************************************************************/
1894562236bSHarry Wentland 
1904562236bSHarry Wentland static void enable_display_pipe_clock_gating(
1914562236bSHarry Wentland 	struct dc_context *ctx,
1924562236bSHarry Wentland 	bool clock_gating)
1934562236bSHarry Wentland {
1944562236bSHarry Wentland 	/*TODO*/
1954562236bSHarry Wentland }
1964562236bSHarry Wentland 
1974562236bSHarry Wentland static bool dce110_enable_display_power_gating(
198fb3466a4SBhawanpreet Lakha 	struct dc *dc,
1994562236bSHarry Wentland 	uint8_t controller_id,
2004562236bSHarry Wentland 	struct dc_bios *dcb,
2014562236bSHarry Wentland 	enum pipe_gating_control power_gating)
2024562236bSHarry Wentland {
2034562236bSHarry Wentland 	enum bp_result bp_result = BP_RESULT_OK;
2044562236bSHarry Wentland 	enum bp_pipe_control_action cntl;
2054562236bSHarry Wentland 	struct dc_context *ctx = dc->ctx;
2064562236bSHarry Wentland 	unsigned int underlay_idx = dc->res_pool->underlay_pipe_index;
2074562236bSHarry Wentland 
2084562236bSHarry Wentland 	if (IS_FPGA_MAXIMUS_DC(ctx->dce_environment))
2094562236bSHarry Wentland 		return true;
2104562236bSHarry Wentland 
2114562236bSHarry Wentland 	if (power_gating == PIPE_GATING_CONTROL_INIT)
2124562236bSHarry Wentland 		cntl = ASIC_PIPE_INIT;
2134562236bSHarry Wentland 	else if (power_gating == PIPE_GATING_CONTROL_ENABLE)
2144562236bSHarry Wentland 		cntl = ASIC_PIPE_ENABLE;
2154562236bSHarry Wentland 	else
2164562236bSHarry Wentland 		cntl = ASIC_PIPE_DISABLE;
2174562236bSHarry Wentland 
2184562236bSHarry Wentland 	if (controller_id == underlay_idx)
2194562236bSHarry Wentland 		controller_id = CONTROLLER_ID_UNDERLAY0 - 1;
2204562236bSHarry Wentland 
2214562236bSHarry Wentland 	if (power_gating != PIPE_GATING_CONTROL_INIT || controller_id == 0){
2224562236bSHarry Wentland 
2234562236bSHarry Wentland 		bp_result = dcb->funcs->enable_disp_power_gating(
2244562236bSHarry Wentland 						dcb, controller_id + 1, cntl);
2254562236bSHarry Wentland 
2264562236bSHarry Wentland 		/* Revert MASTER_UPDATE_MODE to 0 because bios sets it 2
2274562236bSHarry Wentland 		 * by default when command table is called
2284562236bSHarry Wentland 		 *
2294562236bSHarry Wentland 		 * Bios parser accepts controller_id = 6 as indicative of
2304562236bSHarry Wentland 		 * underlay pipe in dce110. But we do not support more
2314562236bSHarry Wentland 		 * than 3.
2324562236bSHarry Wentland 		 */
2334562236bSHarry Wentland 		if (controller_id < CONTROLLER_ID_MAX - 1)
2344562236bSHarry Wentland 			dm_write_reg(ctx,
2354562236bSHarry Wentland 				HW_REG_CRTC(mmCRTC_MASTER_UPDATE_MODE, controller_id),
2364562236bSHarry Wentland 				0);
2374562236bSHarry Wentland 	}
2384562236bSHarry Wentland 
2394562236bSHarry Wentland 	if (power_gating != PIPE_GATING_CONTROL_ENABLE)
2404562236bSHarry Wentland 		dce110_init_pte(ctx);
2414562236bSHarry Wentland 
2424562236bSHarry Wentland 	if (bp_result == BP_RESULT_OK)
2434562236bSHarry Wentland 		return true;
2444562236bSHarry Wentland 	else
2454562236bSHarry Wentland 		return false;
2464562236bSHarry Wentland }
2474562236bSHarry Wentland 
2484562236bSHarry Wentland static void build_prescale_params(struct ipp_prescale_params *prescale_params,
2493be5262eSHarry Wentland 		const struct dc_plane_state *plane_state)
2504562236bSHarry Wentland {
2514562236bSHarry Wentland 	prescale_params->mode = IPP_PRESCALE_MODE_FIXED_UNSIGNED;
2524562236bSHarry Wentland 
2533be5262eSHarry Wentland 	switch (plane_state->format) {
2541352c779SNicholas Kazlauskas 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
2551352c779SNicholas Kazlauskas 		prescale_params->scale = 0x2082;
2561352c779SNicholas Kazlauskas 		break;
2574562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
2588693049aSTony Cheng 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
2594562236bSHarry Wentland 		prescale_params->scale = 0x2020;
2604562236bSHarry Wentland 		break;
2614562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
2624562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
2634562236bSHarry Wentland 		prescale_params->scale = 0x2008;
2644562236bSHarry Wentland 		break;
2654562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
2664562236bSHarry Wentland 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
2674562236bSHarry Wentland 		prescale_params->scale = 0x2000;
2684562236bSHarry Wentland 		break;
2694562236bSHarry Wentland 	default:
2704562236bSHarry Wentland 		ASSERT(false);
271d7194cf6SAric Cyr 		break;
2724562236bSHarry Wentland 	}
2734562236bSHarry Wentland }
2744562236bSHarry Wentland 
275a6114e85SHarry Wentland static bool
27678c77382SAnthony Koo dce110_set_input_transfer_func(struct dc *dc, struct pipe_ctx *pipe_ctx,
2773be5262eSHarry Wentland 			       const struct dc_plane_state *plane_state)
2784562236bSHarry Wentland {
27986a66c4eSHarry Wentland 	struct input_pixel_processor *ipp = pipe_ctx->plane_res.ipp;
2807b0c470fSLeo (Sunpeng) Li 	const struct dc_transfer_func *tf = NULL;
28190e508baSAnthony Koo 	struct ipp_prescale_params prescale_params = { 0 };
28290e508baSAnthony Koo 	bool result = true;
28390e508baSAnthony Koo 
28490e508baSAnthony Koo 	if (ipp == NULL)
28590e508baSAnthony Koo 		return false;
28690e508baSAnthony Koo 
2873be5262eSHarry Wentland 	if (plane_state->in_transfer_func)
2883be5262eSHarry Wentland 		tf = plane_state->in_transfer_func;
28990e508baSAnthony Koo 
2903be5262eSHarry Wentland 	build_prescale_params(&prescale_params, plane_state);
29190e508baSAnthony Koo 	ipp->funcs->ipp_program_prescale(ipp, &prescale_params);
29290e508baSAnthony Koo 
29384ffa801SLeo (Sunpeng) Li 	if (plane_state->gamma_correction &&
29484ffa801SLeo (Sunpeng) Li 			!plane_state->gamma_correction->is_identity &&
29584ffa801SLeo (Sunpeng) Li 			dce_use_lut(plane_state->format))
2963be5262eSHarry Wentland 		ipp->funcs->ipp_program_input_lut(ipp, plane_state->gamma_correction);
297d7194cf6SAric Cyr 
29890e508baSAnthony Koo 	if (tf == NULL) {
29990e508baSAnthony Koo 		/* Default case if no input transfer function specified */
300a6114e85SHarry Wentland 		ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_HW_sRGB);
3017b0c470fSLeo (Sunpeng) Li 	} else if (tf->type == TF_TYPE_PREDEFINED) {
3027b0c470fSLeo (Sunpeng) Li 		switch (tf->tf) {
30390e508baSAnthony Koo 		case TRANSFER_FUNCTION_SRGB:
304a6114e85SHarry Wentland 			ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_HW_sRGB);
30590e508baSAnthony Koo 			break;
30690e508baSAnthony Koo 		case TRANSFER_FUNCTION_BT709:
307a6114e85SHarry Wentland 			ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_HW_xvYCC);
30890e508baSAnthony Koo 			break;
30990e508baSAnthony Koo 		case TRANSFER_FUNCTION_LINEAR:
310a6114e85SHarry Wentland 			ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_BYPASS);
31190e508baSAnthony Koo 			break;
31290e508baSAnthony Koo 		case TRANSFER_FUNCTION_PQ:
31390e508baSAnthony Koo 		default:
31490e508baSAnthony Koo 			result = false;
315d7194cf6SAric Cyr 			break;
31690e508baSAnthony Koo 		}
3177b0c470fSLeo (Sunpeng) Li 	} else if (tf->type == TF_TYPE_BYPASS) {
31870063a59SAmy Zhang 		ipp->funcs->ipp_set_degamma(ipp, IPP_DEGAMMA_MODE_BYPASS);
31990e508baSAnthony Koo 	} else {
32090e508baSAnthony Koo 		/*TF_TYPE_DISTRIBUTED_POINTS - Not supported in DCE 11*/
32190e508baSAnthony Koo 		result = false;
32290e508baSAnthony Koo 	}
32390e508baSAnthony Koo 
32490e508baSAnthony Koo 	return result;
32590e508baSAnthony Koo }
32690e508baSAnthony Koo 
327bd1be8e8SHarry Wentland static bool convert_to_custom_float(struct pwl_result_data *rgb_resulted,
328fcd2f4bfSAmy Zhang 				    struct curve_points *arr_points,
329fcd2f4bfSAmy Zhang 				    uint32_t hw_points_num)
330fcd2f4bfSAmy Zhang {
331fcd2f4bfSAmy Zhang 	struct custom_float_format fmt;
332fcd2f4bfSAmy Zhang 
333fcd2f4bfSAmy Zhang 	struct pwl_result_data *rgb = rgb_resulted;
334fcd2f4bfSAmy Zhang 
335fcd2f4bfSAmy Zhang 	uint32_t i = 0;
336fcd2f4bfSAmy Zhang 
337fcd2f4bfSAmy Zhang 	fmt.exponenta_bits = 6;
338fcd2f4bfSAmy Zhang 	fmt.mantissa_bits = 12;
339fcd2f4bfSAmy Zhang 	fmt.sign = true;
340fcd2f4bfSAmy Zhang 
341bd1be8e8SHarry Wentland 	if (!convert_to_custom_float_format(arr_points[0].x, &fmt,
342fcd2f4bfSAmy Zhang 					    &arr_points[0].custom_float_x)) {
343fcd2f4bfSAmy Zhang 		BREAK_TO_DEBUGGER();
344fcd2f4bfSAmy Zhang 		return false;
345fcd2f4bfSAmy Zhang 	}
346fcd2f4bfSAmy Zhang 
347bd1be8e8SHarry Wentland 	if (!convert_to_custom_float_format(arr_points[0].offset, &fmt,
348fcd2f4bfSAmy Zhang 					    &arr_points[0].custom_float_offset)) {
349fcd2f4bfSAmy Zhang 		BREAK_TO_DEBUGGER();
350fcd2f4bfSAmy Zhang 		return false;
351fcd2f4bfSAmy Zhang 	}
352fcd2f4bfSAmy Zhang 
353bd1be8e8SHarry Wentland 	if (!convert_to_custom_float_format(arr_points[0].slope, &fmt,
354fcd2f4bfSAmy Zhang 					    &arr_points[0].custom_float_slope)) {
355fcd2f4bfSAmy Zhang 		BREAK_TO_DEBUGGER();
356fcd2f4bfSAmy Zhang 		return false;
357fcd2f4bfSAmy Zhang 	}
358fcd2f4bfSAmy Zhang 
359fcd2f4bfSAmy Zhang 	fmt.mantissa_bits = 10;
360fcd2f4bfSAmy Zhang 	fmt.sign = false;
361fcd2f4bfSAmy Zhang 
362bd1be8e8SHarry Wentland 	if (!convert_to_custom_float_format(arr_points[1].x, &fmt,
363fcd2f4bfSAmy Zhang 					    &arr_points[1].custom_float_x)) {
364fcd2f4bfSAmy Zhang 		BREAK_TO_DEBUGGER();
365fcd2f4bfSAmy Zhang 		return false;
366fcd2f4bfSAmy Zhang 	}
367fcd2f4bfSAmy Zhang 
368bd1be8e8SHarry Wentland 	if (!convert_to_custom_float_format(arr_points[1].y, &fmt,
369fcd2f4bfSAmy Zhang 					    &arr_points[1].custom_float_y)) {
370fcd2f4bfSAmy Zhang 		BREAK_TO_DEBUGGER();
371fcd2f4bfSAmy Zhang 		return false;
372fcd2f4bfSAmy Zhang 	}
373fcd2f4bfSAmy Zhang 
3744d06ccd0SHarry Wentland 	if (!convert_to_custom_float_format(arr_points[1].slope, &fmt,
3754d06ccd0SHarry Wentland 					    &arr_points[1].custom_float_slope)) {
376fcd2f4bfSAmy Zhang 		BREAK_TO_DEBUGGER();
377fcd2f4bfSAmy Zhang 		return false;
378fcd2f4bfSAmy Zhang 	}
379fcd2f4bfSAmy Zhang 
380fcd2f4bfSAmy Zhang 	fmt.mantissa_bits = 12;
381fcd2f4bfSAmy Zhang 	fmt.sign = true;
382fcd2f4bfSAmy Zhang 
383fcd2f4bfSAmy Zhang 	while (i != hw_points_num) {
384bd1be8e8SHarry Wentland 		if (!convert_to_custom_float_format(rgb->red, &fmt,
385fcd2f4bfSAmy Zhang 						    &rgb->red_reg)) {
386fcd2f4bfSAmy Zhang 			BREAK_TO_DEBUGGER();
387fcd2f4bfSAmy Zhang 			return false;
388fcd2f4bfSAmy Zhang 		}
389fcd2f4bfSAmy Zhang 
390bd1be8e8SHarry Wentland 		if (!convert_to_custom_float_format(rgb->green, &fmt,
391fcd2f4bfSAmy Zhang 						    &rgb->green_reg)) {
392fcd2f4bfSAmy Zhang 			BREAK_TO_DEBUGGER();
393fcd2f4bfSAmy Zhang 			return false;
394fcd2f4bfSAmy Zhang 		}
395fcd2f4bfSAmy Zhang 
396bd1be8e8SHarry Wentland 		if (!convert_to_custom_float_format(rgb->blue, &fmt,
397fcd2f4bfSAmy Zhang 						    &rgb->blue_reg)) {
398fcd2f4bfSAmy Zhang 			BREAK_TO_DEBUGGER();
399fcd2f4bfSAmy Zhang 			return false;
400fcd2f4bfSAmy Zhang 		}
401fcd2f4bfSAmy Zhang 
402bd1be8e8SHarry Wentland 		if (!convert_to_custom_float_format(rgb->delta_red, &fmt,
403fcd2f4bfSAmy Zhang 						    &rgb->delta_red_reg)) {
404fcd2f4bfSAmy Zhang 			BREAK_TO_DEBUGGER();
405fcd2f4bfSAmy Zhang 			return false;
406fcd2f4bfSAmy Zhang 		}
407fcd2f4bfSAmy Zhang 
408bd1be8e8SHarry Wentland 		if (!convert_to_custom_float_format(rgb->delta_green, &fmt,
409fcd2f4bfSAmy Zhang 						    &rgb->delta_green_reg)) {
410fcd2f4bfSAmy Zhang 			BREAK_TO_DEBUGGER();
411fcd2f4bfSAmy Zhang 			return false;
412fcd2f4bfSAmy Zhang 		}
413fcd2f4bfSAmy Zhang 
414bd1be8e8SHarry Wentland 		if (!convert_to_custom_float_format(rgb->delta_blue, &fmt,
415fcd2f4bfSAmy Zhang 						    &rgb->delta_blue_reg)) {
416fcd2f4bfSAmy Zhang 			BREAK_TO_DEBUGGER();
417fcd2f4bfSAmy Zhang 			return false;
418fcd2f4bfSAmy Zhang 		}
419fcd2f4bfSAmy Zhang 
420fcd2f4bfSAmy Zhang 		++rgb;
421fcd2f4bfSAmy Zhang 		++i;
422fcd2f4bfSAmy Zhang 	}
423fcd2f4bfSAmy Zhang 
424fcd2f4bfSAmy Zhang 	return true;
425fcd2f4bfSAmy Zhang }
426fcd2f4bfSAmy Zhang 
42708616da5SLeo (Sunpeng) Li #define MAX_LOW_POINT      25
4288f8372c7SKrunoslav Kovac #define NUMBER_REGIONS     16
4298f8372c7SKrunoslav Kovac #define NUMBER_SW_SEGMENTS 16
4308f8372c7SKrunoslav Kovac 
431b310b081SHarry Wentland static bool
432b310b081SHarry Wentland dce110_translate_regamma_to_hw_format(const struct dc_transfer_func *output_tf,
433b310b081SHarry Wentland 				      struct pwl_params *regamma_params)
434fcd2f4bfSAmy Zhang {
43523ae4f8eSAmy Zhang 	struct curve_points *arr_points;
43623ae4f8eSAmy Zhang 	struct pwl_result_data *rgb_resulted;
43723ae4f8eSAmy Zhang 	struct pwl_result_data *rgb;
43823ae4f8eSAmy Zhang 	struct pwl_result_data *rgb_plus_1;
439fcd2f4bfSAmy Zhang 	struct fixed31_32 y_r;
440fcd2f4bfSAmy Zhang 	struct fixed31_32 y_g;
441fcd2f4bfSAmy Zhang 	struct fixed31_32 y_b;
442fcd2f4bfSAmy Zhang 	struct fixed31_32 y1_min;
443fcd2f4bfSAmy Zhang 	struct fixed31_32 y3_max;
444fcd2f4bfSAmy Zhang 
4458f8372c7SKrunoslav Kovac 	int32_t region_start, region_end;
4468f8372c7SKrunoslav Kovac 	uint32_t i, j, k, seg_distr[NUMBER_REGIONS], increment, start_index, hw_points;
44723ae4f8eSAmy Zhang 
448b310b081SHarry Wentland 	if (output_tf == NULL || regamma_params == NULL || output_tf->type == TF_TYPE_BYPASS)
44923ae4f8eSAmy Zhang 		return false;
45023ae4f8eSAmy Zhang 
45123ae4f8eSAmy Zhang 	arr_points = regamma_params->arr_points;
45223ae4f8eSAmy Zhang 	rgb_resulted = regamma_params->rgb_resulted;
45323ae4f8eSAmy Zhang 	hw_points = 0;
454fcd2f4bfSAmy Zhang 
455fcd2f4bfSAmy Zhang 	memset(regamma_params, 0, sizeof(struct pwl_params));
456fcd2f4bfSAmy Zhang 
457fcd2f4bfSAmy Zhang 	if (output_tf->tf == TRANSFER_FUNCTION_PQ) {
458534db198SAmy Zhang 		/* 16 segments
459fcd2f4bfSAmy Zhang 		 * segments are from 2^-11 to 2^5
460fcd2f4bfSAmy Zhang 		 */
46108616da5SLeo (Sunpeng) Li 		region_start = -11;
46208616da5SLeo (Sunpeng) Li 		region_end = region_start + NUMBER_REGIONS;
463fcd2f4bfSAmy Zhang 
4648f8372c7SKrunoslav Kovac 		for (i = 0; i < NUMBER_REGIONS; i++)
4658f8372c7SKrunoslav Kovac 			seg_distr[i] = 4;
466534db198SAmy Zhang 
467fcd2f4bfSAmy Zhang 	} else {
468534db198SAmy Zhang 		/* 10 segments
469fc6de1c5SLeo (Sunpeng) Li 		 * segment is from 2^-10 to 2^1
470fc6de1c5SLeo (Sunpeng) Li 		 * We include an extra segment for range [2^0, 2^1). This is to
471fc6de1c5SLeo (Sunpeng) Li 		 * ensure that colors with normalized values of 1 don't miss the
472fc6de1c5SLeo (Sunpeng) Li 		 * LUT.
473fcd2f4bfSAmy Zhang 		 */
4748f8372c7SKrunoslav Kovac 		region_start = -10;
475fc6de1c5SLeo (Sunpeng) Li 		region_end = 1;
476534db198SAmy Zhang 
4778f8372c7SKrunoslav Kovac 		seg_distr[0] = 4;
478534db198SAmy Zhang 		seg_distr[1] = 4;
479534db198SAmy Zhang 		seg_distr[2] = 4;
480534db198SAmy Zhang 		seg_distr[3] = 4;
481534db198SAmy Zhang 		seg_distr[4] = 4;
482534db198SAmy Zhang 		seg_distr[5] = 4;
483534db198SAmy Zhang 		seg_distr[6] = 4;
484534db198SAmy Zhang 		seg_distr[7] = 4;
4858f8372c7SKrunoslav Kovac 		seg_distr[8] = 4;
4868f8372c7SKrunoslav Kovac 		seg_distr[9] = 4;
487fc6de1c5SLeo (Sunpeng) Li 		seg_distr[10] = 0;
488534db198SAmy Zhang 		seg_distr[11] = -1;
489534db198SAmy Zhang 		seg_distr[12] = -1;
490534db198SAmy Zhang 		seg_distr[13] = -1;
491534db198SAmy Zhang 		seg_distr[14] = -1;
492534db198SAmy Zhang 		seg_distr[15] = -1;
493fcd2f4bfSAmy Zhang 	}
494fcd2f4bfSAmy Zhang 
495534db198SAmy Zhang 	for (k = 0; k < 16; k++) {
496534db198SAmy Zhang 		if (seg_distr[k] != -1)
497534db198SAmy Zhang 			hw_points += (1 << seg_distr[k]);
498534db198SAmy Zhang 	}
499534db198SAmy Zhang 
500fcd2f4bfSAmy Zhang 	j = 0;
5018f8372c7SKrunoslav Kovac 	for (k = 0; k < (region_end - region_start); k++) {
502ec47734aSLeo (Sunpeng) Li 		increment = NUMBER_SW_SEGMENTS / (1 << seg_distr[k]);
5038f8372c7SKrunoslav Kovac 		start_index = (region_start + k + MAX_LOW_POINT) *
5048f8372c7SKrunoslav Kovac 				NUMBER_SW_SEGMENTS;
5058f8372c7SKrunoslav Kovac 		for (i = start_index; i < start_index + NUMBER_SW_SEGMENTS;
5068f8372c7SKrunoslav Kovac 				i += increment) {
507534db198SAmy Zhang 			if (j == hw_points - 1)
508fcd2f4bfSAmy Zhang 				break;
509fcd2f4bfSAmy Zhang 			rgb_resulted[j].red = output_tf->tf_pts.red[i];
510fcd2f4bfSAmy Zhang 			rgb_resulted[j].green = output_tf->tf_pts.green[i];
511fcd2f4bfSAmy Zhang 			rgb_resulted[j].blue = output_tf->tf_pts.blue[i];
512fcd2f4bfSAmy Zhang 			j++;
513fcd2f4bfSAmy Zhang 		}
514534db198SAmy Zhang 	}
515534db198SAmy Zhang 
516534db198SAmy Zhang 	/* last point */
5178f8372c7SKrunoslav Kovac 	start_index = (region_end + MAX_LOW_POINT) * NUMBER_SW_SEGMENTS;
518b310b081SHarry Wentland 	rgb_resulted[hw_points - 1].red = output_tf->tf_pts.red[start_index];
519b310b081SHarry Wentland 	rgb_resulted[hw_points - 1].green = output_tf->tf_pts.green[start_index];
520b310b081SHarry Wentland 	rgb_resulted[hw_points - 1].blue = output_tf->tf_pts.blue[start_index];
521fcd2f4bfSAmy Zhang 
522eb0e5154SDmytro Laktyushkin 	arr_points[0].x = dc_fixpt_pow(dc_fixpt_from_int(2),
523eb0e5154SDmytro Laktyushkin 					     dc_fixpt_from_int(region_start));
524eb0e5154SDmytro Laktyushkin 	arr_points[1].x = dc_fixpt_pow(dc_fixpt_from_int(2),
525eb0e5154SDmytro Laktyushkin 					     dc_fixpt_from_int(region_end));
526fcd2f4bfSAmy Zhang 
527fcd2f4bfSAmy Zhang 	y_r = rgb_resulted[0].red;
528fcd2f4bfSAmy Zhang 	y_g = rgb_resulted[0].green;
529fcd2f4bfSAmy Zhang 	y_b = rgb_resulted[0].blue;
530fcd2f4bfSAmy Zhang 
531eb0e5154SDmytro Laktyushkin 	y1_min = dc_fixpt_min(y_r, dc_fixpt_min(y_g, y_b));
532fcd2f4bfSAmy Zhang 
533fcd2f4bfSAmy Zhang 	arr_points[0].y = y1_min;
534eb0e5154SDmytro Laktyushkin 	arr_points[0].slope = dc_fixpt_div(arr_points[0].y,
535fcd2f4bfSAmy Zhang 						 arr_points[0].x);
536fcd2f4bfSAmy Zhang 
537fcd2f4bfSAmy Zhang 	y_r = rgb_resulted[hw_points - 1].red;
538fcd2f4bfSAmy Zhang 	y_g = rgb_resulted[hw_points - 1].green;
539fcd2f4bfSAmy Zhang 	y_b = rgb_resulted[hw_points - 1].blue;
540fcd2f4bfSAmy Zhang 
541fcd2f4bfSAmy Zhang 	/* see comment above, m_arrPoints[1].y should be the Y value for the
542fcd2f4bfSAmy Zhang 	 * region end (m_numOfHwPoints), not last HW point(m_numOfHwPoints - 1)
543fcd2f4bfSAmy Zhang 	 */
544eb0e5154SDmytro Laktyushkin 	y3_max = dc_fixpt_max(y_r, dc_fixpt_max(y_g, y_b));
545fcd2f4bfSAmy Zhang 
546fcd2f4bfSAmy Zhang 	arr_points[1].y = y3_max;
547fcd2f4bfSAmy Zhang 
548eb0e5154SDmytro Laktyushkin 	arr_points[1].slope = dc_fixpt_zero;
549fcd2f4bfSAmy Zhang 
550fcd2f4bfSAmy Zhang 	if (output_tf->tf == TRANSFER_FUNCTION_PQ) {
551fcd2f4bfSAmy Zhang 		/* for PQ, we want to have a straight line from last HW X point,
552fcd2f4bfSAmy Zhang 		 * and the slope to be such that we hit 1.0 at 10000 nits.
553fcd2f4bfSAmy Zhang 		 */
554eb0e5154SDmytro Laktyushkin 		const struct fixed31_32 end_value = dc_fixpt_from_int(125);
555fcd2f4bfSAmy Zhang 
556eb0e5154SDmytro Laktyushkin 		arr_points[1].slope = dc_fixpt_div(
557eb0e5154SDmytro Laktyushkin 				dc_fixpt_sub(dc_fixpt_one, arr_points[1].y),
558eb0e5154SDmytro Laktyushkin 				dc_fixpt_sub(end_value, arr_points[1].x));
559fcd2f4bfSAmy Zhang 	}
560fcd2f4bfSAmy Zhang 
561fcd2f4bfSAmy Zhang 	regamma_params->hw_points_num = hw_points;
562fcd2f4bfSAmy Zhang 
56369133b89SAric Cyr 	k = 0;
56469133b89SAric Cyr 	for (i = 1; i < 16; i++) {
565534db198SAmy Zhang 		if (seg_distr[k] != -1) {
566b310b081SHarry Wentland 			regamma_params->arr_curve_points[k].segments_num = seg_distr[k];
567534db198SAmy Zhang 			regamma_params->arr_curve_points[i].offset =
568b310b081SHarry Wentland 					regamma_params->arr_curve_points[k].offset + (1 << seg_distr[k]);
569fcd2f4bfSAmy Zhang 		}
57069133b89SAric Cyr 		k++;
571534db198SAmy Zhang 	}
572534db198SAmy Zhang 
573534db198SAmy Zhang 	if (seg_distr[k] != -1)
574b310b081SHarry Wentland 		regamma_params->arr_curve_points[k].segments_num = seg_distr[k];
575fcd2f4bfSAmy Zhang 
57623ae4f8eSAmy Zhang 	rgb = rgb_resulted;
57723ae4f8eSAmy Zhang 	rgb_plus_1 = rgb_resulted + 1;
578fcd2f4bfSAmy Zhang 
579fcd2f4bfSAmy Zhang 	i = 1;
580fcd2f4bfSAmy Zhang 
581fcd2f4bfSAmy Zhang 	while (i != hw_points + 1) {
582eb0e5154SDmytro Laktyushkin 		if (dc_fixpt_lt(rgb_plus_1->red, rgb->red))
583fcd2f4bfSAmy Zhang 			rgb_plus_1->red = rgb->red;
584eb0e5154SDmytro Laktyushkin 		if (dc_fixpt_lt(rgb_plus_1->green, rgb->green))
585fcd2f4bfSAmy Zhang 			rgb_plus_1->green = rgb->green;
586eb0e5154SDmytro Laktyushkin 		if (dc_fixpt_lt(rgb_plus_1->blue, rgb->blue))
587fcd2f4bfSAmy Zhang 			rgb_plus_1->blue = rgb->blue;
588fcd2f4bfSAmy Zhang 
589eb0e5154SDmytro Laktyushkin 		rgb->delta_red = dc_fixpt_sub(rgb_plus_1->red, rgb->red);
590eb0e5154SDmytro Laktyushkin 		rgb->delta_green = dc_fixpt_sub(rgb_plus_1->green, rgb->green);
591eb0e5154SDmytro Laktyushkin 		rgb->delta_blue = dc_fixpt_sub(rgb_plus_1->blue, rgb->blue);
592fcd2f4bfSAmy Zhang 
593fcd2f4bfSAmy Zhang 		++rgb_plus_1;
594fcd2f4bfSAmy Zhang 		++rgb;
595fcd2f4bfSAmy Zhang 		++i;
596fcd2f4bfSAmy Zhang 	}
597fcd2f4bfSAmy Zhang 
598fcd2f4bfSAmy Zhang 	convert_to_custom_float(rgb_resulted, arr_points, hw_points);
599fcd2f4bfSAmy Zhang 
600fcd2f4bfSAmy Zhang 	return true;
601fcd2f4bfSAmy Zhang }
602fcd2f4bfSAmy Zhang 
603a6114e85SHarry Wentland static bool
60478c77382SAnthony Koo dce110_set_output_transfer_func(struct dc *dc, struct pipe_ctx *pipe_ctx,
6050971c40eSHarry Wentland 				const struct dc_stream_state *stream)
60690e508baSAnthony Koo {
60786a66c4eSHarry Wentland 	struct transform *xfm = pipe_ctx->plane_res.xfm;
6084562236bSHarry Wentland 
6097a09f5beSYue Hin Lau 	xfm->funcs->opp_power_on_regamma_lut(xfm, true);
6107a09f5beSYue Hin Lau 	xfm->regamma_params.hw_points_num = GAMMA_HW_POINTS_NUM;
6114562236bSHarry Wentland 
6124fa086b9SLeo (Sunpeng) Li 	if (stream->out_transfer_func &&
613efd52204SHarry Wentland 	    stream->out_transfer_func->type == TF_TYPE_PREDEFINED &&
614efd52204SHarry Wentland 	    stream->out_transfer_func->tf == TRANSFER_FUNCTION_SRGB) {
6157a09f5beSYue Hin Lau 		xfm->funcs->opp_set_regamma_mode(xfm, OPP_REGAMMA_SRGB);
616efd52204SHarry Wentland 	} else if (dce110_translate_regamma_to_hw_format(stream->out_transfer_func,
617efd52204SHarry Wentland 							 &xfm->regamma_params)) {
6187a09f5beSYue Hin Lau 		xfm->funcs->opp_program_regamma_pwl(xfm, &xfm->regamma_params);
6197a09f5beSYue Hin Lau 		xfm->funcs->opp_set_regamma_mode(xfm, OPP_REGAMMA_USER);
6204562236bSHarry Wentland 	} else {
6217a09f5beSYue Hin Lau 		xfm->funcs->opp_set_regamma_mode(xfm, OPP_REGAMMA_BYPASS);
6224562236bSHarry Wentland 	}
6234562236bSHarry Wentland 
6247a09f5beSYue Hin Lau 	xfm->funcs->opp_power_on_regamma_lut(xfm, false);
6254562236bSHarry Wentland 
626cc0cb445SLeon Elazar 	return true;
6274562236bSHarry Wentland }
6284562236bSHarry Wentland 
6294562236bSHarry Wentland void dce110_update_info_frame(struct pipe_ctx *pipe_ctx)
6304562236bSHarry Wentland {
63102553f57SEric Bernstein 	bool is_hdmi_tmds;
6326f0db2dcSKrunoslav Kovac 	bool is_dp;
6336f0db2dcSKrunoslav Kovac 
63486e2e1beSHersen Wu 	ASSERT(pipe_ctx->stream);
63586e2e1beSHersen Wu 
6368e9c4c8cSHarry Wentland 	if (pipe_ctx->stream_res.stream_enc == NULL)
63786e2e1beSHersen Wu 		return;  /* this is not root pipe */
63886e2e1beSHersen Wu 
63902553f57SEric Bernstein 	is_hdmi_tmds = dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal);
6406f0db2dcSKrunoslav Kovac 	is_dp = dc_is_dp_signal(pipe_ctx->stream->signal);
6416f0db2dcSKrunoslav Kovac 
64202553f57SEric Bernstein 	if (!is_hdmi_tmds && !is_dp)
6436f0db2dcSKrunoslav Kovac 		return;
6446f0db2dcSKrunoslav Kovac 
64502553f57SEric Bernstein 	if (is_hdmi_tmds)
6468e9c4c8cSHarry Wentland 		pipe_ctx->stream_res.stream_enc->funcs->update_hdmi_info_packets(
6478e9c4c8cSHarry Wentland 			pipe_ctx->stream_res.stream_enc,
64896c50c0dSHarry Wentland 			&pipe_ctx->stream_res.encoder_info_frame);
6496f0db2dcSKrunoslav Kovac 	else
6508e9c4c8cSHarry Wentland 		pipe_ctx->stream_res.stream_enc->funcs->update_dp_info_packets(
6518e9c4c8cSHarry Wentland 			pipe_ctx->stream_res.stream_enc,
65296c50c0dSHarry Wentland 			&pipe_ctx->stream_res.encoder_info_frame);
6534562236bSHarry Wentland }
6544562236bSHarry Wentland 
6554562236bSHarry Wentland void dce110_enable_stream(struct pipe_ctx *pipe_ctx)
6564562236bSHarry Wentland {
6574562236bSHarry Wentland 	enum dc_lane_count lane_count =
658ceb3dbb4SJun Lei 		pipe_ctx->stream->link->cur_link_settings.lane_count;
6594fa086b9SLeo (Sunpeng) Li 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
660ceb3dbb4SJun Lei 	struct dc_link *link = pipe_ctx->stream->link;
661f42ea55bSAnthony Koo 	const struct dc *dc = link->dc;
662f215a57dSEric Yang 
6634562236bSHarry Wentland 	uint32_t active_total_with_borders;
6644562236bSHarry Wentland 	uint32_t early_control = 0;
6656b670fa9SHarry Wentland 	struct timing_generator *tg = pipe_ctx->stream_res.tg;
6664562236bSHarry Wentland 
667f215a57dSEric Yang 	/* For MST, there are multiply stream go to only one link.
668f215a57dSEric Yang 	 * connect DIG back_end to front_end while enable_stream and
669f215a57dSEric Yang 	 * disconnect them during disable_stream
670f215a57dSEric Yang 	 * BY this, it is logic clean to separate stream and link */
671f215a57dSEric Yang 	link->link_enc->funcs->connect_dig_be_to_fe(link->link_enc,
672f215a57dSEric Yang 						    pipe_ctx->stream_res.stream_enc->id, true);
673f215a57dSEric Yang 
674f42ea55bSAnthony Koo 	dc->hwss.update_info_frame(pipe_ctx);
675f215a57dSEric Yang 
6764562236bSHarry Wentland 	/* enable early control to avoid corruption on DP monitor*/
6774562236bSHarry Wentland 	active_total_with_borders =
6784562236bSHarry Wentland 			timing->h_addressable
6794562236bSHarry Wentland 				+ timing->h_border_left
6804562236bSHarry Wentland 				+ timing->h_border_right;
6814562236bSHarry Wentland 
6824562236bSHarry Wentland 	if (lane_count != 0)
6834562236bSHarry Wentland 		early_control = active_total_with_borders % lane_count;
6844562236bSHarry Wentland 
6854562236bSHarry Wentland 	if (early_control == 0)
6864562236bSHarry Wentland 		early_control = lane_count;
6874562236bSHarry Wentland 
6884562236bSHarry Wentland 	tg->funcs->set_early_control(tg, early_control);
6894562236bSHarry Wentland 
6904562236bSHarry Wentland 	/* enable audio only within mode set */
691afaacef4SHarry Wentland 	if (pipe_ctx->stream_res.audio != NULL) {
6924562236bSHarry Wentland 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
6938e9c4c8cSHarry Wentland 			pipe_ctx->stream_res.stream_enc->funcs->dp_audio_enable(pipe_ctx->stream_res.stream_enc);
6944562236bSHarry Wentland 	}
6954562236bSHarry Wentland 
696f215a57dSEric Yang 
697f215a57dSEric Yang 
6984562236bSHarry Wentland 
6994562236bSHarry Wentland }
7004562236bSHarry Wentland 
7015eefbc40SYue Hin Lau static enum bp_result link_transmitter_control(
70287401969SAndrew Jiang 		struct dc_bios *bios,
7035eefbc40SYue Hin Lau 	struct bp_transmitter_control *cntl)
7045eefbc40SYue Hin Lau {
7055eefbc40SYue Hin Lau 	enum bp_result result;
7065eefbc40SYue Hin Lau 
70787401969SAndrew Jiang 	result = bios->funcs->transmitter_control(bios, cntl);
7085eefbc40SYue Hin Lau 
7095eefbc40SYue Hin Lau 	return result;
7105eefbc40SYue Hin Lau }
7115eefbc40SYue Hin Lau 
71287401969SAndrew Jiang /*
71387401969SAndrew Jiang  * @brief
71487401969SAndrew Jiang  * eDP only.
71587401969SAndrew Jiang  */
7168a31820bSMartin Leung void dce110_edp_wait_for_hpd_ready(
717069d418fSAndrew Jiang 		struct dc_link *link,
71887401969SAndrew Jiang 		bool power_up)
71987401969SAndrew Jiang {
720069d418fSAndrew Jiang 	struct dc_context *ctx = link->ctx;
721069d418fSAndrew Jiang 	struct graphics_object_id connector = link->link_enc->connector;
72287401969SAndrew Jiang 	struct gpio *hpd;
7236798d042SLewis Huang 	struct dc_sink *sink = link->local_sink;
72487401969SAndrew Jiang 	bool edp_hpd_high = false;
72587401969SAndrew Jiang 	uint32_t time_elapsed = 0;
72687401969SAndrew Jiang 	uint32_t timeout = power_up ?
72787401969SAndrew Jiang 		PANEL_POWER_UP_TIMEOUT : PANEL_POWER_DOWN_TIMEOUT;
72887401969SAndrew Jiang 
72987401969SAndrew Jiang 	if (dal_graphics_object_id_get_connector_id(connector)
73087401969SAndrew Jiang 			!= CONNECTOR_ID_EDP) {
73187401969SAndrew Jiang 		BREAK_TO_DEBUGGER();
73287401969SAndrew Jiang 		return;
73387401969SAndrew Jiang 	}
73487401969SAndrew Jiang 
73587401969SAndrew Jiang 	if (!power_up)
73687401969SAndrew Jiang 		/*
73787401969SAndrew Jiang 		 * From KV, we will not HPD low after turning off VCC -
73887401969SAndrew Jiang 		 * instead, we will check the SW timer in power_up().
73987401969SAndrew Jiang 		 */
74087401969SAndrew Jiang 		return;
74187401969SAndrew Jiang 
74287401969SAndrew Jiang 	/*
74387401969SAndrew Jiang 	 * When we power on/off the eDP panel,
74487401969SAndrew Jiang 	 * we need to wait until SENSE bit is high/low.
74587401969SAndrew Jiang 	 */
74687401969SAndrew Jiang 
74787401969SAndrew Jiang 	/* obtain HPD */
74887401969SAndrew Jiang 	/* TODO what to do with this? */
74987401969SAndrew Jiang 	hpd = get_hpd_gpio(ctx->dc_bios, connector, ctx->gpio_service);
75087401969SAndrew Jiang 
75187401969SAndrew Jiang 	if (!hpd) {
75287401969SAndrew Jiang 		BREAK_TO_DEBUGGER();
75387401969SAndrew Jiang 		return;
75487401969SAndrew Jiang 	}
75587401969SAndrew Jiang 
7566798d042SLewis Huang 	if (sink != NULL) {
7576798d042SLewis Huang 		if (sink->edid_caps.panel_patch.extra_t3_ms > 0) {
7586798d042SLewis Huang 			int extra_t3_in_ms = sink->edid_caps.panel_patch.extra_t3_ms;
7596798d042SLewis Huang 
7606798d042SLewis Huang 			msleep(extra_t3_in_ms);
7616798d042SLewis Huang 		}
7626798d042SLewis Huang 	}
7636798d042SLewis Huang 
76487401969SAndrew Jiang 	dal_gpio_open(hpd, GPIO_MODE_INTERRUPT);
76587401969SAndrew Jiang 
76687401969SAndrew Jiang 	/* wait until timeout or panel detected */
76787401969SAndrew Jiang 
76887401969SAndrew Jiang 	do {
76987401969SAndrew Jiang 		uint32_t detected = 0;
77087401969SAndrew Jiang 
77187401969SAndrew Jiang 		dal_gpio_get_value(hpd, &detected);
77287401969SAndrew Jiang 
77387401969SAndrew Jiang 		if (!(detected ^ power_up)) {
77487401969SAndrew Jiang 			edp_hpd_high = true;
77587401969SAndrew Jiang 			break;
77687401969SAndrew Jiang 		}
77787401969SAndrew Jiang 
77887401969SAndrew Jiang 		msleep(HPD_CHECK_INTERVAL);
77987401969SAndrew Jiang 
78087401969SAndrew Jiang 		time_elapsed += HPD_CHECK_INTERVAL;
78187401969SAndrew Jiang 	} while (time_elapsed < timeout);
78287401969SAndrew Jiang 
78387401969SAndrew Jiang 	dal_gpio_close(hpd);
78487401969SAndrew Jiang 
78587401969SAndrew Jiang 	dal_gpio_destroy_irq(&hpd);
78687401969SAndrew Jiang 
78787401969SAndrew Jiang 	if (false == edp_hpd_high) {
7881296423bSBhawanpreet Lakha 		DC_LOG_ERROR(
78987401969SAndrew Jiang 				"%s: wait timed out!\n", __func__);
79087401969SAndrew Jiang 	}
79187401969SAndrew Jiang }
79287401969SAndrew Jiang 
7938a31820bSMartin Leung void dce110_edp_power_control(
794069d418fSAndrew Jiang 		struct dc_link *link,
79587401969SAndrew Jiang 		bool power_up)
79687401969SAndrew Jiang {
797069d418fSAndrew Jiang 	struct dc_context *ctx = link->ctx;
79887401969SAndrew Jiang 	struct bp_transmitter_control cntl = { 0 };
79987401969SAndrew Jiang 	enum bp_result bp_result;
800*06ddcee4SJake Wang 	uint8_t panel_instance;
80187401969SAndrew Jiang 
80287401969SAndrew Jiang 
803069d418fSAndrew Jiang 	if (dal_graphics_object_id_get_connector_id(link->link_enc->connector)
80487401969SAndrew Jiang 			!= CONNECTOR_ID_EDP) {
80587401969SAndrew Jiang 		BREAK_TO_DEBUGGER();
80687401969SAndrew Jiang 		return;
80787401969SAndrew Jiang 	}
80887401969SAndrew Jiang 
809ffadb9d6SAnthony Koo 	if (!link->panel_cntl)
810904fb6e0SAnthony Koo 		return;
811d4caa72eSAnthony Koo 	if (power_up !=
812d4caa72eSAnthony Koo 		link->panel_cntl->funcs->is_panel_powered_on(link->panel_cntl)) {
813172c9b77SAshley Thomas 
81478d5d04dSCharlene Liu 		unsigned long long current_ts = dm_get_timestamp(ctx);
815172c9b77SAshley Thomas 		unsigned long long time_since_edp_poweroff_ms =
81693ed1814SHugo Hu 				div64_u64(dm_get_elapse_time_in_ns(
81778d5d04dSCharlene Liu 						ctx,
81878d5d04dSCharlene Liu 						current_ts,
81993ed1814SHugo Hu 						link->link_trace.time_stamp.edp_poweroff), 1000000);
820172c9b77SAshley Thomas 		unsigned long long time_since_edp_poweron_ms =
821172c9b77SAshley Thomas 				div64_u64(dm_get_elapse_time_in_ns(
822172c9b77SAshley Thomas 						ctx,
823172c9b77SAshley Thomas 						current_ts,
824172c9b77SAshley Thomas 						link->link_trace.time_stamp.edp_poweron), 1000000);
825172c9b77SAshley Thomas 		DC_LOG_HW_RESUME_S3(
826172c9b77SAshley Thomas 				"%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",
827172c9b77SAshley Thomas 				__func__,
828172c9b77SAshley Thomas 				power_up,
829172c9b77SAshley Thomas 				current_ts,
830172c9b77SAshley Thomas 				link->link_trace.time_stamp.edp_poweroff,
831172c9b77SAshley Thomas 				link->link_trace.time_stamp.edp_poweron,
832172c9b77SAshley Thomas 				time_since_edp_poweroff_ms,
833172c9b77SAshley Thomas 				time_since_edp_poweron_ms);
83478d5d04dSCharlene Liu 
835172c9b77SAshley Thomas 		/* Send VBIOS command to prompt eDP panel power */
836172c9b77SAshley Thomas 		if (power_up) {
837172c9b77SAshley Thomas 			/* edp requires a min of 500ms from LCDVDD off to on */
838172c9b77SAshley Thomas 			unsigned long long remaining_min_edp_poweroff_time_ms = 500;
839ff587987SHugo Hu 
840172c9b77SAshley Thomas 			/* add time defined by a patch, if any (usually patch extra_t12_ms is 0) */
8416c4fff06SYue Hin Lau 			if (link->local_sink != NULL)
842172c9b77SAshley Thomas 				remaining_min_edp_poweroff_time_ms +=
843172c9b77SAshley Thomas 					link->local_sink->edid_caps.panel_patch.extra_t12_ms;
84478d5d04dSCharlene Liu 
845172c9b77SAshley Thomas 			/* Adjust remaining_min_edp_poweroff_time_ms if this is not the first time. */
846172c9b77SAshley Thomas 			if (link->link_trace.time_stamp.edp_poweroff != 0) {
847172c9b77SAshley Thomas 				if (time_since_edp_poweroff_ms < remaining_min_edp_poweroff_time_ms)
848172c9b77SAshley Thomas 					remaining_min_edp_poweroff_time_ms =
849172c9b77SAshley Thomas 						remaining_min_edp_poweroff_time_ms - time_since_edp_poweroff_ms;
850172c9b77SAshley Thomas 				else
851172c9b77SAshley Thomas 					remaining_min_edp_poweroff_time_ms = 0;
85278d5d04dSCharlene Liu 			}
85378d5d04dSCharlene Liu 
854172c9b77SAshley Thomas 			if (remaining_min_edp_poweroff_time_ms) {
855172c9b77SAshley Thomas 				DC_LOG_HW_RESUME_S3(
856172c9b77SAshley Thomas 						"%s: remaining_min_edp_poweroff_time_ms=%llu: begin wait.\n",
857172c9b77SAshley Thomas 						__func__, remaining_min_edp_poweroff_time_ms);
858172c9b77SAshley Thomas 				msleep(remaining_min_edp_poweroff_time_ms);
859172c9b77SAshley Thomas 				DC_LOG_HW_RESUME_S3(
860172c9b77SAshley Thomas 						"%s: remaining_min_edp_poweroff_time_ms=%llu: end wait.\n",
861172c9b77SAshley Thomas 						__func__, remaining_min_edp_poweroff_time_ms);
862172c9b77SAshley Thomas 				dm_output_to_console("%s: wait %lld ms to power on eDP.\n",
863172c9b77SAshley Thomas 						__func__, remaining_min_edp_poweroff_time_ms);
864172c9b77SAshley Thomas 			} else {
865172c9b77SAshley Thomas 				DC_LOG_HW_RESUME_S3(
866172c9b77SAshley Thomas 						"%s: remaining_min_edp_poweroff_time_ms=%llu: no wait required.\n",
867172c9b77SAshley Thomas 						__func__, remaining_min_edp_poweroff_time_ms);
868172c9b77SAshley Thomas 			}
86978d5d04dSCharlene Liu 		}
87087401969SAndrew Jiang 
8711296423bSBhawanpreet Lakha 		DC_LOG_HW_RESUME_S3(
872172c9b77SAshley Thomas 				"%s: BEGIN: Panel Power action: %s\n",
87387401969SAndrew Jiang 				__func__, (power_up ? "On":"Off"));
87487401969SAndrew Jiang 
87587401969SAndrew Jiang 		cntl.action = power_up ?
87687401969SAndrew Jiang 			TRANSMITTER_CONTROL_POWER_ON :
87787401969SAndrew Jiang 			TRANSMITTER_CONTROL_POWER_OFF;
878069d418fSAndrew Jiang 		cntl.transmitter = link->link_enc->transmitter;
879069d418fSAndrew Jiang 		cntl.connector_obj_id = link->link_enc->connector;
88087401969SAndrew Jiang 		cntl.coherent = false;
88187401969SAndrew Jiang 		cntl.lanes_number = LANE_COUNT_FOUR;
882069d418fSAndrew Jiang 		cntl.hpd_sel = link->link_enc->hpd_source;
883*06ddcee4SJake Wang 		panel_instance = link->panel_cntl->inst;
8848a0e210cSChris Park 
8858a0e210cSChris Park 		if (ctx->dc->ctx->dmub_srv &&
8868a0e210cSChris Park 				ctx->dc->debug.dmub_command_table) {
8878a0e210cSChris Park 			if (cntl.action == TRANSMITTER_CONTROL_POWER_ON)
8888a0e210cSChris Park 				bp_result = ctx->dc_bios->funcs->enable_lvtma_control(ctx->dc_bios,
889*06ddcee4SJake Wang 						LVTMA_CONTROL_POWER_ON,
890*06ddcee4SJake Wang 						panel_instance);
8918a0e210cSChris Park 			else
8928a0e210cSChris Park 				bp_result = ctx->dc_bios->funcs->enable_lvtma_control(ctx->dc_bios,
893*06ddcee4SJake Wang 						LVTMA_CONTROL_POWER_OFF,
894*06ddcee4SJake Wang 						panel_instance);
8958a0e210cSChris Park 		}
8968a0e210cSChris Park 
89787401969SAndrew Jiang 		bp_result = link_transmitter_control(ctx->dc_bios, &cntl);
89887401969SAndrew Jiang 
899172c9b77SAshley Thomas 		DC_LOG_HW_RESUME_S3(
900172c9b77SAshley Thomas 				"%s: END: Panel Power action: %s bp_result=%u\n",
901172c9b77SAshley Thomas 				__func__, (power_up ? "On":"Off"),
902172c9b77SAshley Thomas 				bp_result);
903172c9b77SAshley Thomas 
90478d5d04dSCharlene Liu 		if (!power_up)
90578d5d04dSCharlene Liu 			/*save driver power off time stamp*/
90678d5d04dSCharlene Liu 			link->link_trace.time_stamp.edp_poweroff = dm_get_timestamp(ctx);
90778d5d04dSCharlene Liu 		else
90878d5d04dSCharlene Liu 			link->link_trace.time_stamp.edp_poweron = dm_get_timestamp(ctx);
90978d5d04dSCharlene Liu 
910172c9b77SAshley Thomas 		DC_LOG_HW_RESUME_S3(
911172c9b77SAshley Thomas 				"%s: updated values: edp_poweroff=%llu edp_poweron=%llu\n",
912172c9b77SAshley Thomas 				__func__,
913172c9b77SAshley Thomas 				link->link_trace.time_stamp.edp_poweroff,
914172c9b77SAshley Thomas 				link->link_trace.time_stamp.edp_poweron);
915172c9b77SAshley Thomas 
91687401969SAndrew Jiang 		if (bp_result != BP_RESULT_OK)
9171296423bSBhawanpreet Lakha 			DC_LOG_ERROR(
91887401969SAndrew Jiang 					"%s: Panel Power bp_result: %d\n",
91987401969SAndrew Jiang 					__func__, bp_result);
92087401969SAndrew Jiang 	} else {
9211296423bSBhawanpreet Lakha 		DC_LOG_HW_RESUME_S3(
92287401969SAndrew Jiang 				"%s: Skipping Panel Power action: %s\n",
92387401969SAndrew Jiang 				__func__, (power_up ? "On":"Off"));
92487401969SAndrew Jiang 	}
92587401969SAndrew Jiang }
9265eefbc40SYue Hin Lau 
927cf3a2627SJun Lei void dce110_edp_wait_for_T12(
928cf3a2627SJun Lei 		struct dc_link *link)
929cf3a2627SJun Lei {
930cf3a2627SJun Lei 	struct dc_context *ctx = link->ctx;
931cf3a2627SJun Lei 
932cf3a2627SJun Lei 	if (dal_graphics_object_id_get_connector_id(link->link_enc->connector)
933cf3a2627SJun Lei 			!= CONNECTOR_ID_EDP) {
934cf3a2627SJun Lei 		BREAK_TO_DEBUGGER();
935cf3a2627SJun Lei 		return;
936cf3a2627SJun Lei 	}
937cf3a2627SJun Lei 
938cf3a2627SJun Lei 	if (!link->panel_cntl)
939cf3a2627SJun Lei 		return;
940cf3a2627SJun Lei 
941cf3a2627SJun Lei 	if (!link->panel_cntl->funcs->is_panel_powered_on(link->panel_cntl) &&
942cf3a2627SJun Lei 			link->link_trace.time_stamp.edp_poweroff != 0) {
943cf3a2627SJun Lei 		unsigned int t12_duration = 500; // Default T12 as per spec
944cf3a2627SJun Lei 		unsigned long long current_ts = dm_get_timestamp(ctx);
945cf3a2627SJun Lei 		unsigned long long time_since_edp_poweroff_ms =
946cf3a2627SJun Lei 				div64_u64(dm_get_elapse_time_in_ns(
947cf3a2627SJun Lei 						ctx,
948cf3a2627SJun Lei 						current_ts,
949cf3a2627SJun Lei 						link->link_trace.time_stamp.edp_poweroff), 1000000);
950cf3a2627SJun Lei 
951cf3a2627SJun Lei 		t12_duration += link->local_sink->edid_caps.panel_patch.extra_t12_ms; // Add extra T12
952cf3a2627SJun Lei 
953cf3a2627SJun Lei 		if (time_since_edp_poweroff_ms < t12_duration)
954cf3a2627SJun Lei 			msleep(t12_duration - time_since_edp_poweroff_ms);
955cf3a2627SJun Lei 	}
956cf3a2627SJun Lei }
957cf3a2627SJun Lei 
9585eefbc40SYue Hin Lau /*todo: cloned in stream enc, fix*/
9595eefbc40SYue Hin Lau /*
9605eefbc40SYue Hin Lau  * @brief
9615eefbc40SYue Hin Lau  * eDP only. Control the backlight of the eDP panel
9625eefbc40SYue Hin Lau  */
9638a31820bSMartin Leung void dce110_edp_backlight_control(
9645eefbc40SYue Hin Lau 		struct dc_link *link,
9655eefbc40SYue Hin Lau 		bool enable)
9665eefbc40SYue Hin Lau {
967069d418fSAndrew Jiang 	struct dc_context *ctx = link->ctx;
9685eefbc40SYue Hin Lau 	struct bp_transmitter_control cntl = { 0 };
969*06ddcee4SJake Wang 	uint8_t panel_instance;
9705eefbc40SYue Hin Lau 
971069d418fSAndrew Jiang 	if (dal_graphics_object_id_get_connector_id(link->link_enc->connector)
9725eefbc40SYue Hin Lau 		!= CONNECTOR_ID_EDP) {
9735eefbc40SYue Hin Lau 		BREAK_TO_DEBUGGER();
9745eefbc40SYue Hin Lau 		return;
9755eefbc40SYue Hin Lau 	}
9765eefbc40SYue Hin Lau 
977014427adSSherry 	if (link->panel_cntl) {
978014427adSSherry 		bool is_backlight_on = link->panel_cntl->funcs->is_panel_backlight_on(link->panel_cntl);
979014427adSSherry 
980014427adSSherry 		if ((enable && is_backlight_on) || (!enable && !is_backlight_on)) {
9811296423bSBhawanpreet Lakha 			DC_LOG_HW_RESUME_S3(
982014427adSSherry 				"%s: panel already powered up/off. Do nothing.\n",
9835eefbc40SYue Hin Lau 				__func__);
9845eefbc40SYue Hin Lau 			return;
9855eefbc40SYue Hin Lau 		}
986014427adSSherry 	}
9875eefbc40SYue Hin Lau 
9885eefbc40SYue Hin Lau 	/* Send VBIOS command to control eDP panel backlight */
9895eefbc40SYue Hin Lau 
9901296423bSBhawanpreet Lakha 	DC_LOG_HW_RESUME_S3(
9915eefbc40SYue Hin Lau 			"%s: backlight action: %s\n",
9925eefbc40SYue Hin Lau 			__func__, (enable ? "On":"Off"));
9935eefbc40SYue Hin Lau 
9945eefbc40SYue Hin Lau 	cntl.action = enable ?
9955eefbc40SYue Hin Lau 		TRANSMITTER_CONTROL_BACKLIGHT_ON :
9965eefbc40SYue Hin Lau 		TRANSMITTER_CONTROL_BACKLIGHT_OFF;
99787401969SAndrew Jiang 
9985eefbc40SYue Hin Lau 	/*cntl.engine_id = ctx->engine;*/
9995eefbc40SYue Hin Lau 	cntl.transmitter = link->link_enc->transmitter;
10005eefbc40SYue Hin Lau 	cntl.connector_obj_id = link->link_enc->connector;
10015eefbc40SYue Hin Lau 	/*todo: unhardcode*/
10025eefbc40SYue Hin Lau 	cntl.lanes_number = LANE_COUNT_FOUR;
10035eefbc40SYue Hin Lau 	cntl.hpd_sel = link->link_enc->hpd_source;
1004cf1835f0SCharlene Liu 	cntl.signal = SIGNAL_TYPE_EDP;
10055eefbc40SYue Hin Lau 
10065eefbc40SYue Hin Lau 	/* For eDP, the following delays might need to be considered
10075eefbc40SYue Hin Lau 	 * after link training completed:
10085eefbc40SYue Hin Lau 	 * idle period - min. accounts for required BS-Idle pattern,
10095eefbc40SYue Hin Lau 	 * max. allows for source frame synchronization);
10105eefbc40SYue Hin Lau 	 * 50 msec max. delay from valid video data from source
10115eefbc40SYue Hin Lau 	 * to video on dislpay or backlight enable.
10125eefbc40SYue Hin Lau 	 *
10135eefbc40SYue Hin Lau 	 * Disable the delay for now.
10145eefbc40SYue Hin Lau 	 * Enable it in the future if necessary.
10155eefbc40SYue Hin Lau 	 */
10165eefbc40SYue Hin Lau 	/* dc_service_sleep_in_milliseconds(50); */
10175180d4a4SCharlene Liu 		/*edp 1.2*/
1018*06ddcee4SJake Wang 	panel_instance = link->panel_cntl->inst;
10195180d4a4SCharlene Liu 	if (cntl.action == TRANSMITTER_CONTROL_BACKLIGHT_ON)
10205180d4a4SCharlene Liu 		edp_receiver_ready_T7(link);
10218a0e210cSChris Park 
10228a0e210cSChris Park 	if (ctx->dc->ctx->dmub_srv &&
10238a0e210cSChris Park 			ctx->dc->debug.dmub_command_table) {
10248a0e210cSChris Park 		if (cntl.action == TRANSMITTER_CONTROL_BACKLIGHT_ON)
10258a0e210cSChris Park 			ctx->dc_bios->funcs->enable_lvtma_control(ctx->dc_bios,
1026*06ddcee4SJake Wang 					LVTMA_CONTROL_LCD_BLON,
1027*06ddcee4SJake Wang 					panel_instance);
10288a0e210cSChris Park 		else
10298a0e210cSChris Park 			ctx->dc_bios->funcs->enable_lvtma_control(ctx->dc_bios,
1030*06ddcee4SJake Wang 					LVTMA_CONTROL_LCD_BLOFF,
1031*06ddcee4SJake Wang 					panel_instance);
10328a0e210cSChris Park 	}
10338a0e210cSChris Park 
1034069d418fSAndrew Jiang 	link_transmitter_control(ctx->dc_bios, &cntl);
103596577cf8SHersen Wu 
103696577cf8SHersen Wu 	if (enable && link->dpcd_sink_ext_caps.bits.oled)
103796577cf8SHersen Wu 		msleep(OLED_POST_T7_DELAY);
103896577cf8SHersen Wu 
103996577cf8SHersen Wu 	if (link->dpcd_sink_ext_caps.bits.oled ||
104096577cf8SHersen Wu 		link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1 ||
104196577cf8SHersen Wu 		link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1)
104296577cf8SHersen Wu 		dc_link_backlight_enable_aux(link, enable);
104396577cf8SHersen Wu 
104469b9723aSCharlene Liu 	/*edp 1.2*/
10455180d4a4SCharlene Liu 	if (cntl.action == TRANSMITTER_CONTROL_BACKLIGHT_OFF)
10463a372bedSHugo Hu 		edp_add_delay_for_T9(link);
104796577cf8SHersen Wu 
104896577cf8SHersen Wu 	if (!enable && link->dpcd_sink_ext_caps.bits.oled)
104996577cf8SHersen Wu 		msleep(OLED_PRE_T11_DELAY);
10505eefbc40SYue Hin Lau }
10515eefbc40SYue Hin Lau 
10521a05873fSAnthony Koo void dce110_enable_audio_stream(struct pipe_ctx *pipe_ctx)
10534562236bSHarry Wentland {
10541a05873fSAnthony Koo 	/* notify audio driver for audio modes of monitor */
10552b77dcc5SAnthony Koo 	struct dc *dc;
10561d0610bcSAlvin Lee 	struct clk_mgr *clk_mgr;
10571a05873fSAnthony Koo 	unsigned int i, num_audio = 1;
10581a05873fSAnthony Koo 
10591d0610bcSAlvin Lee 	if (!pipe_ctx->stream)
10601d0610bcSAlvin Lee 		return;
10611d0610bcSAlvin Lee 
10622b77dcc5SAnthony Koo 	dc = pipe_ctx->stream->ctx->dc;
10632b77dcc5SAnthony Koo 	clk_mgr = dc->clk_mgr;
10641d0610bcSAlvin Lee 
10650a32df9cSEryk Brol 	if (pipe_ctx->stream_res.audio && pipe_ctx->stream_res.audio->enabled == true)
10660a32df9cSEryk Brol 		return;
10670a32df9cSEryk Brol 
10681a05873fSAnthony Koo 	if (pipe_ctx->stream_res.audio) {
10691a05873fSAnthony Koo 		for (i = 0; i < MAX_PIPES; i++) {
10701a05873fSAnthony Koo 			/*current_state not updated yet*/
10712b77dcc5SAnthony Koo 			if (dc->current_state->res_ctx.pipe_ctx[i].stream_res.audio != NULL)
10721a05873fSAnthony Koo 				num_audio++;
10731a05873fSAnthony Koo 		}
10741a05873fSAnthony Koo 
10751a05873fSAnthony Koo 		pipe_ctx->stream_res.audio->funcs->az_enable(pipe_ctx->stream_res.audio);
10761a05873fSAnthony Koo 
1077170a2398SSu Sung Chung 		if (num_audio >= 1 && clk_mgr->funcs->enable_pme_wa)
10781a05873fSAnthony Koo 			/*this is the first audio. apply the PME w/a in order to wake AZ from D3*/
1079170a2398SSu Sung Chung 			clk_mgr->funcs->enable_pme_wa(clk_mgr);
10801a05873fSAnthony Koo 		/* un-mute audio */
10811a05873fSAnthony Koo 		/* TODO: audio should be per stream rather than per link */
10821a05873fSAnthony Koo 		pipe_ctx->stream_res.stream_enc->funcs->audio_mute_control(
10831a05873fSAnthony Koo 					pipe_ctx->stream_res.stream_enc, false);
10840a32df9cSEryk Brol 		if (pipe_ctx->stream_res.audio)
10850a32df9cSEryk Brol 			pipe_ctx->stream_res.audio->enabled = true;
10861a05873fSAnthony Koo 	}
10871a05873fSAnthony Koo }
10881a05873fSAnthony Koo 
108957430404SSu Sung Chung void dce110_disable_audio_stream(struct pipe_ctx *pipe_ctx)
10901a05873fSAnthony Koo {
10911d0610bcSAlvin Lee 	struct dc *dc;
10921d0610bcSAlvin Lee 	struct clk_mgr *clk_mgr;
10931d0610bcSAlvin Lee 
10941d0610bcSAlvin Lee 	if (!pipe_ctx || !pipe_ctx->stream)
10951d0610bcSAlvin Lee 		return;
10961d0610bcSAlvin Lee 
10971d0610bcSAlvin Lee 	dc = pipe_ctx->stream->ctx->dc;
10981d0610bcSAlvin Lee 	clk_mgr = dc->clk_mgr;
10994562236bSHarry Wentland 
11000a32df9cSEryk Brol 	if (pipe_ctx->stream_res.audio && pipe_ctx->stream_res.audio->enabled == false)
11010a32df9cSEryk Brol 		return;
11020a32df9cSEryk Brol 
11032b7c97d6SCharlene Liu 	pipe_ctx->stream_res.stream_enc->funcs->audio_mute_control(
11042b7c97d6SCharlene Liu 			pipe_ctx->stream_res.stream_enc, true);
1105afaacef4SHarry Wentland 	if (pipe_ctx->stream_res.audio) {
11063f52aa9fSNicholas Kazlauskas 		pipe_ctx->stream_res.audio->enabled = false;
11073f52aa9fSNicholas Kazlauskas 
11084562236bSHarry Wentland 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
11098e9c4c8cSHarry Wentland 			pipe_ctx->stream_res.stream_enc->funcs->dp_audio_disable(
11108e9c4c8cSHarry Wentland 					pipe_ctx->stream_res.stream_enc);
11114562236bSHarry Wentland 		else
11128e9c4c8cSHarry Wentland 			pipe_ctx->stream_res.stream_enc->funcs->hdmi_audio_disable(
11138e9c4c8cSHarry Wentland 					pipe_ctx->stream_res.stream_enc);
111457430404SSu Sung Chung 
1115170a2398SSu Sung Chung 		if (clk_mgr->funcs->enable_pme_wa)
1116070fe724SCharlene Liu 			/*this is the first audio. apply the PME w/a in order to wake AZ from D3*/
1117170a2398SSu Sung Chung 			clk_mgr->funcs->enable_pme_wa(clk_mgr);
11184562236bSHarry Wentland 
11194562236bSHarry Wentland 		/* TODO: notify audio driver for if audio modes list changed
11204562236bSHarry Wentland 		 * add audio mode list change flag */
11214562236bSHarry Wentland 		/* dal_audio_disable_azalia_audio_jack_presence(stream->audio,
11224562236bSHarry Wentland 		 * stream->stream_engine_id);
11234562236bSHarry Wentland 		 */
11244562236bSHarry Wentland 	}
11251a05873fSAnthony Koo }
11264562236bSHarry Wentland 
112757430404SSu Sung Chung void dce110_disable_stream(struct pipe_ctx *pipe_ctx)
11281a05873fSAnthony Koo {
11291a05873fSAnthony Koo 	struct dc_stream_state *stream = pipe_ctx->stream;
1130ceb3dbb4SJun Lei 	struct dc_link *link = stream->link;
11311a05873fSAnthony Koo 	struct dc *dc = pipe_ctx->stream->ctx->dc;
11321a05873fSAnthony Koo 
1133ac42fd63SWenjing Liu 	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal)) {
11341a05873fSAnthony Koo 		pipe_ctx->stream_res.stream_enc->funcs->stop_hdmi_info_packets(
11351a05873fSAnthony Koo 			pipe_ctx->stream_res.stream_enc);
1136ac42fd63SWenjing Liu 		pipe_ctx->stream_res.stream_enc->funcs->hdmi_reset_stream_attribute(
1137ac42fd63SWenjing Liu 			pipe_ctx->stream_res.stream_enc);
1138ac42fd63SWenjing Liu 	}
11391a05873fSAnthony Koo 
11401a05873fSAnthony Koo 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
11411a05873fSAnthony Koo 		pipe_ctx->stream_res.stream_enc->funcs->stop_dp_info_packets(
11421a05873fSAnthony Koo 			pipe_ctx->stream_res.stream_enc);
11431a05873fSAnthony Koo 
114457430404SSu Sung Chung 	dc->hwss.disable_audio_stream(pipe_ctx);
1145904623eeSYongqiang Sun 
11464562236bSHarry Wentland 	link->link_enc->funcs->connect_dig_be_to_fe(
11474562236bSHarry Wentland 			link->link_enc,
11488e9c4c8cSHarry Wentland 			pipe_ctx->stream_res.stream_enc->id,
11494562236bSHarry Wentland 			false);
11504562236bSHarry Wentland 
11514562236bSHarry Wentland }
11524562236bSHarry Wentland 
11534562236bSHarry Wentland void dce110_unblank_stream(struct pipe_ctx *pipe_ctx,
11544562236bSHarry Wentland 		struct dc_link_settings *link_settings)
11554562236bSHarry Wentland {
11564562236bSHarry Wentland 	struct encoder_unblank_param params = { { 0 } };
115741b49742SCharlene Liu 	struct dc_stream_state *stream = pipe_ctx->stream;
1158ceb3dbb4SJun Lei 	struct dc_link *link = stream->link;
1159f42ea55bSAnthony Koo 	struct dce_hwseq *hws = link->dc->hwseq;
11604562236bSHarry Wentland 
11614562236bSHarry Wentland 	/* only 3 items below are used by unblank */
11627fe538a4SCharlene Liu 	params.timing = pipe_ctx->stream->timing;
11634562236bSHarry Wentland 	params.link_settings.link_rate = link_settings->link_rate;
116441b49742SCharlene Liu 
116541b49742SCharlene Liu 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
11668e9c4c8cSHarry Wentland 		pipe_ctx->stream_res.stream_enc->funcs->dp_unblank(pipe_ctx->stream_res.stream_enc, &params);
116741b49742SCharlene Liu 
116814d6f644SYongqiang Sun 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1169f42ea55bSAnthony Koo 		hws->funcs.edp_backlight_control(link, true);
117014d6f644SYongqiang Sun 	}
117141b49742SCharlene Liu }
11722c850b7bSDmytro Laktyushkin 
117341b49742SCharlene Liu void dce110_blank_stream(struct pipe_ctx *pipe_ctx)
117441b49742SCharlene Liu {
117541b49742SCharlene Liu 	struct dc_stream_state *stream = pipe_ctx->stream;
1176ceb3dbb4SJun Lei 	struct dc_link *link = stream->link;
1177f42ea55bSAnthony Koo 	struct dce_hwseq *hws = link->dc->hwseq;
117841b49742SCharlene Liu 
1179ab892598SRoman Li 	if (link->local_sink && link->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
1180f42ea55bSAnthony Koo 		hws->funcs.edp_backlight_control(link, false);
11813ba01817SYongqiang Sun 		link->dc->hwss.set_abm_immediate_disable(pipe_ctx);
1182ab892598SRoman Li 	}
118341b49742SCharlene Liu 
1184eec3303dSAric Cyr 	if (dc_is_dp_signal(pipe_ctx->stream->signal)) {
118541b49742SCharlene Liu 		pipe_ctx->stream_res.stream_enc->funcs->dp_blank(pipe_ctx->stream_res.stream_enc);
1186eec3303dSAric Cyr 
11873a372bedSHugo Hu 		if (!dc_is_embedded_signal(pipe_ctx->stream->signal)) {
1188eec3303dSAric Cyr 			/*
1189eec3303dSAric Cyr 			 * After output is idle pattern some sinks need time to recognize the stream
1190eec3303dSAric Cyr 			 * has changed or they enter protection state and hang.
1191eec3303dSAric Cyr 			 */
1192eec3303dSAric Cyr 			msleep(60);
11933a372bedSHugo Hu 		} else if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP)
11943a372bedSHugo Hu 			edp_receiver_ready_T9(link);
1195eec3303dSAric Cyr 	}
1196eec3303dSAric Cyr 
11974562236bSHarry Wentland }
11984562236bSHarry Wentland 
119915e17335SCharlene Liu 
120015e17335SCharlene Liu void dce110_set_avmute(struct pipe_ctx *pipe_ctx, bool enable)
120115e17335SCharlene Liu {
12028e9c4c8cSHarry Wentland 	if (pipe_ctx != NULL && pipe_ctx->stream_res.stream_enc != NULL)
12038e9c4c8cSHarry Wentland 		pipe_ctx->stream_res.stream_enc->funcs->set_avmute(pipe_ctx->stream_res.stream_enc, enable);
120415e17335SCharlene Liu }
120515e17335SCharlene Liu 
12064562236bSHarry Wentland static enum audio_dto_source translate_to_dto_source(enum controller_id crtc_id)
12074562236bSHarry Wentland {
12084562236bSHarry Wentland 	switch (crtc_id) {
12094562236bSHarry Wentland 	case CONTROLLER_ID_D0:
12104562236bSHarry Wentland 		return DTO_SOURCE_ID0;
12114562236bSHarry Wentland 	case CONTROLLER_ID_D1:
12124562236bSHarry Wentland 		return DTO_SOURCE_ID1;
12134562236bSHarry Wentland 	case CONTROLLER_ID_D2:
12144562236bSHarry Wentland 		return DTO_SOURCE_ID2;
12154562236bSHarry Wentland 	case CONTROLLER_ID_D3:
12164562236bSHarry Wentland 		return DTO_SOURCE_ID3;
12174562236bSHarry Wentland 	case CONTROLLER_ID_D4:
12184562236bSHarry Wentland 		return DTO_SOURCE_ID4;
12194562236bSHarry Wentland 	case CONTROLLER_ID_D5:
12204562236bSHarry Wentland 		return DTO_SOURCE_ID5;
12214562236bSHarry Wentland 	default:
12224562236bSHarry Wentland 		return DTO_SOURCE_UNKNOWN;
12234562236bSHarry Wentland 	}
12244562236bSHarry Wentland }
12254562236bSHarry Wentland 
12264562236bSHarry Wentland static void build_audio_output(
1227ab8db3e1SAndrey Grodzovsky 	struct dc_state *state,
12284562236bSHarry Wentland 	const struct pipe_ctx *pipe_ctx,
12294562236bSHarry Wentland 	struct audio_output *audio_output)
12304562236bSHarry Wentland {
12310971c40eSHarry Wentland 	const struct dc_stream_state *stream = pipe_ctx->stream;
12328e9c4c8cSHarry Wentland 	audio_output->engine_id = pipe_ctx->stream_res.stream_enc->id;
12334562236bSHarry Wentland 
12344562236bSHarry Wentland 	audio_output->signal = pipe_ctx->stream->signal;
12354562236bSHarry Wentland 
12364562236bSHarry Wentland 	/* audio_crtc_info  */
12374562236bSHarry Wentland 
12384562236bSHarry Wentland 	audio_output->crtc_info.h_total =
12394fa086b9SLeo (Sunpeng) Li 		stream->timing.h_total;
12404562236bSHarry Wentland 
12414562236bSHarry Wentland 	/*
12424562236bSHarry Wentland 	 * Audio packets are sent during actual CRTC blank physical signal, we
12434562236bSHarry Wentland 	 * need to specify actual active signal portion
12444562236bSHarry Wentland 	 */
12454562236bSHarry Wentland 	audio_output->crtc_info.h_active =
12464fa086b9SLeo (Sunpeng) Li 			stream->timing.h_addressable
12474fa086b9SLeo (Sunpeng) Li 			+ stream->timing.h_border_left
12484fa086b9SLeo (Sunpeng) Li 			+ stream->timing.h_border_right;
12494562236bSHarry Wentland 
12504562236bSHarry Wentland 	audio_output->crtc_info.v_active =
12514fa086b9SLeo (Sunpeng) Li 			stream->timing.v_addressable
12524fa086b9SLeo (Sunpeng) Li 			+ stream->timing.v_border_top
12534fa086b9SLeo (Sunpeng) Li 			+ stream->timing.v_border_bottom;
12544562236bSHarry Wentland 
12554562236bSHarry Wentland 	audio_output->crtc_info.pixel_repetition = 1;
12564562236bSHarry Wentland 
12574562236bSHarry Wentland 	audio_output->crtc_info.interlaced =
12584fa086b9SLeo (Sunpeng) Li 			stream->timing.flags.INTERLACE;
12594562236bSHarry Wentland 
12604562236bSHarry Wentland 	audio_output->crtc_info.refresh_rate =
126140fd9090SNevenko Stupar 		(stream->timing.pix_clk_100hz*100)/
12624fa086b9SLeo (Sunpeng) Li 		(stream->timing.h_total*stream->timing.v_total);
12634562236bSHarry Wentland 
12644562236bSHarry Wentland 	audio_output->crtc_info.color_depth =
12654fa086b9SLeo (Sunpeng) Li 		stream->timing.display_color_depth;
12664562236bSHarry Wentland 
126740fd9090SNevenko Stupar 	audio_output->crtc_info.requested_pixel_clock_100Hz =
126840fd9090SNevenko Stupar 			pipe_ctx->stream_res.pix_clk_params.requested_pix_clk_100hz;
12694562236bSHarry Wentland 
127040fd9090SNevenko Stupar 	audio_output->crtc_info.calculated_pixel_clock_100Hz =
127140fd9090SNevenko Stupar 			pipe_ctx->stream_res.pix_clk_params.requested_pix_clk_100hz;
12724562236bSHarry Wentland 
127387b58768SCharlene Liu /*for HDMI, audio ACR is with deep color ratio factor*/
12742166d9fbSCharlene Liu 	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal) &&
127540fd9090SNevenko Stupar 		audio_output->crtc_info.requested_pixel_clock_100Hz ==
127640fd9090SNevenko Stupar 				(stream->timing.pix_clk_100hz)) {
127710688217SHarry Wentland 		if (pipe_ctx->stream_res.pix_clk_params.pixel_encoding == PIXEL_ENCODING_YCBCR420) {
127840fd9090SNevenko Stupar 			audio_output->crtc_info.requested_pixel_clock_100Hz =
127940fd9090SNevenko Stupar 					audio_output->crtc_info.requested_pixel_clock_100Hz/2;
128040fd9090SNevenko Stupar 			audio_output->crtc_info.calculated_pixel_clock_100Hz =
128140fd9090SNevenko Stupar 					pipe_ctx->stream_res.pix_clk_params.requested_pix_clk_100hz/2;
128287b58768SCharlene Liu 
128387b58768SCharlene Liu 		}
128487b58768SCharlene Liu 	}
128587b58768SCharlene Liu 
1286ed476602SAhzo 	if (state->clk_mgr &&
1287ed476602SAhzo 		(pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT ||
1288ed476602SAhzo 			pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)) {
12894562236bSHarry Wentland 		audio_output->pll_info.dp_dto_source_clock_in_khz =
12900de34efcSDmytro Laktyushkin 				state->clk_mgr->funcs->get_dp_ref_clk_frequency(
12910de34efcSDmytro Laktyushkin 						state->clk_mgr);
12924562236bSHarry Wentland 	}
12934562236bSHarry Wentland 
12944562236bSHarry Wentland 	audio_output->pll_info.feed_back_divider =
12954562236bSHarry Wentland 			pipe_ctx->pll_settings.feedback_divider;
12964562236bSHarry Wentland 
12974562236bSHarry Wentland 	audio_output->pll_info.dto_source =
12984562236bSHarry Wentland 		translate_to_dto_source(
1299e07f541fSYongqiang Sun 			pipe_ctx->stream_res.tg->inst + 1);
13004562236bSHarry Wentland 
13014562236bSHarry Wentland 	/* TODO hard code to enable for now. Need get from stream */
13024562236bSHarry Wentland 	audio_output->pll_info.ss_enabled = true;
13034562236bSHarry Wentland 
13044562236bSHarry Wentland 	audio_output->pll_info.ss_percentage =
13054562236bSHarry Wentland 			pipe_ctx->pll_settings.ss_percentage;
13064562236bSHarry Wentland }
13074562236bSHarry Wentland 
13084562236bSHarry Wentland static void get_surface_visual_confirm_color(const struct pipe_ctx *pipe_ctx,
13094562236bSHarry Wentland 		struct tg_color *color)
13104562236bSHarry Wentland {
13112a54bd6eSJerry (Fangzhi) Zuo 	uint32_t color_value = MAX_TG_COLOR_VALUE * (4 - pipe_ctx->stream_res.tg->inst) / 4;
13124562236bSHarry Wentland 
13136702a9acSHarry Wentland 	switch (pipe_ctx->plane_res.scl_data.format) {
13144562236bSHarry Wentland 	case PIXEL_FORMAT_ARGB8888:
13154562236bSHarry Wentland 		/* set boarder color to red */
13164562236bSHarry Wentland 		color->color_r_cr = color_value;
13174562236bSHarry Wentland 		break;
13184562236bSHarry Wentland 
13194562236bSHarry Wentland 	case PIXEL_FORMAT_ARGB2101010:
13204562236bSHarry Wentland 		/* set boarder color to blue */
13214562236bSHarry Wentland 		color->color_b_cb = color_value;
13224562236bSHarry Wentland 		break;
132387449a90SAnthony Koo 	case PIXEL_FORMAT_420BPP8:
13244562236bSHarry Wentland 		/* set boarder color to green */
13254562236bSHarry Wentland 		color->color_g_y = color_value;
13264562236bSHarry Wentland 		break;
132787449a90SAnthony Koo 	case PIXEL_FORMAT_420BPP10:
132887449a90SAnthony Koo 		/* set boarder color to yellow */
132987449a90SAnthony Koo 		color->color_g_y = color_value;
133087449a90SAnthony Koo 		color->color_r_cr = color_value;
133187449a90SAnthony Koo 		break;
13324562236bSHarry Wentland 	case PIXEL_FORMAT_FP16:
13334562236bSHarry Wentland 		/* set boarder color to white */
13344562236bSHarry Wentland 		color->color_r_cr = color_value;
13354562236bSHarry Wentland 		color->color_b_cb = color_value;
13364562236bSHarry Wentland 		color->color_g_y = color_value;
13374562236bSHarry Wentland 		break;
13384562236bSHarry Wentland 	default:
13394562236bSHarry Wentland 		break;
13404562236bSHarry Wentland 	}
13414562236bSHarry Wentland }
13424562236bSHarry Wentland 
1343fb3466a4SBhawanpreet Lakha static void program_scaler(const struct dc *dc,
13444562236bSHarry Wentland 		const struct pipe_ctx *pipe_ctx)
13454562236bSHarry Wentland {
13464562236bSHarry Wentland 	struct tg_color color = {0};
13474562236bSHarry Wentland 
1348b86a1aa3SBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC_DCN)
1349ff5ef992SAlex Deucher 	/* TOFPGA */
135086a66c4eSHarry Wentland 	if (pipe_ctx->plane_res.xfm->funcs->transform_set_pixel_storage_depth == NULL)
1351ff5ef992SAlex Deucher 		return;
1352ff5ef992SAlex Deucher #endif
1353ff5ef992SAlex Deucher 
1354bf53769dSGloria Li 	if (dc->debug.visual_confirm == VISUAL_CONFIRM_SURFACE)
13554562236bSHarry Wentland 		get_surface_visual_confirm_color(pipe_ctx, &color);
13564562236bSHarry Wentland 	else
13574562236bSHarry Wentland 		color_space_to_black_color(dc,
13584fa086b9SLeo (Sunpeng) Li 				pipe_ctx->stream->output_color_space,
13594562236bSHarry Wentland 				&color);
13604562236bSHarry Wentland 
136186a66c4eSHarry Wentland 	pipe_ctx->plane_res.xfm->funcs->transform_set_pixel_storage_depth(
136286a66c4eSHarry Wentland 		pipe_ctx->plane_res.xfm,
13636702a9acSHarry Wentland 		pipe_ctx->plane_res.scl_data.lb_params.depth,
13644562236bSHarry Wentland 		&pipe_ctx->stream->bit_depth_params);
13654562236bSHarry Wentland 
136612750d16SEric Yang 	if (pipe_ctx->stream_res.tg->funcs->set_overscan_blank_color) {
136712750d16SEric Yang 		/*
136812750d16SEric Yang 		 * The way 420 is packed, 2 channels carry Y component, 1 channel
136912750d16SEric Yang 		 * alternate between Cb and Cr, so both channels need the pixel
137012750d16SEric Yang 		 * value for Y
137112750d16SEric Yang 		 */
137212750d16SEric Yang 		if (pipe_ctx->stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420)
137312750d16SEric Yang 			color.color_r_cr = color.color_g_y;
137412750d16SEric Yang 
13756b670fa9SHarry Wentland 		pipe_ctx->stream_res.tg->funcs->set_overscan_blank_color(
13766b670fa9SHarry Wentland 				pipe_ctx->stream_res.tg,
13774562236bSHarry Wentland 				&color);
137812750d16SEric Yang 	}
13794562236bSHarry Wentland 
138086a66c4eSHarry Wentland 	pipe_ctx->plane_res.xfm->funcs->transform_set_scaler(pipe_ctx->plane_res.xfm,
13816702a9acSHarry Wentland 		&pipe_ctx->plane_res.scl_data);
13824562236bSHarry Wentland }
13834562236bSHarry Wentland 
13843158223eSEric Bernstein static enum dc_status dce110_enable_stream_timing(
13854562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx,
1386608ac7bbSJerry Zuo 		struct dc_state *context,
1387fb3466a4SBhawanpreet Lakha 		struct dc *dc)
13884562236bSHarry Wentland {
13890971c40eSHarry Wentland 	struct dc_stream_state *stream = pipe_ctx->stream;
1390608ac7bbSJerry Zuo 	struct pipe_ctx *pipe_ctx_old = &dc->current_state->res_ctx.
13914562236bSHarry Wentland 			pipe_ctx[pipe_ctx->pipe_idx];
13924562236bSHarry Wentland 	struct tg_color black_color = {0};
13934562236bSHarry Wentland 
13944562236bSHarry Wentland 	if (!pipe_ctx_old->stream) {
13954562236bSHarry Wentland 
13964562236bSHarry Wentland 		/* program blank color */
13974562236bSHarry Wentland 		color_space_to_black_color(dc,
13984fa086b9SLeo (Sunpeng) Li 				stream->output_color_space, &black_color);
13996b670fa9SHarry Wentland 		pipe_ctx->stream_res.tg->funcs->set_blank_color(
14006b670fa9SHarry Wentland 				pipe_ctx->stream_res.tg,
14014562236bSHarry Wentland 				&black_color);
14024b5e7d62SHersen Wu 
14034562236bSHarry Wentland 		/*
14044562236bSHarry Wentland 		 * Must blank CRTC after disabling power gating and before any
14054562236bSHarry Wentland 		 * programming, otherwise CRTC will be hung in bad state
14064562236bSHarry Wentland 		 */
14076b670fa9SHarry Wentland 		pipe_ctx->stream_res.tg->funcs->set_blank(pipe_ctx->stream_res.tg, true);
14084562236bSHarry Wentland 
14094562236bSHarry Wentland 		if (false == pipe_ctx->clock_source->funcs->program_pix_clk(
14104562236bSHarry Wentland 				pipe_ctx->clock_source,
141110688217SHarry Wentland 				&pipe_ctx->stream_res.pix_clk_params,
14124562236bSHarry Wentland 				&pipe_ctx->pll_settings)) {
14134562236bSHarry Wentland 			BREAK_TO_DEBUGGER();
14144562236bSHarry Wentland 			return DC_ERROR_UNEXPECTED;
14154562236bSHarry Wentland 		}
14164562236bSHarry Wentland 
14176b670fa9SHarry Wentland 		pipe_ctx->stream_res.tg->funcs->program_timing(
14186b670fa9SHarry Wentland 				pipe_ctx->stream_res.tg,
14194fa086b9SLeo (Sunpeng) Li 				&stream->timing,
1420e7e10c46SDmytro Laktyushkin 				0,
1421e7e10c46SDmytro Laktyushkin 				0,
1422e7e10c46SDmytro Laktyushkin 				0,
1423e7e10c46SDmytro Laktyushkin 				0,
1424e7e10c46SDmytro Laktyushkin 				pipe_ctx->stream->signal,
14254562236bSHarry Wentland 				true);
14264562236bSHarry Wentland 	}
14274562236bSHarry Wentland 
14284562236bSHarry Wentland 	if (!pipe_ctx_old->stream) {
14296b670fa9SHarry Wentland 		if (false == pipe_ctx->stream_res.tg->funcs->enable_crtc(
14306b670fa9SHarry Wentland 				pipe_ctx->stream_res.tg)) {
14314562236bSHarry Wentland 			BREAK_TO_DEBUGGER();
14324562236bSHarry Wentland 			return DC_ERROR_UNEXPECTED;
14334562236bSHarry Wentland 		}
14344562236bSHarry Wentland 	}
14354562236bSHarry Wentland 
14364562236bSHarry Wentland 	return DC_OK;
14374562236bSHarry Wentland }
14384562236bSHarry Wentland 
14394562236bSHarry Wentland static enum dc_status apply_single_controller_ctx_to_hw(
14404562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx,
1441608ac7bbSJerry Zuo 		struct dc_state *context,
1442fb3466a4SBhawanpreet Lakha 		struct dc *dc)
14434562236bSHarry Wentland {
14440971c40eSHarry Wentland 	struct dc_stream_state *stream = pipe_ctx->stream;
14459c0fb8d4SAnthony Koo 	struct drr_params params = {0};
14469c0fb8d4SAnthony Koo 	unsigned int event_triggers = 0;
1447b1f6d01cSDmytro Laktyushkin 	struct pipe_ctx *odm_pipe = pipe_ctx->next_odm_pipe;
1448f42ea55bSAnthony Koo 	struct dce_hwseq *hws = dc->hwseq;
14494562236bSHarry Wentland 
1450f42ea55bSAnthony Koo 	if (hws->funcs.disable_stream_gating) {
1451f42ea55bSAnthony Koo 		hws->funcs.disable_stream_gating(dc, pipe_ctx);
1452240d09d0SGary Kattan 	}
1453240d09d0SGary Kattan 
14541a05873fSAnthony Koo 	if (pipe_ctx->stream_res.audio != NULL) {
14551a05873fSAnthony Koo 		struct audio_output audio_output;
14561a05873fSAnthony Koo 
14571a05873fSAnthony Koo 		build_audio_output(context, pipe_ctx, &audio_output);
14581a05873fSAnthony Koo 
14591a05873fSAnthony Koo 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
14601a05873fSAnthony Koo 			pipe_ctx->stream_res.stream_enc->funcs->dp_audio_setup(
14611a05873fSAnthony Koo 					pipe_ctx->stream_res.stream_enc,
14621a05873fSAnthony Koo 					pipe_ctx->stream_res.audio->inst,
14631a05873fSAnthony Koo 					&pipe_ctx->stream->audio_info);
14641a05873fSAnthony Koo 		else
14651a05873fSAnthony Koo 			pipe_ctx->stream_res.stream_enc->funcs->hdmi_audio_setup(
14661a05873fSAnthony Koo 					pipe_ctx->stream_res.stream_enc,
14671a05873fSAnthony Koo 					pipe_ctx->stream_res.audio->inst,
14681a05873fSAnthony Koo 					&pipe_ctx->stream->audio_info,
14691a05873fSAnthony Koo 					&audio_output.crtc_info);
14701a05873fSAnthony Koo 
14711a05873fSAnthony Koo 		pipe_ctx->stream_res.audio->funcs->az_configure(
14721a05873fSAnthony Koo 				pipe_ctx->stream_res.audio,
14731a05873fSAnthony Koo 				pipe_ctx->stream->signal,
14741a05873fSAnthony Koo 				&audio_output.crtc_info,
14751a05873fSAnthony Koo 				&pipe_ctx->stream->audio_info);
14761a05873fSAnthony Koo 	}
14771a05873fSAnthony Koo 
14784562236bSHarry Wentland 	/*  */
1479d2d7885fSAnthony Koo 	/* Do not touch stream timing on seamless boot optimization. */
1480d2d7885fSAnthony Koo 	if (!pipe_ctx->stream->apply_seamless_boot_optimization)
1481f42ea55bSAnthony Koo 		hws->funcs.enable_stream_timing(pipe_ctx, context, dc);
14824562236bSHarry Wentland 
1483f42ea55bSAnthony Koo 	if (hws->funcs.setup_vupdate_interrupt)
1484f42ea55bSAnthony Koo 		hws->funcs.setup_vupdate_interrupt(dc, pipe_ctx);
1485a122b62dSAnthony Koo 
14869c0fb8d4SAnthony Koo 	params.vertical_total_min = stream->adjust.v_total_min;
14879c0fb8d4SAnthony Koo 	params.vertical_total_max = stream->adjust.v_total_max;
14889c0fb8d4SAnthony Koo 	if (pipe_ctx->stream_res.tg->funcs->set_drr)
14899c0fb8d4SAnthony Koo 		pipe_ctx->stream_res.tg->funcs->set_drr(
14909c0fb8d4SAnthony Koo 			pipe_ctx->stream_res.tg, &params);
14919c0fb8d4SAnthony Koo 
14929c0fb8d4SAnthony Koo 	// DRR should set trigger event to monitor surface update event
14939c0fb8d4SAnthony Koo 	if (stream->adjust.v_total_min != 0 && stream->adjust.v_total_max != 0)
14949c0fb8d4SAnthony Koo 		event_triggers = 0x80;
14955b5abe95SAnthony Koo 	/* Event triggers and num frames initialized for DRR, but can be
14965b5abe95SAnthony Koo 	 * later updated for PSR use. Note DRR trigger events are generated
14975b5abe95SAnthony Koo 	 * regardless of whether num frames met.
14985b5abe95SAnthony Koo 	 */
14999c0fb8d4SAnthony Koo 	if (pipe_ctx->stream_res.tg->funcs->set_static_screen_control)
15009c0fb8d4SAnthony Koo 		pipe_ctx->stream_res.tg->funcs->set_static_screen_control(
15015b5abe95SAnthony Koo 				pipe_ctx->stream_res.tg, event_triggers, 2);
15029c0fb8d4SAnthony Koo 
1503248cbed6SEric Bernstein 	if (!dc_is_virtual_signal(pipe_ctx->stream->signal))
1504d2c460e7Shersen wu 		pipe_ctx->stream_res.stream_enc->funcs->dig_connect_to_otg(
1505d2c460e7Shersen wu 			pipe_ctx->stream_res.stream_enc,
1506d2c460e7Shersen wu 			pipe_ctx->stream_res.tg->inst);
1507aa9c4abeSNikola Cornij 
1508f0c4d997SCorbin McElhanney 	pipe_ctx->stream_res.opp->funcs->opp_set_dyn_expansion(
1509f0c4d997SCorbin McElhanney 			pipe_ctx->stream_res.opp,
1510f0c4d997SCorbin McElhanney 			COLOR_SPACE_YCBCR601,
1511f0c4d997SCorbin McElhanney 			stream->timing.display_color_depth,
1512661a8cd9SDmytro Laktyushkin 			stream->signal);
15134562236bSHarry Wentland 
1514a6a6cb34SHarry Wentland 	pipe_ctx->stream_res.opp->funcs->opp_program_fmt(
1515a6a6cb34SHarry Wentland 		pipe_ctx->stream_res.opp,
1516181a888fSCharlene Liu 		&stream->bit_depth_params,
1517181a888fSCharlene Liu 		&stream->clamping);
1518b1f6d01cSDmytro Laktyushkin 	while (odm_pipe) {
15197ed4e635SHarry Wentland 		odm_pipe->stream_res.opp->funcs->opp_set_dyn_expansion(
15207ed4e635SHarry Wentland 				odm_pipe->stream_res.opp,
15217ed4e635SHarry Wentland 				COLOR_SPACE_YCBCR601,
15227ed4e635SHarry Wentland 				stream->timing.display_color_depth,
15237ed4e635SHarry Wentland 				stream->signal);
15247ed4e635SHarry Wentland 
15257ed4e635SHarry Wentland 		odm_pipe->stream_res.opp->funcs->opp_program_fmt(
15267ed4e635SHarry Wentland 				odm_pipe->stream_res.opp,
15277ed4e635SHarry Wentland 				&stream->bit_depth_params,
15287ed4e635SHarry Wentland 				&stream->clamping);
1529b1f6d01cSDmytro Laktyushkin 		odm_pipe = odm_pipe->next_odm_pipe;
15307ed4e635SHarry Wentland 	}
1531603767f9STony Cheng 
15321e7e86c4SSamson Tam 	if (!stream->dpms_off)
1533ab8db3e1SAndrey Grodzovsky 		core_link_enable_stream(context, pipe_ctx);
15344562236bSHarry Wentland 
15356702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.lb_params.alpha_en = pipe_ctx->bottom_pipe != 0;
15364562236bSHarry Wentland 
1537d1ebfdd8SWyatt Wood 	pipe_ctx->stream->link->psr_settings.psr_feature_enabled = false;
153894267b3dSSylvia Tsai 
15394562236bSHarry Wentland 	return DC_OK;
15404562236bSHarry Wentland }
15414562236bSHarry Wentland 
15424562236bSHarry Wentland /******************************************************************************/
15434562236bSHarry Wentland 
1544fb3466a4SBhawanpreet Lakha static void power_down_encoders(struct dc *dc)
15454562236bSHarry Wentland {
15464562236bSHarry Wentland 	int i;
1547b9b171ffSHersen Wu 
1548b9b171ffSHersen Wu 	/* do not know BIOS back-front mapping, simply blank all. It will not
1549b9b171ffSHersen Wu 	 * hurt for non-DP
1550b9b171ffSHersen Wu 	 */
1551b9b171ffSHersen Wu 	for (i = 0; i < dc->res_pool->stream_enc_count; i++) {
1552b9b171ffSHersen Wu 		dc->res_pool->stream_enc[i]->funcs->dp_blank(
1553b9b171ffSHersen Wu 					dc->res_pool->stream_enc[i]);
1554b9b171ffSHersen Wu 	}
1555b9b171ffSHersen Wu 
15564562236bSHarry Wentland 	for (i = 0; i < dc->link_count; i++) {
1557d4c2a96fSLewis Huang 		enum signal_type signal = dc->links[i]->connector_signal;
1558a0c38ebaSCharlene Liu 
1559d4c2a96fSLewis Huang 		if ((signal == SIGNAL_TYPE_EDP) ||
1560d4c2a96fSLewis Huang 			(signal == SIGNAL_TYPE_DISPLAY_PORT))
1561a0c38ebaSCharlene Liu 			if (!dc->links[i]->wa_flags.dp_keep_receiver_powered)
1562a0c38ebaSCharlene Liu 				dp_receiver_power_ctrl(dc->links[i], false);
1563a0c38ebaSCharlene Liu 
15644338ffa8SSung Lee 		if (signal != SIGNAL_TYPE_EDP)
15654338ffa8SSung Lee 			signal = SIGNAL_TYPE_NONE;
15664338ffa8SSung Lee 
15674562236bSHarry Wentland 		dc->links[i]->link_enc->funcs->disable_output(
1568069d418fSAndrew Jiang 				dc->links[i]->link_enc, signal);
1569b56e90eaSPaul Hsieh 
1570b56e90eaSPaul Hsieh 		dc->links[i]->link_status.link_active = false;
157107920450SJoshua Aberback 		memset(&dc->links[i]->cur_link_settings, 0,
157207920450SJoshua Aberback 				sizeof(dc->links[i]->cur_link_settings));
15734562236bSHarry Wentland 	}
15744562236bSHarry Wentland }
15754562236bSHarry Wentland 
1576fb3466a4SBhawanpreet Lakha static void power_down_controllers(struct dc *dc)
15774562236bSHarry Wentland {
15784562236bSHarry Wentland 	int i;
15794562236bSHarry Wentland 
15807f93c1deSCharlene Liu 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
15814562236bSHarry Wentland 		dc->res_pool->timing_generators[i]->funcs->disable_crtc(
15824562236bSHarry Wentland 				dc->res_pool->timing_generators[i]);
15834562236bSHarry Wentland 	}
15844562236bSHarry Wentland }
15854562236bSHarry Wentland 
1586fb3466a4SBhawanpreet Lakha static void power_down_clock_sources(struct dc *dc)
15874562236bSHarry Wentland {
15884562236bSHarry Wentland 	int i;
15894562236bSHarry Wentland 
15904562236bSHarry Wentland 	if (dc->res_pool->dp_clock_source->funcs->cs_power_down(
15914562236bSHarry Wentland 		dc->res_pool->dp_clock_source) == false)
15924562236bSHarry Wentland 		dm_error("Failed to power down pll! (dp clk src)\n");
15934562236bSHarry Wentland 
15944562236bSHarry Wentland 	for (i = 0; i < dc->res_pool->clk_src_count; i++) {
15954562236bSHarry Wentland 		if (dc->res_pool->clock_sources[i]->funcs->cs_power_down(
15964562236bSHarry Wentland 				dc->res_pool->clock_sources[i]) == false)
15974562236bSHarry Wentland 			dm_error("Failed to power down pll! (clk src index=%d)\n", i);
15984562236bSHarry Wentland 	}
15994562236bSHarry Wentland }
16004562236bSHarry Wentland 
1601fb3466a4SBhawanpreet Lakha static void power_down_all_hw_blocks(struct dc *dc)
16024562236bSHarry Wentland {
16034562236bSHarry Wentland 	power_down_encoders(dc);
16044562236bSHarry Wentland 
16054562236bSHarry Wentland 	power_down_controllers(dc);
16064562236bSHarry Wentland 
16074562236bSHarry Wentland 	power_down_clock_sources(dc);
16081663ae1cSBhawanpreet Lakha 
16092f3bfb27SRoman Li 	if (dc->fbc_compressor)
16101663ae1cSBhawanpreet Lakha 		dc->fbc_compressor->funcs->disable_fbc(dc->fbc_compressor);
16114562236bSHarry Wentland }
16124562236bSHarry Wentland 
16134562236bSHarry Wentland static void disable_vga_and_power_gate_all_controllers(
1614fb3466a4SBhawanpreet Lakha 		struct dc *dc)
16154562236bSHarry Wentland {
16164562236bSHarry Wentland 	int i;
16174562236bSHarry Wentland 	struct timing_generator *tg;
16184562236bSHarry Wentland 	struct dc_context *ctx = dc->ctx;
16194562236bSHarry Wentland 
16207f93c1deSCharlene Liu 	for (i = 0; i < dc->res_pool->timing_generator_count; i++) {
16214562236bSHarry Wentland 		tg = dc->res_pool->timing_generators[i];
16224562236bSHarry Wentland 
16230a87425aSTony Cheng 		if (tg->funcs->disable_vga)
16244562236bSHarry Wentland 			tg->funcs->disable_vga(tg);
16257f93c1deSCharlene Liu 	}
16267f93c1deSCharlene Liu 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
16274562236bSHarry Wentland 		/* Enable CLOCK gating for each pipe BEFORE controller
16284562236bSHarry Wentland 		 * powergating. */
16294562236bSHarry Wentland 		enable_display_pipe_clock_gating(ctx,
16304562236bSHarry Wentland 				true);
16314562236bSHarry Wentland 
1632e6c258cbSYongqiang Sun 		dc->current_state->res_ctx.pipe_ctx[i].pipe_idx = i;
16337f914a62SYongqiang Sun 		dc->hwss.disable_plane(dc,
1634e6c258cbSYongqiang Sun 			&dc->current_state->res_ctx.pipe_ctx[i]);
16354562236bSHarry Wentland 	}
16364562236bSHarry Wentland }
16374562236bSHarry Wentland 
16383de5aa81SSivapiriyanKumarasamy 
163945a1261bSJake Wang static void get_edp_streams(struct dc_state *context,
164045a1261bSJake Wang 		struct dc_stream_state **edp_streams,
164145a1261bSJake Wang 		int *edp_stream_num)
16423de5aa81SSivapiriyanKumarasamy {
16433de5aa81SSivapiriyanKumarasamy 	int i;
16443de5aa81SSivapiriyanKumarasamy 
164545a1261bSJake Wang 	*edp_stream_num = 0;
16463de5aa81SSivapiriyanKumarasamy 	for (i = 0; i < context->stream_count; i++) {
164745a1261bSJake Wang 		if (context->streams[i]->signal == SIGNAL_TYPE_EDP) {
164845a1261bSJake Wang 			edp_streams[*edp_stream_num] = context->streams[i];
164945a1261bSJake Wang 			if (++(*edp_stream_num) == MAX_NUM_EDP)
165045a1261bSJake Wang 				return;
16513de5aa81SSivapiriyanKumarasamy 		}
165245a1261bSJake Wang 	}
16533de5aa81SSivapiriyanKumarasamy }
16543de5aa81SSivapiriyanKumarasamy 
165545a1261bSJake Wang static void get_edp_links_with_sink(
165625292028SYongqiang Sun 		struct dc *dc,
165745a1261bSJake Wang 		struct dc_link **edp_links_with_sink,
165845a1261bSJake Wang 		int *edp_with_sink_num)
165925292028SYongqiang Sun {
166025292028SYongqiang Sun 	int i;
166125292028SYongqiang Sun 
166225292028SYongqiang Sun 	/* check if there is an eDP panel not in use */
166345a1261bSJake Wang 	*edp_with_sink_num = 0;
166425292028SYongqiang Sun 	for (i = 0; i < dc->link_count; i++) {
166525292028SYongqiang Sun 		if (dc->links[i]->local_sink &&
166625292028SYongqiang Sun 			dc->links[i]->local_sink->sink_signal == SIGNAL_TYPE_EDP) {
166745a1261bSJake Wang 			edp_links_with_sink[*edp_with_sink_num] = dc->links[i];
166845a1261bSJake Wang 			if (++(*edp_with_sink_num) == MAX_NUM_EDP)
166945a1261bSJake Wang 				return;
167025292028SYongqiang Sun 		}
167125292028SYongqiang Sun 	}
167225292028SYongqiang Sun }
167325292028SYongqiang Sun 
16741c17952eSLee Jones /*
16754562236bSHarry Wentland  * When ASIC goes from VBIOS/VGA mode to driver/accelerated mode we need:
16764562236bSHarry Wentland  *  1. Power down all DC HW blocks
16774562236bSHarry Wentland  *  2. Disable VGA engine on all controllers
16784562236bSHarry Wentland  *  3. Enable power gating for controller
16794562236bSHarry Wentland  *  4. Set acc_mode_change bit (VBIOS will clear this bit when going to FSDOS)
16804562236bSHarry Wentland  */
168125292028SYongqiang Sun void dce110_enable_accelerated_mode(struct dc *dc, struct dc_state *context)
16824562236bSHarry Wentland {
168345a1261bSJake Wang 	struct dc_link *edp_links_with_sink[MAX_NUM_EDP];
168445a1261bSJake Wang 	struct dc_link *edp_links[MAX_NUM_EDP];
168545a1261bSJake Wang 	struct dc_stream_state *edp_streams[MAX_NUM_EDP];
168645a1261bSJake Wang 	struct dc_link *edp_link_with_sink = NULL;
168745a1261bSJake Wang 	struct dc_link *edp_link = NULL;
16883de5aa81SSivapiriyanKumarasamy 	struct dc_stream_state *edp_stream = NULL;
168945a1261bSJake Wang 	struct dce_hwseq *hws = dc->hwseq;
169045a1261bSJake Wang 	int edp_with_sink_num;
169145a1261bSJake Wang 	int edp_num;
169245a1261bSJake Wang 	int edp_stream_num;
169345a1261bSJake Wang 	int i;
1694be4b289fSSivapiriyanKumarasamy 	bool can_apply_edp_fast_boot = false;
1695ce72741bSAnthony Koo 	bool can_apply_seamless_boot = false;
16963de5aa81SSivapiriyanKumarasamy 	bool keep_edp_vdd_on = false;
169745a1261bSJake Wang 
169845a1261bSJake Wang 	get_edp_links_with_sink(dc, edp_links_with_sink, &edp_with_sink_num);
169945a1261bSJake Wang 	get_edp_links(dc, edp_links, &edp_num);
1700ce72741bSAnthony Koo 
1701f42ea55bSAnthony Koo 	if (hws->funcs.init_pipes)
1702f42ea55bSAnthony Koo 		hws->funcs.init_pipes(dc, context);
1703be4b289fSSivapiriyanKumarasamy 
170445a1261bSJake Wang 	get_edp_streams(context, edp_streams, &edp_stream_num);
17053de5aa81SSivapiriyanKumarasamy 
1706be4b289fSSivapiriyanKumarasamy 	// Check fastboot support, disable on DCE8 because of blank screens
170745a1261bSJake Wang 	if (edp_num && dc->ctx->dce_version != DCE_VERSION_8_0 &&
1708be4b289fSSivapiriyanKumarasamy 		    dc->ctx->dce_version != DCE_VERSION_8_1 &&
1709be4b289fSSivapiriyanKumarasamy 		    dc->ctx->dce_version != DCE_VERSION_8_3) {
171045a1261bSJake Wang 		for (i = 0; i < edp_num; i++) {
171145a1261bSJake Wang 			edp_link = edp_links[i];
1712be4b289fSSivapiriyanKumarasamy 			// enable fastboot if backend is enabled on eDP
1713be4b289fSSivapiriyanKumarasamy 			if (edp_link->link_enc->funcs->is_dig_enabled(edp_link->link_enc)) {
17143de5aa81SSivapiriyanKumarasamy 				/* Set optimization flag on eDP stream*/
171545a1261bSJake Wang 				if (edp_stream_num && edp_link->link_status.link_active) {
171645a1261bSJake Wang 					edp_stream = edp_streams[0];
17173de5aa81SSivapiriyanKumarasamy 					edp_stream->apply_edp_fast_boot_optimization = true;
1718be4b289fSSivapiriyanKumarasamy 					can_apply_edp_fast_boot = true;
171945a1261bSJake Wang 					break;
1720be4b289fSSivapiriyanKumarasamy 				}
1721be4b289fSSivapiriyanKumarasamy 			}
172245a1261bSJake Wang 		}
17233de5aa81SSivapiriyanKumarasamy 		// We are trying to enable eDP, don't power down VDD
172445a1261bSJake Wang 		if (edp_stream_num)
17253de5aa81SSivapiriyanKumarasamy 			keep_edp_vdd_on = true;
1726be4b289fSSivapiriyanKumarasamy 	}
1727be4b289fSSivapiriyanKumarasamy 
1728be4b289fSSivapiriyanKumarasamy 	// Check seamless boot support
1729ce72741bSAnthony Koo 	for (i = 0; i < context->stream_count; i++) {
1730ce72741bSAnthony Koo 		if (context->streams[i]->apply_seamless_boot_optimization) {
1731ce72741bSAnthony Koo 			can_apply_seamless_boot = true;
1732ce72741bSAnthony Koo 			break;
1733ce72741bSAnthony Koo 		}
1734ce72741bSAnthony Koo 	}
17354cac1e6dSYongqiang Sun 
1736be4b289fSSivapiriyanKumarasamy 	/* eDP should not have stream in resume from S4 and so even with VBios post
1737be4b289fSSivapiriyanKumarasamy 	 * it should get turned off
17382c37e49aSYongqiang Sun 	 */
173945a1261bSJake Wang 	if (edp_with_sink_num)
174045a1261bSJake Wang 		edp_link_with_sink = edp_links_with_sink[0];
174145a1261bSJake Wang 
1742be4b289fSSivapiriyanKumarasamy 	if (!can_apply_edp_fast_boot && !can_apply_seamless_boot) {
17433de5aa81SSivapiriyanKumarasamy 		if (edp_link_with_sink && !keep_edp_vdd_on) {
17444cac1e6dSYongqiang Sun 			/*turn off backlight before DP_blank and encoder powered down*/
1745f42ea55bSAnthony Koo 			hws->funcs.edp_backlight_control(edp_link_with_sink, false);
1746c5fc7f59SCharlene Liu 		}
1747c5fc7f59SCharlene Liu 		/*resume from S3, no vbios posting, no need to power down again*/
174825292028SYongqiang Sun 		power_down_all_hw_blocks(dc);
17494562236bSHarry Wentland 		disable_vga_and_power_gate_all_controllers(dc);
17503de5aa81SSivapiriyanKumarasamy 		if (edp_link_with_sink && !keep_edp_vdd_on)
1751be4b289fSSivapiriyanKumarasamy 			dc->hwss.edp_power_control(edp_link_with_sink, false);
1752c5fc7f59SCharlene Liu 	}
17534562236bSHarry Wentland 	bios_set_scratch_acc_mode_change(dc->ctx->dc_bios);
17544562236bSHarry Wentland }
17554562236bSHarry Wentland 
17564562236bSHarry Wentland static uint32_t compute_pstate_blackout_duration(
17574562236bSHarry Wentland 	struct bw_fixed blackout_duration,
17580971c40eSHarry Wentland 	const struct dc_stream_state *stream)
17594562236bSHarry Wentland {
17604562236bSHarry Wentland 	uint32_t total_dest_line_time_ns;
17614562236bSHarry Wentland 	uint32_t pstate_blackout_duration_ns;
17624562236bSHarry Wentland 
17634562236bSHarry Wentland 	pstate_blackout_duration_ns = 1000 * blackout_duration.value >> 24;
17644562236bSHarry Wentland 
17654562236bSHarry Wentland 	total_dest_line_time_ns = 1000000UL *
1766380604e2SKen Chalmers 		(stream->timing.h_total * 10) /
1767380604e2SKen Chalmers 		stream->timing.pix_clk_100hz +
17684562236bSHarry Wentland 		pstate_blackout_duration_ns;
17694562236bSHarry Wentland 
17704562236bSHarry Wentland 	return total_dest_line_time_ns;
17714562236bSHarry Wentland }
17724562236bSHarry Wentland 
1773f774b339SEric Yang static void dce110_set_displaymarks(
1774fb3466a4SBhawanpreet Lakha 	const struct dc *dc,
1775608ac7bbSJerry Zuo 	struct dc_state *context)
17764562236bSHarry Wentland {
17774562236bSHarry Wentland 	uint8_t i, num_pipes;
17784562236bSHarry Wentland 	unsigned int underlay_idx = dc->res_pool->underlay_pipe_index;
17794562236bSHarry Wentland 
17804562236bSHarry Wentland 	for (i = 0, num_pipes = 0; i < MAX_PIPES; i++) {
17814562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
17824562236bSHarry Wentland 		uint32_t total_dest_line_time_ns;
17834562236bSHarry Wentland 
17844562236bSHarry Wentland 		if (pipe_ctx->stream == NULL)
17854562236bSHarry Wentland 			continue;
17864562236bSHarry Wentland 
17874562236bSHarry Wentland 		total_dest_line_time_ns = compute_pstate_blackout_duration(
178877a4ea53SBhawanpreet Lakha 			dc->bw_vbios->blackout_duration, pipe_ctx->stream);
178986a66c4eSHarry Wentland 		pipe_ctx->plane_res.mi->funcs->mem_input_program_display_marks(
179086a66c4eSHarry Wentland 			pipe_ctx->plane_res.mi,
1791813d20dcSAidan Wood 			context->bw_ctx.bw.dce.nbp_state_change_wm_ns[num_pipes],
1792813d20dcSAidan Wood 			context->bw_ctx.bw.dce.stutter_exit_wm_ns[num_pipes],
1793813d20dcSAidan Wood 			context->bw_ctx.bw.dce.stutter_entry_wm_ns[num_pipes],
1794813d20dcSAidan Wood 			context->bw_ctx.bw.dce.urgent_wm_ns[num_pipes],
17954562236bSHarry Wentland 			total_dest_line_time_ns);
17964562236bSHarry Wentland 		if (i == underlay_idx) {
17974562236bSHarry Wentland 			num_pipes++;
179886a66c4eSHarry Wentland 			pipe_ctx->plane_res.mi->funcs->mem_input_program_chroma_display_marks(
179986a66c4eSHarry Wentland 				pipe_ctx->plane_res.mi,
1800813d20dcSAidan Wood 				context->bw_ctx.bw.dce.nbp_state_change_wm_ns[num_pipes],
1801813d20dcSAidan Wood 				context->bw_ctx.bw.dce.stutter_exit_wm_ns[num_pipes],
1802813d20dcSAidan Wood 				context->bw_ctx.bw.dce.urgent_wm_ns[num_pipes],
18034562236bSHarry Wentland 				total_dest_line_time_ns);
18044562236bSHarry Wentland 		}
18054562236bSHarry Wentland 		num_pipes++;
18064562236bSHarry Wentland 	}
18074562236bSHarry Wentland }
18084562236bSHarry Wentland 
1809fab55d61SDmytro Laktyushkin void dce110_set_safe_displaymarks(
1810a2b8659dSTony Cheng 		struct resource_context *res_ctx,
1811a2b8659dSTony Cheng 		const struct resource_pool *pool)
18124562236bSHarry Wentland {
18134562236bSHarry Wentland 	int i;
1814a2b8659dSTony Cheng 	int underlay_idx = pool->underlay_pipe_index;
18159037d802SDmytro Laktyushkin 	struct dce_watermarks max_marks = {
18164562236bSHarry Wentland 		MAX_WATERMARK, MAX_WATERMARK, MAX_WATERMARK, MAX_WATERMARK };
18179037d802SDmytro Laktyushkin 	struct dce_watermarks nbp_marks = {
18184562236bSHarry Wentland 		SAFE_NBP_MARK, SAFE_NBP_MARK, SAFE_NBP_MARK, SAFE_NBP_MARK };
18193722c794SMikita Lipski 	struct dce_watermarks min_marks = { 0, 0, 0, 0};
18204562236bSHarry Wentland 
18214562236bSHarry Wentland 	for (i = 0; i < MAX_PIPES; i++) {
18228feabd03SYue Hin Lau 		if (res_ctx->pipe_ctx[i].stream == NULL || res_ctx->pipe_ctx[i].plane_res.mi == NULL)
18234562236bSHarry Wentland 			continue;
18244562236bSHarry Wentland 
182586a66c4eSHarry Wentland 		res_ctx->pipe_ctx[i].plane_res.mi->funcs->mem_input_program_display_marks(
182686a66c4eSHarry Wentland 				res_ctx->pipe_ctx[i].plane_res.mi,
18274562236bSHarry Wentland 				nbp_marks,
18284562236bSHarry Wentland 				max_marks,
18293722c794SMikita Lipski 				min_marks,
18304562236bSHarry Wentland 				max_marks,
18314562236bSHarry Wentland 				MAX_WATERMARK);
18328feabd03SYue Hin Lau 
18334562236bSHarry Wentland 		if (i == underlay_idx)
183486a66c4eSHarry Wentland 			res_ctx->pipe_ctx[i].plane_res.mi->funcs->mem_input_program_chroma_display_marks(
183586a66c4eSHarry Wentland 				res_ctx->pipe_ctx[i].plane_res.mi,
18364562236bSHarry Wentland 				nbp_marks,
18374562236bSHarry Wentland 				max_marks,
18384562236bSHarry Wentland 				max_marks,
18394562236bSHarry Wentland 				MAX_WATERMARK);
18408feabd03SYue Hin Lau 
18414562236bSHarry Wentland 	}
18424562236bSHarry Wentland }
18434562236bSHarry Wentland 
18444562236bSHarry Wentland /*******************************************************************************
18454562236bSHarry Wentland  * Public functions
18464562236bSHarry Wentland  ******************************************************************************/
18474562236bSHarry Wentland 
18484562236bSHarry Wentland static void set_drr(struct pipe_ctx **pipe_ctx,
1849470e2ca5SBayan Zabihiyan 		int num_pipes, unsigned int vmin, unsigned int vmax,
1850470e2ca5SBayan Zabihiyan 		unsigned int vmid, unsigned int vmid_frame_number)
18514562236bSHarry Wentland {
18524562236bSHarry Wentland 	int i = 0;
18534562236bSHarry Wentland 	struct drr_params params = {0};
185498e6436dSAnthony Koo 	// DRR should set trigger event to monitor surface update event
185598e6436dSAnthony Koo 	unsigned int event_triggers = 0x80;
18565b5abe95SAnthony Koo 	// Note DRR trigger events are generated regardless of whether num frames met.
18575b5abe95SAnthony Koo 	unsigned int num_frames = 2;
18584562236bSHarry Wentland 
18594562236bSHarry Wentland 	params.vertical_total_max = vmax;
18604562236bSHarry Wentland 	params.vertical_total_min = vmin;
18614562236bSHarry Wentland 
18624562236bSHarry Wentland 	/* TODO: If multiple pipes are to be supported, you need
186398e6436dSAnthony Koo 	 * some GSL stuff. Static screen triggers may be programmed differently
186498e6436dSAnthony Koo 	 * as well.
18654562236bSHarry Wentland 	 */
18664562236bSHarry Wentland 	for (i = 0; i < num_pipes; i++) {
186798e6436dSAnthony Koo 		pipe_ctx[i]->stream_res.tg->funcs->set_drr(
186898e6436dSAnthony Koo 			pipe_ctx[i]->stream_res.tg, &params);
186998e6436dSAnthony Koo 
187098e6436dSAnthony Koo 		if (vmax != 0 && vmin != 0)
187198e6436dSAnthony Koo 			pipe_ctx[i]->stream_res.tg->funcs->set_static_screen_control(
187298e6436dSAnthony Koo 					pipe_ctx[i]->stream_res.tg,
18735b5abe95SAnthony Koo 					event_triggers, num_frames);
18744562236bSHarry Wentland 	}
18754562236bSHarry Wentland }
18764562236bSHarry Wentland 
187772ada5f7SEric Cook static void get_position(struct pipe_ctx **pipe_ctx,
187872ada5f7SEric Cook 		int num_pipes,
187972ada5f7SEric Cook 		struct crtc_position *position)
188072ada5f7SEric Cook {
188172ada5f7SEric Cook 	int i = 0;
188272ada5f7SEric Cook 
188372ada5f7SEric Cook 	/* TODO: handle pipes > 1
188472ada5f7SEric Cook 	 */
188572ada5f7SEric Cook 	for (i = 0; i < num_pipes; i++)
18866b670fa9SHarry Wentland 		pipe_ctx[i]->stream_res.tg->funcs->get_position(pipe_ctx[i]->stream_res.tg, position);
188772ada5f7SEric Cook }
188872ada5f7SEric Cook 
18894562236bSHarry Wentland static void set_static_screen_control(struct pipe_ctx **pipe_ctx,
18905b5abe95SAnthony Koo 		int num_pipes, const struct dc_static_screen_params *params)
18914562236bSHarry Wentland {
18924562236bSHarry Wentland 	unsigned int i;
18935b5abe95SAnthony Koo 	unsigned int triggers = 0;
189494267b3dSSylvia Tsai 
18955b5abe95SAnthony Koo 	if (params->triggers.overlay_update)
18965b5abe95SAnthony Koo 		triggers |= 0x100;
18975b5abe95SAnthony Koo 	if (params->triggers.surface_update)
18985b5abe95SAnthony Koo 		triggers |= 0x80;
18995b5abe95SAnthony Koo 	if (params->triggers.cursor_update)
19005b5abe95SAnthony Koo 		triggers |= 0x2;
19015b5abe95SAnthony Koo 	if (params->triggers.force_trigger)
19025b5abe95SAnthony Koo 		triggers |= 0x1;
19034562236bSHarry Wentland 
1904593f79a2SAlex Deucher 	if (num_pipes) {
1905593f79a2SAlex Deucher 		struct dc *dc = pipe_ctx[0]->stream->ctx->dc;
1906593f79a2SAlex Deucher 
1907593f79a2SAlex Deucher 		if (dc->fbc_compressor)
19085b5abe95SAnthony Koo 			triggers |= 0x84;
1909593f79a2SAlex Deucher 	}
1910c3aa1d67SBhawanpreet Lakha 
19114562236bSHarry Wentland 	for (i = 0; i < num_pipes; i++)
19126b670fa9SHarry Wentland 		pipe_ctx[i]->stream_res.tg->funcs->
19135b5abe95SAnthony Koo 			set_static_screen_control(pipe_ctx[i]->stream_res.tg,
19145b5abe95SAnthony Koo 					triggers, params->num_frames);
19154562236bSHarry Wentland }
19164562236bSHarry Wentland 
1917f6baff4dSHarry Wentland /*
1918690b5e39SRoman Li  *  Check if FBC can be enabled
1919690b5e39SRoman Li  */
19209c6569deSHarry Wentland static bool should_enable_fbc(struct dc *dc,
19213bc4aaa9SRoman Li 		struct dc_state *context,
19223bc4aaa9SRoman Li 		uint32_t *pipe_idx)
1923690b5e39SRoman Li {
19243bc4aaa9SRoman Li 	uint32_t i;
19253bc4aaa9SRoman Li 	struct pipe_ctx *pipe_ctx = NULL;
19263bc4aaa9SRoman Li 	struct resource_context *res_ctx = &context->res_ctx;
192765d38262Shersen wu 	unsigned int underlay_idx = dc->res_pool->underlay_pipe_index;
19283bc4aaa9SRoman Li 
1929690b5e39SRoman Li 
1930690b5e39SRoman Li 	ASSERT(dc->fbc_compressor);
1931690b5e39SRoman Li 
1932690b5e39SRoman Li 	/* FBC memory should be allocated */
1933690b5e39SRoman Li 	if (!dc->ctx->fbc_gpu_addr)
19349c6569deSHarry Wentland 		return false;
1935690b5e39SRoman Li 
1936690b5e39SRoman Li 	/* Only supports single display */
1937690b5e39SRoman Li 	if (context->stream_count != 1)
19389c6569deSHarry Wentland 		return false;
1939690b5e39SRoman Li 
19403bc4aaa9SRoman Li 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
19413bc4aaa9SRoman Li 		if (res_ctx->pipe_ctx[i].stream) {
194265d38262Shersen wu 
19433bc4aaa9SRoman Li 			pipe_ctx = &res_ctx->pipe_ctx[i];
194465d38262Shersen wu 
194565d38262Shersen wu 			if (!pipe_ctx)
194665d38262Shersen wu 				continue;
194765d38262Shersen wu 
194865d38262Shersen wu 			/* fbc not applicable on underlay pipe */
194965d38262Shersen wu 			if (pipe_ctx->pipe_idx != underlay_idx) {
19503bc4aaa9SRoman Li 				*pipe_idx = i;
19513bc4aaa9SRoman Li 				break;
19523bc4aaa9SRoman Li 			}
19533bc4aaa9SRoman Li 		}
195465d38262Shersen wu 	}
19553bc4aaa9SRoman Li 
195665d38262Shersen wu 	if (i == dc->res_pool->pipe_count)
195765d38262Shersen wu 		return false;
195865d38262Shersen wu 
1959ceb3dbb4SJun Lei 	if (!pipe_ctx->stream->link)
196065d38262Shersen wu 		return false;
19617a840773SRoman Li 
1962690b5e39SRoman Li 	/* Only supports eDP */
1963ceb3dbb4SJun Lei 	if (pipe_ctx->stream->link->connector_signal != SIGNAL_TYPE_EDP)
19649c6569deSHarry Wentland 		return false;
1965690b5e39SRoman Li 
1966690b5e39SRoman Li 	/* PSR should not be enabled */
1967d1ebfdd8SWyatt Wood 	if (pipe_ctx->stream->link->psr_settings.psr_feature_enabled)
19689c6569deSHarry Wentland 		return false;
1969690b5e39SRoman Li 
197093984bbcSShirish S 	/* Nothing to compress */
197193984bbcSShirish S 	if (!pipe_ctx->plane_state)
19729c6569deSHarry Wentland 		return false;
197393984bbcSShirish S 
197405230fa9SRoman Li 	/* Only for non-linear tiling */
197505230fa9SRoman Li 	if (pipe_ctx->plane_state->tiling_info.gfx8.array_mode == DC_ARRAY_LINEAR_GENERAL)
19769c6569deSHarry Wentland 		return false;
197705230fa9SRoman Li 
19789c6569deSHarry Wentland 	return true;
1979690b5e39SRoman Li }
1980690b5e39SRoman Li 
1981690b5e39SRoman Li /*
1982690b5e39SRoman Li  *  Enable FBC
1983690b5e39SRoman Li  */
198465d38262Shersen wu static void enable_fbc(
198565d38262Shersen wu 		struct dc *dc,
1986608ac7bbSJerry Zuo 		struct dc_state *context)
1987690b5e39SRoman Li {
19883bc4aaa9SRoman Li 	uint32_t pipe_idx = 0;
19893bc4aaa9SRoman Li 
19903bc4aaa9SRoman Li 	if (should_enable_fbc(dc, context, &pipe_idx)) {
1991690b5e39SRoman Li 		/* Program GRPH COMPRESSED ADDRESS and PITCH */
1992690b5e39SRoman Li 		struct compr_addr_and_pitch_params params = {0, 0, 0};
1993690b5e39SRoman Li 		struct compressor *compr = dc->fbc_compressor;
19943bc4aaa9SRoman Li 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
19953bc4aaa9SRoman Li 
19969c6569deSHarry Wentland 		params.source_view_width = pipe_ctx->stream->timing.h_addressable;
19979c6569deSHarry Wentland 		params.source_view_height = pipe_ctx->stream->timing.v_addressable;
199865d38262Shersen wu 		params.inst = pipe_ctx->stream_res.tg->inst;
1999690b5e39SRoman Li 		compr->compr_surface_address.quad_part = dc->ctx->fbc_gpu_addr;
2000690b5e39SRoman Li 
2001690b5e39SRoman Li 		compr->funcs->surface_address_and_pitch(compr, &params);
2002690b5e39SRoman Li 		compr->funcs->set_fbc_invalidation_triggers(compr, 1);
2003690b5e39SRoman Li 
2004690b5e39SRoman Li 		compr->funcs->enable_fbc(compr, &params);
2005690b5e39SRoman Li 	}
2006690b5e39SRoman Li }
2007690b5e39SRoman Li 
200854e8695eSDmytro Laktyushkin static void dce110_reset_hw_ctx_wrap(
2009fb3466a4SBhawanpreet Lakha 		struct dc *dc,
2010608ac7bbSJerry Zuo 		struct dc_state *context)
20114562236bSHarry Wentland {
20124562236bSHarry Wentland 	int i;
20134562236bSHarry Wentland 
20144562236bSHarry Wentland 	/* Reset old context */
20154562236bSHarry Wentland 	/* look up the targets that have been removed since last commit */
2016a2b8659dSTony Cheng 	for (i = 0; i < MAX_PIPES; i++) {
20174562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx_old =
2018608ac7bbSJerry Zuo 			&dc->current_state->res_ctx.pipe_ctx[i];
20194562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
20204562236bSHarry Wentland 
20214562236bSHarry Wentland 		/* Note: We need to disable output if clock sources change,
20224562236bSHarry Wentland 		 * since bios does optimization and doesn't apply if changing
20234562236bSHarry Wentland 		 * PHY when not already disabled.
20244562236bSHarry Wentland 		 */
20254562236bSHarry Wentland 
20264562236bSHarry Wentland 		/* Skip underlay pipe since it will be handled in commit surface*/
20274562236bSHarry Wentland 		if (!pipe_ctx_old->stream || pipe_ctx_old->top_pipe)
20284562236bSHarry Wentland 			continue;
20294562236bSHarry Wentland 
20304562236bSHarry Wentland 		if (!pipe_ctx->stream ||
203154e8695eSDmytro Laktyushkin 				pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
203221e67d4dSHarry Wentland 			struct clock_source *old_clk = pipe_ctx_old->clock_source;
203321e67d4dSHarry Wentland 
2034827f11e9SLeo (Sunpeng) Li 			/* Disable if new stream is null. O/w, if stream is
2035827f11e9SLeo (Sunpeng) Li 			 * disabled already, no need to disable again.
2036827f11e9SLeo (Sunpeng) Li 			 */
203757430404SSu Sung Chung 			if (!pipe_ctx->stream || !pipe_ctx->stream->dpms_off) {
203857430404SSu Sung Chung 				core_link_disable_stream(pipe_ctx_old);
203957430404SSu Sung Chung 
204057430404SSu Sung Chung 				/* free acquired resources*/
204157430404SSu Sung Chung 				if (pipe_ctx_old->stream_res.audio) {
204257430404SSu Sung Chung 					/*disable az_endpoint*/
204357430404SSu Sung Chung 					pipe_ctx_old->stream_res.audio->funcs->
204457430404SSu Sung Chung 							az_disable(pipe_ctx_old->stream_res.audio);
204557430404SSu Sung Chung 
204657430404SSu Sung Chung 					/*free audio*/
204757430404SSu Sung Chung 					if (dc->caps.dynamic_audio == true) {
204857430404SSu Sung Chung 						/*we have to dynamic arbitrate the audio endpoints*/
204957430404SSu Sung Chung 						/*we free the resource, need reset is_audio_acquired*/
205057430404SSu Sung Chung 						update_audio_usage(&dc->current_state->res_ctx, dc->res_pool,
205157430404SSu Sung Chung 								pipe_ctx_old->stream_res.audio, false);
205257430404SSu Sung Chung 						pipe_ctx_old->stream_res.audio = NULL;
205357430404SSu Sung Chung 					}
205457430404SSu Sung Chung 				}
205557430404SSu Sung Chung 			}
2056d050f8edSHersen Wu 
20576b670fa9SHarry Wentland 			pipe_ctx_old->stream_res.tg->funcs->set_blank(pipe_ctx_old->stream_res.tg, true);
20586b670fa9SHarry Wentland 			if (!hwss_wait_for_blank_complete(pipe_ctx_old->stream_res.tg)) {
205954e8695eSDmytro Laktyushkin 				dm_error("DC: failed to blank crtc!\n");
206054e8695eSDmytro Laktyushkin 				BREAK_TO_DEBUGGER();
206154e8695eSDmytro Laktyushkin 			}
20626b670fa9SHarry Wentland 			pipe_ctx_old->stream_res.tg->funcs->disable_crtc(pipe_ctx_old->stream_res.tg);
206386a66c4eSHarry Wentland 			pipe_ctx_old->plane_res.mi->funcs->free_mem_input(
2064608ac7bbSJerry Zuo 					pipe_ctx_old->plane_res.mi, dc->current_state->stream_count);
206554e8695eSDmytro Laktyushkin 
2066ad8960a6SMikita Lipski 			if (old_clk && 0 == resource_get_clock_source_reference(&context->res_ctx,
2067ad8960a6SMikita Lipski 										dc->res_pool,
2068ad8960a6SMikita Lipski 										old_clk))
206921e67d4dSHarry Wentland 				old_clk->funcs->cs_power_down(old_clk);
207021e67d4dSHarry Wentland 
20717f914a62SYongqiang Sun 			dc->hwss.disable_plane(dc, pipe_ctx_old);
207254e8695eSDmytro Laktyushkin 
207354e8695eSDmytro Laktyushkin 			pipe_ctx_old->stream = NULL;
207454e8695eSDmytro Laktyushkin 		}
20754562236bSHarry Wentland 	}
20764562236bSHarry Wentland }
20774562236bSHarry Wentland 
20781a05873fSAnthony Koo static void dce110_setup_audio_dto(
20791a05873fSAnthony Koo 		struct dc *dc,
20801a05873fSAnthony Koo 		struct dc_state *context)
20811a05873fSAnthony Koo {
20821a05873fSAnthony Koo 	int i;
20831a05873fSAnthony Koo 
20841a05873fSAnthony Koo 	/* program audio wall clock. use HDMI as clock source if HDMI
20851a05873fSAnthony Koo 	 * audio active. Otherwise, use DP as clock source
20861a05873fSAnthony Koo 	 * first, loop to find any HDMI audio, if not, loop find DP audio
20871a05873fSAnthony Koo 	 */
20881a05873fSAnthony Koo 	/* Setup audio rate clock source */
20891a05873fSAnthony Koo 	/* Issue:
20901a05873fSAnthony Koo 	* Audio lag happened on DP monitor when unplug a HDMI monitor
20911a05873fSAnthony Koo 	*
20921a05873fSAnthony Koo 	* Cause:
20931a05873fSAnthony Koo 	* In case of DP and HDMI connected or HDMI only, DCCG_AUDIO_DTO_SEL
20941a05873fSAnthony Koo 	* is set to either dto0 or dto1, audio should work fine.
20951a05873fSAnthony Koo 	* In case of DP connected only, DCCG_AUDIO_DTO_SEL should be dto1,
20961a05873fSAnthony Koo 	* set to dto0 will cause audio lag.
20971a05873fSAnthony Koo 	*
20981a05873fSAnthony Koo 	* Solution:
20991a05873fSAnthony Koo 	* Not optimized audio wall dto setup. When mode set, iterate pipe_ctx,
21001a05873fSAnthony Koo 	* find first available pipe with audio, setup audio wall DTO per topology
21011a05873fSAnthony Koo 	* instead of per pipe.
21021a05873fSAnthony Koo 	*/
21031a05873fSAnthony Koo 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
21041a05873fSAnthony Koo 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
21051a05873fSAnthony Koo 
21061a05873fSAnthony Koo 		if (pipe_ctx->stream == NULL)
21071a05873fSAnthony Koo 			continue;
21081a05873fSAnthony Koo 
21091a05873fSAnthony Koo 		if (pipe_ctx->top_pipe)
21101a05873fSAnthony Koo 			continue;
21111a05873fSAnthony Koo 		if (pipe_ctx->stream->signal != SIGNAL_TYPE_HDMI_TYPE_A)
21121a05873fSAnthony Koo 			continue;
21131a05873fSAnthony Koo 		if (pipe_ctx->stream_res.audio != NULL) {
21141a05873fSAnthony Koo 			struct audio_output audio_output;
21151a05873fSAnthony Koo 
21161a05873fSAnthony Koo 			build_audio_output(context, pipe_ctx, &audio_output);
21171a05873fSAnthony Koo 
21181a05873fSAnthony Koo 			pipe_ctx->stream_res.audio->funcs->wall_dto_setup(
21191a05873fSAnthony Koo 				pipe_ctx->stream_res.audio,
21201a05873fSAnthony Koo 				pipe_ctx->stream->signal,
21211a05873fSAnthony Koo 				&audio_output.crtc_info,
21221a05873fSAnthony Koo 				&audio_output.pll_info);
21231a05873fSAnthony Koo 			break;
21241a05873fSAnthony Koo 		}
21251a05873fSAnthony Koo 	}
21261a05873fSAnthony Koo 
21271a05873fSAnthony Koo 	/* no HDMI audio is found, try DP audio */
21281a05873fSAnthony Koo 	if (i == dc->res_pool->pipe_count) {
21291a05873fSAnthony Koo 		for (i = 0; i < dc->res_pool->pipe_count; i++) {
21301a05873fSAnthony Koo 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
21311a05873fSAnthony Koo 
21321a05873fSAnthony Koo 			if (pipe_ctx->stream == NULL)
21331a05873fSAnthony Koo 				continue;
21341a05873fSAnthony Koo 
21351a05873fSAnthony Koo 			if (pipe_ctx->top_pipe)
21361a05873fSAnthony Koo 				continue;
21371a05873fSAnthony Koo 
21381a05873fSAnthony Koo 			if (!dc_is_dp_signal(pipe_ctx->stream->signal))
21391a05873fSAnthony Koo 				continue;
21401a05873fSAnthony Koo 
21411a05873fSAnthony Koo 			if (pipe_ctx->stream_res.audio != NULL) {
21421a05873fSAnthony Koo 				struct audio_output audio_output;
21431a05873fSAnthony Koo 
21441a05873fSAnthony Koo 				build_audio_output(context, pipe_ctx, &audio_output);
21451a05873fSAnthony Koo 
21461a05873fSAnthony Koo 				pipe_ctx->stream_res.audio->funcs->wall_dto_setup(
21471a05873fSAnthony Koo 					pipe_ctx->stream_res.audio,
21481a05873fSAnthony Koo 					pipe_ctx->stream->signal,
21491a05873fSAnthony Koo 					&audio_output.crtc_info,
21501a05873fSAnthony Koo 					&audio_output.pll_info);
21511a05873fSAnthony Koo 				break;
21521a05873fSAnthony Koo 			}
21531a05873fSAnthony Koo 		}
21541a05873fSAnthony Koo 	}
21551a05873fSAnthony Koo }
2156cf437593SDmytro Laktyushkin 
21574562236bSHarry Wentland enum dc_status dce110_apply_ctx_to_hw(
2158fb3466a4SBhawanpreet Lakha 		struct dc *dc,
2159608ac7bbSJerry Zuo 		struct dc_state *context)
21604562236bSHarry Wentland {
2161f42ea55bSAnthony Koo 	struct dce_hwseq *hws = dc->hwseq;
21624562236bSHarry Wentland 	struct dc_bios *dcb = dc->ctx->dc_bios;
21634562236bSHarry Wentland 	enum dc_status status;
21644562236bSHarry Wentland 	int i;
21654562236bSHarry Wentland 
21664562236bSHarry Wentland 	/* Reset old context */
21674562236bSHarry Wentland 	/* look up the targets that have been removed since last commit */
2168f42ea55bSAnthony Koo 	hws->funcs.reset_hw_ctx_wrap(dc, context);
21694562236bSHarry Wentland 
21704562236bSHarry Wentland 	/* Skip applying if no targets */
2171ab2541b6SAric Cyr 	if (context->stream_count <= 0)
21724562236bSHarry Wentland 		return DC_OK;
21734562236bSHarry Wentland 
21744562236bSHarry Wentland 	/* Apply new context */
21754562236bSHarry Wentland 	dcb->funcs->set_scratch_critical_state(dcb, true);
21764562236bSHarry Wentland 
21774562236bSHarry Wentland 	/* below is for real asic only */
2178a2b8659dSTony Cheng 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
21794562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx_old =
2180608ac7bbSJerry Zuo 					&dc->current_state->res_ctx.pipe_ctx[i];
21814562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
21824562236bSHarry Wentland 
21834562236bSHarry Wentland 		if (pipe_ctx->stream == NULL || pipe_ctx->top_pipe)
21844562236bSHarry Wentland 			continue;
21854562236bSHarry Wentland 
21864562236bSHarry Wentland 		if (pipe_ctx->stream == pipe_ctx_old->stream) {
21874562236bSHarry Wentland 			if (pipe_ctx_old->clock_source != pipe_ctx->clock_source)
21884562236bSHarry Wentland 				dce_crtc_switch_to_clk_src(dc->hwseq,
21894562236bSHarry Wentland 						pipe_ctx->clock_source, i);
21904562236bSHarry Wentland 			continue;
21914562236bSHarry Wentland 		}
21924562236bSHarry Wentland 
2193f42ea55bSAnthony Koo 		hws->funcs.enable_display_power_gating(
21944562236bSHarry Wentland 				dc, i, dc->ctx->dc_bios,
21954562236bSHarry Wentland 				PIPE_GATING_CONTROL_DISABLE);
21964562236bSHarry Wentland 	}
21974562236bSHarry Wentland 
21982f3bfb27SRoman Li 	if (dc->fbc_compressor)
21991663ae1cSBhawanpreet Lakha 		dc->fbc_compressor->funcs->disable_fbc(dc->fbc_compressor);
22005099114bSAlex Deucher 
22011a05873fSAnthony Koo 	dce110_setup_audio_dto(dc, context);
2202ab8812a3SHersen Wu 
2203a2b8659dSTony Cheng 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
2204ab8812a3SHersen Wu 		struct pipe_ctx *pipe_ctx_old =
2205608ac7bbSJerry Zuo 					&dc->current_state->res_ctx.pipe_ctx[i];
2206ab8812a3SHersen Wu 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
2207ab8812a3SHersen Wu 
2208ab8812a3SHersen Wu 		if (pipe_ctx->stream == NULL)
2209ab8812a3SHersen Wu 			continue;
2210ab8812a3SHersen Wu 
2211eed928dcSCharlene Liu 		if (pipe_ctx->stream == pipe_ctx_old->stream &&
2212eed928dcSCharlene Liu 			pipe_ctx->stream->link->link_state_valid) {
2213ab8812a3SHersen Wu 			continue;
2214eed928dcSCharlene Liu 		}
2215ab8812a3SHersen Wu 
22165b92d9d4SHarry Wentland 		if (pipe_ctx_old->stream && !pipe_need_reprogram(pipe_ctx_old, pipe_ctx))
2217313bf4ffSYongqiang Sun 			continue;
2218313bf4ffSYongqiang Sun 
2219b1f6d01cSDmytro Laktyushkin 		if (pipe_ctx->top_pipe || pipe_ctx->prev_odm_pipe)
2220ab8812a3SHersen Wu 			continue;
2221ab8812a3SHersen Wu 
22224562236bSHarry Wentland 		status = apply_single_controller_ctx_to_hw(
22234562236bSHarry Wentland 				pipe_ctx,
22244562236bSHarry Wentland 				context,
22254562236bSHarry Wentland 				dc);
22264562236bSHarry Wentland 
22274562236bSHarry Wentland 		if (DC_OK != status)
22284562236bSHarry Wentland 			return status;
22294562236bSHarry Wentland 	}
22304562236bSHarry Wentland 
2231690b5e39SRoman Li 	if (dc->fbc_compressor)
223265d38262Shersen wu 		enable_fbc(dc, dc->current_state);
223365d38262Shersen wu 
223465d38262Shersen wu 	dcb->funcs->set_scratch_critical_state(dcb, false);
2235690b5e39SRoman Li 
22364562236bSHarry Wentland 	return DC_OK;
22374562236bSHarry Wentland }
22384562236bSHarry Wentland 
22394562236bSHarry Wentland /*******************************************************************************
22404562236bSHarry Wentland  * Front End programming
22414562236bSHarry Wentland  ******************************************************************************/
22424562236bSHarry Wentland static void set_default_colors(struct pipe_ctx *pipe_ctx)
22434562236bSHarry Wentland {
22444562236bSHarry Wentland 	struct default_adjustment default_adjust = { 0 };
22454562236bSHarry Wentland 
22464562236bSHarry Wentland 	default_adjust.force_hw_default = false;
224734996173SHarry Wentland 	default_adjust.in_color_space = pipe_ctx->plane_state->color_space;
224834996173SHarry Wentland 	default_adjust.out_color_space = pipe_ctx->stream->output_color_space;
22494562236bSHarry Wentland 	default_adjust.csc_adjust_type = GRAPHICS_CSC_ADJUST_TYPE_SW;
22506702a9acSHarry Wentland 	default_adjust.surface_pixel_format = pipe_ctx->plane_res.scl_data.format;
22514562236bSHarry Wentland 
22524562236bSHarry Wentland 	/* display color depth */
22534562236bSHarry Wentland 	default_adjust.color_depth =
22544fa086b9SLeo (Sunpeng) Li 		pipe_ctx->stream->timing.display_color_depth;
22554562236bSHarry Wentland 
22564562236bSHarry Wentland 	/* Lb color depth */
22576702a9acSHarry Wentland 	default_adjust.lb_color_depth = pipe_ctx->plane_res.scl_data.lb_params.depth;
22584562236bSHarry Wentland 
225986a66c4eSHarry Wentland 	pipe_ctx->plane_res.xfm->funcs->opp_set_csc_default(
226086a66c4eSHarry Wentland 					pipe_ctx->plane_res.xfm, &default_adjust);
22614562236bSHarry Wentland }
22624562236bSHarry Wentland 
2263b06b7680SLeon Elazar 
2264b06b7680SLeon Elazar /*******************************************************************************
2265b06b7680SLeon Elazar  * In order to turn on/off specific surface we will program
2266b06b7680SLeon Elazar  * Blender + CRTC
2267b06b7680SLeon Elazar  *
2268b06b7680SLeon Elazar  * In case that we have two surfaces and they have a different visibility
2269b06b7680SLeon Elazar  * we can't turn off the CRTC since it will turn off the entire display
2270b06b7680SLeon Elazar  *
2271b06b7680SLeon Elazar  * |----------------------------------------------- |
2272b06b7680SLeon Elazar  * |bottom pipe|curr pipe  |              |         |
2273b06b7680SLeon Elazar  * |Surface    |Surface    | Blender      |  CRCT   |
2274b06b7680SLeon Elazar  * |visibility |visibility | Configuration|         |
2275b06b7680SLeon Elazar  * |------------------------------------------------|
2276b06b7680SLeon Elazar  * |   off     |    off    | CURRENT_PIPE | blank   |
2277b06b7680SLeon Elazar  * |   off     |    on     | CURRENT_PIPE | unblank |
2278b06b7680SLeon Elazar  * |   on      |    off    | OTHER_PIPE   | unblank |
2279b06b7680SLeon Elazar  * |   on      |    on     | BLENDING     | unblank |
2280b06b7680SLeon Elazar  * -------------------------------------------------|
2281b06b7680SLeon Elazar  *
2282b06b7680SLeon Elazar  ******************************************************************************/
2283fb3466a4SBhawanpreet Lakha static void program_surface_visibility(const struct dc *dc,
22844562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
22854562236bSHarry Wentland {
22864562236bSHarry Wentland 	enum blnd_mode blender_mode = BLND_MODE_CURRENT_PIPE;
2287b06b7680SLeon Elazar 	bool blank_target = false;
22884562236bSHarry Wentland 
22894562236bSHarry Wentland 	if (pipe_ctx->bottom_pipe) {
2290b06b7680SLeon Elazar 
2291b06b7680SLeon Elazar 		/* For now we are supporting only two pipes */
2292b06b7680SLeon Elazar 		ASSERT(pipe_ctx->bottom_pipe->bottom_pipe == NULL);
2293b06b7680SLeon Elazar 
22943be5262eSHarry Wentland 		if (pipe_ctx->bottom_pipe->plane_state->visible) {
22953be5262eSHarry Wentland 			if (pipe_ctx->plane_state->visible)
22964562236bSHarry Wentland 				blender_mode = BLND_MODE_BLENDING;
22974562236bSHarry Wentland 			else
22984562236bSHarry Wentland 				blender_mode = BLND_MODE_OTHER_PIPE;
2299b06b7680SLeon Elazar 
23003be5262eSHarry Wentland 		} else if (!pipe_ctx->plane_state->visible)
2301b06b7680SLeon Elazar 			blank_target = true;
2302b06b7680SLeon Elazar 
23033be5262eSHarry Wentland 	} else if (!pipe_ctx->plane_state->visible)
2304b06b7680SLeon Elazar 		blank_target = true;
2305b06b7680SLeon Elazar 
2306e07f541fSYongqiang Sun 	dce_set_blender_mode(dc->hwseq, pipe_ctx->stream_res.tg->inst, blender_mode);
23076b670fa9SHarry Wentland 	pipe_ctx->stream_res.tg->funcs->set_blank(pipe_ctx->stream_res.tg, blank_target);
2308b06b7680SLeon Elazar 
23094562236bSHarry Wentland }
23104562236bSHarry Wentland 
23111bf56e62SZeyu Fan static void program_gamut_remap(struct pipe_ctx *pipe_ctx)
23121bf56e62SZeyu Fan {
2313146a9f63SKrunoslav Kovac 	int i = 0;
23141bf56e62SZeyu Fan 	struct xfm_grph_csc_adjustment adjust;
23151bf56e62SZeyu Fan 	memset(&adjust, 0, sizeof(adjust));
23161bf56e62SZeyu Fan 	adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_BYPASS;
23171bf56e62SZeyu Fan 
23181bf56e62SZeyu Fan 
23194fa086b9SLeo (Sunpeng) Li 	if (pipe_ctx->stream->gamut_remap_matrix.enable_remap == true) {
23201bf56e62SZeyu Fan 		adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_SW;
2321146a9f63SKrunoslav Kovac 
2322146a9f63SKrunoslav Kovac 		for (i = 0; i < CSC_TEMPERATURE_MATRIX_SIZE; i++)
2323146a9f63SKrunoslav Kovac 			adjust.temperature_matrix[i] =
2324146a9f63SKrunoslav Kovac 				pipe_ctx->stream->gamut_remap_matrix.matrix[i];
23251bf56e62SZeyu Fan 	}
23261bf56e62SZeyu Fan 
232786a66c4eSHarry Wentland 	pipe_ctx->plane_res.xfm->funcs->transform_set_gamut_remap(pipe_ctx->plane_res.xfm, &adjust);
23281bf56e62SZeyu Fan }
2329fb3466a4SBhawanpreet Lakha static void update_plane_addr(const struct dc *dc,
23304562236bSHarry Wentland 		struct pipe_ctx *pipe_ctx)
23314562236bSHarry Wentland {
23323be5262eSHarry Wentland 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
23334562236bSHarry Wentland 
23343be5262eSHarry Wentland 	if (plane_state == NULL)
23354562236bSHarry Wentland 		return;
23364562236bSHarry Wentland 
233786a66c4eSHarry Wentland 	pipe_ctx->plane_res.mi->funcs->mem_input_program_surface_flip_and_addr(
233886a66c4eSHarry Wentland 			pipe_ctx->plane_res.mi,
23393be5262eSHarry Wentland 			&plane_state->address,
23403be5262eSHarry Wentland 			plane_state->flip_immediate);
23414562236bSHarry Wentland 
23423be5262eSHarry Wentland 	plane_state->status.requested_address = plane_state->address;
23434562236bSHarry Wentland }
23444562236bSHarry Wentland 
2345f774b339SEric Yang static void dce110_update_pending_status(struct pipe_ctx *pipe_ctx)
23464562236bSHarry Wentland {
23473be5262eSHarry Wentland 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
23484562236bSHarry Wentland 
23493be5262eSHarry Wentland 	if (plane_state == NULL)
23504562236bSHarry Wentland 		return;
23514562236bSHarry Wentland 
23523be5262eSHarry Wentland 	plane_state->status.is_flip_pending =
235386a66c4eSHarry Wentland 			pipe_ctx->plane_res.mi->funcs->mem_input_is_flip_pending(
235486a66c4eSHarry Wentland 					pipe_ctx->plane_res.mi);
23554562236bSHarry Wentland 
23563be5262eSHarry Wentland 	if (plane_state->status.is_flip_pending && !plane_state->visible)
235786a66c4eSHarry Wentland 		pipe_ctx->plane_res.mi->current_address = pipe_ctx->plane_res.mi->request_address;
23584562236bSHarry Wentland 
235986a66c4eSHarry Wentland 	plane_state->status.current_address = pipe_ctx->plane_res.mi->current_address;
236086a66c4eSHarry Wentland 	if (pipe_ctx->plane_res.mi->current_address.type == PLN_ADDR_TYPE_GRPH_STEREO &&
23616b670fa9SHarry Wentland 			pipe_ctx->stream_res.tg->funcs->is_stereo_left_eye) {
23623be5262eSHarry Wentland 		plane_state->status.is_right_eye =\
23636b670fa9SHarry Wentland 				!pipe_ctx->stream_res.tg->funcs->is_stereo_left_eye(pipe_ctx->stream_res.tg);
23647f5c22d1SVitaly Prosyak 	}
23654562236bSHarry Wentland }
23664562236bSHarry Wentland 
2367fb3466a4SBhawanpreet Lakha void dce110_power_down(struct dc *dc)
23684562236bSHarry Wentland {
23694562236bSHarry Wentland 	power_down_all_hw_blocks(dc);
23704562236bSHarry Wentland 	disable_vga_and_power_gate_all_controllers(dc);
23714562236bSHarry Wentland }
23724562236bSHarry Wentland 
23734562236bSHarry Wentland static bool wait_for_reset_trigger_to_occur(
23744562236bSHarry Wentland 	struct dc_context *dc_ctx,
23754562236bSHarry Wentland 	struct timing_generator *tg)
23764562236bSHarry Wentland {
23774562236bSHarry Wentland 	bool rc = false;
23784562236bSHarry Wentland 
23794562236bSHarry Wentland 	/* To avoid endless loop we wait at most
23804562236bSHarry Wentland 	 * frames_to_wait_on_triggered_reset frames for the reset to occur. */
23814562236bSHarry Wentland 	const uint32_t frames_to_wait_on_triggered_reset = 10;
23824562236bSHarry Wentland 	uint32_t i;
23834562236bSHarry Wentland 
23844562236bSHarry Wentland 	for (i = 0; i < frames_to_wait_on_triggered_reset; i++) {
23854562236bSHarry Wentland 
23864562236bSHarry Wentland 		if (!tg->funcs->is_counter_moving(tg)) {
23874562236bSHarry Wentland 			DC_ERROR("TG counter is not moving!\n");
23884562236bSHarry Wentland 			break;
23894562236bSHarry Wentland 		}
23904562236bSHarry Wentland 
23914562236bSHarry Wentland 		if (tg->funcs->did_triggered_reset_occur(tg)) {
23924562236bSHarry Wentland 			rc = true;
23934562236bSHarry Wentland 			/* usually occurs at i=1 */
23944562236bSHarry Wentland 			DC_SYNC_INFO("GSL: reset occurred at wait count: %d\n",
23954562236bSHarry Wentland 					i);
23964562236bSHarry Wentland 			break;
23974562236bSHarry Wentland 		}
23984562236bSHarry Wentland 
23994562236bSHarry Wentland 		/* Wait for one frame. */
24004562236bSHarry Wentland 		tg->funcs->wait_for_state(tg, CRTC_STATE_VACTIVE);
24014562236bSHarry Wentland 		tg->funcs->wait_for_state(tg, CRTC_STATE_VBLANK);
24024562236bSHarry Wentland 	}
24034562236bSHarry Wentland 
24044562236bSHarry Wentland 	if (false == rc)
24054562236bSHarry Wentland 		DC_ERROR("GSL: Timeout on reset trigger!\n");
24064562236bSHarry Wentland 
24074562236bSHarry Wentland 	return rc;
24084562236bSHarry Wentland }
24094562236bSHarry Wentland 
24104562236bSHarry Wentland /* Enable timing synchronization for a group of Timing Generators. */
24114562236bSHarry Wentland static void dce110_enable_timing_synchronization(
2412fb3466a4SBhawanpreet Lakha 		struct dc *dc,
24134562236bSHarry Wentland 		int group_index,
24144562236bSHarry Wentland 		int group_size,
24154562236bSHarry Wentland 		struct pipe_ctx *grouped_pipes[])
24164562236bSHarry Wentland {
24174562236bSHarry Wentland 	struct dc_context *dc_ctx = dc->ctx;
24184562236bSHarry Wentland 	struct dcp_gsl_params gsl_params = { 0 };
24194562236bSHarry Wentland 	int i;
24204562236bSHarry Wentland 
24214562236bSHarry Wentland 	DC_SYNC_INFO("GSL: Setting-up...\n");
24224562236bSHarry Wentland 
24234562236bSHarry Wentland 	/* Designate a single TG in the group as a master.
24244562236bSHarry Wentland 	 * Since HW doesn't care which one, we always assign
24254562236bSHarry Wentland 	 * the 1st one in the group. */
24264562236bSHarry Wentland 	gsl_params.gsl_group = 0;
24276b670fa9SHarry Wentland 	gsl_params.gsl_master = grouped_pipes[0]->stream_res.tg->inst;
24284562236bSHarry Wentland 
24294562236bSHarry Wentland 	for (i = 0; i < group_size; i++)
24306b670fa9SHarry Wentland 		grouped_pipes[i]->stream_res.tg->funcs->setup_global_swap_lock(
24316b670fa9SHarry Wentland 					grouped_pipes[i]->stream_res.tg, &gsl_params);
24324562236bSHarry Wentland 
24334562236bSHarry Wentland 	/* Reset slave controllers on master VSync */
24344562236bSHarry Wentland 	DC_SYNC_INFO("GSL: enabling trigger-reset\n");
24354562236bSHarry Wentland 
24364562236bSHarry Wentland 	for (i = 1 /* skip the master */; i < group_size; i++)
24376b670fa9SHarry Wentland 		grouped_pipes[i]->stream_res.tg->funcs->enable_reset_trigger(
2438fa2123dbSMikita Lipski 				grouped_pipes[i]->stream_res.tg,
2439fa2123dbSMikita Lipski 				gsl_params.gsl_group);
24404562236bSHarry Wentland 
24414562236bSHarry Wentland 	for (i = 1 /* skip the master */; i < group_size; i++) {
24424562236bSHarry Wentland 		DC_SYNC_INFO("GSL: waiting for reset to occur.\n");
24436b670fa9SHarry Wentland 		wait_for_reset_trigger_to_occur(dc_ctx, grouped_pipes[i]->stream_res.tg);
2444fa2123dbSMikita Lipski 		grouped_pipes[i]->stream_res.tg->funcs->disable_reset_trigger(
2445fa2123dbSMikita Lipski 				grouped_pipes[i]->stream_res.tg);
24464562236bSHarry Wentland 	}
24474562236bSHarry Wentland 
24484562236bSHarry Wentland 	/* GSL Vblank synchronization is a one time sync mechanism, assumption
24494562236bSHarry Wentland 	 * is that the sync'ed displays will not drift out of sync over time*/
24504562236bSHarry Wentland 	DC_SYNC_INFO("GSL: Restoring register states.\n");
24514562236bSHarry Wentland 	for (i = 0; i < group_size; i++)
24526b670fa9SHarry Wentland 		grouped_pipes[i]->stream_res.tg->funcs->tear_down_global_swap_lock(grouped_pipes[i]->stream_res.tg);
24534562236bSHarry Wentland 
24544562236bSHarry Wentland 	DC_SYNC_INFO("GSL: Set-up complete.\n");
24554562236bSHarry Wentland }
24564562236bSHarry Wentland 
2457fa2123dbSMikita Lipski static void dce110_enable_per_frame_crtc_position_reset(
2458fa2123dbSMikita Lipski 		struct dc *dc,
2459fa2123dbSMikita Lipski 		int group_size,
2460fa2123dbSMikita Lipski 		struct pipe_ctx *grouped_pipes[])
2461fa2123dbSMikita Lipski {
2462fa2123dbSMikita Lipski 	struct dc_context *dc_ctx = dc->ctx;
2463fa2123dbSMikita Lipski 	struct dcp_gsl_params gsl_params = { 0 };
2464fa2123dbSMikita Lipski 	int i;
2465fa2123dbSMikita Lipski 
2466fa2123dbSMikita Lipski 	gsl_params.gsl_group = 0;
246737cd85ceSDavid Francis 	gsl_params.gsl_master = 0;
2468fa2123dbSMikita Lipski 
2469fa2123dbSMikita Lipski 	for (i = 0; i < group_size; i++)
2470fa2123dbSMikita Lipski 		grouped_pipes[i]->stream_res.tg->funcs->setup_global_swap_lock(
2471fa2123dbSMikita Lipski 					grouped_pipes[i]->stream_res.tg, &gsl_params);
2472fa2123dbSMikita Lipski 
2473fa2123dbSMikita Lipski 	DC_SYNC_INFO("GSL: enabling trigger-reset\n");
2474fa2123dbSMikita Lipski 
2475fa2123dbSMikita Lipski 	for (i = 1; i < group_size; i++)
2476fa2123dbSMikita Lipski 		grouped_pipes[i]->stream_res.tg->funcs->enable_crtc_reset(
2477fa2123dbSMikita Lipski 				grouped_pipes[i]->stream_res.tg,
2478fa2123dbSMikita Lipski 				gsl_params.gsl_master,
2479fa2123dbSMikita Lipski 				&grouped_pipes[i]->stream->triggered_crtc_reset);
2480fa2123dbSMikita Lipski 
2481fa2123dbSMikita Lipski 	DC_SYNC_INFO("GSL: waiting for reset to occur.\n");
2482fa2123dbSMikita Lipski 	for (i = 1; i < group_size; i++)
2483fa2123dbSMikita Lipski 		wait_for_reset_trigger_to_occur(dc_ctx, grouped_pipes[i]->stream_res.tg);
2484fa2123dbSMikita Lipski 
2485fa2123dbSMikita Lipski 	for (i = 0; i < group_size; i++)
2486fa2123dbSMikita Lipski 		grouped_pipes[i]->stream_res.tg->funcs->tear_down_global_swap_lock(grouped_pipes[i]->stream_res.tg);
2487fa2123dbSMikita Lipski 
2488fa2123dbSMikita Lipski }
2489fa2123dbSMikita Lipski 
2490fb55546eSAnthony Koo static void init_pipes(struct dc *dc, struct dc_state *context)
2491fb55546eSAnthony Koo {
2492fb55546eSAnthony Koo 	// Do nothing
2493fb55546eSAnthony Koo }
2494fb55546eSAnthony Koo 
2495fb3466a4SBhawanpreet Lakha static void init_hw(struct dc *dc)
24964562236bSHarry Wentland {
24974562236bSHarry Wentland 	int i;
24984562236bSHarry Wentland 	struct dc_bios *bp;
24994562236bSHarry Wentland 	struct transform *xfm;
25005e7773a2SAnthony Koo 	struct abm *abm;
250170d9e8cbSPaul Hsieh 	struct dmcu *dmcu;
2502f42ea55bSAnthony Koo 	struct dce_hwseq *hws = dc->hwseq;
25033ba01817SYongqiang Sun 	uint32_t backlight = MAX_BACKLIGHT_LEVEL;
25044562236bSHarry Wentland 
25054562236bSHarry Wentland 	bp = dc->ctx->dc_bios;
25064562236bSHarry Wentland 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
25074562236bSHarry Wentland 		xfm = dc->res_pool->transforms[i];
25084562236bSHarry Wentland 		xfm->funcs->transform_reset(xfm);
25094562236bSHarry Wentland 
2510f42ea55bSAnthony Koo 		hws->funcs.enable_display_power_gating(
25114562236bSHarry Wentland 				dc, i, bp,
25124562236bSHarry Wentland 				PIPE_GATING_CONTROL_INIT);
2513f42ea55bSAnthony Koo 		hws->funcs.enable_display_power_gating(
25144562236bSHarry Wentland 				dc, i, bp,
25154562236bSHarry Wentland 				PIPE_GATING_CONTROL_DISABLE);
2516f42ea55bSAnthony Koo 		hws->funcs.enable_display_pipe_clock_gating(
25174562236bSHarry Wentland 			dc->ctx,
25184562236bSHarry Wentland 			true);
25194562236bSHarry Wentland 	}
25204562236bSHarry Wentland 
2521e166ad43SJulia Lawall 	dce_clock_gating_power_up(dc->hwseq, false);
25224562236bSHarry Wentland 	/***************************************/
25234562236bSHarry Wentland 
25244562236bSHarry Wentland 	for (i = 0; i < dc->link_count; i++) {
25254562236bSHarry Wentland 		/****************************************/
25264562236bSHarry Wentland 		/* Power up AND update implementation according to the
25274562236bSHarry Wentland 		 * required signal (which may be different from the
25284562236bSHarry Wentland 		 * default signal on connector). */
2529d0778ebfSHarry Wentland 		struct dc_link *link = dc->links[i];
2530069d418fSAndrew Jiang 
25314562236bSHarry Wentland 		link->link_enc->funcs->hw_init(link->link_enc);
25324562236bSHarry Wentland 	}
25334562236bSHarry Wentland 
25344562236bSHarry Wentland 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
25354562236bSHarry Wentland 		struct timing_generator *tg = dc->res_pool->timing_generators[i];
25364562236bSHarry Wentland 
25374562236bSHarry Wentland 		tg->funcs->disable_vga(tg);
25384562236bSHarry Wentland 
25394562236bSHarry Wentland 		/* Blank controller using driver code instead of
25404562236bSHarry Wentland 		 * command table. */
25414562236bSHarry Wentland 		tg->funcs->set_blank(tg, true);
25424b5e7d62SHersen Wu 		hwss_wait_for_blank_complete(tg);
25434562236bSHarry Wentland 	}
25444562236bSHarry Wentland 
25454562236bSHarry Wentland 	for (i = 0; i < dc->res_pool->audio_count; i++) {
25464562236bSHarry Wentland 		struct audio *audio = dc->res_pool->audios[i];
25474562236bSHarry Wentland 		audio->funcs->hw_init(audio);
25484562236bSHarry Wentland 	}
25495e7773a2SAnthony Koo 
25503ba01817SYongqiang Sun 	for (i = 0; i < dc->link_count; i++) {
25513ba01817SYongqiang Sun 		struct dc_link *link = dc->links[i];
25523ba01817SYongqiang Sun 
25533ba01817SYongqiang Sun 		if (link->panel_cntl)
25543ba01817SYongqiang Sun 			backlight = link->panel_cntl->funcs->hw_init(link->panel_cntl);
25554562236bSHarry Wentland 	}
25565099114bSAlex Deucher 
25573ba01817SYongqiang Sun 	abm = dc->res_pool->abm;
25583ba01817SYongqiang Sun 	if (abm != NULL)
25593ba01817SYongqiang Sun 		abm->funcs->abm_init(abm, backlight);
25603ba01817SYongqiang Sun 
256170d9e8cbSPaul Hsieh 	dmcu = dc->res_pool->dmcu;
256270d9e8cbSPaul Hsieh 	if (dmcu != NULL && abm != NULL)
256370d9e8cbSPaul Hsieh 		abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
256470d9e8cbSPaul Hsieh 
25652f3bfb27SRoman Li 	if (dc->fbc_compressor)
25661663ae1cSBhawanpreet Lakha 		dc->fbc_compressor->funcs->power_up_fbc(dc->fbc_compressor);
2567690b5e39SRoman Li 
25686728b30cSAnthony Koo }
25694562236bSHarry Wentland 
25709566b675SDmytro Laktyushkin 
25719566b675SDmytro Laktyushkin void dce110_prepare_bandwidth(
2572fb3466a4SBhawanpreet Lakha 		struct dc *dc,
25739566b675SDmytro Laktyushkin 		struct dc_state *context)
2574cf437593SDmytro Laktyushkin {
2575dc88b4a6SEric Yang 	struct clk_mgr *dccg = dc->clk_mgr;
2576fab55d61SDmytro Laktyushkin 
2577fab55d61SDmytro Laktyushkin 	dce110_set_safe_displaymarks(&context->res_ctx, dc->res_pool);
2578cf437593SDmytro Laktyushkin 
25795a83c932SNicholas Kazlauskas 	dccg->funcs->update_clocks(
25805a83c932SNicholas Kazlauskas 			dccg,
258124f7dd7eSDmytro Laktyushkin 			context,
25829566b675SDmytro Laktyushkin 			false);
25839566b675SDmytro Laktyushkin }
25849566b675SDmytro Laktyushkin 
25859566b675SDmytro Laktyushkin void dce110_optimize_bandwidth(
25869566b675SDmytro Laktyushkin 		struct dc *dc,
25879566b675SDmytro Laktyushkin 		struct dc_state *context)
25889566b675SDmytro Laktyushkin {
2589dc88b4a6SEric Yang 	struct clk_mgr *dccg = dc->clk_mgr;
25909566b675SDmytro Laktyushkin 
25919566b675SDmytro Laktyushkin 	dce110_set_displaymarks(dc, context);
25929566b675SDmytro Laktyushkin 
25939566b675SDmytro Laktyushkin 	dccg->funcs->update_clocks(
25949566b675SDmytro Laktyushkin 			dccg,
25959566b675SDmytro Laktyushkin 			context,
25969566b675SDmytro Laktyushkin 			true);
25974562236bSHarry Wentland }
25984562236bSHarry Wentland 
25994562236bSHarry Wentland static void dce110_program_front_end_for_pipe(
2600fb3466a4SBhawanpreet Lakha 		struct dc *dc, struct pipe_ctx *pipe_ctx)
26014562236bSHarry Wentland {
260286a66c4eSHarry Wentland 	struct mem_input *mi = pipe_ctx->plane_res.mi;
26033be5262eSHarry Wentland 	struct dc_plane_state *plane_state = pipe_ctx->plane_state;
26044562236bSHarry Wentland 	struct xfm_grph_csc_adjustment adjust;
26054562236bSHarry Wentland 	struct out_csc_color_matrix tbl_entry;
26064562236bSHarry Wentland 	unsigned int i;
2607f42ea55bSAnthony Koo 	struct dce_hwseq *hws = dc->hwseq;
2608f42ea55bSAnthony Koo 
26095d4b05ddSBhawanpreet Lakha 	DC_LOGGER_INIT();
26104562236bSHarry Wentland 	memset(&tbl_entry, 0, sizeof(tbl_entry));
26114562236bSHarry Wentland 
26124562236bSHarry Wentland 	memset(&adjust, 0, sizeof(adjust));
26134562236bSHarry Wentland 	adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_BYPASS;
26144562236bSHarry Wentland 
2615e07f541fSYongqiang Sun 	dce_enable_fe_clock(dc->hwseq, mi->inst, true);
26164562236bSHarry Wentland 
26174562236bSHarry Wentland 	set_default_colors(pipe_ctx);
26184fa086b9SLeo (Sunpeng) Li 	if (pipe_ctx->stream->csc_color_matrix.enable_adjustment
26194562236bSHarry Wentland 			== true) {
26204562236bSHarry Wentland 		tbl_entry.color_space =
26214fa086b9SLeo (Sunpeng) Li 			pipe_ctx->stream->output_color_space;
26224562236bSHarry Wentland 
26234562236bSHarry Wentland 		for (i = 0; i < 12; i++)
26244562236bSHarry Wentland 			tbl_entry.regval[i] =
26254fa086b9SLeo (Sunpeng) Li 			pipe_ctx->stream->csc_color_matrix.matrix[i];
26264562236bSHarry Wentland 
262786a66c4eSHarry Wentland 		pipe_ctx->plane_res.xfm->funcs->opp_set_csc_adjustment
262886a66c4eSHarry Wentland 				(pipe_ctx->plane_res.xfm, &tbl_entry);
26294562236bSHarry Wentland 	}
26304562236bSHarry Wentland 
26314fa086b9SLeo (Sunpeng) Li 	if (pipe_ctx->stream->gamut_remap_matrix.enable_remap == true) {
26324562236bSHarry Wentland 		adjust.gamut_adjust_type = GRAPHICS_GAMUT_ADJUST_TYPE_SW;
2633146a9f63SKrunoslav Kovac 
2634146a9f63SKrunoslav Kovac 		for (i = 0; i < CSC_TEMPERATURE_MATRIX_SIZE; i++)
2635146a9f63SKrunoslav Kovac 			adjust.temperature_matrix[i] =
2636146a9f63SKrunoslav Kovac 				pipe_ctx->stream->gamut_remap_matrix.matrix[i];
26374562236bSHarry Wentland 	}
26384562236bSHarry Wentland 
263986a66c4eSHarry Wentland 	pipe_ctx->plane_res.xfm->funcs->transform_set_gamut_remap(pipe_ctx->plane_res.xfm, &adjust);
26404562236bSHarry Wentland 
26416702a9acSHarry Wentland 	pipe_ctx->plane_res.scl_data.lb_params.alpha_en = pipe_ctx->bottom_pipe != 0;
2642c1473558SAndrey Grodzovsky 
26434562236bSHarry Wentland 	program_scaler(dc, pipe_ctx);
26444562236bSHarry Wentland 
26454562236bSHarry Wentland 	mi->funcs->mem_input_program_surface_config(
26464562236bSHarry Wentland 			mi,
26473be5262eSHarry Wentland 			plane_state->format,
26483be5262eSHarry Wentland 			&plane_state->tiling_info,
26493be5262eSHarry Wentland 			&plane_state->plane_size,
26503be5262eSHarry Wentland 			plane_state->rotation,
2651624d7c47SYongqiang Sun 			NULL,
26524b28b76bSDmytro Laktyushkin 			false);
26534b28b76bSDmytro Laktyushkin 	if (mi->funcs->set_blank)
26543be5262eSHarry Wentland 		mi->funcs->set_blank(mi, pipe_ctx->plane_state->visible);
26554562236bSHarry Wentland 
2656fb3466a4SBhawanpreet Lakha 	if (dc->config.gpu_vm_support)
26574562236bSHarry Wentland 		mi->funcs->mem_input_program_pte_vm(
265886a66c4eSHarry Wentland 				pipe_ctx->plane_res.mi,
26593be5262eSHarry Wentland 				plane_state->format,
26603be5262eSHarry Wentland 				&plane_state->tiling_info,
26613be5262eSHarry Wentland 				plane_state->rotation);
26624562236bSHarry Wentland 
2663067c878aSYongqiang Sun 	/* Moved programming gamma from dc to hwss */
2664405c50a0SAndrew Jiang 	if (pipe_ctx->plane_state->update_flags.bits.full_update ||
2665405c50a0SAndrew Jiang 			pipe_ctx->plane_state->update_flags.bits.in_transfer_func_change ||
2666405c50a0SAndrew Jiang 			pipe_ctx->plane_state->update_flags.bits.gamma_change)
2667f42ea55bSAnthony Koo 		hws->funcs.set_input_transfer_func(dc, pipe_ctx, pipe_ctx->plane_state);
2668405c50a0SAndrew Jiang 
2669405c50a0SAndrew Jiang 	if (pipe_ctx->plane_state->update_flags.bits.full_update)
2670f42ea55bSAnthony Koo 		hws->funcs.set_output_transfer_func(dc, pipe_ctx, pipe_ctx->stream);
2671067c878aSYongqiang Sun 
26721296423bSBhawanpreet Lakha 	DC_LOG_SURFACE(
26733032deb5SBhawanpreet Lakha 			"Pipe:%d %p: addr hi:0x%x, "
26744562236bSHarry Wentland 			"addr low:0x%x, "
26754562236bSHarry Wentland 			"src: %d, %d, %d,"
26764562236bSHarry Wentland 			" %d; dst: %d, %d, %d, %d;"
26774562236bSHarry Wentland 			"clip: %d, %d, %d, %d\n",
26784562236bSHarry Wentland 			pipe_ctx->pipe_idx,
26793032deb5SBhawanpreet Lakha 			(void *) pipe_ctx->plane_state,
26803be5262eSHarry Wentland 			pipe_ctx->plane_state->address.grph.addr.high_part,
26813be5262eSHarry Wentland 			pipe_ctx->plane_state->address.grph.addr.low_part,
26823be5262eSHarry Wentland 			pipe_ctx->plane_state->src_rect.x,
26833be5262eSHarry Wentland 			pipe_ctx->plane_state->src_rect.y,
26843be5262eSHarry Wentland 			pipe_ctx->plane_state->src_rect.width,
26853be5262eSHarry Wentland 			pipe_ctx->plane_state->src_rect.height,
26863be5262eSHarry Wentland 			pipe_ctx->plane_state->dst_rect.x,
26873be5262eSHarry Wentland 			pipe_ctx->plane_state->dst_rect.y,
26883be5262eSHarry Wentland 			pipe_ctx->plane_state->dst_rect.width,
26893be5262eSHarry Wentland 			pipe_ctx->plane_state->dst_rect.height,
26903be5262eSHarry Wentland 			pipe_ctx->plane_state->clip_rect.x,
26913be5262eSHarry Wentland 			pipe_ctx->plane_state->clip_rect.y,
26923be5262eSHarry Wentland 			pipe_ctx->plane_state->clip_rect.width,
26933be5262eSHarry Wentland 			pipe_ctx->plane_state->clip_rect.height);
26944562236bSHarry Wentland 
26951296423bSBhawanpreet Lakha 	DC_LOG_SURFACE(
26964562236bSHarry Wentland 			"Pipe %d: width, height, x, y\n"
26974562236bSHarry Wentland 			"viewport:%d, %d, %d, %d\n"
26984562236bSHarry Wentland 			"recout:  %d, %d, %d, %d\n",
26994562236bSHarry Wentland 			pipe_ctx->pipe_idx,
27006702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.viewport.width,
27016702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.viewport.height,
27026702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.viewport.x,
27036702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.viewport.y,
27046702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.width,
27056702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.height,
27066702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.x,
27076702a9acSHarry Wentland 			pipe_ctx->plane_res.scl_data.recout.y);
27084562236bSHarry Wentland }
27094562236bSHarry Wentland 
27104562236bSHarry Wentland static void dce110_apply_ctx_for_surface(
2711fb3466a4SBhawanpreet Lakha 		struct dc *dc,
27123e9ad616SEric Yang 		const struct dc_stream_state *stream,
27133e9ad616SEric Yang 		int num_planes,
2714608ac7bbSJerry Zuo 		struct dc_state *context)
27154562236bSHarry Wentland {
27162194e3aeSRoman Li 	int i;
27174562236bSHarry Wentland 
27183e9ad616SEric Yang 	if (num_planes == 0)
27194562236bSHarry Wentland 		return;
27204562236bSHarry Wentland 
272165d38262Shersen wu 	if (dc->fbc_compressor)
272265d38262Shersen wu 		dc->fbc_compressor->funcs->disable_fbc(dc->fbc_compressor);
272365d38262Shersen wu 
27243e9ad616SEric Yang 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
27253dc780ecSYongqiang Sun 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
27264562236bSHarry Wentland 
2727a2607aefSHarry Wentland 		if (pipe_ctx->stream != stream)
27284562236bSHarry Wentland 			continue;
27294562236bSHarry Wentland 
27303b21b6d2SJerry Zuo 		/* Need to allocate mem before program front end for Fiji */
27313b21b6d2SJerry Zuo 		pipe_ctx->plane_res.mi->funcs->allocate_mem_input(
27323b21b6d2SJerry Zuo 				pipe_ctx->plane_res.mi,
27333b21b6d2SJerry Zuo 				pipe_ctx->stream->timing.h_total,
27343b21b6d2SJerry Zuo 				pipe_ctx->stream->timing.v_total,
2735380604e2SKen Chalmers 				pipe_ctx->stream->timing.pix_clk_100hz / 10,
27363b21b6d2SJerry Zuo 				context->stream_count);
27373b21b6d2SJerry Zuo 
27384562236bSHarry Wentland 		dce110_program_front_end_for_pipe(dc, pipe_ctx);
27394f804817SYongqiang Sun 
27404f804817SYongqiang Sun 		dc->hwss.update_plane_addr(dc, pipe_ctx);
27414f804817SYongqiang Sun 
2742b06b7680SLeon Elazar 		program_surface_visibility(dc, pipe_ctx);
27434562236bSHarry Wentland 
27444562236bSHarry Wentland 	}
27453dc780ecSYongqiang Sun 
274665d38262Shersen wu 	if (dc->fbc_compressor)
274712a8bd88SShirish S 		enable_fbc(dc, context);
27484562236bSHarry Wentland }
27494562236bSHarry Wentland 
2750bbf5f6c3SAnthony Koo static void dce110_post_unlock_program_front_end(
2751bbf5f6c3SAnthony Koo 		struct dc *dc,
2752bbf5f6c3SAnthony Koo 		struct dc_state *context)
2753bbf5f6c3SAnthony Koo {
2754bbf5f6c3SAnthony Koo }
2755009114f6SAnthony Koo 
2756e6c258cbSYongqiang Sun static void dce110_power_down_fe(struct dc *dc, struct pipe_ctx *pipe_ctx)
27574562236bSHarry Wentland {
2758f42ea55bSAnthony Koo 	struct dce_hwseq *hws = dc->hwseq;
2759bc373a89SRoman Li 	int fe_idx = pipe_ctx->plane_res.mi ?
2760bc373a89SRoman Li 		pipe_ctx->plane_res.mi->inst : pipe_ctx->pipe_idx;
2761e6c258cbSYongqiang Sun 
27627950f0f9SDmytro Laktyushkin 	/* Do not power down fe when stream is active on dce*/
2763608ac7bbSJerry Zuo 	if (dc->current_state->res_ctx.pipe_ctx[fe_idx].stream)
27644562236bSHarry Wentland 		return;
27654562236bSHarry Wentland 
2766f42ea55bSAnthony Koo 	hws->funcs.enable_display_power_gating(
2767cfe4645eSDmytro Laktyushkin 		dc, fe_idx, dc->ctx->dc_bios, PIPE_GATING_CONTROL_ENABLE);
2768cfe4645eSDmytro Laktyushkin 
2769cfe4645eSDmytro Laktyushkin 	dc->res_pool->transforms[fe_idx]->funcs->transform_reset(
2770cfe4645eSDmytro Laktyushkin 				dc->res_pool->transforms[fe_idx]);
27714562236bSHarry Wentland }
27724562236bSHarry Wentland 
27736be425f3SEric Yang static void dce110_wait_for_mpcc_disconnect(
2774fb3466a4SBhawanpreet Lakha 		struct dc *dc,
27756be425f3SEric Yang 		struct resource_pool *res_pool,
27766be425f3SEric Yang 		struct pipe_ctx *pipe_ctx)
2777b6762f0cSEric Yang {
2778b6762f0cSEric Yang 	/* do nothing*/
2779b6762f0cSEric Yang }
2780b6762f0cSEric Yang 
27814bd0dc68SJoshua Aberback static void program_output_csc(struct dc *dc,
27824bd0dc68SJoshua Aberback 		struct pipe_ctx *pipe_ctx,
27834bd0dc68SJoshua Aberback 		enum dc_color_space colorspace,
27844bd0dc68SJoshua Aberback 		uint16_t *matrix,
27854bd0dc68SJoshua Aberback 		int opp_id)
27864bd0dc68SJoshua Aberback {
27874bd0dc68SJoshua Aberback 	int i;
27884bd0dc68SJoshua Aberback 	struct out_csc_color_matrix tbl_entry;
27894bd0dc68SJoshua Aberback 
27904bd0dc68SJoshua Aberback 	if (pipe_ctx->stream->csc_color_matrix.enable_adjustment == true) {
27914bd0dc68SJoshua Aberback 		enum dc_color_space color_space = pipe_ctx->stream->output_color_space;
27924bd0dc68SJoshua Aberback 
27934bd0dc68SJoshua Aberback 		for (i = 0; i < 12; i++)
27944bd0dc68SJoshua Aberback 			tbl_entry.regval[i] = pipe_ctx->stream->csc_color_matrix.matrix[i];
27954bd0dc68SJoshua Aberback 
27964bd0dc68SJoshua Aberback 		tbl_entry.color_space = color_space;
27974bd0dc68SJoshua Aberback 
27984bd0dc68SJoshua Aberback 		pipe_ctx->plane_res.xfm->funcs->opp_set_csc_adjustment(
27994bd0dc68SJoshua Aberback 				pipe_ctx->plane_res.xfm, &tbl_entry);
28004bd0dc68SJoshua Aberback 	}
28014bd0dc68SJoshua Aberback }
28024bd0dc68SJoshua Aberback 
2803faf0389fSJason Yan static void dce110_set_cursor_position(struct pipe_ctx *pipe_ctx)
280433fd17d9SEric Yang {
280533fd17d9SEric Yang 	struct dc_cursor_position pos_cpy = pipe_ctx->stream->cursor_position;
280633fd17d9SEric Yang 	struct input_pixel_processor *ipp = pipe_ctx->plane_res.ipp;
280733fd17d9SEric Yang 	struct mem_input *mi = pipe_ctx->plane_res.mi;
280833fd17d9SEric Yang 	struct dc_cursor_mi_param param = {
2809380604e2SKen Chalmers 		.pixel_clk_khz = pipe_ctx->stream->timing.pix_clk_100hz / 10,
281033d7598dSJun Lei 		.ref_clk_khz = pipe_ctx->stream->ctx->dc->res_pool->ref_clocks.xtalin_clock_inKhz,
281139a9f4d8SDmytro Laktyushkin 		.viewport = pipe_ctx->plane_res.scl_data.viewport,
281239a9f4d8SDmytro Laktyushkin 		.h_scale_ratio = pipe_ctx->plane_res.scl_data.ratios.horz,
281339a9f4d8SDmytro Laktyushkin 		.v_scale_ratio = pipe_ctx->plane_res.scl_data.ratios.vert,
281408ed681cSDmytro Laktyushkin 		.rotation = pipe_ctx->plane_state->rotation,
281508ed681cSDmytro Laktyushkin 		.mirror = pipe_ctx->plane_state->horizontal_mirror
281633fd17d9SEric Yang 	};
281733fd17d9SEric Yang 
281803a4059bSNicholas Kazlauskas 	/**
281903a4059bSNicholas Kazlauskas 	 * If the cursor's source viewport is clipped then we need to
282003a4059bSNicholas Kazlauskas 	 * translate the cursor to appear in the correct position on
282103a4059bSNicholas Kazlauskas 	 * the screen.
282203a4059bSNicholas Kazlauskas 	 *
282303a4059bSNicholas Kazlauskas 	 * This translation isn't affected by scaling so it needs to be
282403a4059bSNicholas Kazlauskas 	 * done *after* we adjust the position for the scale factor.
2825033baeeeSNicholas Kazlauskas 	 *
2826033baeeeSNicholas Kazlauskas 	 * This is only done by opt-in for now since there are still
2827033baeeeSNicholas Kazlauskas 	 * some usecases like tiled display that might enable the
2828033baeeeSNicholas Kazlauskas 	 * cursor on both streams while expecting dc to clip it.
282903a4059bSNicholas Kazlauskas 	 */
2830033baeeeSNicholas Kazlauskas 	if (pos_cpy.translate_by_source) {
283103a4059bSNicholas Kazlauskas 		pos_cpy.x += pipe_ctx->plane_state->src_rect.x;
283203a4059bSNicholas Kazlauskas 		pos_cpy.y += pipe_ctx->plane_state->src_rect.y;
2833033baeeeSNicholas Kazlauskas 	}
283403a4059bSNicholas Kazlauskas 
283533fd17d9SEric Yang 	if (pipe_ctx->plane_state->address.type
283633fd17d9SEric Yang 			== PLN_ADDR_TYPE_VIDEO_PROGRESSIVE)
283733fd17d9SEric Yang 		pos_cpy.enable = false;
283833fd17d9SEric Yang 
283933fd17d9SEric Yang 	if (pipe_ctx->top_pipe && pipe_ctx->plane_state != pipe_ctx->top_pipe->plane_state)
284033fd17d9SEric Yang 		pos_cpy.enable = false;
284133fd17d9SEric Yang 
2842dc75dd70SRoman Li 	if (ipp->funcs->ipp_cursor_set_position)
284333fd17d9SEric Yang 		ipp->funcs->ipp_cursor_set_position(ipp, &pos_cpy, &param);
2844dc75dd70SRoman Li 	if (mi->funcs->set_cursor_position)
284533fd17d9SEric Yang 		mi->funcs->set_cursor_position(mi, &pos_cpy, &param);
284633fd17d9SEric Yang }
284733fd17d9SEric Yang 
2848faf0389fSJason Yan static void dce110_set_cursor_attribute(struct pipe_ctx *pipe_ctx)
284933fd17d9SEric Yang {
285033fd17d9SEric Yang 	struct dc_cursor_attributes *attributes = &pipe_ctx->stream->cursor_attributes;
285133fd17d9SEric Yang 
2852d1aaad05SHarry Wentland 	if (pipe_ctx->plane_res.ipp &&
2853d1aaad05SHarry Wentland 	    pipe_ctx->plane_res.ipp->funcs->ipp_cursor_set_attributes)
285433fd17d9SEric Yang 		pipe_ctx->plane_res.ipp->funcs->ipp_cursor_set_attributes(
285533fd17d9SEric Yang 				pipe_ctx->plane_res.ipp, attributes);
285633fd17d9SEric Yang 
2857d1aaad05SHarry Wentland 	if (pipe_ctx->plane_res.mi &&
2858d1aaad05SHarry Wentland 	    pipe_ctx->plane_res.mi->funcs->set_cursor_attributes)
285933fd17d9SEric Yang 		pipe_ctx->plane_res.mi->funcs->set_cursor_attributes(
286033fd17d9SEric Yang 				pipe_ctx->plane_res.mi, attributes);
286133fd17d9SEric Yang 
2862d1aaad05SHarry Wentland 	if (pipe_ctx->plane_res.xfm &&
2863d1aaad05SHarry Wentland 	    pipe_ctx->plane_res.xfm->funcs->set_cursor_attributes)
286433fd17d9SEric Yang 		pipe_ctx->plane_res.xfm->funcs->set_cursor_attributes(
286533fd17d9SEric Yang 				pipe_ctx->plane_res.xfm, attributes);
286633fd17d9SEric Yang }
286733fd17d9SEric Yang 
28684b0e95d1SYongqiang Sun bool dce110_set_backlight_level(struct pipe_ctx *pipe_ctx,
28694b0e95d1SYongqiang Sun 		uint32_t backlight_pwm_u16_16,
28704b0e95d1SYongqiang Sun 		uint32_t frame_ramp)
28714b0e95d1SYongqiang Sun {
28724b0e95d1SYongqiang Sun 	struct dc_link *link = pipe_ctx->stream->link;
28734b0e95d1SYongqiang Sun 	struct dc  *dc = link->ctx->dc;
28744b0e95d1SYongqiang Sun 	struct abm *abm = pipe_ctx->stream_res.abm;
28753ba01817SYongqiang Sun 	struct panel_cntl *panel_cntl = link->panel_cntl;
28764b0e95d1SYongqiang Sun 	struct dmcu *dmcu = dc->res_pool->dmcu;
28774b0e95d1SYongqiang Sun 	bool fw_set_brightness = true;
28784b0e95d1SYongqiang Sun 	/* DMCU -1 for all controller id values,
28794b0e95d1SYongqiang Sun 	 * therefore +1 here
28804b0e95d1SYongqiang Sun 	 */
28814b0e95d1SYongqiang Sun 	uint32_t controller_id = pipe_ctx->stream_res.tg->inst + 1;
28824b0e95d1SYongqiang Sun 
28833ba01817SYongqiang Sun 	if (abm == NULL || panel_cntl == NULL || (abm->funcs->set_backlight_level_pwm == NULL))
28844b0e95d1SYongqiang Sun 		return false;
28854b0e95d1SYongqiang Sun 
28864b0e95d1SYongqiang Sun 	if (dmcu)
28874b0e95d1SYongqiang Sun 		fw_set_brightness = dmcu->funcs->is_dmcu_initialized(dmcu);
28884b0e95d1SYongqiang Sun 
28893ba01817SYongqiang Sun 	if (!fw_set_brightness && panel_cntl->funcs->driver_set_backlight)
28903ba01817SYongqiang Sun 		panel_cntl->funcs->driver_set_backlight(panel_cntl, backlight_pwm_u16_16);
28913ba01817SYongqiang Sun 	else
28924b0e95d1SYongqiang Sun 		abm->funcs->set_backlight_level_pwm(
28934b0e95d1SYongqiang Sun 				abm,
28944b0e95d1SYongqiang Sun 				backlight_pwm_u16_16,
28954b0e95d1SYongqiang Sun 				frame_ramp,
28964b0e95d1SYongqiang Sun 				controller_id,
28973ba01817SYongqiang Sun 				link->panel_cntl->inst);
28984b0e95d1SYongqiang Sun 
28994b0e95d1SYongqiang Sun 	return true;
29004b0e95d1SYongqiang Sun }
29014b0e95d1SYongqiang Sun 
29023ba01817SYongqiang Sun void dce110_set_abm_immediate_disable(struct pipe_ctx *pipe_ctx)
29033ba01817SYongqiang Sun {
29043ba01817SYongqiang Sun 	struct abm *abm = pipe_ctx->stream_res.abm;
29053ba01817SYongqiang Sun 	struct panel_cntl *panel_cntl = pipe_ctx->stream->link->panel_cntl;
29063ba01817SYongqiang Sun 
29073ba01817SYongqiang Sun 	if (abm)
29083ba01817SYongqiang Sun 		abm->funcs->set_abm_immediate_disable(abm,
29093ba01817SYongqiang Sun 				pipe_ctx->stream->link->panel_cntl->inst);
29103ba01817SYongqiang Sun 
29113ba01817SYongqiang Sun 	if (panel_cntl)
29123ba01817SYongqiang Sun 		panel_cntl->funcs->store_backlight_level(panel_cntl);
29133ba01817SYongqiang Sun }
29143ba01817SYongqiang Sun 
2915474ac4a8SYongqiang Sun void dce110_set_pipe(struct pipe_ctx *pipe_ctx)
2916474ac4a8SYongqiang Sun {
2917474ac4a8SYongqiang Sun 	struct abm *abm = pipe_ctx->stream_res.abm;
2918474ac4a8SYongqiang Sun 	struct panel_cntl *panel_cntl = pipe_ctx->stream->link->panel_cntl;
2919474ac4a8SYongqiang Sun 	uint32_t otg_inst = pipe_ctx->stream_res.tg->inst + 1;
2920474ac4a8SYongqiang Sun 
2921474ac4a8SYongqiang Sun 	if (abm && panel_cntl)
2922474ac4a8SYongqiang Sun 		abm->funcs->set_pipe(abm, otg_inst, panel_cntl->inst);
2923474ac4a8SYongqiang Sun }
2924474ac4a8SYongqiang Sun 
29254562236bSHarry Wentland static const struct hw_sequencer_funcs dce110_funcs = {
29261bf56e62SZeyu Fan 	.program_gamut_remap = program_gamut_remap,
29274bd0dc68SJoshua Aberback 	.program_output_csc = program_output_csc,
29284562236bSHarry Wentland 	.init_hw = init_hw,
29294562236bSHarry Wentland 	.apply_ctx_to_hw = dce110_apply_ctx_to_hw,
29304562236bSHarry Wentland 	.apply_ctx_for_surface = dce110_apply_ctx_for_surface,
2931bbf5f6c3SAnthony Koo 	.post_unlock_program_front_end = dce110_post_unlock_program_front_end,
29324562236bSHarry Wentland 	.update_plane_addr = update_plane_addr,
29334562236bSHarry Wentland 	.update_pending_status = dce110_update_pending_status,
29344562236bSHarry Wentland 	.enable_accelerated_mode = dce110_enable_accelerated_mode,
29354562236bSHarry Wentland 	.enable_timing_synchronization = dce110_enable_timing_synchronization,
2936fa2123dbSMikita Lipski 	.enable_per_frame_crtc_position_reset = dce110_enable_per_frame_crtc_position_reset,
29374562236bSHarry Wentland 	.update_info_frame = dce110_update_info_frame,
29384562236bSHarry Wentland 	.enable_stream = dce110_enable_stream,
29394562236bSHarry Wentland 	.disable_stream = dce110_disable_stream,
29404562236bSHarry Wentland 	.unblank_stream = dce110_unblank_stream,
294141b49742SCharlene Liu 	.blank_stream = dce110_blank_stream,
29421a05873fSAnthony Koo 	.enable_audio_stream = dce110_enable_audio_stream,
29431a05873fSAnthony Koo 	.disable_audio_stream = dce110_disable_audio_stream,
29447f914a62SYongqiang Sun 	.disable_plane = dce110_power_down_fe,
29454562236bSHarry Wentland 	.pipe_control_lock = dce_pipe_control_lock,
2946009114f6SAnthony Koo 	.interdependent_update_lock = NULL,
29471e461c37SAric Cyr 	.cursor_lock = dce_pipe_control_lock,
29489566b675SDmytro Laktyushkin 	.prepare_bandwidth = dce110_prepare_bandwidth,
29499566b675SDmytro Laktyushkin 	.optimize_bandwidth = dce110_optimize_bandwidth,
29504562236bSHarry Wentland 	.set_drr = set_drr,
295172ada5f7SEric Cook 	.get_position = get_position,
29524562236bSHarry Wentland 	.set_static_screen_control = set_static_screen_control,
295315e17335SCharlene Liu 	.setup_stereo = NULL,
295415e17335SCharlene Liu 	.set_avmute = dce110_set_avmute,
295541f97c07SHersen Wu 	.wait_for_mpcc_disconnect = dce110_wait_for_mpcc_disconnect,
2956099303e9SPeikang Zhang 	.edp_backlight_control = dce110_edp_backlight_control,
29578a31820bSMartin Leung 	.edp_power_control = dce110_edp_power_control,
29588a31820bSMartin Leung 	.edp_wait_for_hpd_ready = dce110_edp_wait_for_hpd_ready,
295933fd17d9SEric Yang 	.set_cursor_position = dce110_set_cursor_position,
29604b0e95d1SYongqiang Sun 	.set_cursor_attribute = dce110_set_cursor_attribute,
29614b0e95d1SYongqiang Sun 	.set_backlight_level = dce110_set_backlight_level,
29623ba01817SYongqiang Sun 	.set_abm_immediate_disable = dce110_set_abm_immediate_disable,
2963474ac4a8SYongqiang Sun 	.set_pipe = dce110_set_pipe,
29644562236bSHarry Wentland };
29654562236bSHarry Wentland 
2966f42ea55bSAnthony Koo static const struct hwseq_private_funcs dce110_private_funcs = {
2967f42ea55bSAnthony Koo 	.init_pipes = init_pipes,
2968f42ea55bSAnthony Koo 	.update_plane_addr = update_plane_addr,
2969f42ea55bSAnthony Koo 	.set_input_transfer_func = dce110_set_input_transfer_func,
2970f42ea55bSAnthony Koo 	.set_output_transfer_func = dce110_set_output_transfer_func,
2971f42ea55bSAnthony Koo 	.power_down = dce110_power_down,
2972f42ea55bSAnthony Koo 	.enable_display_pipe_clock_gating = enable_display_pipe_clock_gating,
2973f42ea55bSAnthony Koo 	.enable_display_power_gating = dce110_enable_display_power_gating,
2974f42ea55bSAnthony Koo 	.reset_hw_ctx_wrap = dce110_reset_hw_ctx_wrap,
2975f42ea55bSAnthony Koo 	.enable_stream_timing = dce110_enable_stream_timing,
2976f42ea55bSAnthony Koo 	.disable_stream_gating = NULL,
2977f42ea55bSAnthony Koo 	.enable_stream_gating = NULL,
2978f42ea55bSAnthony Koo 	.edp_backlight_control = dce110_edp_backlight_control,
2979f42ea55bSAnthony Koo };
2980f42ea55bSAnthony Koo 
2981c13b408bSDave Airlie void dce110_hw_sequencer_construct(struct dc *dc)
29824562236bSHarry Wentland {
29834562236bSHarry Wentland 	dc->hwss = dce110_funcs;
2984f42ea55bSAnthony Koo 	dc->hwseq->funcs = dce110_private_funcs;
29854562236bSHarry Wentland }
29864562236bSHarry Wentland 
2987