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