xref: /linux/drivers/gpu/drm/tegra/plane.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25acd3514SThierry Reding /*
35acd3514SThierry Reding  * Copyright (C) 2017 NVIDIA CORPORATION.  All rights reserved.
45acd3514SThierry Reding  */
55acd3514SThierry Reding 
65acd3514SThierry Reding #include <drm/drm_atomic.h>
75acd3514SThierry Reding #include <drm/drm_atomic_helper.h>
85acd3514SThierry Reding #include <drm/drm_plane_helper.h>
95acd3514SThierry Reding 
105acd3514SThierry Reding #include "dc.h"
115acd3514SThierry Reding #include "plane.h"
125acd3514SThierry Reding 
135acd3514SThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
145acd3514SThierry Reding {
155acd3514SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
165acd3514SThierry Reding 
175acd3514SThierry Reding 	drm_plane_cleanup(plane);
185acd3514SThierry Reding 	kfree(p);
195acd3514SThierry Reding }
205acd3514SThierry Reding 
215acd3514SThierry Reding static void tegra_plane_reset(struct drm_plane *plane)
225acd3514SThierry Reding {
233dae08bcSDmitry Osipenko 	struct tegra_plane *p = to_tegra_plane(plane);
245acd3514SThierry Reding 	struct tegra_plane_state *state;
255acd3514SThierry Reding 
265acd3514SThierry Reding 	if (plane->state)
275acd3514SThierry Reding 		__drm_atomic_helper_plane_destroy_state(plane->state);
285acd3514SThierry Reding 
295acd3514SThierry Reding 	kfree(plane->state);
305acd3514SThierry Reding 	plane->state = NULL;
315acd3514SThierry Reding 
325acd3514SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
335acd3514SThierry Reding 	if (state) {
345acd3514SThierry Reding 		plane->state = &state->base;
355acd3514SThierry Reding 		plane->state->plane = plane;
363dae08bcSDmitry Osipenko 		plane->state->zpos = p->index;
373dae08bcSDmitry Osipenko 		plane->state->normalized_zpos = p->index;
385acd3514SThierry Reding 	}
395acd3514SThierry Reding }
405acd3514SThierry Reding 
415acd3514SThierry Reding static struct drm_plane_state *
425acd3514SThierry Reding tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
435acd3514SThierry Reding {
445acd3514SThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
455acd3514SThierry Reding 	struct tegra_plane_state *copy;
46ebae8d07SThierry Reding 	unsigned int i;
475acd3514SThierry Reding 
485acd3514SThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
495acd3514SThierry Reding 	if (!copy)
505acd3514SThierry Reding 		return NULL;
515acd3514SThierry Reding 
525acd3514SThierry Reding 	__drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
535acd3514SThierry Reding 	copy->tiling = state->tiling;
545acd3514SThierry Reding 	copy->format = state->format;
555acd3514SThierry Reding 	copy->swap = state->swap;
56995c5a50SThierry Reding 	copy->bottom_up = state->bottom_up;
57ebae8d07SThierry Reding 	copy->opaque = state->opaque;
58ebae8d07SThierry Reding 
593dae08bcSDmitry Osipenko 	for (i = 0; i < 2; i++)
603dae08bcSDmitry Osipenko 		copy->blending[i] = state->blending[i];
615acd3514SThierry Reding 
625acd3514SThierry Reding 	return &copy->base;
635acd3514SThierry Reding }
645acd3514SThierry Reding 
655acd3514SThierry Reding static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
665acd3514SThierry Reding 					     struct drm_plane_state *state)
675acd3514SThierry Reding {
685acd3514SThierry Reding 	__drm_atomic_helper_plane_destroy_state(state);
695acd3514SThierry Reding 	kfree(state);
705acd3514SThierry Reding }
715acd3514SThierry Reding 
72e90124cbSThierry Reding static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
73e90124cbSThierry Reding 					     uint32_t format,
74e90124cbSThierry Reding 					     uint64_t modifier)
75e90124cbSThierry Reding {
76e90124cbSThierry Reding 	const struct drm_format_info *info = drm_format_info(format);
77e90124cbSThierry Reding 
78e90124cbSThierry Reding 	if (modifier == DRM_FORMAT_MOD_LINEAR)
79e90124cbSThierry Reding 		return true;
80e90124cbSThierry Reding 
81e90124cbSThierry Reding 	if (info->num_planes == 1)
82e90124cbSThierry Reding 		return true;
83e90124cbSThierry Reding 
84e90124cbSThierry Reding 	return false;
85e90124cbSThierry Reding }
86e90124cbSThierry Reding 
875acd3514SThierry Reding const struct drm_plane_funcs tegra_plane_funcs = {
885acd3514SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
895acd3514SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
905acd3514SThierry Reding 	.destroy = tegra_plane_destroy,
915acd3514SThierry Reding 	.reset = tegra_plane_reset,
925acd3514SThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
935acd3514SThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
94e90124cbSThierry Reding 	.format_mod_supported = tegra_plane_format_mod_supported,
955acd3514SThierry Reding };
965acd3514SThierry Reding 
975acd3514SThierry Reding int tegra_plane_state_add(struct tegra_plane *plane,
985acd3514SThierry Reding 			  struct drm_plane_state *state)
995acd3514SThierry Reding {
1005acd3514SThierry Reding 	struct drm_crtc_state *crtc_state;
1015acd3514SThierry Reding 	struct tegra_dc_state *tegra;
1025acd3514SThierry Reding 	int err;
1035acd3514SThierry Reding 
1045acd3514SThierry Reding 	/* Propagate errors from allocation or locking failures. */
1055acd3514SThierry Reding 	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
1065acd3514SThierry Reding 	if (IS_ERR(crtc_state))
1075acd3514SThierry Reding 		return PTR_ERR(crtc_state);
1085acd3514SThierry Reding 
1095acd3514SThierry Reding 	/* Check plane state for visibility and calculate clipping bounds */
11081af63a4SVille Syrjälä 	err = drm_atomic_helper_check_plane_state(state, crtc_state,
1115acd3514SThierry Reding 						  0, INT_MAX, true, true);
1125acd3514SThierry Reding 	if (err < 0)
1135acd3514SThierry Reding 		return err;
1145acd3514SThierry Reding 
1155acd3514SThierry Reding 	tegra = to_dc_state(crtc_state);
1165acd3514SThierry Reding 
1175acd3514SThierry Reding 	tegra->planes |= WIN_A_ACT_REQ << plane->index;
1185acd3514SThierry Reding 
1195acd3514SThierry Reding 	return 0;
1205acd3514SThierry Reding }
1215acd3514SThierry Reding 
1225acd3514SThierry Reding int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
1235acd3514SThierry Reding {
1245acd3514SThierry Reding 	/* assume no swapping of fetched data */
1255acd3514SThierry Reding 	if (swap)
1265acd3514SThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
1275acd3514SThierry Reding 
1285acd3514SThierry Reding 	switch (fourcc) {
129511c7023SThierry Reding 	case DRM_FORMAT_ARGB4444:
130511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B4G4R4A4;
1317772fdaeSThierry Reding 		break;
1327772fdaeSThierry Reding 
133511c7023SThierry Reding 	case DRM_FORMAT_ARGB1555:
134511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B5G5R5A1;
1355acd3514SThierry Reding 		break;
1365acd3514SThierry Reding 
137511c7023SThierry Reding 	case DRM_FORMAT_RGB565:
138511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B5G6R5;
139511c7023SThierry Reding 		break;
140511c7023SThierry Reding 
141511c7023SThierry Reding 	case DRM_FORMAT_RGBA5551:
142511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_A1B5G5R5;
1437772fdaeSThierry Reding 		break;
1447772fdaeSThierry Reding 
1457772fdaeSThierry Reding 	case DRM_FORMAT_ARGB8888:
1465acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8A8;
1475acd3514SThierry Reding 		break;
1485acd3514SThierry Reding 
149511c7023SThierry Reding 	case DRM_FORMAT_ABGR8888:
150511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8A8;
151511c7023SThierry Reding 		break;
152511c7023SThierry Reding 
153511c7023SThierry Reding 	case DRM_FORMAT_ABGR4444:
154511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R4G4B4A4;
155511c7023SThierry Reding 		break;
156511c7023SThierry Reding 
157511c7023SThierry Reding 	case DRM_FORMAT_ABGR1555:
158511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R5G5B5A;
159511c7023SThierry Reding 		break;
160511c7023SThierry Reding 
161511c7023SThierry Reding 	case DRM_FORMAT_BGRA5551:
162511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_AR5G5B5;
163511c7023SThierry Reding 		break;
164511c7023SThierry Reding 
165511c7023SThierry Reding 	case DRM_FORMAT_XRGB1555:
166511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B5G5R5X1;
167511c7023SThierry Reding 		break;
168511c7023SThierry Reding 
169511c7023SThierry Reding 	case DRM_FORMAT_RGBX5551:
170511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_X1B5G5R5;
171511c7023SThierry Reding 		break;
172511c7023SThierry Reding 
173511c7023SThierry Reding 	case DRM_FORMAT_XBGR1555:
174511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R5G5B5X1;
175511c7023SThierry Reding 		break;
176511c7023SThierry Reding 
177511c7023SThierry Reding 	case DRM_FORMAT_BGRX5551:
178511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_X1R5G5B5;
179511c7023SThierry Reding 		break;
180511c7023SThierry Reding 
181511c7023SThierry Reding 	case DRM_FORMAT_BGR565:
182511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R5G6B5;
183511c7023SThierry Reding 		break;
184511c7023SThierry Reding 
185511c7023SThierry Reding 	case DRM_FORMAT_BGRA8888:
186511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_A8R8G8B8;
187511c7023SThierry Reding 		break;
188511c7023SThierry Reding 
189511c7023SThierry Reding 	case DRM_FORMAT_RGBA8888:
190511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_A8B8G8R8;
191511c7023SThierry Reding 		break;
192511c7023SThierry Reding 
193511c7023SThierry Reding 	case DRM_FORMAT_XRGB8888:
194511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8X8;
195511c7023SThierry Reding 		break;
196511c7023SThierry Reding 
197511c7023SThierry Reding 	case DRM_FORMAT_XBGR8888:
198511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8X8;
1995acd3514SThierry Reding 		break;
2005acd3514SThierry Reding 
2015acd3514SThierry Reding 	case DRM_FORMAT_UYVY:
2025acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
2035acd3514SThierry Reding 		break;
2045acd3514SThierry Reding 
2055acd3514SThierry Reding 	case DRM_FORMAT_YUYV:
2065acd3514SThierry Reding 		if (!swap)
2075acd3514SThierry Reding 			return -EINVAL;
2085acd3514SThierry Reding 
2095acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
2105acd3514SThierry Reding 		*swap = BYTE_SWAP_SWAP2;
2115acd3514SThierry Reding 		break;
2125acd3514SThierry Reding 
2135acd3514SThierry Reding 	case DRM_FORMAT_YUV420:
2145acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr420P;
2155acd3514SThierry Reding 		break;
2165acd3514SThierry Reding 
2175acd3514SThierry Reding 	case DRM_FORMAT_YUV422:
2185acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422P;
2195acd3514SThierry Reding 		break;
2205acd3514SThierry Reding 
2215acd3514SThierry Reding 	default:
2225acd3514SThierry Reding 		return -EINVAL;
2235acd3514SThierry Reding 	}
2245acd3514SThierry Reding 
2255acd3514SThierry Reding 	return 0;
2265acd3514SThierry Reding }
2275acd3514SThierry Reding 
2285acd3514SThierry Reding bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
2295acd3514SThierry Reding {
2305acd3514SThierry Reding 	switch (format) {
2315acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
2325acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
2335acd3514SThierry Reding 		if (planar)
2345acd3514SThierry Reding 			*planar = false;
2355acd3514SThierry Reding 
2365acd3514SThierry Reding 		return true;
2375acd3514SThierry Reding 
2385acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
2395acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
2405acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
2415acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
2425acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
2435acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
2445acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
2455acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
2465acd3514SThierry Reding 		if (planar)
2475acd3514SThierry Reding 			*planar = true;
2485acd3514SThierry Reding 
2495acd3514SThierry Reding 		return true;
2505acd3514SThierry Reding 	}
2515acd3514SThierry Reding 
2525acd3514SThierry Reding 	if (planar)
2535acd3514SThierry Reding 		*planar = false;
2545acd3514SThierry Reding 
2555acd3514SThierry Reding 	return false;
2565acd3514SThierry Reding }
257ebae8d07SThierry Reding 
258ebae8d07SThierry Reding static bool __drm_format_has_alpha(u32 format)
259ebae8d07SThierry Reding {
260ebae8d07SThierry Reding 	switch (format) {
261ebae8d07SThierry Reding 	case DRM_FORMAT_ARGB1555:
262ebae8d07SThierry Reding 	case DRM_FORMAT_RGBA5551:
263ebae8d07SThierry Reding 	case DRM_FORMAT_ABGR8888:
264ebae8d07SThierry Reding 	case DRM_FORMAT_ARGB8888:
265ebae8d07SThierry Reding 		return true;
266ebae8d07SThierry Reding 	}
267ebae8d07SThierry Reding 
268ebae8d07SThierry Reding 	return false;
269ebae8d07SThierry Reding }
270ebae8d07SThierry Reding 
2713dae08bcSDmitry Osipenko static int tegra_plane_format_get_alpha(unsigned int opaque,
2723dae08bcSDmitry Osipenko 					unsigned int *alpha)
273ebae8d07SThierry Reding {
2745467a8b8SThierry Reding 	if (tegra_plane_format_is_yuv(opaque, NULL)) {
2755467a8b8SThierry Reding 		*alpha = opaque;
2765467a8b8SThierry Reding 		return 0;
2775467a8b8SThierry Reding 	}
2785467a8b8SThierry Reding 
279ebae8d07SThierry Reding 	switch (opaque) {
280ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_B5G5R5X1:
281ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_B5G5R5A1;
282ebae8d07SThierry Reding 		return 0;
283ebae8d07SThierry Reding 
284ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_X1B5G5R5:
285ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_A1B5G5R5;
286ebae8d07SThierry Reding 		return 0;
287ebae8d07SThierry Reding 
288ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_R8G8B8X8:
289ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_R8G8B8A8;
290ebae8d07SThierry Reding 		return 0;
291ebae8d07SThierry Reding 
292ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_B8G8R8X8:
293ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_B8G8R8A8;
294ebae8d07SThierry Reding 		return 0;
2958a927d64SThierry Reding 
2968a927d64SThierry Reding 	case WIN_COLOR_DEPTH_B5G6R5:
2978a927d64SThierry Reding 		*alpha = opaque;
2988a927d64SThierry Reding 		return 0;
299ebae8d07SThierry Reding 	}
300ebae8d07SThierry Reding 
301ebae8d07SThierry Reding 	return -EINVAL;
302ebae8d07SThierry Reding }
303ebae8d07SThierry Reding 
3043dae08bcSDmitry Osipenko /*
3053dae08bcSDmitry Osipenko  * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
3063dae08bcSDmitry Osipenko  * be emulated using the alpha formats and alpha blending disabled.
3073dae08bcSDmitry Osipenko  */
3083dae08bcSDmitry Osipenko static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
3093dae08bcSDmitry Osipenko 				     struct tegra_plane_state *state)
3103dae08bcSDmitry Osipenko {
3113dae08bcSDmitry Osipenko 	unsigned int format;
3123dae08bcSDmitry Osipenko 	int err;
3133dae08bcSDmitry Osipenko 
3143dae08bcSDmitry Osipenko 	switch (state->format) {
3153dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_B5G5R5A1:
3163dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_A1B5G5R5:
3173dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_R8G8B8A8:
3183dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_B8G8R8A8:
3193dae08bcSDmitry Osipenko 		state->opaque = false;
3203dae08bcSDmitry Osipenko 		break;
3213dae08bcSDmitry Osipenko 
3223dae08bcSDmitry Osipenko 	default:
3233dae08bcSDmitry Osipenko 		err = tegra_plane_format_get_alpha(state->format, &format);
3243dae08bcSDmitry Osipenko 		if (err < 0)
3253dae08bcSDmitry Osipenko 			return err;
3263dae08bcSDmitry Osipenko 
3273dae08bcSDmitry Osipenko 		state->format = format;
3283dae08bcSDmitry Osipenko 		state->opaque = true;
3293dae08bcSDmitry Osipenko 		break;
3303dae08bcSDmitry Osipenko 	}
3313dae08bcSDmitry Osipenko 
3323dae08bcSDmitry Osipenko 	return 0;
3333dae08bcSDmitry Osipenko }
3343dae08bcSDmitry Osipenko 
3353dae08bcSDmitry Osipenko static int tegra_plane_check_transparency(struct tegra_plane *tegra,
3363dae08bcSDmitry Osipenko 					  struct tegra_plane_state *state)
3373dae08bcSDmitry Osipenko {
3383dae08bcSDmitry Osipenko 	struct drm_plane_state *old, *plane_state;
3393dae08bcSDmitry Osipenko 	struct drm_plane *plane;
3403dae08bcSDmitry Osipenko 
3413dae08bcSDmitry Osipenko 	old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
3423dae08bcSDmitry Osipenko 
3433dae08bcSDmitry Osipenko 	/* check if zpos / transparency changed */
3443dae08bcSDmitry Osipenko 	if (old->normalized_zpos == state->base.normalized_zpos &&
3453dae08bcSDmitry Osipenko 	    to_tegra_plane_state(old)->opaque == state->opaque)
3463dae08bcSDmitry Osipenko 		return 0;
3473dae08bcSDmitry Osipenko 
3483dae08bcSDmitry Osipenko 	/* include all sibling planes into this commit */
3493dae08bcSDmitry Osipenko 	drm_for_each_plane(plane, tegra->base.dev) {
3503dae08bcSDmitry Osipenko 		struct tegra_plane *p = to_tegra_plane(plane);
3513dae08bcSDmitry Osipenko 
3523dae08bcSDmitry Osipenko 		/* skip this plane and planes on different CRTCs */
3533dae08bcSDmitry Osipenko 		if (p == tegra || p->dc != tegra->dc)
3543dae08bcSDmitry Osipenko 			continue;
3553dae08bcSDmitry Osipenko 
3563dae08bcSDmitry Osipenko 		plane_state = drm_atomic_get_plane_state(state->base.state,
3573dae08bcSDmitry Osipenko 							 plane);
3583dae08bcSDmitry Osipenko 		if (IS_ERR(plane_state))
3593dae08bcSDmitry Osipenko 			return PTR_ERR(plane_state);
3603dae08bcSDmitry Osipenko 	}
3613dae08bcSDmitry Osipenko 
3623dae08bcSDmitry Osipenko 	return 1;
3633dae08bcSDmitry Osipenko }
3643dae08bcSDmitry Osipenko 
3655e2e86f1SDmitry Osipenko static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
366ebae8d07SThierry Reding 						  struct tegra_plane *other)
367ebae8d07SThierry Reding {
368ebae8d07SThierry Reding 	unsigned int index = 0, i;
369ebae8d07SThierry Reding 
370ebae8d07SThierry Reding 	WARN_ON(plane == other);
371ebae8d07SThierry Reding 
372ebae8d07SThierry Reding 	for (i = 0; i < 3; i++) {
373ebae8d07SThierry Reding 		if (i == plane->index)
374ebae8d07SThierry Reding 			continue;
375ebae8d07SThierry Reding 
376ebae8d07SThierry Reding 		if (i == other->index)
377ebae8d07SThierry Reding 			break;
378ebae8d07SThierry Reding 
379ebae8d07SThierry Reding 		index++;
380ebae8d07SThierry Reding 	}
381ebae8d07SThierry Reding 
382ebae8d07SThierry Reding 	return index;
383ebae8d07SThierry Reding }
384ebae8d07SThierry Reding 
3853dae08bcSDmitry Osipenko static void tegra_plane_update_transparency(struct tegra_plane *tegra,
386ebae8d07SThierry Reding 					    struct tegra_plane_state *state)
387ebae8d07SThierry Reding {
3883dae08bcSDmitry Osipenko 	struct drm_plane_state *new;
389ebae8d07SThierry Reding 	struct drm_plane *plane;
390ebae8d07SThierry Reding 	unsigned int i;
391ebae8d07SThierry Reding 
3923dae08bcSDmitry Osipenko 	for_each_new_plane_in_state(state->base.state, plane, new, i) {
393ebae8d07SThierry Reding 		struct tegra_plane *p = to_tegra_plane(plane);
394ebae8d07SThierry Reding 		unsigned index;
395ebae8d07SThierry Reding 
396ebae8d07SThierry Reding 		/* skip this plane and planes on different CRTCs */
3973dae08bcSDmitry Osipenko 		if (p == tegra || p->dc != tegra->dc)
398ebae8d07SThierry Reding 			continue;
399ebae8d07SThierry Reding 
400ebae8d07SThierry Reding 		index = tegra_plane_get_overlap_index(tegra, p);
401ebae8d07SThierry Reding 
4023dae08bcSDmitry Osipenko 		if (new->fb && __drm_format_has_alpha(new->fb->format->format))
4033dae08bcSDmitry Osipenko 			state->blending[index].alpha = true;
4043dae08bcSDmitry Osipenko 		else
4053dae08bcSDmitry Osipenko 			state->blending[index].alpha = false;
4063dae08bcSDmitry Osipenko 
4073dae08bcSDmitry Osipenko 		if (new->normalized_zpos > state->base.normalized_zpos)
4083dae08bcSDmitry Osipenko 			state->blending[index].top = true;
4093dae08bcSDmitry Osipenko 		else
4103dae08bcSDmitry Osipenko 			state->blending[index].top = false;
41148519232SDmitry Osipenko 
412ebae8d07SThierry Reding 		/*
4133dae08bcSDmitry Osipenko 		 * Missing framebuffer means that plane is disabled, in this
4143dae08bcSDmitry Osipenko 		 * case mark B / C window as top to be able to differentiate
4153dae08bcSDmitry Osipenko 		 * windows indices order in regards to zPos for the middle
4163dae08bcSDmitry Osipenko 		 * window X / Y registers programming.
417ebae8d07SThierry Reding 		 */
4183dae08bcSDmitry Osipenko 		if (!new->fb)
4193dae08bcSDmitry Osipenko 			state->blending[index].top = (index == 1);
420ebae8d07SThierry Reding 	}
421ebae8d07SThierry Reding }
422ebae8d07SThierry Reding 
4233dae08bcSDmitry Osipenko static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
4243dae08bcSDmitry Osipenko 					  struct tegra_plane_state *state)
4253dae08bcSDmitry Osipenko {
4263dae08bcSDmitry Osipenko 	struct tegra_plane_state *tegra_state;
4273dae08bcSDmitry Osipenko 	struct drm_plane_state *new;
4283dae08bcSDmitry Osipenko 	struct drm_plane *plane;
4293dae08bcSDmitry Osipenko 	int err;
430ebae8d07SThierry Reding 
431ebae8d07SThierry Reding 	/*
4323dae08bcSDmitry Osipenko 	 * If planes zpos / transparency changed, sibling planes blending
4333dae08bcSDmitry Osipenko 	 * state may require adjustment and in this case they will be included
4343dae08bcSDmitry Osipenko 	 * into this atom commit, otherwise blending state is unchanged.
435ebae8d07SThierry Reding 	 */
4363dae08bcSDmitry Osipenko 	err = tegra_plane_check_transparency(tegra, state);
4373dae08bcSDmitry Osipenko 	if (err <= 0)
4383dae08bcSDmitry Osipenko 		return err;
4393dae08bcSDmitry Osipenko 
4403dae08bcSDmitry Osipenko 	/*
4413dae08bcSDmitry Osipenko 	 * All planes are now in the atomic state, walk them up and update
4423dae08bcSDmitry Osipenko 	 * transparency state for each plane.
4433dae08bcSDmitry Osipenko 	 */
4443dae08bcSDmitry Osipenko 	drm_for_each_plane(plane, tegra->base.dev) {
4453dae08bcSDmitry Osipenko 		struct tegra_plane *p = to_tegra_plane(plane);
4463dae08bcSDmitry Osipenko 
4473dae08bcSDmitry Osipenko 		/* skip planes on different CRTCs */
4483dae08bcSDmitry Osipenko 		if (p->dc != tegra->dc)
4493dae08bcSDmitry Osipenko 			continue;
4503dae08bcSDmitry Osipenko 
4513dae08bcSDmitry Osipenko 		new = drm_atomic_get_new_plane_state(state->base.state, plane);
4523dae08bcSDmitry Osipenko 		tegra_state = to_tegra_plane_state(new);
4533dae08bcSDmitry Osipenko 
4543dae08bcSDmitry Osipenko 		/*
4553dae08bcSDmitry Osipenko 		 * There is no need to update blending state for the disabled
4563dae08bcSDmitry Osipenko 		 * plane.
4573dae08bcSDmitry Osipenko 		 */
4583dae08bcSDmitry Osipenko 		if (new->fb)
4593dae08bcSDmitry Osipenko 			tegra_plane_update_transparency(p, tegra_state);
460ebae8d07SThierry Reding 	}
4613dae08bcSDmitry Osipenko 
4623dae08bcSDmitry Osipenko 	return 0;
4633dae08bcSDmitry Osipenko }
4643dae08bcSDmitry Osipenko 
4653dae08bcSDmitry Osipenko int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
4663dae08bcSDmitry Osipenko 				   struct tegra_plane_state *state)
4673dae08bcSDmitry Osipenko {
4683dae08bcSDmitry Osipenko 	int err;
4693dae08bcSDmitry Osipenko 
4703dae08bcSDmitry Osipenko 	err = tegra_plane_setup_opacity(tegra, state);
4713dae08bcSDmitry Osipenko 	if (err < 0)
4723dae08bcSDmitry Osipenko 		return err;
4733dae08bcSDmitry Osipenko 
4743dae08bcSDmitry Osipenko 	err = tegra_plane_setup_transparency(tegra, state);
4753dae08bcSDmitry Osipenko 	if (err < 0)
4763dae08bcSDmitry Osipenko 		return err;
4773dae08bcSDmitry Osipenko 
4783dae08bcSDmitry Osipenko 	return 0;
479ebae8d07SThierry Reding }
480