xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/arm/malidp_planes.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
4*4882a593Smuzhiyun  * Author: Liviu Dudau <Liviu.Dudau@arm.com>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * ARM Mali DP plane manipulation routines.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/iommu.h>
10*4882a593Smuzhiyun #include <linux/platform_device.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <drm/drm_atomic.h>
13*4882a593Smuzhiyun #include <drm/drm_atomic_helper.h>
14*4882a593Smuzhiyun #include <drm/drm_drv.h>
15*4882a593Smuzhiyun #include <drm/drm_fb_cma_helper.h>
16*4882a593Smuzhiyun #include <drm/drm_fourcc.h>
17*4882a593Smuzhiyun #include <drm/drm_gem_cma_helper.h>
18*4882a593Smuzhiyun #include <drm/drm_gem_framebuffer_helper.h>
19*4882a593Smuzhiyun #include <drm/drm_plane_helper.h>
20*4882a593Smuzhiyun #include <drm/drm_print.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "malidp_hw.h"
23*4882a593Smuzhiyun #include "malidp_drv.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /* Layer specific register offsets */
26*4882a593Smuzhiyun #define MALIDP_LAYER_FORMAT		0x000
27*4882a593Smuzhiyun #define   LAYER_FORMAT_MASK		0x3f
28*4882a593Smuzhiyun #define MALIDP_LAYER_CONTROL		0x004
29*4882a593Smuzhiyun #define   LAYER_ENABLE			(1 << 0)
30*4882a593Smuzhiyun #define   LAYER_FLOWCFG_MASK		7
31*4882a593Smuzhiyun #define   LAYER_FLOWCFG(x)		(((x) & LAYER_FLOWCFG_MASK) << 1)
32*4882a593Smuzhiyun #define     LAYER_FLOWCFG_SCALE_SE	3
33*4882a593Smuzhiyun #define   LAYER_ROT_OFFSET		8
34*4882a593Smuzhiyun #define   LAYER_H_FLIP			(1 << 10)
35*4882a593Smuzhiyun #define   LAYER_V_FLIP			(1 << 11)
36*4882a593Smuzhiyun #define   LAYER_ROT_MASK		(0xf << 8)
37*4882a593Smuzhiyun #define   LAYER_COMP_MASK		(0x3 << 12)
38*4882a593Smuzhiyun #define   LAYER_COMP_PIXEL		(0x3 << 12)
39*4882a593Smuzhiyun #define   LAYER_COMP_PLANE		(0x2 << 12)
40*4882a593Smuzhiyun #define   LAYER_PMUL_ENABLE		(0x1 << 14)
41*4882a593Smuzhiyun #define   LAYER_ALPHA_OFFSET		(16)
42*4882a593Smuzhiyun #define   LAYER_ALPHA_MASK		(0xff)
43*4882a593Smuzhiyun #define   LAYER_ALPHA(x)		(((x) & LAYER_ALPHA_MASK) << LAYER_ALPHA_OFFSET)
44*4882a593Smuzhiyun #define MALIDP_LAYER_COMPOSE		0x008
45*4882a593Smuzhiyun #define MALIDP_LAYER_SIZE		0x00c
46*4882a593Smuzhiyun #define   LAYER_H_VAL(x)		(((x) & 0x1fff) << 0)
47*4882a593Smuzhiyun #define   LAYER_V_VAL(x)		(((x) & 0x1fff) << 16)
48*4882a593Smuzhiyun #define MALIDP_LAYER_COMP_SIZE		0x010
49*4882a593Smuzhiyun #define MALIDP_LAYER_OFFSET		0x014
50*4882a593Smuzhiyun #define MALIDP550_LS_ENABLE		0x01c
51*4882a593Smuzhiyun #define MALIDP550_LS_R1_IN_SIZE		0x020
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #define MODIFIERS_COUNT_MAX		15
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun  * This 4-entry look-up-table is used to determine the full 8-bit alpha value
57*4882a593Smuzhiyun  * for formats with 1- or 2-bit alpha channels.
58*4882a593Smuzhiyun  * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0%
59*4882a593Smuzhiyun  * opacity for 2-bit formats.
60*4882a593Smuzhiyun  */
61*4882a593Smuzhiyun #define MALIDP_ALPHA_LUT 0xffaa5500
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* page sizes the MMU prefetcher can support */
64*4882a593Smuzhiyun #define MALIDP_MMU_PREFETCH_PARTIAL_PGSIZES	(SZ_4K | SZ_64K)
65*4882a593Smuzhiyun #define MALIDP_MMU_PREFETCH_FULL_PGSIZES	(SZ_1M | SZ_2M)
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* readahead for partial-frame prefetch */
68*4882a593Smuzhiyun #define MALIDP_MMU_PREFETCH_READAHEAD		8
69*4882a593Smuzhiyun 
malidp_de_plane_destroy(struct drm_plane * plane)70*4882a593Smuzhiyun static void malidp_de_plane_destroy(struct drm_plane *plane)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	struct malidp_plane *mp = to_malidp_plane(plane);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	drm_plane_cleanup(plane);
75*4882a593Smuzhiyun 	kfree(mp);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun  * Replicate what the default ->reset hook does: free the state pointer and
80*4882a593Smuzhiyun  * allocate a new empty object. We just need enough space to store
81*4882a593Smuzhiyun  * a malidp_plane_state instead of a drm_plane_state.
82*4882a593Smuzhiyun  */
malidp_plane_reset(struct drm_plane * plane)83*4882a593Smuzhiyun static void malidp_plane_reset(struct drm_plane *plane)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	struct malidp_plane_state *state = to_malidp_plane_state(plane->state);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	if (state)
88*4882a593Smuzhiyun 		__drm_atomic_helper_plane_destroy_state(&state->base);
89*4882a593Smuzhiyun 	kfree(state);
90*4882a593Smuzhiyun 	plane->state = NULL;
91*4882a593Smuzhiyun 	state = kzalloc(sizeof(*state), GFP_KERNEL);
92*4882a593Smuzhiyun 	if (state)
93*4882a593Smuzhiyun 		__drm_atomic_helper_plane_reset(plane, &state->base);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun static struct
malidp_duplicate_plane_state(struct drm_plane * plane)97*4882a593Smuzhiyun drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	struct malidp_plane_state *state, *m_state;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	if (!plane->state)
102*4882a593Smuzhiyun 		return NULL;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	state = kmalloc(sizeof(*state), GFP_KERNEL);
105*4882a593Smuzhiyun 	if (!state)
106*4882a593Smuzhiyun 		return NULL;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	m_state = to_malidp_plane_state(plane->state);
109*4882a593Smuzhiyun 	__drm_atomic_helper_plane_duplicate_state(plane, &state->base);
110*4882a593Smuzhiyun 	state->rotmem_size = m_state->rotmem_size;
111*4882a593Smuzhiyun 	state->format = m_state->format;
112*4882a593Smuzhiyun 	state->n_planes = m_state->n_planes;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	state->mmu_prefetch_mode = m_state->mmu_prefetch_mode;
115*4882a593Smuzhiyun 	state->mmu_prefetch_pgsize = m_state->mmu_prefetch_pgsize;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	return &state->base;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
malidp_destroy_plane_state(struct drm_plane * plane,struct drm_plane_state * state)120*4882a593Smuzhiyun static void malidp_destroy_plane_state(struct drm_plane *plane,
121*4882a593Smuzhiyun 				       struct drm_plane_state *state)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	struct malidp_plane_state *m_state = to_malidp_plane_state(state);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	__drm_atomic_helper_plane_destroy_state(state);
126*4882a593Smuzhiyun 	kfree(m_state);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun static const char * const prefetch_mode_names[] = {
130*4882a593Smuzhiyun 	[MALIDP_PREFETCH_MODE_NONE] = "MMU_PREFETCH_NONE",
131*4882a593Smuzhiyun 	[MALIDP_PREFETCH_MODE_PARTIAL] = "MMU_PREFETCH_PARTIAL",
132*4882a593Smuzhiyun 	[MALIDP_PREFETCH_MODE_FULL] = "MMU_PREFETCH_FULL",
133*4882a593Smuzhiyun };
134*4882a593Smuzhiyun 
malidp_plane_atomic_print_state(struct drm_printer * p,const struct drm_plane_state * state)135*4882a593Smuzhiyun static void malidp_plane_atomic_print_state(struct drm_printer *p,
136*4882a593Smuzhiyun 					    const struct drm_plane_state *state)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	struct malidp_plane_state *ms = to_malidp_plane_state(state);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	drm_printf(p, "\trotmem_size=%u\n", ms->rotmem_size);
141*4882a593Smuzhiyun 	drm_printf(p, "\tformat_id=%u\n", ms->format);
142*4882a593Smuzhiyun 	drm_printf(p, "\tn_planes=%u\n", ms->n_planes);
143*4882a593Smuzhiyun 	drm_printf(p, "\tmmu_prefetch_mode=%s\n",
144*4882a593Smuzhiyun 		   prefetch_mode_names[ms->mmu_prefetch_mode]);
145*4882a593Smuzhiyun 	drm_printf(p, "\tmmu_prefetch_pgsize=%d\n", ms->mmu_prefetch_pgsize);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
malidp_format_mod_supported(struct drm_device * drm,u32 format,u64 modifier)148*4882a593Smuzhiyun bool malidp_format_mod_supported(struct drm_device *drm,
149*4882a593Smuzhiyun 				 u32 format, u64 modifier)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	const struct drm_format_info *info;
152*4882a593Smuzhiyun 	const u64 *modifiers;
153*4882a593Smuzhiyun 	struct malidp_drm *malidp = drm->dev_private;
154*4882a593Smuzhiyun 	const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
157*4882a593Smuzhiyun 		return false;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* Some pixel formats are supported without any modifier */
160*4882a593Smuzhiyun 	if (modifier == DRM_FORMAT_MOD_LINEAR) {
161*4882a593Smuzhiyun 		/*
162*4882a593Smuzhiyun 		 * However these pixel formats need to be supported with
163*4882a593Smuzhiyun 		 * modifiers only
164*4882a593Smuzhiyun 		 */
165*4882a593Smuzhiyun 		return !malidp_hw_format_is_afbc_only(format);
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_ARM) {
169*4882a593Smuzhiyun 		DRM_ERROR("Unknown modifier (not Arm)\n");
170*4882a593Smuzhiyun 		return false;
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	if (modifier &
174*4882a593Smuzhiyun 	    ~DRM_FORMAT_MOD_ARM_AFBC(AFBC_MOD_VALID_BITS)) {
175*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Unsupported modifiers\n");
176*4882a593Smuzhiyun 		return false;
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	modifiers = malidp_format_modifiers;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* SPLIT buffers must use SPARSE layout */
182*4882a593Smuzhiyun 	if (WARN_ON_ONCE((modifier & AFBC_SPLIT) && !(modifier & AFBC_SPARSE)))
183*4882a593Smuzhiyun 		return false;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	/* CBR only applies to YUV formats, where YTR should be always 0 */
186*4882a593Smuzhiyun 	if (WARN_ON_ONCE((modifier & AFBC_CBR) && (modifier & AFBC_YTR)))
187*4882a593Smuzhiyun 		return false;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	while (*modifiers != DRM_FORMAT_MOD_INVALID) {
190*4882a593Smuzhiyun 		if (*modifiers == modifier)
191*4882a593Smuzhiyun 			break;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		modifiers++;
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	/* return false, if the modifier was not found */
197*4882a593Smuzhiyun 	if (*modifiers == DRM_FORMAT_MOD_INVALID) {
198*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Unsupported modifier\n");
199*4882a593Smuzhiyun 		return false;
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	info = drm_format_info(format);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	if (info->num_planes != 1) {
205*4882a593Smuzhiyun 		DRM_DEBUG_KMS("AFBC buffers expect one plane\n");
206*4882a593Smuzhiyun 		return false;
207*4882a593Smuzhiyun 	}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	if (malidp_hw_format_is_linear_only(format) == true) {
210*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Given format (0x%x) is supported is linear mode only\n",
211*4882a593Smuzhiyun 			      format);
212*4882a593Smuzhiyun 		return false;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	/*
216*4882a593Smuzhiyun 	 * RGB formats need to provide YTR modifier and YUV formats should not
217*4882a593Smuzhiyun 	 * provide YTR modifier.
218*4882a593Smuzhiyun 	 */
219*4882a593Smuzhiyun 	if (!(info->is_yuv) != !!(modifier & AFBC_FORMAT_MOD_YTR)) {
220*4882a593Smuzhiyun 		DRM_DEBUG_KMS("AFBC_FORMAT_MOD_YTR is %s for %s formats\n",
221*4882a593Smuzhiyun 			      info->is_yuv ? "disallowed" : "mandatory",
222*4882a593Smuzhiyun 			      info->is_yuv ? "YUV" : "RGB");
223*4882a593Smuzhiyun 		return false;
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	if (modifier & AFBC_SPLIT) {
227*4882a593Smuzhiyun 		if (!info->is_yuv) {
228*4882a593Smuzhiyun 			if (info->cpp[0] <= 2) {
229*4882a593Smuzhiyun 				DRM_DEBUG_KMS("RGB formats <= 16bpp are not supported with SPLIT\n");
230*4882a593Smuzhiyun 				return false;
231*4882a593Smuzhiyun 			}
232*4882a593Smuzhiyun 		}
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 		if ((info->hsub != 1) || (info->vsub != 1)) {
235*4882a593Smuzhiyun 			if (!(format == DRM_FORMAT_YUV420_10BIT &&
236*4882a593Smuzhiyun 			      (map->features & MALIDP_DEVICE_AFBC_YUV_420_10_SUPPORT_SPLIT))) {
237*4882a593Smuzhiyun 				DRM_DEBUG_KMS("Formats which are sub-sampled should never be split\n");
238*4882a593Smuzhiyun 				return false;
239*4882a593Smuzhiyun 			}
240*4882a593Smuzhiyun 		}
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	if (modifier & AFBC_CBR) {
244*4882a593Smuzhiyun 		if ((info->hsub == 1) || (info->vsub == 1)) {
245*4882a593Smuzhiyun 			DRM_DEBUG_KMS("Formats which are not sub-sampled should not have CBR set\n");
246*4882a593Smuzhiyun 			return false;
247*4882a593Smuzhiyun 		}
248*4882a593Smuzhiyun 	}
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	return true;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
malidp_format_mod_supported_per_plane(struct drm_plane * plane,u32 format,u64 modifier)253*4882a593Smuzhiyun static bool malidp_format_mod_supported_per_plane(struct drm_plane *plane,
254*4882a593Smuzhiyun 						  u32 format, u64 modifier)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun 	return malidp_format_mod_supported(plane->dev, format, modifier);
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun static const struct drm_plane_funcs malidp_de_plane_funcs = {
260*4882a593Smuzhiyun 	.update_plane = drm_atomic_helper_update_plane,
261*4882a593Smuzhiyun 	.disable_plane = drm_atomic_helper_disable_plane,
262*4882a593Smuzhiyun 	.destroy = malidp_de_plane_destroy,
263*4882a593Smuzhiyun 	.reset = malidp_plane_reset,
264*4882a593Smuzhiyun 	.atomic_duplicate_state = malidp_duplicate_plane_state,
265*4882a593Smuzhiyun 	.atomic_destroy_state = malidp_destroy_plane_state,
266*4882a593Smuzhiyun 	.atomic_print_state = malidp_plane_atomic_print_state,
267*4882a593Smuzhiyun 	.format_mod_supported = malidp_format_mod_supported_per_plane,
268*4882a593Smuzhiyun };
269*4882a593Smuzhiyun 
malidp_se_check_scaling(struct malidp_plane * mp,struct drm_plane_state * state)270*4882a593Smuzhiyun static int malidp_se_check_scaling(struct malidp_plane *mp,
271*4882a593Smuzhiyun 				   struct drm_plane_state *state)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	struct drm_crtc_state *crtc_state =
274*4882a593Smuzhiyun 		drm_atomic_get_existing_crtc_state(state->state, state->crtc);
275*4882a593Smuzhiyun 	struct malidp_crtc_state *mc;
276*4882a593Smuzhiyun 	u32 src_w, src_h;
277*4882a593Smuzhiyun 	int ret;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	if (!crtc_state)
280*4882a593Smuzhiyun 		return -EINVAL;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	mc = to_malidp_crtc_state(crtc_state);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	ret = drm_atomic_helper_check_plane_state(state, crtc_state,
285*4882a593Smuzhiyun 						  0, INT_MAX, true, true);
286*4882a593Smuzhiyun 	if (ret)
287*4882a593Smuzhiyun 		return ret;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	if (state->rotation & MALIDP_ROTATED_MASK) {
290*4882a593Smuzhiyun 		src_w = state->src_h >> 16;
291*4882a593Smuzhiyun 		src_h = state->src_w >> 16;
292*4882a593Smuzhiyun 	} else {
293*4882a593Smuzhiyun 		src_w = state->src_w >> 16;
294*4882a593Smuzhiyun 		src_h = state->src_h >> 16;
295*4882a593Smuzhiyun 	}
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	if ((state->crtc_w == src_w) && (state->crtc_h == src_h)) {
298*4882a593Smuzhiyun 		/* Scaling not necessary for this plane. */
299*4882a593Smuzhiyun 		mc->scaled_planes_mask &= ~(mp->layer->id);
300*4882a593Smuzhiyun 		return 0;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	if (mp->layer->id & (DE_SMART | DE_GRAPHICS2))
304*4882a593Smuzhiyun 		return -EINVAL;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	mc->scaled_planes_mask |= mp->layer->id;
307*4882a593Smuzhiyun 	/* Defer scaling requirements calculation to the crtc check. */
308*4882a593Smuzhiyun 	return 0;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
malidp_get_pgsize_bitmap(struct malidp_plane * mp)311*4882a593Smuzhiyun static u32 malidp_get_pgsize_bitmap(struct malidp_plane *mp)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	u32 pgsize_bitmap = 0;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	if (iommu_present(&platform_bus_type)) {
316*4882a593Smuzhiyun 		struct iommu_domain *mmu_dom =
317*4882a593Smuzhiyun 			iommu_get_domain_for_dev(mp->base.dev->dev);
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 		if (mmu_dom)
320*4882a593Smuzhiyun 			pgsize_bitmap = mmu_dom->pgsize_bitmap;
321*4882a593Smuzhiyun 	}
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	return pgsize_bitmap;
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun /*
327*4882a593Smuzhiyun  * Check if the framebuffer is entirely made up of pages at least pgsize in
328*4882a593Smuzhiyun  * size. Only a heuristic: assumes that each scatterlist entry has been aligned
329*4882a593Smuzhiyun  * to the largest page size smaller than its length and that the MMU maps to
330*4882a593Smuzhiyun  * the largest page size possible.
331*4882a593Smuzhiyun  */
malidp_check_pages_threshold(struct malidp_plane_state * ms,u32 pgsize)332*4882a593Smuzhiyun static bool malidp_check_pages_threshold(struct malidp_plane_state *ms,
333*4882a593Smuzhiyun 					 u32 pgsize)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	int i;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	for (i = 0; i < ms->n_planes; i++) {
338*4882a593Smuzhiyun 		struct drm_gem_object *obj;
339*4882a593Smuzhiyun 		struct drm_gem_cma_object *cma_obj;
340*4882a593Smuzhiyun 		struct sg_table *sgt;
341*4882a593Smuzhiyun 		struct scatterlist *sgl;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 		obj = drm_gem_fb_get_obj(ms->base.fb, i);
344*4882a593Smuzhiyun 		cma_obj = to_drm_gem_cma_obj(obj);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 		if (cma_obj->sgt)
347*4882a593Smuzhiyun 			sgt = cma_obj->sgt;
348*4882a593Smuzhiyun 		else
349*4882a593Smuzhiyun 			sgt = obj->funcs->get_sg_table(obj);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 		if (!sgt)
352*4882a593Smuzhiyun 			return false;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 		sgl = sgt->sgl;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 		while (sgl) {
357*4882a593Smuzhiyun 			if (sgl->length < pgsize) {
358*4882a593Smuzhiyun 				if (!cma_obj->sgt)
359*4882a593Smuzhiyun 					kfree(sgt);
360*4882a593Smuzhiyun 				return false;
361*4882a593Smuzhiyun 			}
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 			sgl = sg_next(sgl);
364*4882a593Smuzhiyun 		}
365*4882a593Smuzhiyun 		if (!cma_obj->sgt)
366*4882a593Smuzhiyun 			kfree(sgt);
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	return true;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun /*
373*4882a593Smuzhiyun  * Check if it is possible to enable partial-frame MMU prefetch given the
374*4882a593Smuzhiyun  * current format, AFBC state and rotation.
375*4882a593Smuzhiyun  */
malidp_partial_prefetch_supported(u32 format,u64 modifier,unsigned int rotation)376*4882a593Smuzhiyun static bool malidp_partial_prefetch_supported(u32 format, u64 modifier,
377*4882a593Smuzhiyun 					      unsigned int rotation)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	bool afbc, sparse;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	/* rotation and horizontal flip not supported for partial prefetch */
382*4882a593Smuzhiyun 	if (rotation & (DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
383*4882a593Smuzhiyun 			DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X))
384*4882a593Smuzhiyun 		return false;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	afbc = modifier & DRM_FORMAT_MOD_ARM_AFBC(0);
387*4882a593Smuzhiyun 	sparse = modifier & AFBC_FORMAT_MOD_SPARSE;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	switch (format) {
390*4882a593Smuzhiyun 	case DRM_FORMAT_ARGB2101010:
391*4882a593Smuzhiyun 	case DRM_FORMAT_RGBA1010102:
392*4882a593Smuzhiyun 	case DRM_FORMAT_BGRA1010102:
393*4882a593Smuzhiyun 	case DRM_FORMAT_ARGB8888:
394*4882a593Smuzhiyun 	case DRM_FORMAT_RGBA8888:
395*4882a593Smuzhiyun 	case DRM_FORMAT_BGRA8888:
396*4882a593Smuzhiyun 	case DRM_FORMAT_XRGB8888:
397*4882a593Smuzhiyun 	case DRM_FORMAT_XBGR8888:
398*4882a593Smuzhiyun 	case DRM_FORMAT_RGBX8888:
399*4882a593Smuzhiyun 	case DRM_FORMAT_BGRX8888:
400*4882a593Smuzhiyun 	case DRM_FORMAT_RGB888:
401*4882a593Smuzhiyun 	case DRM_FORMAT_RGBA5551:
402*4882a593Smuzhiyun 	case DRM_FORMAT_RGB565:
403*4882a593Smuzhiyun 		/* always supported */
404*4882a593Smuzhiyun 		return true;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	case DRM_FORMAT_ABGR2101010:
407*4882a593Smuzhiyun 	case DRM_FORMAT_ABGR8888:
408*4882a593Smuzhiyun 	case DRM_FORMAT_ABGR1555:
409*4882a593Smuzhiyun 	case DRM_FORMAT_BGR565:
410*4882a593Smuzhiyun 		/* supported, but if AFBC then must be sparse mode */
411*4882a593Smuzhiyun 		return (!afbc) || (afbc && sparse);
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	case DRM_FORMAT_BGR888:
414*4882a593Smuzhiyun 		/* supported, but not for AFBC */
415*4882a593Smuzhiyun 		return !afbc;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	case DRM_FORMAT_YUYV:
418*4882a593Smuzhiyun 	case DRM_FORMAT_UYVY:
419*4882a593Smuzhiyun 	case DRM_FORMAT_NV12:
420*4882a593Smuzhiyun 	case DRM_FORMAT_YUV420:
421*4882a593Smuzhiyun 		/* not supported */
422*4882a593Smuzhiyun 		return false;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	default:
425*4882a593Smuzhiyun 		return false;
426*4882a593Smuzhiyun 	}
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun /*
430*4882a593Smuzhiyun  * Select the preferred MMU prefetch mode. Full-frame prefetch is preferred as
431*4882a593Smuzhiyun  * long as the framebuffer is all large pages. Otherwise partial-frame prefetch
432*4882a593Smuzhiyun  * is selected as long as it is supported for the current format. The selected
433*4882a593Smuzhiyun  * page size for prefetch is returned in pgsize_bitmap.
434*4882a593Smuzhiyun  */
malidp_mmu_prefetch_select_mode(struct malidp_plane_state * ms,u32 * pgsize_bitmap)435*4882a593Smuzhiyun static enum mmu_prefetch_mode malidp_mmu_prefetch_select_mode
436*4882a593Smuzhiyun 		(struct malidp_plane_state *ms,	u32 *pgsize_bitmap)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	u32 pgsizes;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	/* get the full-frame prefetch page size(s) supported by the MMU */
441*4882a593Smuzhiyun 	pgsizes = *pgsize_bitmap & MALIDP_MMU_PREFETCH_FULL_PGSIZES;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	while (pgsizes) {
444*4882a593Smuzhiyun 		u32 largest_pgsize = 1 << __fls(pgsizes);
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 		if (malidp_check_pages_threshold(ms, largest_pgsize)) {
447*4882a593Smuzhiyun 			*pgsize_bitmap = largest_pgsize;
448*4882a593Smuzhiyun 			return MALIDP_PREFETCH_MODE_FULL;
449*4882a593Smuzhiyun 		}
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 		pgsizes -= largest_pgsize;
452*4882a593Smuzhiyun 	}
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	/* get the partial-frame prefetch page size(s) supported by the MMU */
455*4882a593Smuzhiyun 	pgsizes = *pgsize_bitmap & MALIDP_MMU_PREFETCH_PARTIAL_PGSIZES;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	if (malidp_partial_prefetch_supported(ms->base.fb->format->format,
458*4882a593Smuzhiyun 					      ms->base.fb->modifier,
459*4882a593Smuzhiyun 					      ms->base.rotation)) {
460*4882a593Smuzhiyun 		/* partial prefetch using the smallest page size */
461*4882a593Smuzhiyun 		*pgsize_bitmap = 1 << __ffs(pgsizes);
462*4882a593Smuzhiyun 		return MALIDP_PREFETCH_MODE_PARTIAL;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 	*pgsize_bitmap = 0;
465*4882a593Smuzhiyun 	return MALIDP_PREFETCH_MODE_NONE;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun 
malidp_calc_mmu_control_value(enum mmu_prefetch_mode mode,u8 readahead,u8 n_planes,u32 pgsize)468*4882a593Smuzhiyun static u32 malidp_calc_mmu_control_value(enum mmu_prefetch_mode mode,
469*4882a593Smuzhiyun 					 u8 readahead, u8 n_planes, u32 pgsize)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun 	u32 mmu_ctrl = 0;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	if (mode != MALIDP_PREFETCH_MODE_NONE) {
474*4882a593Smuzhiyun 		mmu_ctrl |= MALIDP_MMU_CTRL_EN;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 		if (mode == MALIDP_PREFETCH_MODE_PARTIAL) {
477*4882a593Smuzhiyun 			mmu_ctrl |= MALIDP_MMU_CTRL_MODE;
478*4882a593Smuzhiyun 			mmu_ctrl |= MALIDP_MMU_CTRL_PP_NUM_REQ(readahead);
479*4882a593Smuzhiyun 		}
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 		if (pgsize == SZ_64K || pgsize == SZ_2M) {
482*4882a593Smuzhiyun 			int i;
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 			for (i = 0; i < n_planes; i++)
485*4882a593Smuzhiyun 				mmu_ctrl |= MALIDP_MMU_CTRL_PX_PS(i);
486*4882a593Smuzhiyun 		}
487*4882a593Smuzhiyun 	}
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	return mmu_ctrl;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
malidp_de_prefetch_settings(struct malidp_plane * mp,struct malidp_plane_state * ms)492*4882a593Smuzhiyun static void malidp_de_prefetch_settings(struct malidp_plane *mp,
493*4882a593Smuzhiyun 					struct malidp_plane_state *ms)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun 	if (!mp->layer->mmu_ctrl_offset)
496*4882a593Smuzhiyun 		return;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	/* get the page sizes supported by the MMU */
499*4882a593Smuzhiyun 	ms->mmu_prefetch_pgsize = malidp_get_pgsize_bitmap(mp);
500*4882a593Smuzhiyun 	ms->mmu_prefetch_mode  =
501*4882a593Smuzhiyun 		malidp_mmu_prefetch_select_mode(ms, &ms->mmu_prefetch_pgsize);
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
malidp_de_plane_check(struct drm_plane * plane,struct drm_plane_state * state)504*4882a593Smuzhiyun static int malidp_de_plane_check(struct drm_plane *plane,
505*4882a593Smuzhiyun 				 struct drm_plane_state *state)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun 	struct malidp_plane *mp = to_malidp_plane(plane);
508*4882a593Smuzhiyun 	struct malidp_plane_state *ms = to_malidp_plane_state(state);
509*4882a593Smuzhiyun 	bool rotated = state->rotation & MALIDP_ROTATED_MASK;
510*4882a593Smuzhiyun 	struct drm_framebuffer *fb;
511*4882a593Smuzhiyun 	u16 pixel_alpha = state->pixel_blend_mode;
512*4882a593Smuzhiyun 	int i, ret;
513*4882a593Smuzhiyun 	unsigned int block_w, block_h;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	if (!state->crtc || WARN_ON(!state->fb))
516*4882a593Smuzhiyun 		return 0;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	fb = state->fb;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	ms->format = malidp_hw_get_format_id(&mp->hwdev->hw->map,
521*4882a593Smuzhiyun 					     mp->layer->id, fb->format->format,
522*4882a593Smuzhiyun 					     !!fb->modifier);
523*4882a593Smuzhiyun 	if (ms->format == MALIDP_INVALID_FORMAT_ID)
524*4882a593Smuzhiyun 		return -EINVAL;
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	ms->n_planes = fb->format->num_planes;
527*4882a593Smuzhiyun 	for (i = 0; i < ms->n_planes; i++) {
528*4882a593Smuzhiyun 		u8 alignment = malidp_hw_get_pitch_align(mp->hwdev, rotated);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 		if (((fb->pitches[i] * drm_format_info_block_height(fb->format, i))
531*4882a593Smuzhiyun 				& (alignment - 1)) && !(fb->modifier)) {
532*4882a593Smuzhiyun 			DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
533*4882a593Smuzhiyun 				      fb->pitches[i], i);
534*4882a593Smuzhiyun 			return -EINVAL;
535*4882a593Smuzhiyun 		}
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	block_w = drm_format_info_block_width(fb->format, 0);
539*4882a593Smuzhiyun 	block_h = drm_format_info_block_height(fb->format, 0);
540*4882a593Smuzhiyun 	if (fb->width % block_w || fb->height % block_h) {
541*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Buffer width/height needs to be a multiple of tile sizes");
542*4882a593Smuzhiyun 		return -EINVAL;
543*4882a593Smuzhiyun 	}
544*4882a593Smuzhiyun 	if ((state->src_x >> 16) % block_w || (state->src_y >> 16) % block_h) {
545*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Plane src_x/src_y needs to be a multiple of tile sizes");
546*4882a593Smuzhiyun 		return -EINVAL;
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	if ((state->crtc_w > mp->hwdev->max_line_size) ||
550*4882a593Smuzhiyun 	    (state->crtc_h > mp->hwdev->max_line_size) ||
551*4882a593Smuzhiyun 	    (state->crtc_w < mp->hwdev->min_line_size) ||
552*4882a593Smuzhiyun 	    (state->crtc_h < mp->hwdev->min_line_size))
553*4882a593Smuzhiyun 		return -EINVAL;
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	/*
556*4882a593Smuzhiyun 	 * DP550/650 video layers can accept 3 plane formats only if
557*4882a593Smuzhiyun 	 * fb->pitches[1] == fb->pitches[2] since they don't have a
558*4882a593Smuzhiyun 	 * third plane stride register.
559*4882a593Smuzhiyun 	 */
560*4882a593Smuzhiyun 	if (ms->n_planes == 3 &&
561*4882a593Smuzhiyun 	    !(mp->hwdev->hw->features & MALIDP_DEVICE_LV_HAS_3_STRIDES) &&
562*4882a593Smuzhiyun 	    (state->fb->pitches[1] != state->fb->pitches[2]))
563*4882a593Smuzhiyun 		return -EINVAL;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	ret = malidp_se_check_scaling(mp, state);
566*4882a593Smuzhiyun 	if (ret)
567*4882a593Smuzhiyun 		return ret;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	/* validate the rotation constraints for each layer */
570*4882a593Smuzhiyun 	if (state->rotation != DRM_MODE_ROTATE_0) {
571*4882a593Smuzhiyun 		if (mp->layer->rot == ROTATE_NONE)
572*4882a593Smuzhiyun 			return -EINVAL;
573*4882a593Smuzhiyun 		if ((mp->layer->rot == ROTATE_COMPRESSED) && !(fb->modifier))
574*4882a593Smuzhiyun 			return -EINVAL;
575*4882a593Smuzhiyun 		/*
576*4882a593Smuzhiyun 		 * packed RGB888 / BGR888 can't be rotated or flipped
577*4882a593Smuzhiyun 		 * unless they are stored in a compressed way
578*4882a593Smuzhiyun 		 */
579*4882a593Smuzhiyun 		if ((fb->format->format == DRM_FORMAT_RGB888 ||
580*4882a593Smuzhiyun 		     fb->format->format == DRM_FORMAT_BGR888) && !(fb->modifier))
581*4882a593Smuzhiyun 			return -EINVAL;
582*4882a593Smuzhiyun 	}
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* SMART layer does not support AFBC */
585*4882a593Smuzhiyun 	if (mp->layer->id == DE_SMART && fb->modifier) {
586*4882a593Smuzhiyun 		DRM_ERROR("AFBC framebuffer not supported in SMART layer");
587*4882a593Smuzhiyun 		return -EINVAL;
588*4882a593Smuzhiyun 	}
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	ms->rotmem_size = 0;
591*4882a593Smuzhiyun 	if (state->rotation & MALIDP_ROTATED_MASK) {
592*4882a593Smuzhiyun 		int val;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 		val = mp->hwdev->hw->rotmem_required(mp->hwdev, state->crtc_w,
595*4882a593Smuzhiyun 						     state->crtc_h,
596*4882a593Smuzhiyun 						     fb->format->format,
597*4882a593Smuzhiyun 						     !!(fb->modifier));
598*4882a593Smuzhiyun 		if (val < 0)
599*4882a593Smuzhiyun 			return val;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 		ms->rotmem_size = val;
602*4882a593Smuzhiyun 	}
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	/* HW can't support plane + pixel blending */
605*4882a593Smuzhiyun 	if ((state->alpha != DRM_BLEND_ALPHA_OPAQUE) &&
606*4882a593Smuzhiyun 	    (pixel_alpha != DRM_MODE_BLEND_PIXEL_NONE) &&
607*4882a593Smuzhiyun 	    fb->format->has_alpha)
608*4882a593Smuzhiyun 		return -EINVAL;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	malidp_de_prefetch_settings(mp, ms);
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	return 0;
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun 
malidp_de_set_plane_pitches(struct malidp_plane * mp,int num_planes,unsigned int pitches[3])615*4882a593Smuzhiyun static void malidp_de_set_plane_pitches(struct malidp_plane *mp,
616*4882a593Smuzhiyun 					int num_planes, unsigned int pitches[3])
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun 	int i;
619*4882a593Smuzhiyun 	int num_strides = num_planes;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	if (!mp->layer->stride_offset)
622*4882a593Smuzhiyun 		return;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	if (num_planes == 3)
625*4882a593Smuzhiyun 		num_strides = (mp->hwdev->hw->features &
626*4882a593Smuzhiyun 			       MALIDP_DEVICE_LV_HAS_3_STRIDES) ? 3 : 2;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	/*
629*4882a593Smuzhiyun 	 * The drm convention for pitch is that it needs to cover width * cpp,
630*4882a593Smuzhiyun 	 * but our hardware wants the pitch/stride to cover all rows included
631*4882a593Smuzhiyun 	 * in a tile.
632*4882a593Smuzhiyun 	 */
633*4882a593Smuzhiyun 	for (i = 0; i < num_strides; ++i) {
634*4882a593Smuzhiyun 		unsigned int block_h = drm_format_info_block_height(mp->base.state->fb->format, i);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 		malidp_hw_write(mp->hwdev, pitches[i] * block_h,
637*4882a593Smuzhiyun 				mp->layer->base +
638*4882a593Smuzhiyun 				mp->layer->stride_offset + i * 4);
639*4882a593Smuzhiyun 	}
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun static const s16
643*4882a593Smuzhiyun malidp_yuv2rgb_coeffs[][DRM_COLOR_RANGE_MAX][MALIDP_COLORADJ_NUM_COEFFS] = {
644*4882a593Smuzhiyun 	[DRM_COLOR_YCBCR_BT601][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
645*4882a593Smuzhiyun 		1192,    0, 1634,
646*4882a593Smuzhiyun 		1192, -401, -832,
647*4882a593Smuzhiyun 		1192, 2066,    0,
648*4882a593Smuzhiyun 		  64,  512,  512
649*4882a593Smuzhiyun 	},
650*4882a593Smuzhiyun 	[DRM_COLOR_YCBCR_BT601][DRM_COLOR_YCBCR_FULL_RANGE] = {
651*4882a593Smuzhiyun 		1024,    0, 1436,
652*4882a593Smuzhiyun 		1024, -352, -731,
653*4882a593Smuzhiyun 		1024, 1815,    0,
654*4882a593Smuzhiyun 		   0,  512,  512
655*4882a593Smuzhiyun 	},
656*4882a593Smuzhiyun 	[DRM_COLOR_YCBCR_BT709][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
657*4882a593Smuzhiyun 		1192,    0, 1836,
658*4882a593Smuzhiyun 		1192, -218, -546,
659*4882a593Smuzhiyun 		1192, 2163,    0,
660*4882a593Smuzhiyun 		  64,  512,  512
661*4882a593Smuzhiyun 	},
662*4882a593Smuzhiyun 	[DRM_COLOR_YCBCR_BT709][DRM_COLOR_YCBCR_FULL_RANGE] = {
663*4882a593Smuzhiyun 		1024,    0, 1613,
664*4882a593Smuzhiyun 		1024, -192, -479,
665*4882a593Smuzhiyun 		1024, 1900,    0,
666*4882a593Smuzhiyun 		   0,  512,  512
667*4882a593Smuzhiyun 	},
668*4882a593Smuzhiyun 	[DRM_COLOR_YCBCR_BT2020][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
669*4882a593Smuzhiyun 		1024,    0, 1476,
670*4882a593Smuzhiyun 		1024, -165, -572,
671*4882a593Smuzhiyun 		1024, 1884,    0,
672*4882a593Smuzhiyun 		   0,  512,  512
673*4882a593Smuzhiyun 	},
674*4882a593Smuzhiyun 	[DRM_COLOR_YCBCR_BT2020][DRM_COLOR_YCBCR_FULL_RANGE] = {
675*4882a593Smuzhiyun 		1024,    0, 1510,
676*4882a593Smuzhiyun 		1024, -168, -585,
677*4882a593Smuzhiyun 		1024, 1927,    0,
678*4882a593Smuzhiyun 		   0,  512,  512
679*4882a593Smuzhiyun 	}
680*4882a593Smuzhiyun };
681*4882a593Smuzhiyun 
malidp_de_set_color_encoding(struct malidp_plane * plane,enum drm_color_encoding enc,enum drm_color_range range)682*4882a593Smuzhiyun static void malidp_de_set_color_encoding(struct malidp_plane *plane,
683*4882a593Smuzhiyun 					 enum drm_color_encoding enc,
684*4882a593Smuzhiyun 					 enum drm_color_range range)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	unsigned int i;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	for (i = 0; i < MALIDP_COLORADJ_NUM_COEFFS; i++) {
689*4882a593Smuzhiyun 		/* coefficients are signed, two's complement values */
690*4882a593Smuzhiyun 		malidp_hw_write(plane->hwdev, malidp_yuv2rgb_coeffs[enc][range][i],
691*4882a593Smuzhiyun 				plane->layer->base + plane->layer->yuv2rgb_offset +
692*4882a593Smuzhiyun 				i * 4);
693*4882a593Smuzhiyun 	}
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun 
malidp_de_set_mmu_control(struct malidp_plane * mp,struct malidp_plane_state * ms)696*4882a593Smuzhiyun static void malidp_de_set_mmu_control(struct malidp_plane *mp,
697*4882a593Smuzhiyun 				      struct malidp_plane_state *ms)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun 	u32 mmu_ctrl;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	/* check hardware supports MMU prefetch */
702*4882a593Smuzhiyun 	if (!mp->layer->mmu_ctrl_offset)
703*4882a593Smuzhiyun 		return;
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	mmu_ctrl = malidp_calc_mmu_control_value(ms->mmu_prefetch_mode,
706*4882a593Smuzhiyun 						 MALIDP_MMU_PREFETCH_READAHEAD,
707*4882a593Smuzhiyun 						 ms->n_planes,
708*4882a593Smuzhiyun 						 ms->mmu_prefetch_pgsize);
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, mmu_ctrl,
711*4882a593Smuzhiyun 			mp->layer->base + mp->layer->mmu_ctrl_offset);
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun 
malidp_set_plane_base_addr(struct drm_framebuffer * fb,struct malidp_plane * mp,int plane_index)714*4882a593Smuzhiyun static void malidp_set_plane_base_addr(struct drm_framebuffer *fb,
715*4882a593Smuzhiyun 				       struct malidp_plane *mp,
716*4882a593Smuzhiyun 				       int plane_index)
717*4882a593Smuzhiyun {
718*4882a593Smuzhiyun 	dma_addr_t paddr;
719*4882a593Smuzhiyun 	u16 ptr;
720*4882a593Smuzhiyun 	struct drm_plane *plane = &mp->base;
721*4882a593Smuzhiyun 	bool afbc = fb->modifier ? true : false;
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	ptr = mp->layer->ptr + (plane_index << 4);
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	/*
726*4882a593Smuzhiyun 	 * drm_fb_cma_get_gem_addr() alters the physical base address of the
727*4882a593Smuzhiyun 	 * framebuffer as per the plane's src_x, src_y co-ordinates (ie to
728*4882a593Smuzhiyun 	 * take care of source cropping).
729*4882a593Smuzhiyun 	 * For AFBC, this is not needed as the cropping is handled by _AD_CROP_H
730*4882a593Smuzhiyun 	 * and _AD_CROP_V registers.
731*4882a593Smuzhiyun 	 */
732*4882a593Smuzhiyun 	if (!afbc) {
733*4882a593Smuzhiyun 		paddr = drm_fb_cma_get_gem_addr(fb, plane->state,
734*4882a593Smuzhiyun 						plane_index);
735*4882a593Smuzhiyun 	} else {
736*4882a593Smuzhiyun 		struct drm_gem_cma_object *obj;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 		obj = drm_fb_cma_get_gem_obj(fb, plane_index);
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 		if (WARN_ON(!obj))
741*4882a593Smuzhiyun 			return;
742*4882a593Smuzhiyun 		paddr = obj->paddr;
743*4882a593Smuzhiyun 	}
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, lower_32_bits(paddr), ptr);
746*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, upper_32_bits(paddr), ptr + 4);
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun 
malidp_de_set_plane_afbc(struct drm_plane * plane)749*4882a593Smuzhiyun static void malidp_de_set_plane_afbc(struct drm_plane *plane)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun 	struct malidp_plane *mp;
752*4882a593Smuzhiyun 	u32 src_w, src_h, val = 0, src_x, src_y;
753*4882a593Smuzhiyun 	struct drm_framebuffer *fb = plane->state->fb;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	mp = to_malidp_plane(plane);
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	/* no afbc_decoder_offset means AFBC is not supported on this plane */
758*4882a593Smuzhiyun 	if (!mp->layer->afbc_decoder_offset)
759*4882a593Smuzhiyun 		return;
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	if (!fb->modifier) {
762*4882a593Smuzhiyun 		malidp_hw_write(mp->hwdev, 0, mp->layer->afbc_decoder_offset);
763*4882a593Smuzhiyun 		return;
764*4882a593Smuzhiyun 	}
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	/* convert src values from Q16 fixed point to integer */
767*4882a593Smuzhiyun 	src_w = plane->state->src_w >> 16;
768*4882a593Smuzhiyun 	src_h = plane->state->src_h >> 16;
769*4882a593Smuzhiyun 	src_x = plane->state->src_x >> 16;
770*4882a593Smuzhiyun 	src_y = plane->state->src_y >> 16;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	val = ((fb->width - (src_x + src_w)) << MALIDP_AD_CROP_RIGHT_OFFSET) |
773*4882a593Smuzhiyun 		   src_x;
774*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, val,
775*4882a593Smuzhiyun 			mp->layer->afbc_decoder_offset + MALIDP_AD_CROP_H);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	val = ((fb->height - (src_y + src_h)) << MALIDP_AD_CROP_BOTTOM_OFFSET) |
778*4882a593Smuzhiyun 		   src_y;
779*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, val,
780*4882a593Smuzhiyun 			mp->layer->afbc_decoder_offset + MALIDP_AD_CROP_V);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	val = MALIDP_AD_EN;
783*4882a593Smuzhiyun 	if (fb->modifier & AFBC_FORMAT_MOD_SPLIT)
784*4882a593Smuzhiyun 		val |= MALIDP_AD_BS;
785*4882a593Smuzhiyun 	if (fb->modifier & AFBC_FORMAT_MOD_YTR)
786*4882a593Smuzhiyun 		val |= MALIDP_AD_YTR;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, val, mp->layer->afbc_decoder_offset);
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun 
malidp_de_plane_update(struct drm_plane * plane,struct drm_plane_state * old_state)791*4882a593Smuzhiyun static void malidp_de_plane_update(struct drm_plane *plane,
792*4882a593Smuzhiyun 				   struct drm_plane_state *old_state)
793*4882a593Smuzhiyun {
794*4882a593Smuzhiyun 	struct malidp_plane *mp;
795*4882a593Smuzhiyun 	struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
796*4882a593Smuzhiyun 	struct drm_plane_state *state = plane->state;
797*4882a593Smuzhiyun 	u16 pixel_alpha = state->pixel_blend_mode;
798*4882a593Smuzhiyun 	u8 plane_alpha = state->alpha >> 8;
799*4882a593Smuzhiyun 	u32 src_w, src_h, dest_w, dest_h, val;
800*4882a593Smuzhiyun 	int i;
801*4882a593Smuzhiyun 	struct drm_framebuffer *fb = plane->state->fb;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	mp = to_malidp_plane(plane);
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	/*
806*4882a593Smuzhiyun 	 * For AFBC framebuffer, use the framebuffer width and height for
807*4882a593Smuzhiyun 	 * configuring layer input size register.
808*4882a593Smuzhiyun 	 */
809*4882a593Smuzhiyun 	if (fb->modifier) {
810*4882a593Smuzhiyun 		src_w = fb->width;
811*4882a593Smuzhiyun 		src_h = fb->height;
812*4882a593Smuzhiyun 	} else {
813*4882a593Smuzhiyun 		/* convert src values from Q16 fixed point to integer */
814*4882a593Smuzhiyun 		src_w = state->src_w >> 16;
815*4882a593Smuzhiyun 		src_h = state->src_h >> 16;
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	dest_w = state->crtc_w;
819*4882a593Smuzhiyun 	dest_h = state->crtc_h;
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	val = malidp_hw_read(mp->hwdev, mp->layer->base);
822*4882a593Smuzhiyun 	val = (val & ~LAYER_FORMAT_MASK) | ms->format;
823*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, val, mp->layer->base);
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	for (i = 0; i < ms->n_planes; i++)
826*4882a593Smuzhiyun 		malidp_set_plane_base_addr(fb, mp, i);
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	malidp_de_set_mmu_control(mp, ms);
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	malidp_de_set_plane_pitches(mp, ms->n_planes,
831*4882a593Smuzhiyun 				    state->fb->pitches);
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	if ((plane->state->color_encoding != old_state->color_encoding) ||
834*4882a593Smuzhiyun 	    (plane->state->color_range != old_state->color_range))
835*4882a593Smuzhiyun 		malidp_de_set_color_encoding(mp, plane->state->color_encoding,
836*4882a593Smuzhiyun 					     plane->state->color_range);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
839*4882a593Smuzhiyun 			mp->layer->base + MALIDP_LAYER_SIZE);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
842*4882a593Smuzhiyun 			mp->layer->base + MALIDP_LAYER_COMP_SIZE);
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, LAYER_H_VAL(state->crtc_x) |
845*4882a593Smuzhiyun 			LAYER_V_VAL(state->crtc_y),
846*4882a593Smuzhiyun 			mp->layer->base + MALIDP_LAYER_OFFSET);
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	if (mp->layer->id == DE_SMART) {
849*4882a593Smuzhiyun 		/*
850*4882a593Smuzhiyun 		 * Enable the first rectangle in the SMART layer to be
851*4882a593Smuzhiyun 		 * able to use it as a drm plane.
852*4882a593Smuzhiyun 		 */
853*4882a593Smuzhiyun 		malidp_hw_write(mp->hwdev, 1,
854*4882a593Smuzhiyun 				mp->layer->base + MALIDP550_LS_ENABLE);
855*4882a593Smuzhiyun 		malidp_hw_write(mp->hwdev,
856*4882a593Smuzhiyun 				LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
857*4882a593Smuzhiyun 				mp->layer->base + MALIDP550_LS_R1_IN_SIZE);
858*4882a593Smuzhiyun 	}
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	malidp_de_set_plane_afbc(plane);
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	/* first clear the rotation bits */
863*4882a593Smuzhiyun 	val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
864*4882a593Smuzhiyun 	val &= ~LAYER_ROT_MASK;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	/* setup the rotation and axis flip bits */
867*4882a593Smuzhiyun 	if (state->rotation & DRM_MODE_ROTATE_MASK)
868*4882a593Smuzhiyun 		val |= ilog2(plane->state->rotation & DRM_MODE_ROTATE_MASK) <<
869*4882a593Smuzhiyun 		       LAYER_ROT_OFFSET;
870*4882a593Smuzhiyun 	if (state->rotation & DRM_MODE_REFLECT_X)
871*4882a593Smuzhiyun 		val |= LAYER_H_FLIP;
872*4882a593Smuzhiyun 	if (state->rotation & DRM_MODE_REFLECT_Y)
873*4882a593Smuzhiyun 		val |= LAYER_V_FLIP;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	val &= ~(LAYER_COMP_MASK | LAYER_PMUL_ENABLE | LAYER_ALPHA(0xff));
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	if (state->alpha != DRM_BLEND_ALPHA_OPAQUE) {
878*4882a593Smuzhiyun 		val |= LAYER_COMP_PLANE;
879*4882a593Smuzhiyun 	} else if (state->fb->format->has_alpha) {
880*4882a593Smuzhiyun 		/* We only care about blend mode if the format has alpha */
881*4882a593Smuzhiyun 		switch (pixel_alpha) {
882*4882a593Smuzhiyun 		case DRM_MODE_BLEND_PREMULTI:
883*4882a593Smuzhiyun 			val |= LAYER_COMP_PIXEL | LAYER_PMUL_ENABLE;
884*4882a593Smuzhiyun 			break;
885*4882a593Smuzhiyun 		case DRM_MODE_BLEND_COVERAGE:
886*4882a593Smuzhiyun 			val |= LAYER_COMP_PIXEL;
887*4882a593Smuzhiyun 			break;
888*4882a593Smuzhiyun 		}
889*4882a593Smuzhiyun 	}
890*4882a593Smuzhiyun 	val |= LAYER_ALPHA(plane_alpha);
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	val &= ~LAYER_FLOWCFG(LAYER_FLOWCFG_MASK);
893*4882a593Smuzhiyun 	if (state->crtc) {
894*4882a593Smuzhiyun 		struct malidp_crtc_state *m =
895*4882a593Smuzhiyun 			to_malidp_crtc_state(state->crtc->state);
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 		if (m->scaler_config.scale_enable &&
898*4882a593Smuzhiyun 		    m->scaler_config.plane_src_id == mp->layer->id)
899*4882a593Smuzhiyun 			val |= LAYER_FLOWCFG(LAYER_FLOWCFG_SCALE_SE);
900*4882a593Smuzhiyun 	}
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	/* set the 'enable layer' bit */
903*4882a593Smuzhiyun 	val |= LAYER_ENABLE;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	malidp_hw_write(mp->hwdev, val,
906*4882a593Smuzhiyun 			mp->layer->base + MALIDP_LAYER_CONTROL);
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun 
malidp_de_plane_disable(struct drm_plane * plane,struct drm_plane_state * state)909*4882a593Smuzhiyun static void malidp_de_plane_disable(struct drm_plane *plane,
910*4882a593Smuzhiyun 				    struct drm_plane_state *state)
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun 	struct malidp_plane *mp = to_malidp_plane(plane);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	malidp_hw_clearbits(mp->hwdev,
915*4882a593Smuzhiyun 			    LAYER_ENABLE | LAYER_FLOWCFG(LAYER_FLOWCFG_MASK),
916*4882a593Smuzhiyun 			    mp->layer->base + MALIDP_LAYER_CONTROL);
917*4882a593Smuzhiyun }
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
920*4882a593Smuzhiyun 	.atomic_check = malidp_de_plane_check,
921*4882a593Smuzhiyun 	.atomic_update = malidp_de_plane_update,
922*4882a593Smuzhiyun 	.atomic_disable = malidp_de_plane_disable,
923*4882a593Smuzhiyun };
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun static const uint64_t linear_only_modifiers[] = {
926*4882a593Smuzhiyun 	DRM_FORMAT_MOD_LINEAR,
927*4882a593Smuzhiyun 	DRM_FORMAT_MOD_INVALID
928*4882a593Smuzhiyun };
929*4882a593Smuzhiyun 
malidp_de_planes_init(struct drm_device * drm)930*4882a593Smuzhiyun int malidp_de_planes_init(struct drm_device *drm)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	struct malidp_drm *malidp = drm->dev_private;
933*4882a593Smuzhiyun 	const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
934*4882a593Smuzhiyun 	struct malidp_plane *plane = NULL;
935*4882a593Smuzhiyun 	enum drm_plane_type plane_type;
936*4882a593Smuzhiyun 	unsigned long crtcs = BIT(drm->mode_config.num_crtc);
937*4882a593Smuzhiyun 	unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
938*4882a593Smuzhiyun 			      DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
939*4882a593Smuzhiyun 	unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
940*4882a593Smuzhiyun 				  BIT(DRM_MODE_BLEND_PREMULTI)   |
941*4882a593Smuzhiyun 				  BIT(DRM_MODE_BLEND_COVERAGE);
942*4882a593Smuzhiyun 	u32 *formats;
943*4882a593Smuzhiyun 	int ret, i = 0, j = 0, n;
944*4882a593Smuzhiyun 	u64 supported_modifiers[MODIFIERS_COUNT_MAX];
945*4882a593Smuzhiyun 	const u64 *modifiers;
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	modifiers = malidp_format_modifiers;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	if (!(map->features & MALIDP_DEVICE_AFBC_SUPPORT_SPLIT)) {
950*4882a593Smuzhiyun 		/*
951*4882a593Smuzhiyun 		 * Since our hardware does not support SPLIT, so build the list
952*4882a593Smuzhiyun 		 * of supported modifiers excluding SPLIT ones.
953*4882a593Smuzhiyun 		 */
954*4882a593Smuzhiyun 		while (*modifiers != DRM_FORMAT_MOD_INVALID) {
955*4882a593Smuzhiyun 			if (!(*modifiers & AFBC_SPLIT))
956*4882a593Smuzhiyun 				supported_modifiers[j++] = *modifiers;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 			modifiers++;
959*4882a593Smuzhiyun 		}
960*4882a593Smuzhiyun 		supported_modifiers[j++] = DRM_FORMAT_MOD_INVALID;
961*4882a593Smuzhiyun 		modifiers = supported_modifiers;
962*4882a593Smuzhiyun 	}
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	formats = kcalloc(map->n_pixel_formats, sizeof(*formats), GFP_KERNEL);
965*4882a593Smuzhiyun 	if (!formats) {
966*4882a593Smuzhiyun 		ret = -ENOMEM;
967*4882a593Smuzhiyun 		goto cleanup;
968*4882a593Smuzhiyun 	}
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	for (i = 0; i < map->n_layers; i++) {
971*4882a593Smuzhiyun 		u8 id = map->layers[i].id;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 		plane = kzalloc(sizeof(*plane), GFP_KERNEL);
974*4882a593Smuzhiyun 		if (!plane) {
975*4882a593Smuzhiyun 			ret = -ENOMEM;
976*4882a593Smuzhiyun 			goto cleanup;
977*4882a593Smuzhiyun 		}
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 		/* build the list of DRM supported formats based on the map */
980*4882a593Smuzhiyun 		for (n = 0, j = 0;  j < map->n_pixel_formats; j++) {
981*4882a593Smuzhiyun 			if ((map->pixel_formats[j].layer & id) == id)
982*4882a593Smuzhiyun 				formats[n++] = map->pixel_formats[j].format;
983*4882a593Smuzhiyun 		}
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 		plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
986*4882a593Smuzhiyun 					DRM_PLANE_TYPE_OVERLAY;
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 		/*
989*4882a593Smuzhiyun 		 * All the layers except smart layer supports AFBC modifiers.
990*4882a593Smuzhiyun 		 */
991*4882a593Smuzhiyun 		ret = drm_universal_plane_init(drm, &plane->base, crtcs,
992*4882a593Smuzhiyun 				&malidp_de_plane_funcs, formats, n,
993*4882a593Smuzhiyun 				(id == DE_SMART) ? linear_only_modifiers : modifiers,
994*4882a593Smuzhiyun 				plane_type, NULL);
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 		if (ret < 0)
997*4882a593Smuzhiyun 			goto cleanup;
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 		drm_plane_helper_add(&plane->base,
1000*4882a593Smuzhiyun 				     &malidp_de_plane_helper_funcs);
1001*4882a593Smuzhiyun 		plane->hwdev = malidp->dev;
1002*4882a593Smuzhiyun 		plane->layer = &map->layers[i];
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 		drm_plane_create_alpha_property(&plane->base);
1005*4882a593Smuzhiyun 		drm_plane_create_blend_mode_property(&plane->base, blend_caps);
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 		if (id == DE_SMART) {
1008*4882a593Smuzhiyun 			/* Skip the features which the SMART layer doesn't have. */
1009*4882a593Smuzhiyun 			continue;
1010*4882a593Smuzhiyun 		}
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 		drm_plane_create_rotation_property(&plane->base, DRM_MODE_ROTATE_0, flags);
1013*4882a593Smuzhiyun 		malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
1014*4882a593Smuzhiyun 				plane->layer->base + MALIDP_LAYER_COMPOSE);
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 		/* Attach the YUV->RGB property only to video layers */
1017*4882a593Smuzhiyun 		if (id & (DE_VIDEO1 | DE_VIDEO2)) {
1018*4882a593Smuzhiyun 			/* default encoding for YUV->RGB is BT601 NARROW */
1019*4882a593Smuzhiyun 			enum drm_color_encoding enc = DRM_COLOR_YCBCR_BT601;
1020*4882a593Smuzhiyun 			enum drm_color_range range = DRM_COLOR_YCBCR_LIMITED_RANGE;
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 			ret = drm_plane_create_color_properties(&plane->base,
1023*4882a593Smuzhiyun 					BIT(DRM_COLOR_YCBCR_BT601) | \
1024*4882a593Smuzhiyun 					BIT(DRM_COLOR_YCBCR_BT709) | \
1025*4882a593Smuzhiyun 					BIT(DRM_COLOR_YCBCR_BT2020),
1026*4882a593Smuzhiyun 					BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) | \
1027*4882a593Smuzhiyun 					BIT(DRM_COLOR_YCBCR_FULL_RANGE),
1028*4882a593Smuzhiyun 					enc, range);
1029*4882a593Smuzhiyun 			if (!ret)
1030*4882a593Smuzhiyun 				/* program the HW registers */
1031*4882a593Smuzhiyun 				malidp_de_set_color_encoding(plane, enc, range);
1032*4882a593Smuzhiyun 			else
1033*4882a593Smuzhiyun 				DRM_WARN("Failed to create video layer %d color properties\n", id);
1034*4882a593Smuzhiyun 		}
1035*4882a593Smuzhiyun 	}
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	kfree(formats);
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	return 0;
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun cleanup:
1042*4882a593Smuzhiyun 	kfree(formats);
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	return ret;
1045*4882a593Smuzhiyun }
1046