1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2016 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  * Authors: AMD
23b843c749SSergey Zigachev  *
24b843c749SSergey Zigachev  */
25b843c749SSergey Zigachev 
26b843c749SSergey Zigachev #include "dm_services.h"
27b843c749SSergey Zigachev 
28b843c749SSergey Zigachev #include "core_types.h"
29b843c749SSergey Zigachev 
30b843c749SSergey Zigachev #include "reg_helper.h"
31b843c749SSergey Zigachev #include "dcn10_dpp.h"
32b843c749SSergey Zigachev #include "basics/conversion.h"
33b843c749SSergey Zigachev 
34b843c749SSergey Zigachev #define NUM_PHASES    64
35b843c749SSergey Zigachev #define HORZ_MAX_TAPS 8
36b843c749SSergey Zigachev #define VERT_MAX_TAPS 8
37b843c749SSergey Zigachev 
38b843c749SSergey Zigachev #define BLACK_OFFSET_RGB_Y 0x0
39b843c749SSergey Zigachev #define BLACK_OFFSET_CBCR  0x8000
40b843c749SSergey Zigachev 
41b843c749SSergey Zigachev #define REG(reg)\
42b843c749SSergey Zigachev 	dpp->tf_regs->reg
43b843c749SSergey Zigachev 
44b843c749SSergey Zigachev #define CTX \
45b843c749SSergey Zigachev 	dpp->base.ctx
46b843c749SSergey Zigachev 
47b843c749SSergey Zigachev #undef FN
48b843c749SSergey Zigachev #define FN(reg_name, field_name) \
49b843c749SSergey Zigachev 	dpp->tf_shift->field_name, dpp->tf_mask->field_name
50b843c749SSergey Zigachev 
51b843c749SSergey Zigachev enum pixel_format_description {
52b843c749SSergey Zigachev 	PIXEL_FORMAT_FIXED = 0,
53b843c749SSergey Zigachev 	PIXEL_FORMAT_FIXED16,
54b843c749SSergey Zigachev 	PIXEL_FORMAT_FLOAT
55b843c749SSergey Zigachev 
56b843c749SSergey Zigachev };
57b843c749SSergey Zigachev 
58b843c749SSergey Zigachev enum dcn10_coef_filter_type_sel {
59b843c749SSergey Zigachev 	SCL_COEF_LUMA_VERT_FILTER = 0,
60b843c749SSergey Zigachev 	SCL_COEF_LUMA_HORZ_FILTER = 1,
61b843c749SSergey Zigachev 	SCL_COEF_CHROMA_VERT_FILTER = 2,
62b843c749SSergey Zigachev 	SCL_COEF_CHROMA_HORZ_FILTER = 3,
63b843c749SSergey Zigachev 	SCL_COEF_ALPHA_VERT_FILTER = 4,
64b843c749SSergey Zigachev 	SCL_COEF_ALPHA_HORZ_FILTER = 5
65b843c749SSergey Zigachev };
66b843c749SSergey Zigachev 
67b843c749SSergey Zigachev enum dscl_autocal_mode {
68b843c749SSergey Zigachev 	AUTOCAL_MODE_OFF = 0,
69b843c749SSergey Zigachev 
70b843c749SSergey Zigachev 	/* Autocal calculate the scaling ratio and initial phase and the
71b843c749SSergey Zigachev 	 * DSCL_MODE_SEL must be set to 1
72b843c749SSergey Zigachev 	 */
73b843c749SSergey Zigachev 	AUTOCAL_MODE_AUTOSCALE = 1,
74b843c749SSergey Zigachev 	/* Autocal perform auto centering without replication and the
75b843c749SSergey Zigachev 	 * DSCL_MODE_SEL must be set to 0
76b843c749SSergey Zigachev 	 */
77b843c749SSergey Zigachev 	AUTOCAL_MODE_AUTOCENTER = 2,
78b843c749SSergey Zigachev 	/* Autocal perform auto centering and auto replication and the
79b843c749SSergey Zigachev 	 * DSCL_MODE_SEL must be set to 0
80b843c749SSergey Zigachev 	 */
81b843c749SSergey Zigachev 	AUTOCAL_MODE_AUTOREPLICATE = 3
82b843c749SSergey Zigachev };
83b843c749SSergey Zigachev 
84b843c749SSergey Zigachev enum dscl_mode_sel {
85b843c749SSergey Zigachev 	DSCL_MODE_SCALING_444_BYPASS = 0,
86b843c749SSergey Zigachev 	DSCL_MODE_SCALING_444_RGB_ENABLE = 1,
87b843c749SSergey Zigachev 	DSCL_MODE_SCALING_444_YCBCR_ENABLE = 2,
88b843c749SSergey Zigachev 	DSCL_MODE_SCALING_420_YCBCR_ENABLE = 3,
89b843c749SSergey Zigachev 	DSCL_MODE_SCALING_420_LUMA_BYPASS = 4,
90b843c749SSergey Zigachev 	DSCL_MODE_SCALING_420_CHROMA_BYPASS = 5,
91b843c749SSergey Zigachev 	DSCL_MODE_DSCL_BYPASS = 6
92b843c749SSergey Zigachev };
93b843c749SSergey Zigachev 
94b843c749SSergey Zigachev enum gamut_remap_select {
95b843c749SSergey Zigachev 	GAMUT_REMAP_BYPASS = 0,
96b843c749SSergey Zigachev 	GAMUT_REMAP_COEFF,
97b843c749SSergey Zigachev 	GAMUT_REMAP_COMA_COEFF,
98b843c749SSergey Zigachev 	GAMUT_REMAP_COMB_COEFF
99b843c749SSergey Zigachev };
100b843c749SSergey Zigachev 
dpp_read_state(struct dpp * dpp_base,struct dcn_dpp_state * s)101b843c749SSergey Zigachev void dpp_read_state(struct dpp *dpp_base,
102b843c749SSergey Zigachev 		struct dcn_dpp_state *s)
103b843c749SSergey Zigachev {
104b843c749SSergey Zigachev 	struct dcn10_dpp *dpp = TO_DCN10_DPP(dpp_base);
105b843c749SSergey Zigachev 
106b843c749SSergey Zigachev 	REG_GET(CM_IGAM_CONTROL,
107b843c749SSergey Zigachev 			CM_IGAM_LUT_MODE, &s->igam_lut_mode);
108b843c749SSergey Zigachev 	REG_GET(CM_IGAM_CONTROL,
109b843c749SSergey Zigachev 			CM_IGAM_INPUT_FORMAT, &s->igam_input_format);
110b843c749SSergey Zigachev 	REG_GET(CM_DGAM_CONTROL,
111b843c749SSergey Zigachev 			CM_DGAM_LUT_MODE, &s->dgam_lut_mode);
112b843c749SSergey Zigachev 	REG_GET(CM_RGAM_CONTROL,
113b843c749SSergey Zigachev 			CM_RGAM_LUT_MODE, &s->rgam_lut_mode);
114b843c749SSergey Zigachev 	REG_GET(CM_GAMUT_REMAP_CONTROL,
115b843c749SSergey Zigachev 			CM_GAMUT_REMAP_MODE, &s->gamut_remap_mode);
116b843c749SSergey Zigachev 
117b843c749SSergey Zigachev 	s->gamut_remap_c11_c12 = REG_READ(CM_GAMUT_REMAP_C11_C12);
118b843c749SSergey Zigachev 	s->gamut_remap_c13_c14 = REG_READ(CM_GAMUT_REMAP_C13_C14);
119b843c749SSergey Zigachev 	s->gamut_remap_c21_c22 = REG_READ(CM_GAMUT_REMAP_C21_C22);
120b843c749SSergey Zigachev 	s->gamut_remap_c23_c24 = REG_READ(CM_GAMUT_REMAP_C23_C24);
121b843c749SSergey Zigachev 	s->gamut_remap_c31_c32 = REG_READ(CM_GAMUT_REMAP_C31_C32);
122b843c749SSergey Zigachev 	s->gamut_remap_c33_c34 = REG_READ(CM_GAMUT_REMAP_C33_C34);
123b843c749SSergey Zigachev }
124b843c749SSergey Zigachev 
125b843c749SSergey Zigachev /* Program gamut remap in bypass mode */
126*78973132SSergey Zigachev #if 0
127b843c749SSergey Zigachev void dpp_set_gamut_remap_bypass(struct dcn10_dpp *dpp)
128b843c749SSergey Zigachev {
129b843c749SSergey Zigachev 	REG_SET(CM_GAMUT_REMAP_CONTROL, 0,
130b843c749SSergey Zigachev 			CM_GAMUT_REMAP_MODE, 0);
131b843c749SSergey Zigachev 	/* Gamut remap in bypass */
132b843c749SSergey Zigachev }
133*78973132SSergey Zigachev #endif
134b843c749SSergey Zigachev 
135b843c749SSergey Zigachev #define IDENTITY_RATIO(ratio) (dc_fixpt_u2d19(ratio) == (1 << 19))
136b843c749SSergey Zigachev 
dpp_get_optimal_number_of_taps(struct dpp * dpp,struct scaler_data * scl_data,const struct scaling_taps * in_taps)137b843c749SSergey Zigachev static bool dpp_get_optimal_number_of_taps(
138b843c749SSergey Zigachev 		struct dpp *dpp,
139b843c749SSergey Zigachev 		struct scaler_data *scl_data,
140b843c749SSergey Zigachev 		const struct scaling_taps *in_taps)
141b843c749SSergey Zigachev {
142b843c749SSergey Zigachev 	uint32_t pixel_width;
143b843c749SSergey Zigachev 
144b843c749SSergey Zigachev 	if (scl_data->viewport.width > scl_data->recout.width)
145b843c749SSergey Zigachev 		pixel_width = scl_data->recout.width;
146b843c749SSergey Zigachev 	else
147b843c749SSergey Zigachev 		pixel_width = scl_data->viewport.width;
148b843c749SSergey Zigachev 
149b843c749SSergey Zigachev 	/* Some ASICs does not support  FP16 scaling, so we reject modes require this*/
150b843c749SSergey Zigachev 	if (scl_data->format == PIXEL_FORMAT_FP16 &&
151b843c749SSergey Zigachev 		dpp->caps->dscl_data_proc_format == DSCL_DATA_PRCESSING_FIXED_FORMAT &&
152b843c749SSergey Zigachev 		scl_data->ratios.horz.value != dc_fixpt_one.value &&
153b843c749SSergey Zigachev 		scl_data->ratios.vert.value != dc_fixpt_one.value)
154b843c749SSergey Zigachev 		return false;
155b843c749SSergey Zigachev 
156b843c749SSergey Zigachev 	if (scl_data->viewport.width > scl_data->h_active &&
157b843c749SSergey Zigachev 		dpp->ctx->dc->debug.max_downscale_src_width != 0 &&
158b843c749SSergey Zigachev 		scl_data->viewport.width > dpp->ctx->dc->debug.max_downscale_src_width)
159b843c749SSergey Zigachev 		return false;
160b843c749SSergey Zigachev 
161b843c749SSergey Zigachev 	/* TODO: add lb check */
162b843c749SSergey Zigachev 
163b843c749SSergey Zigachev 	/* No support for programming ratio of 4, drop to 3.99999.. */
164b843c749SSergey Zigachev 	if (scl_data->ratios.horz.value == (4ll << 32))
165b843c749SSergey Zigachev 		scl_data->ratios.horz.value--;
166b843c749SSergey Zigachev 	if (scl_data->ratios.vert.value == (4ll << 32))
167b843c749SSergey Zigachev 		scl_data->ratios.vert.value--;
168b843c749SSergey Zigachev 	if (scl_data->ratios.horz_c.value == (4ll << 32))
169b843c749SSergey Zigachev 		scl_data->ratios.horz_c.value--;
170b843c749SSergey Zigachev 	if (scl_data->ratios.vert_c.value == (4ll << 32))
171b843c749SSergey Zigachev 		scl_data->ratios.vert_c.value--;
172b843c749SSergey Zigachev 
173b843c749SSergey Zigachev 	/* Set default taps if none are provided */
174b843c749SSergey Zigachev 	if (in_taps->h_taps == 0)
175b843c749SSergey Zigachev 		scl_data->taps.h_taps = 4;
176b843c749SSergey Zigachev 	else
177b843c749SSergey Zigachev 		scl_data->taps.h_taps = in_taps->h_taps;
178b843c749SSergey Zigachev 	if (in_taps->v_taps == 0)
179b843c749SSergey Zigachev 		scl_data->taps.v_taps = 4;
180b843c749SSergey Zigachev 	else
181b843c749SSergey Zigachev 		scl_data->taps.v_taps = in_taps->v_taps;
182b843c749SSergey Zigachev 	if (in_taps->v_taps_c == 0)
183b843c749SSergey Zigachev 		scl_data->taps.v_taps_c = 2;
184b843c749SSergey Zigachev 	else
185b843c749SSergey Zigachev 		scl_data->taps.v_taps_c = in_taps->v_taps_c;
186b843c749SSergey Zigachev 	if (in_taps->h_taps_c == 0)
187b843c749SSergey Zigachev 		scl_data->taps.h_taps_c = 2;
188b843c749SSergey Zigachev 	/* Only 1 and even h_taps_c are supported by hw */
189b843c749SSergey Zigachev 	else if ((in_taps->h_taps_c % 2) != 0 && in_taps->h_taps_c != 1)
190b843c749SSergey Zigachev 		scl_data->taps.h_taps_c = in_taps->h_taps_c - 1;
191b843c749SSergey Zigachev 	else
192b843c749SSergey Zigachev 		scl_data->taps.h_taps_c = in_taps->h_taps_c;
193b843c749SSergey Zigachev 
194b843c749SSergey Zigachev 	if (!dpp->ctx->dc->debug.always_scale) {
195b843c749SSergey Zigachev 		if (IDENTITY_RATIO(scl_data->ratios.horz))
196b843c749SSergey Zigachev 			scl_data->taps.h_taps = 1;
197b843c749SSergey Zigachev 		if (IDENTITY_RATIO(scl_data->ratios.vert))
198b843c749SSergey Zigachev 			scl_data->taps.v_taps = 1;
199b843c749SSergey Zigachev 		if (IDENTITY_RATIO(scl_data->ratios.horz_c))
200b843c749SSergey Zigachev 			scl_data->taps.h_taps_c = 1;
201b843c749SSergey Zigachev 		if (IDENTITY_RATIO(scl_data->ratios.vert_c))
202b843c749SSergey Zigachev 			scl_data->taps.v_taps_c = 1;
203b843c749SSergey Zigachev 	}
204b843c749SSergey Zigachev 
205b843c749SSergey Zigachev 	return true;
206b843c749SSergey Zigachev }
207b843c749SSergey Zigachev 
dpp_reset(struct dpp * dpp_base)208b843c749SSergey Zigachev void dpp_reset(struct dpp *dpp_base)
209b843c749SSergey Zigachev {
210b843c749SSergey Zigachev 	struct dcn10_dpp *dpp = TO_DCN10_DPP(dpp_base);
211b843c749SSergey Zigachev 
212b843c749SSergey Zigachev 	dpp->filter_h_c = NULL;
213b843c749SSergey Zigachev 	dpp->filter_v_c = NULL;
214b843c749SSergey Zigachev 	dpp->filter_h = NULL;
215b843c749SSergey Zigachev 	dpp->filter_v = NULL;
216b843c749SSergey Zigachev 
217b843c749SSergey Zigachev 	memset(&dpp->scl_data, 0, sizeof(dpp->scl_data));
218b843c749SSergey Zigachev 	memset(&dpp->pwl_data, 0, sizeof(dpp->pwl_data));
219b843c749SSergey Zigachev }
220b843c749SSergey Zigachev 
221b843c749SSergey Zigachev 
222b843c749SSergey Zigachev 
dpp1_cm_set_regamma_pwl(struct dpp * dpp_base,const struct pwl_params * params,enum opp_regamma mode)223b843c749SSergey Zigachev static void dpp1_cm_set_regamma_pwl(
224b843c749SSergey Zigachev 	struct dpp *dpp_base, const struct pwl_params *params, enum opp_regamma mode)
225b843c749SSergey Zigachev {
226b843c749SSergey Zigachev 	struct dcn10_dpp *dpp = TO_DCN10_DPP(dpp_base);
227b843c749SSergey Zigachev 	uint32_t re_mode = 0;
228b843c749SSergey Zigachev 
229b843c749SSergey Zigachev 	switch (mode) {
230b843c749SSergey Zigachev 	case OPP_REGAMMA_BYPASS:
231b843c749SSergey Zigachev 		re_mode = 0;
232b843c749SSergey Zigachev 		break;
233b843c749SSergey Zigachev 	case OPP_REGAMMA_SRGB:
234b843c749SSergey Zigachev 		re_mode = 1;
235b843c749SSergey Zigachev 		break;
236b843c749SSergey Zigachev 	case OPP_REGAMMA_XVYCC:
237b843c749SSergey Zigachev 		re_mode = 2;
238b843c749SSergey Zigachev 		break;
239b843c749SSergey Zigachev 	case OPP_REGAMMA_USER:
240b843c749SSergey Zigachev 		re_mode = dpp->is_write_to_ram_a_safe ? 4 : 3;
241b843c749SSergey Zigachev 		if (memcmp(&dpp->pwl_data, params, sizeof(*params)) == 0)
242b843c749SSergey Zigachev 			break;
243b843c749SSergey Zigachev 
244b843c749SSergey Zigachev 		dpp1_cm_power_on_regamma_lut(dpp_base, true);
245b843c749SSergey Zigachev 		dpp1_cm_configure_regamma_lut(dpp_base, dpp->is_write_to_ram_a_safe);
246b843c749SSergey Zigachev 
247b843c749SSergey Zigachev 		if (dpp->is_write_to_ram_a_safe)
248b843c749SSergey Zigachev 			dpp1_cm_program_regamma_luta_settings(dpp_base, params);
249b843c749SSergey Zigachev 		else
250b843c749SSergey Zigachev 			dpp1_cm_program_regamma_lutb_settings(dpp_base, params);
251b843c749SSergey Zigachev 
252b843c749SSergey Zigachev 		dpp1_cm_program_regamma_lut(dpp_base, params->rgb_resulted,
253b843c749SSergey Zigachev 					    params->hw_points_num);
254b843c749SSergey Zigachev 		dpp->pwl_data = *params;
255b843c749SSergey Zigachev 
256b843c749SSergey Zigachev 		re_mode = dpp->is_write_to_ram_a_safe ? 3 : 4;
257b843c749SSergey Zigachev 		dpp->is_write_to_ram_a_safe = !dpp->is_write_to_ram_a_safe;
258b843c749SSergey Zigachev 		break;
259b843c749SSergey Zigachev 	default:
260b843c749SSergey Zigachev 		break;
261b843c749SSergey Zigachev 	}
262b843c749SSergey Zigachev 	REG_SET(CM_RGAM_CONTROL, 0, CM_RGAM_LUT_MODE, re_mode);
263b843c749SSergey Zigachev }
264b843c749SSergey Zigachev 
dpp1_setup_format_flags(enum surface_pixel_format input_format,enum pixel_format_description * fmt)265b843c749SSergey Zigachev static void dpp1_setup_format_flags(enum surface_pixel_format input_format,\
266b843c749SSergey Zigachev 						enum pixel_format_description *fmt)
267b843c749SSergey Zigachev {
268b843c749SSergey Zigachev 
269b843c749SSergey Zigachev 	if (input_format == SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F ||
270b843c749SSergey Zigachev 		input_format == SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F)
271b843c749SSergey Zigachev 		*fmt = PIXEL_FORMAT_FLOAT;
272b843c749SSergey Zigachev 	else if (input_format == SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616)
273b843c749SSergey Zigachev 		*fmt = PIXEL_FORMAT_FIXED16;
274b843c749SSergey Zigachev 	else
275b843c749SSergey Zigachev 		*fmt = PIXEL_FORMAT_FIXED;
276b843c749SSergey Zigachev }
277b843c749SSergey Zigachev 
dpp1_set_degamma_format_float(struct dpp * dpp_base,bool is_float)278b843c749SSergey Zigachev static void dpp1_set_degamma_format_float(
279b843c749SSergey Zigachev 		struct dpp *dpp_base,
280b843c749SSergey Zigachev 		bool is_float)
281b843c749SSergey Zigachev {
282b843c749SSergey Zigachev 	struct dcn10_dpp *dpp = TO_DCN10_DPP(dpp_base);
283b843c749SSergey Zigachev 
284b843c749SSergey Zigachev 	if (is_float) {
285b843c749SSergey Zigachev 		REG_UPDATE(CM_IGAM_CONTROL, CM_IGAM_INPUT_FORMAT, 3);
286b843c749SSergey Zigachev 		REG_UPDATE(CM_IGAM_CONTROL, CM_IGAM_LUT_MODE, 1);
287b843c749SSergey Zigachev 	} else {
288b843c749SSergey Zigachev 		REG_UPDATE(CM_IGAM_CONTROL, CM_IGAM_INPUT_FORMAT, 2);
289b843c749SSergey Zigachev 		REG_UPDATE(CM_IGAM_CONTROL, CM_IGAM_LUT_MODE, 0);
290b843c749SSergey Zigachev 	}
291b843c749SSergey Zigachev }
292b843c749SSergey Zigachev 
dpp1_cnv_setup(struct dpp * dpp_base,enum surface_pixel_format format,enum expansion_mode mode,struct dc_csc_transform input_csc_color_matrix,enum dc_color_space input_color_space)293b843c749SSergey Zigachev void dpp1_cnv_setup (
294b843c749SSergey Zigachev 		struct dpp *dpp_base,
295b843c749SSergey Zigachev 		enum surface_pixel_format format,
296b843c749SSergey Zigachev 		enum expansion_mode mode,
297b843c749SSergey Zigachev 		struct dc_csc_transform input_csc_color_matrix,
298b843c749SSergey Zigachev 		enum dc_color_space input_color_space)
299b843c749SSergey Zigachev {
300b843c749SSergey Zigachev 	uint32_t pixel_format;
301b843c749SSergey Zigachev 	uint32_t alpha_en;
302b843c749SSergey Zigachev 	enum pixel_format_description fmt ;
303b843c749SSergey Zigachev 	enum dc_color_space color_space;
304b843c749SSergey Zigachev 	enum dcn10_input_csc_select select;
305b843c749SSergey Zigachev 	bool is_float;
306b843c749SSergey Zigachev 	struct dcn10_dpp *dpp = TO_DCN10_DPP(dpp_base);
307b843c749SSergey Zigachev 	bool force_disable_cursor = false;
308b843c749SSergey Zigachev 	struct out_csc_color_matrix tbl_entry;
309b843c749SSergey Zigachev 	int i = 0;
310b843c749SSergey Zigachev 
311b843c749SSergey Zigachev 	dpp1_setup_format_flags(format, &fmt);
312b843c749SSergey Zigachev 	alpha_en = 1;
313b843c749SSergey Zigachev 	pixel_format = 0;
314b843c749SSergey Zigachev 	color_space = COLOR_SPACE_SRGB;
315b843c749SSergey Zigachev 	select = INPUT_CSC_SELECT_BYPASS;
316b843c749SSergey Zigachev 	is_float = false;
317b843c749SSergey Zigachev 
318b843c749SSergey Zigachev 	switch (fmt) {
319b843c749SSergey Zigachev 	case PIXEL_FORMAT_FIXED:
320b843c749SSergey Zigachev 	case PIXEL_FORMAT_FIXED16:
321b843c749SSergey Zigachev 	/*when output is float then FORMAT_CONTROL__OUTPUT_FP=1*/
322b843c749SSergey Zigachev 		REG_SET_3(FORMAT_CONTROL, 0,
323b843c749SSergey Zigachev 			CNVC_BYPASS, 0,
324b843c749SSergey Zigachev 			FORMAT_EXPANSION_MODE, mode,
325b843c749SSergey Zigachev 			OUTPUT_FP, 0);
326b843c749SSergey Zigachev 		break;
327b843c749SSergey Zigachev 	case PIXEL_FORMAT_FLOAT:
328b843c749SSergey Zigachev 		REG_SET_3(FORMAT_CONTROL, 0,
329b843c749SSergey Zigachev 			CNVC_BYPASS, 0,
330b843c749SSergey Zigachev 			FORMAT_EXPANSION_MODE, mode,
331b843c749SSergey Zigachev 			OUTPUT_FP, 1);
332b843c749SSergey Zigachev 		is_float = true;
333b843c749SSergey Zigachev 		break;
334b843c749SSergey Zigachev 	default:
335b843c749SSergey Zigachev 
336b843c749SSergey Zigachev 		break;
337b843c749SSergey Zigachev 	}
338b843c749SSergey Zigachev 
339b843c749SSergey Zigachev 	dpp1_set_degamma_format_float(dpp_base, is_float);
340b843c749SSergey Zigachev 
341b843c749SSergey Zigachev 	switch (format) {
342b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
343b843c749SSergey Zigachev 		pixel_format = 1;
344b843c749SSergey Zigachev 		break;
345b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
346b843c749SSergey Zigachev 		pixel_format = 3;
347b843c749SSergey Zigachev 		alpha_en = 0;
348b843c749SSergey Zigachev 		break;
349b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
350b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
351b843c749SSergey Zigachev 		pixel_format = 8;
352b843c749SSergey Zigachev 		break;
353b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
354b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
355b843c749SSergey Zigachev 		pixel_format = 10;
356b843c749SSergey Zigachev 		break;
357b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
358b843c749SSergey Zigachev 		force_disable_cursor = false;
359b843c749SSergey Zigachev 		pixel_format = 65;
360b843c749SSergey Zigachev 		color_space = COLOR_SPACE_YCBCR709;
361b843c749SSergey Zigachev 		select = INPUT_CSC_SELECT_ICSC;
362b843c749SSergey Zigachev 		break;
363b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
364b843c749SSergey Zigachev 		force_disable_cursor = true;
365b843c749SSergey Zigachev 		pixel_format = 64;
366b843c749SSergey Zigachev 		color_space = COLOR_SPACE_YCBCR709;
367b843c749SSergey Zigachev 		select = INPUT_CSC_SELECT_ICSC;
368b843c749SSergey Zigachev 		break;
369b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
370b843c749SSergey Zigachev 		force_disable_cursor = true;
371b843c749SSergey Zigachev 		pixel_format = 67;
372b843c749SSergey Zigachev 		color_space = COLOR_SPACE_YCBCR709;
373b843c749SSergey Zigachev 		select = INPUT_CSC_SELECT_ICSC;
374b843c749SSergey Zigachev 		break;
375b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
376b843c749SSergey Zigachev 		force_disable_cursor = true;
377b843c749SSergey Zigachev 		pixel_format = 66;
378b843c749SSergey Zigachev 		color_space = COLOR_SPACE_YCBCR709;
379b843c749SSergey Zigachev 		select = INPUT_CSC_SELECT_ICSC;
380b843c749SSergey Zigachev 		break;
381b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
382b843c749SSergey Zigachev 		pixel_format = 22;
383b843c749SSergey Zigachev 		break;
384b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
385b843c749SSergey Zigachev 		pixel_format = 24;
386b843c749SSergey Zigachev 		break;
387b843c749SSergey Zigachev 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
388b843c749SSergey Zigachev 		pixel_format = 25;
389b843c749SSergey Zigachev 		break;
390b843c749SSergey Zigachev 	default:
391b843c749SSergey Zigachev 		break;
392b843c749SSergey Zigachev 	}
393b843c749SSergey Zigachev 
394b843c749SSergey Zigachev 	/* Set default color space based on format if none is given. */
395b843c749SSergey Zigachev 	color_space = input_color_space ? input_color_space : color_space;
396b843c749SSergey Zigachev 
397b843c749SSergey Zigachev 	REG_SET(CNVC_SURFACE_PIXEL_FORMAT, 0,
398b843c749SSergey Zigachev 			CNVC_SURFACE_PIXEL_FORMAT, pixel_format);
399b843c749SSergey Zigachev 	REG_UPDATE(FORMAT_CONTROL, FORMAT_CONTROL__ALPHA_EN, alpha_en);
400b843c749SSergey Zigachev 
401b843c749SSergey Zigachev 	// if input adjustments exist, program icsc with those values
402b843c749SSergey Zigachev 
403b843c749SSergey Zigachev 	if (input_csc_color_matrix.enable_adjustment
404b843c749SSergey Zigachev 				== true) {
405b843c749SSergey Zigachev 		for (i = 0; i < 12; i++)
406b843c749SSergey Zigachev 			tbl_entry.regval[i] = input_csc_color_matrix.matrix[i];
407b843c749SSergey Zigachev 
408b843c749SSergey Zigachev 		tbl_entry.color_space = color_space;
409b843c749SSergey Zigachev 
410b843c749SSergey Zigachev 		if (color_space >= COLOR_SPACE_YCBCR601)
411b843c749SSergey Zigachev 			select = INPUT_CSC_SELECT_ICSC;
412b843c749SSergey Zigachev 		else
413b843c749SSergey Zigachev 			select = INPUT_CSC_SELECT_BYPASS;
414b843c749SSergey Zigachev 
415b843c749SSergey Zigachev 		dpp1_program_input_csc(dpp_base, color_space, select, &tbl_entry);
416b843c749SSergey Zigachev 	} else
417b843c749SSergey Zigachev 		dpp1_program_input_csc(dpp_base, color_space, select, NULL);
418b843c749SSergey Zigachev 
419b843c749SSergey Zigachev 	if (force_disable_cursor) {
420b843c749SSergey Zigachev 		REG_UPDATE(CURSOR_CONTROL,
421b843c749SSergey Zigachev 				CURSOR_ENABLE, 0);
422b843c749SSergey Zigachev 		REG_UPDATE(CURSOR0_CONTROL,
423b843c749SSergey Zigachev 				CUR0_ENABLE, 0);
424b843c749SSergey Zigachev 	}
425b843c749SSergey Zigachev }
426b843c749SSergey Zigachev 
dpp1_set_cursor_attributes(struct dpp * dpp_base,enum dc_cursor_color_format color_format)427b843c749SSergey Zigachev void dpp1_set_cursor_attributes(
428b843c749SSergey Zigachev 		struct dpp *dpp_base,
429b843c749SSergey Zigachev 		enum dc_cursor_color_format color_format)
430b843c749SSergey Zigachev {
431b843c749SSergey Zigachev 	struct dcn10_dpp *dpp = TO_DCN10_DPP(dpp_base);
432b843c749SSergey Zigachev 
433b843c749SSergey Zigachev 	REG_UPDATE_2(CURSOR0_CONTROL,
434b843c749SSergey Zigachev 			CUR0_MODE, color_format,
435b843c749SSergey Zigachev 			CUR0_EXPANSION_MODE, 0);
436b843c749SSergey Zigachev 
437b843c749SSergey Zigachev 	if (color_format == CURSOR_MODE_MONO) {
438b843c749SSergey Zigachev 		/* todo: clarify what to program these to */
439b843c749SSergey Zigachev 		REG_UPDATE(CURSOR0_COLOR0,
440b843c749SSergey Zigachev 				CUR0_COLOR0, 0x00000000);
441b843c749SSergey Zigachev 		REG_UPDATE(CURSOR0_COLOR1,
442b843c749SSergey Zigachev 				CUR0_COLOR1, 0xFFFFFFFF);
443b843c749SSergey Zigachev 	}
444b843c749SSergey Zigachev }
445b843c749SSergey Zigachev 
446b843c749SSergey Zigachev 
dpp1_set_cursor_position(struct dpp * dpp_base,const struct dc_cursor_position * pos,const struct dc_cursor_mi_param * param,uint32_t width)447b843c749SSergey Zigachev void dpp1_set_cursor_position(
448b843c749SSergey Zigachev 		struct dpp *dpp_base,
449b843c749SSergey Zigachev 		const struct dc_cursor_position *pos,
450b843c749SSergey Zigachev 		const struct dc_cursor_mi_param *param,
451b843c749SSergey Zigachev 		uint32_t width)
452b843c749SSergey Zigachev {
453b843c749SSergey Zigachev 	struct dcn10_dpp *dpp = TO_DCN10_DPP(dpp_base);
454b843c749SSergey Zigachev 	int src_x_offset = pos->x - pos->x_hotspot - param->viewport.x;
455b843c749SSergey Zigachev 	uint32_t cur_en = pos->enable ? 1 : 0;
456b843c749SSergey Zigachev 
457b843c749SSergey Zigachev 	if (src_x_offset >= (int)param->viewport.width)
458b843c749SSergey Zigachev 		cur_en = 0;  /* not visible beyond right edge*/
459b843c749SSergey Zigachev 
460b843c749SSergey Zigachev 	if (src_x_offset + (int)width <= 0)
461b843c749SSergey Zigachev 		cur_en = 0;  /* not visible beyond left edge*/
462b843c749SSergey Zigachev 
463b843c749SSergey Zigachev 	REG_UPDATE(CURSOR0_CONTROL,
464b843c749SSergey Zigachev 			CUR0_ENABLE, cur_en);
465b843c749SSergey Zigachev 
466b843c749SSergey Zigachev }
467b843c749SSergey Zigachev 
dpp1_cnv_set_optional_cursor_attributes(struct dpp * dpp_base,struct dpp_cursor_attributes * attr)468b843c749SSergey Zigachev void dpp1_cnv_set_optional_cursor_attributes(
469b843c749SSergey Zigachev 		struct dpp *dpp_base,
470b843c749SSergey Zigachev 		struct dpp_cursor_attributes *attr)
471b843c749SSergey Zigachev {
472b843c749SSergey Zigachev 	struct dcn10_dpp *dpp = TO_DCN10_DPP(dpp_base);
473b843c749SSergey Zigachev 
474b843c749SSergey Zigachev 	if (attr) {
475b843c749SSergey Zigachev 		REG_UPDATE(CURSOR0_FP_SCALE_BIAS,  CUR0_FP_BIAS,  attr->bias);
476b843c749SSergey Zigachev 		REG_UPDATE(CURSOR0_FP_SCALE_BIAS,  CUR0_FP_SCALE, attr->scale);
477b843c749SSergey Zigachev 	}
478b843c749SSergey Zigachev }
479b843c749SSergey Zigachev 
dpp1_dppclk_control(struct dpp * dpp_base,bool dppclk_div,bool enable)480b843c749SSergey Zigachev void dpp1_dppclk_control(
481b843c749SSergey Zigachev 		struct dpp *dpp_base,
482b843c749SSergey Zigachev 		bool dppclk_div,
483b843c749SSergey Zigachev 		bool enable)
484b843c749SSergey Zigachev {
485b843c749SSergey Zigachev 	struct dcn10_dpp *dpp = TO_DCN10_DPP(dpp_base);
486b843c749SSergey Zigachev 
487b843c749SSergey Zigachev 	if (enable) {
488b843c749SSergey Zigachev 		if (dpp->tf_mask->DPPCLK_RATE_CONTROL)
489b843c749SSergey Zigachev 			REG_UPDATE_2(DPP_CONTROL,
490b843c749SSergey Zigachev 				DPPCLK_RATE_CONTROL, dppclk_div,
491b843c749SSergey Zigachev 				DPP_CLOCK_ENABLE, 1);
492b843c749SSergey Zigachev 		else
493b843c749SSergey Zigachev 			REG_UPDATE(DPP_CONTROL, DPP_CLOCK_ENABLE, 1);
494b843c749SSergey Zigachev 	} else
495b843c749SSergey Zigachev 		REG_UPDATE(DPP_CONTROL, DPP_CLOCK_ENABLE, 0);
496b843c749SSergey Zigachev }
497b843c749SSergey Zigachev 
498b843c749SSergey Zigachev static const struct dpp_funcs dcn10_dpp_funcs = {
499b843c749SSergey Zigachev 		.dpp_read_state = dpp_read_state,
500b843c749SSergey Zigachev 		.dpp_reset = dpp_reset,
501b843c749SSergey Zigachev 		.dpp_set_scaler = dpp1_dscl_set_scaler_manual_scale,
502b843c749SSergey Zigachev 		.dpp_get_optimal_number_of_taps = dpp_get_optimal_number_of_taps,
503b843c749SSergey Zigachev 		.dpp_set_gamut_remap = dpp1_cm_set_gamut_remap,
504b843c749SSergey Zigachev 		.dpp_set_csc_adjustment = dpp1_cm_set_output_csc_adjustment,
505b843c749SSergey Zigachev 		.dpp_set_csc_default = dpp1_cm_set_output_csc_default,
506b843c749SSergey Zigachev 		.dpp_power_on_regamma_lut = dpp1_cm_power_on_regamma_lut,
507b843c749SSergey Zigachev 		.dpp_program_regamma_lut = dpp1_cm_program_regamma_lut,
508b843c749SSergey Zigachev 		.dpp_configure_regamma_lut = dpp1_cm_configure_regamma_lut,
509b843c749SSergey Zigachev 		.dpp_program_regamma_lutb_settings = dpp1_cm_program_regamma_lutb_settings,
510b843c749SSergey Zigachev 		.dpp_program_regamma_luta_settings = dpp1_cm_program_regamma_luta_settings,
511b843c749SSergey Zigachev 		.dpp_program_regamma_pwl = dpp1_cm_set_regamma_pwl,
512b843c749SSergey Zigachev 		.dpp_program_bias_and_scale = dpp1_program_bias_and_scale,
513b843c749SSergey Zigachev 		.dpp_set_degamma = dpp1_set_degamma,
514b843c749SSergey Zigachev 		.dpp_program_input_lut		= dpp1_program_input_lut,
515b843c749SSergey Zigachev 		.dpp_program_degamma_pwl	= dpp1_set_degamma_pwl,
516b843c749SSergey Zigachev 		.dpp_setup			= dpp1_cnv_setup,
517b843c749SSergey Zigachev 		.dpp_full_bypass		= dpp1_full_bypass,
518b843c749SSergey Zigachev 		.set_cursor_attributes = dpp1_set_cursor_attributes,
519b843c749SSergey Zigachev 		.set_cursor_position = dpp1_set_cursor_position,
520b843c749SSergey Zigachev 		.set_optional_cursor_attributes = dpp1_cnv_set_optional_cursor_attributes,
521b843c749SSergey Zigachev 		.dpp_dppclk_control = dpp1_dppclk_control,
522b843c749SSergey Zigachev 		.dpp_set_hdr_multiplier = dpp1_set_hdr_multiplier,
523b843c749SSergey Zigachev };
524b843c749SSergey Zigachev 
525b843c749SSergey Zigachev static struct dpp_caps dcn10_dpp_cap = {
526b843c749SSergey Zigachev 	.dscl_data_proc_format = DSCL_DATA_PRCESSING_FIXED_FORMAT,
527b843c749SSergey Zigachev 	.dscl_calc_lb_num_partitions = dpp1_dscl_calc_lb_num_partitions,
528b843c749SSergey Zigachev };
529b843c749SSergey Zigachev 
530b843c749SSergey Zigachev /*****************************************/
531b843c749SSergey Zigachev /* Constructor, Destructor               */
532b843c749SSergey Zigachev /*****************************************/
533b843c749SSergey Zigachev 
dpp1_construct(struct dcn10_dpp * dpp,struct dc_context * ctx,uint32_t inst,const struct dcn_dpp_registers * tf_regs,const struct dcn_dpp_shift * tf_shift,const struct dcn_dpp_mask * tf_mask)534b843c749SSergey Zigachev void dpp1_construct(
535b843c749SSergey Zigachev 	struct dcn10_dpp *dpp,
536b843c749SSergey Zigachev 	struct dc_context *ctx,
537b843c749SSergey Zigachev 	uint32_t inst,
538b843c749SSergey Zigachev 	const struct dcn_dpp_registers *tf_regs,
539b843c749SSergey Zigachev 	const struct dcn_dpp_shift *tf_shift,
540b843c749SSergey Zigachev 	const struct dcn_dpp_mask *tf_mask)
541b843c749SSergey Zigachev {
542b843c749SSergey Zigachev 	dpp->base.ctx = ctx;
543b843c749SSergey Zigachev 
544b843c749SSergey Zigachev 	dpp->base.inst = inst;
545b843c749SSergey Zigachev 	dpp->base.funcs = &dcn10_dpp_funcs;
546b843c749SSergey Zigachev 	dpp->base.caps = &dcn10_dpp_cap;
547b843c749SSergey Zigachev 
548b843c749SSergey Zigachev 	dpp->tf_regs = tf_regs;
549b843c749SSergey Zigachev 	dpp->tf_shift = tf_shift;
550b843c749SSergey Zigachev 	dpp->tf_mask = tf_mask;
551b843c749SSergey Zigachev 
552b843c749SSergey Zigachev 	dpp->lb_pixel_depth_supported =
553b843c749SSergey Zigachev 		LB_PIXEL_DEPTH_18BPP |
554b843c749SSergey Zigachev 		LB_PIXEL_DEPTH_24BPP |
555b843c749SSergey Zigachev 		LB_PIXEL_DEPTH_30BPP;
556b843c749SSergey Zigachev 
557b843c749SSergey Zigachev 	dpp->lb_bits_per_entry = LB_BITS_PER_ENTRY;
558b843c749SSergey Zigachev 	dpp->lb_memory_size = LB_TOTAL_NUMBER_OF_ENTRIES; /*0x1404*/
559b843c749SSergey Zigachev }
560