xref: /linux/drivers/gpu/drm/tegra/plane.c (revision 49f821919bb9d45de7f1cde6072de01d36235b5d)
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>
8eb1df694SSam Ravnborg #include <drm/drm_fourcc.h>
92e8d8749SThierry Reding #include <drm/drm_gem_framebuffer_helper.h>
105acd3514SThierry Reding #include <drm/drm_plane_helper.h>
115acd3514SThierry Reding 
125acd3514SThierry Reding #include "dc.h"
135acd3514SThierry Reding #include "plane.h"
145acd3514SThierry Reding 
155acd3514SThierry Reding static void tegra_plane_destroy(struct drm_plane *plane)
165acd3514SThierry Reding {
175acd3514SThierry Reding 	struct tegra_plane *p = to_tegra_plane(plane);
185acd3514SThierry Reding 
195acd3514SThierry Reding 	drm_plane_cleanup(plane);
205acd3514SThierry Reding 	kfree(p);
215acd3514SThierry Reding }
225acd3514SThierry Reding 
235acd3514SThierry Reding static void tegra_plane_reset(struct drm_plane *plane)
245acd3514SThierry Reding {
253dae08bcSDmitry Osipenko 	struct tegra_plane *p = to_tegra_plane(plane);
265acd3514SThierry Reding 	struct tegra_plane_state *state;
272e8d8749SThierry Reding 	unsigned int i;
285acd3514SThierry Reding 
295acd3514SThierry Reding 	if (plane->state)
305acd3514SThierry Reding 		__drm_atomic_helper_plane_destroy_state(plane->state);
315acd3514SThierry Reding 
325acd3514SThierry Reding 	kfree(plane->state);
335acd3514SThierry Reding 	plane->state = NULL;
345acd3514SThierry Reding 
355acd3514SThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
365acd3514SThierry Reding 	if (state) {
375acd3514SThierry Reding 		plane->state = &state->base;
385acd3514SThierry Reding 		plane->state->plane = plane;
393dae08bcSDmitry Osipenko 		plane->state->zpos = p->index;
403dae08bcSDmitry Osipenko 		plane->state->normalized_zpos = p->index;
412e8d8749SThierry Reding 
422e8d8749SThierry Reding 		for (i = 0; i < 3; i++)
432e8d8749SThierry Reding 			state->iova[i] = DMA_MAPPING_ERROR;
445acd3514SThierry Reding 	}
455acd3514SThierry Reding }
465acd3514SThierry Reding 
475acd3514SThierry Reding static struct drm_plane_state *
485acd3514SThierry Reding tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
495acd3514SThierry Reding {
505acd3514SThierry Reding 	struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
515acd3514SThierry Reding 	struct tegra_plane_state *copy;
52ebae8d07SThierry Reding 	unsigned int i;
535acd3514SThierry Reding 
545acd3514SThierry Reding 	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
555acd3514SThierry Reding 	if (!copy)
565acd3514SThierry Reding 		return NULL;
575acd3514SThierry Reding 
585acd3514SThierry Reding 	__drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
595acd3514SThierry Reding 	copy->tiling = state->tiling;
605acd3514SThierry Reding 	copy->format = state->format;
615acd3514SThierry Reding 	copy->swap = state->swap;
62995c5a50SThierry Reding 	copy->bottom_up = state->bottom_up;
63ebae8d07SThierry Reding 	copy->opaque = state->opaque;
64ebae8d07SThierry Reding 
653dae08bcSDmitry Osipenko 	for (i = 0; i < 2; i++)
663dae08bcSDmitry Osipenko 		copy->blending[i] = state->blending[i];
675acd3514SThierry Reding 
682e8d8749SThierry Reding 	for (i = 0; i < 3; i++) {
692e8d8749SThierry Reding 		copy->iova[i] = DMA_MAPPING_ERROR;
702e8d8749SThierry Reding 		copy->sgt[i] = NULL;
712e8d8749SThierry Reding 	}
722e8d8749SThierry Reding 
735acd3514SThierry Reding 	return &copy->base;
745acd3514SThierry Reding }
755acd3514SThierry Reding 
765acd3514SThierry Reding static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
775acd3514SThierry Reding 					     struct drm_plane_state *state)
785acd3514SThierry Reding {
795acd3514SThierry Reding 	__drm_atomic_helper_plane_destroy_state(state);
805acd3514SThierry Reding 	kfree(state);
815acd3514SThierry Reding }
825acd3514SThierry Reding 
83e90124cbSThierry Reding static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
84e90124cbSThierry Reding 					     uint32_t format,
85e90124cbSThierry Reding 					     uint64_t modifier)
86e90124cbSThierry Reding {
87e90124cbSThierry Reding 	const struct drm_format_info *info = drm_format_info(format);
88e90124cbSThierry Reding 
89e90124cbSThierry Reding 	if (modifier == DRM_FORMAT_MOD_LINEAR)
90e90124cbSThierry Reding 		return true;
91e90124cbSThierry Reding 
92e90124cbSThierry Reding 	if (info->num_planes == 1)
93e90124cbSThierry Reding 		return true;
94e90124cbSThierry Reding 
95e90124cbSThierry Reding 	return false;
96e90124cbSThierry Reding }
97e90124cbSThierry Reding 
985acd3514SThierry Reding const struct drm_plane_funcs tegra_plane_funcs = {
995acd3514SThierry Reding 	.update_plane = drm_atomic_helper_update_plane,
1005acd3514SThierry Reding 	.disable_plane = drm_atomic_helper_disable_plane,
1015acd3514SThierry Reding 	.destroy = tegra_plane_destroy,
1025acd3514SThierry Reding 	.reset = tegra_plane_reset,
1035acd3514SThierry Reding 	.atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
1045acd3514SThierry Reding 	.atomic_destroy_state = tegra_plane_atomic_destroy_state,
105e90124cbSThierry Reding 	.format_mod_supported = tegra_plane_format_mod_supported,
1065acd3514SThierry Reding };
1075acd3514SThierry Reding 
1082e8d8749SThierry Reding static int tegra_dc_pin(struct tegra_dc *dc, struct tegra_plane_state *state)
1092e8d8749SThierry Reding {
1102e8d8749SThierry Reding 	unsigned int i;
1112e8d8749SThierry Reding 	int err;
1122e8d8749SThierry Reding 
1132e8d8749SThierry Reding 	for (i = 0; i < state->base.fb->format->num_planes; i++) {
1142e8d8749SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
1152e8d8749SThierry Reding 
1162e8d8749SThierry Reding 		if (!dc->client.group) {
1172e8d8749SThierry Reding 			struct sg_table *sgt;
1182e8d8749SThierry Reding 
1192e8d8749SThierry Reding 			sgt = host1x_bo_pin(dc->dev, &bo->base, NULL);
1202e8d8749SThierry Reding 			if (IS_ERR(sgt)) {
1212e8d8749SThierry Reding 				err = PTR_ERR(sgt);
1222e8d8749SThierry Reding 				goto unpin;
1232e8d8749SThierry Reding 			}
1242e8d8749SThierry Reding 
1252e8d8749SThierry Reding 			err = dma_map_sg(dc->dev, sgt->sgl, sgt->nents,
1262e8d8749SThierry Reding 					 DMA_TO_DEVICE);
1272e8d8749SThierry Reding 			if (err == 0) {
1282e8d8749SThierry Reding 				err = -ENOMEM;
1292e8d8749SThierry Reding 				goto unpin;
1302e8d8749SThierry Reding 			}
1312e8d8749SThierry Reding 
132*49f82191SThierry Reding 			/*
133*49f82191SThierry Reding 			 * The display controller needs contiguous memory, so
134*49f82191SThierry Reding 			 * fail if the buffer is discontiguous and we fail to
135*49f82191SThierry Reding 			 * map its SG table to a single contiguous chunk of
136*49f82191SThierry Reding 			 * I/O virtual memory.
137*49f82191SThierry Reding 			 */
138*49f82191SThierry Reding 			if (err > 1) {
139*49f82191SThierry Reding 				err = -EINVAL;
140*49f82191SThierry Reding 				goto unpin;
141*49f82191SThierry Reding 			}
142*49f82191SThierry Reding 
1432e8d8749SThierry Reding 			state->iova[i] = sg_dma_address(sgt->sgl);
1442e8d8749SThierry Reding 			state->sgt[i] = sgt;
1452e8d8749SThierry Reding 		} else {
1462e8d8749SThierry Reding 			state->iova[i] = bo->iova;
1472e8d8749SThierry Reding 		}
1482e8d8749SThierry Reding 	}
1492e8d8749SThierry Reding 
1502e8d8749SThierry Reding 	return 0;
1512e8d8749SThierry Reding 
1522e8d8749SThierry Reding unpin:
1532e8d8749SThierry Reding 	dev_err(dc->dev, "failed to map plane %u: %d\n", i, err);
1542e8d8749SThierry Reding 
1552e8d8749SThierry Reding 	while (i--) {
1562e8d8749SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
1572e8d8749SThierry Reding 		struct sg_table *sgt = state->sgt[i];
1582e8d8749SThierry Reding 
1592e8d8749SThierry Reding 		dma_unmap_sg(dc->dev, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
1602e8d8749SThierry Reding 		host1x_bo_unpin(dc->dev, &bo->base, sgt);
1612e8d8749SThierry Reding 
1622e8d8749SThierry Reding 		state->iova[i] = DMA_MAPPING_ERROR;
1632e8d8749SThierry Reding 		state->sgt[i] = NULL;
1642e8d8749SThierry Reding 	}
1652e8d8749SThierry Reding 
1662e8d8749SThierry Reding 	return err;
1672e8d8749SThierry Reding }
1682e8d8749SThierry Reding 
1692e8d8749SThierry Reding static void tegra_dc_unpin(struct tegra_dc *dc, struct tegra_plane_state *state)
1702e8d8749SThierry Reding {
1712e8d8749SThierry Reding 	unsigned int i;
1722e8d8749SThierry Reding 
1732e8d8749SThierry Reding 	for (i = 0; i < state->base.fb->format->num_planes; i++) {
1742e8d8749SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
1752e8d8749SThierry Reding 
1762e8d8749SThierry Reding 		if (!dc->client.group) {
1772e8d8749SThierry Reding 			struct sg_table *sgt = state->sgt[i];
1782e8d8749SThierry Reding 
1792e8d8749SThierry Reding 			if (sgt) {
1802e8d8749SThierry Reding 				dma_unmap_sg(dc->dev, sgt->sgl, sgt->nents,
1812e8d8749SThierry Reding 					     DMA_TO_DEVICE);
1822e8d8749SThierry Reding 				host1x_bo_unpin(dc->dev, &bo->base, sgt);
1832e8d8749SThierry Reding 			}
1842e8d8749SThierry Reding 		}
1852e8d8749SThierry Reding 
1862e8d8749SThierry Reding 		state->iova[i] = DMA_MAPPING_ERROR;
1872e8d8749SThierry Reding 		state->sgt[i] = NULL;
1882e8d8749SThierry Reding 	}
1892e8d8749SThierry Reding }
1902e8d8749SThierry Reding 
1912e8d8749SThierry Reding int tegra_plane_prepare_fb(struct drm_plane *plane,
1922e8d8749SThierry Reding 			   struct drm_plane_state *state)
1932e8d8749SThierry Reding {
1942e8d8749SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
1952e8d8749SThierry Reding 
1962e8d8749SThierry Reding 	if (!state->fb)
1972e8d8749SThierry Reding 		return 0;
1982e8d8749SThierry Reding 
1992e8d8749SThierry Reding 	drm_gem_fb_prepare_fb(plane, state);
2002e8d8749SThierry Reding 
2012e8d8749SThierry Reding 	return tegra_dc_pin(dc, to_tegra_plane_state(state));
2022e8d8749SThierry Reding }
2032e8d8749SThierry Reding 
2042e8d8749SThierry Reding void tegra_plane_cleanup_fb(struct drm_plane *plane,
2052e8d8749SThierry Reding 			    struct drm_plane_state *state)
2062e8d8749SThierry Reding {
2072e8d8749SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
2082e8d8749SThierry Reding 
2092e8d8749SThierry Reding 	if (dc)
2102e8d8749SThierry Reding 		tegra_dc_unpin(dc, to_tegra_plane_state(state));
2112e8d8749SThierry Reding }
2122e8d8749SThierry Reding 
2135acd3514SThierry Reding int tegra_plane_state_add(struct tegra_plane *plane,
2145acd3514SThierry Reding 			  struct drm_plane_state *state)
2155acd3514SThierry Reding {
2165acd3514SThierry Reding 	struct drm_crtc_state *crtc_state;
2175acd3514SThierry Reding 	struct tegra_dc_state *tegra;
2185acd3514SThierry Reding 	int err;
2195acd3514SThierry Reding 
2205acd3514SThierry Reding 	/* Propagate errors from allocation or locking failures. */
2215acd3514SThierry Reding 	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
2225acd3514SThierry Reding 	if (IS_ERR(crtc_state))
2235acd3514SThierry Reding 		return PTR_ERR(crtc_state);
2245acd3514SThierry Reding 
2255acd3514SThierry Reding 	/* Check plane state for visibility and calculate clipping bounds */
22681af63a4SVille Syrjälä 	err = drm_atomic_helper_check_plane_state(state, crtc_state,
2275acd3514SThierry Reding 						  0, INT_MAX, true, true);
2285acd3514SThierry Reding 	if (err < 0)
2295acd3514SThierry Reding 		return err;
2305acd3514SThierry Reding 
2315acd3514SThierry Reding 	tegra = to_dc_state(crtc_state);
2325acd3514SThierry Reding 
2335acd3514SThierry Reding 	tegra->planes |= WIN_A_ACT_REQ << plane->index;
2345acd3514SThierry Reding 
2355acd3514SThierry Reding 	return 0;
2365acd3514SThierry Reding }
2375acd3514SThierry Reding 
2385acd3514SThierry Reding int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
2395acd3514SThierry Reding {
2405acd3514SThierry Reding 	/* assume no swapping of fetched data */
2415acd3514SThierry Reding 	if (swap)
2425acd3514SThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
2435acd3514SThierry Reding 
2445acd3514SThierry Reding 	switch (fourcc) {
245511c7023SThierry Reding 	case DRM_FORMAT_ARGB4444:
246511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B4G4R4A4;
2477772fdaeSThierry Reding 		break;
2487772fdaeSThierry Reding 
249511c7023SThierry Reding 	case DRM_FORMAT_ARGB1555:
250511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B5G5R5A1;
2515acd3514SThierry Reding 		break;
2525acd3514SThierry Reding 
253511c7023SThierry Reding 	case DRM_FORMAT_RGB565:
254511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B5G6R5;
255511c7023SThierry Reding 		break;
256511c7023SThierry Reding 
257511c7023SThierry Reding 	case DRM_FORMAT_RGBA5551:
258511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_A1B5G5R5;
2597772fdaeSThierry Reding 		break;
2607772fdaeSThierry Reding 
2617772fdaeSThierry Reding 	case DRM_FORMAT_ARGB8888:
2625acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8A8;
2635acd3514SThierry Reding 		break;
2645acd3514SThierry Reding 
265511c7023SThierry Reding 	case DRM_FORMAT_ABGR8888:
266511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8A8;
267511c7023SThierry Reding 		break;
268511c7023SThierry Reding 
269511c7023SThierry Reding 	case DRM_FORMAT_ABGR4444:
270511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R4G4B4A4;
271511c7023SThierry Reding 		break;
272511c7023SThierry Reding 
273511c7023SThierry Reding 	case DRM_FORMAT_ABGR1555:
274511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R5G5B5A;
275511c7023SThierry Reding 		break;
276511c7023SThierry Reding 
277511c7023SThierry Reding 	case DRM_FORMAT_BGRA5551:
278511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_AR5G5B5;
279511c7023SThierry Reding 		break;
280511c7023SThierry Reding 
281511c7023SThierry Reding 	case DRM_FORMAT_XRGB1555:
282511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B5G5R5X1;
283511c7023SThierry Reding 		break;
284511c7023SThierry Reding 
285511c7023SThierry Reding 	case DRM_FORMAT_RGBX5551:
286511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_X1B5G5R5;
287511c7023SThierry Reding 		break;
288511c7023SThierry Reding 
289511c7023SThierry Reding 	case DRM_FORMAT_XBGR1555:
290511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R5G5B5X1;
291511c7023SThierry Reding 		break;
292511c7023SThierry Reding 
293511c7023SThierry Reding 	case DRM_FORMAT_BGRX5551:
294511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_X1R5G5B5;
295511c7023SThierry Reding 		break;
296511c7023SThierry Reding 
297511c7023SThierry Reding 	case DRM_FORMAT_BGR565:
298511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R5G6B5;
299511c7023SThierry Reding 		break;
300511c7023SThierry Reding 
301511c7023SThierry Reding 	case DRM_FORMAT_BGRA8888:
302511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_A8R8G8B8;
303511c7023SThierry Reding 		break;
304511c7023SThierry Reding 
305511c7023SThierry Reding 	case DRM_FORMAT_RGBA8888:
306511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_A8B8G8R8;
307511c7023SThierry Reding 		break;
308511c7023SThierry Reding 
309511c7023SThierry Reding 	case DRM_FORMAT_XRGB8888:
310511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8X8;
311511c7023SThierry Reding 		break;
312511c7023SThierry Reding 
313511c7023SThierry Reding 	case DRM_FORMAT_XBGR8888:
314511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8X8;
3155acd3514SThierry Reding 		break;
3165acd3514SThierry Reding 
3175acd3514SThierry Reding 	case DRM_FORMAT_UYVY:
3185acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
3195acd3514SThierry Reding 		break;
3205acd3514SThierry Reding 
3215acd3514SThierry Reding 	case DRM_FORMAT_YUYV:
3225acd3514SThierry Reding 		if (!swap)
3235acd3514SThierry Reding 			return -EINVAL;
3245acd3514SThierry Reding 
3255acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
3265acd3514SThierry Reding 		*swap = BYTE_SWAP_SWAP2;
3275acd3514SThierry Reding 		break;
3285acd3514SThierry Reding 
3295acd3514SThierry Reding 	case DRM_FORMAT_YUV420:
3305acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr420P;
3315acd3514SThierry Reding 		break;
3325acd3514SThierry Reding 
3335acd3514SThierry Reding 	case DRM_FORMAT_YUV422:
3345acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422P;
3355acd3514SThierry Reding 		break;
3365acd3514SThierry Reding 
3375acd3514SThierry Reding 	default:
3385acd3514SThierry Reding 		return -EINVAL;
3395acd3514SThierry Reding 	}
3405acd3514SThierry Reding 
3415acd3514SThierry Reding 	return 0;
3425acd3514SThierry Reding }
3435acd3514SThierry Reding 
3445acd3514SThierry Reding bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
3455acd3514SThierry Reding {
3465acd3514SThierry Reding 	switch (format) {
3475acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
3485acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
3495acd3514SThierry Reding 		if (planar)
3505acd3514SThierry Reding 			*planar = false;
3515acd3514SThierry Reding 
3525acd3514SThierry Reding 		return true;
3535acd3514SThierry Reding 
3545acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
3555acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
3565acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
3575acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
3585acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
3595acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
3605acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
3615acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
3625acd3514SThierry Reding 		if (planar)
3635acd3514SThierry Reding 			*planar = true;
3645acd3514SThierry Reding 
3655acd3514SThierry Reding 		return true;
3665acd3514SThierry Reding 	}
3675acd3514SThierry Reding 
3685acd3514SThierry Reding 	if (planar)
3695acd3514SThierry Reding 		*planar = false;
3705acd3514SThierry Reding 
3715acd3514SThierry Reding 	return false;
3725acd3514SThierry Reding }
373ebae8d07SThierry Reding 
374ebae8d07SThierry Reding static bool __drm_format_has_alpha(u32 format)
375ebae8d07SThierry Reding {
376ebae8d07SThierry Reding 	switch (format) {
377ebae8d07SThierry Reding 	case DRM_FORMAT_ARGB1555:
378ebae8d07SThierry Reding 	case DRM_FORMAT_RGBA5551:
379ebae8d07SThierry Reding 	case DRM_FORMAT_ABGR8888:
380ebae8d07SThierry Reding 	case DRM_FORMAT_ARGB8888:
381ebae8d07SThierry Reding 		return true;
382ebae8d07SThierry Reding 	}
383ebae8d07SThierry Reding 
384ebae8d07SThierry Reding 	return false;
385ebae8d07SThierry Reding }
386ebae8d07SThierry Reding 
3873dae08bcSDmitry Osipenko static int tegra_plane_format_get_alpha(unsigned int opaque,
3883dae08bcSDmitry Osipenko 					unsigned int *alpha)
389ebae8d07SThierry Reding {
3905467a8b8SThierry Reding 	if (tegra_plane_format_is_yuv(opaque, NULL)) {
3915467a8b8SThierry Reding 		*alpha = opaque;
3925467a8b8SThierry Reding 		return 0;
3935467a8b8SThierry Reding 	}
3945467a8b8SThierry Reding 
395ebae8d07SThierry Reding 	switch (opaque) {
396ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_B5G5R5X1:
397ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_B5G5R5A1;
398ebae8d07SThierry Reding 		return 0;
399ebae8d07SThierry Reding 
400ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_X1B5G5R5:
401ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_A1B5G5R5;
402ebae8d07SThierry Reding 		return 0;
403ebae8d07SThierry Reding 
404ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_R8G8B8X8:
405ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_R8G8B8A8;
406ebae8d07SThierry Reding 		return 0;
407ebae8d07SThierry Reding 
408ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_B8G8R8X8:
409ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_B8G8R8A8;
410ebae8d07SThierry Reding 		return 0;
4118a927d64SThierry Reding 
4128a927d64SThierry Reding 	case WIN_COLOR_DEPTH_B5G6R5:
4138a927d64SThierry Reding 		*alpha = opaque;
4148a927d64SThierry Reding 		return 0;
415ebae8d07SThierry Reding 	}
416ebae8d07SThierry Reding 
417ebae8d07SThierry Reding 	return -EINVAL;
418ebae8d07SThierry Reding }
419ebae8d07SThierry Reding 
4203dae08bcSDmitry Osipenko /*
4213dae08bcSDmitry Osipenko  * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
4223dae08bcSDmitry Osipenko  * be emulated using the alpha formats and alpha blending disabled.
4233dae08bcSDmitry Osipenko  */
4243dae08bcSDmitry Osipenko static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
4253dae08bcSDmitry Osipenko 				     struct tegra_plane_state *state)
4263dae08bcSDmitry Osipenko {
4273dae08bcSDmitry Osipenko 	unsigned int format;
4283dae08bcSDmitry Osipenko 	int err;
4293dae08bcSDmitry Osipenko 
4303dae08bcSDmitry Osipenko 	switch (state->format) {
4313dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_B5G5R5A1:
4323dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_A1B5G5R5:
4333dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_R8G8B8A8:
4343dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_B8G8R8A8:
4353dae08bcSDmitry Osipenko 		state->opaque = false;
4363dae08bcSDmitry Osipenko 		break;
4373dae08bcSDmitry Osipenko 
4383dae08bcSDmitry Osipenko 	default:
4393dae08bcSDmitry Osipenko 		err = tegra_plane_format_get_alpha(state->format, &format);
4403dae08bcSDmitry Osipenko 		if (err < 0)
4413dae08bcSDmitry Osipenko 			return err;
4423dae08bcSDmitry Osipenko 
4433dae08bcSDmitry Osipenko 		state->format = format;
4443dae08bcSDmitry Osipenko 		state->opaque = true;
4453dae08bcSDmitry Osipenko 		break;
4463dae08bcSDmitry Osipenko 	}
4473dae08bcSDmitry Osipenko 
4483dae08bcSDmitry Osipenko 	return 0;
4493dae08bcSDmitry Osipenko }
4503dae08bcSDmitry Osipenko 
4513dae08bcSDmitry Osipenko static int tegra_plane_check_transparency(struct tegra_plane *tegra,
4523dae08bcSDmitry Osipenko 					  struct tegra_plane_state *state)
4533dae08bcSDmitry Osipenko {
4543dae08bcSDmitry Osipenko 	struct drm_plane_state *old, *plane_state;
4553dae08bcSDmitry Osipenko 	struct drm_plane *plane;
4563dae08bcSDmitry Osipenko 
4573dae08bcSDmitry Osipenko 	old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
4583dae08bcSDmitry Osipenko 
4593dae08bcSDmitry Osipenko 	/* check if zpos / transparency changed */
4603dae08bcSDmitry Osipenko 	if (old->normalized_zpos == state->base.normalized_zpos &&
4613dae08bcSDmitry Osipenko 	    to_tegra_plane_state(old)->opaque == state->opaque)
4623dae08bcSDmitry Osipenko 		return 0;
4633dae08bcSDmitry Osipenko 
4643dae08bcSDmitry Osipenko 	/* include all sibling planes into this commit */
4653dae08bcSDmitry Osipenko 	drm_for_each_plane(plane, tegra->base.dev) {
4663dae08bcSDmitry Osipenko 		struct tegra_plane *p = to_tegra_plane(plane);
4673dae08bcSDmitry Osipenko 
4683dae08bcSDmitry Osipenko 		/* skip this plane and planes on different CRTCs */
4693dae08bcSDmitry Osipenko 		if (p == tegra || p->dc != tegra->dc)
4703dae08bcSDmitry Osipenko 			continue;
4713dae08bcSDmitry Osipenko 
4723dae08bcSDmitry Osipenko 		plane_state = drm_atomic_get_plane_state(state->base.state,
4733dae08bcSDmitry Osipenko 							 plane);
4743dae08bcSDmitry Osipenko 		if (IS_ERR(plane_state))
4753dae08bcSDmitry Osipenko 			return PTR_ERR(plane_state);
4763dae08bcSDmitry Osipenko 	}
4773dae08bcSDmitry Osipenko 
4783dae08bcSDmitry Osipenko 	return 1;
4793dae08bcSDmitry Osipenko }
4803dae08bcSDmitry Osipenko 
4815e2e86f1SDmitry Osipenko static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
482ebae8d07SThierry Reding 						  struct tegra_plane *other)
483ebae8d07SThierry Reding {
484ebae8d07SThierry Reding 	unsigned int index = 0, i;
485ebae8d07SThierry Reding 
486ebae8d07SThierry Reding 	WARN_ON(plane == other);
487ebae8d07SThierry Reding 
488ebae8d07SThierry Reding 	for (i = 0; i < 3; i++) {
489ebae8d07SThierry Reding 		if (i == plane->index)
490ebae8d07SThierry Reding 			continue;
491ebae8d07SThierry Reding 
492ebae8d07SThierry Reding 		if (i == other->index)
493ebae8d07SThierry Reding 			break;
494ebae8d07SThierry Reding 
495ebae8d07SThierry Reding 		index++;
496ebae8d07SThierry Reding 	}
497ebae8d07SThierry Reding 
498ebae8d07SThierry Reding 	return index;
499ebae8d07SThierry Reding }
500ebae8d07SThierry Reding 
5013dae08bcSDmitry Osipenko static void tegra_plane_update_transparency(struct tegra_plane *tegra,
502ebae8d07SThierry Reding 					    struct tegra_plane_state *state)
503ebae8d07SThierry Reding {
5043dae08bcSDmitry Osipenko 	struct drm_plane_state *new;
505ebae8d07SThierry Reding 	struct drm_plane *plane;
506ebae8d07SThierry Reding 	unsigned int i;
507ebae8d07SThierry Reding 
5083dae08bcSDmitry Osipenko 	for_each_new_plane_in_state(state->base.state, plane, new, i) {
509ebae8d07SThierry Reding 		struct tegra_plane *p = to_tegra_plane(plane);
510ebae8d07SThierry Reding 		unsigned index;
511ebae8d07SThierry Reding 
512ebae8d07SThierry Reding 		/* skip this plane and planes on different CRTCs */
5133dae08bcSDmitry Osipenko 		if (p == tegra || p->dc != tegra->dc)
514ebae8d07SThierry Reding 			continue;
515ebae8d07SThierry Reding 
516ebae8d07SThierry Reding 		index = tegra_plane_get_overlap_index(tegra, p);
517ebae8d07SThierry Reding 
5183dae08bcSDmitry Osipenko 		if (new->fb && __drm_format_has_alpha(new->fb->format->format))
5193dae08bcSDmitry Osipenko 			state->blending[index].alpha = true;
5203dae08bcSDmitry Osipenko 		else
5213dae08bcSDmitry Osipenko 			state->blending[index].alpha = false;
5223dae08bcSDmitry Osipenko 
5233dae08bcSDmitry Osipenko 		if (new->normalized_zpos > state->base.normalized_zpos)
5243dae08bcSDmitry Osipenko 			state->blending[index].top = true;
5253dae08bcSDmitry Osipenko 		else
5263dae08bcSDmitry Osipenko 			state->blending[index].top = false;
52748519232SDmitry Osipenko 
528ebae8d07SThierry Reding 		/*
5293dae08bcSDmitry Osipenko 		 * Missing framebuffer means that plane is disabled, in this
5303dae08bcSDmitry Osipenko 		 * case mark B / C window as top to be able to differentiate
5313dae08bcSDmitry Osipenko 		 * windows indices order in regards to zPos for the middle
5323dae08bcSDmitry Osipenko 		 * window X / Y registers programming.
533ebae8d07SThierry Reding 		 */
5343dae08bcSDmitry Osipenko 		if (!new->fb)
5353dae08bcSDmitry Osipenko 			state->blending[index].top = (index == 1);
536ebae8d07SThierry Reding 	}
537ebae8d07SThierry Reding }
538ebae8d07SThierry Reding 
5393dae08bcSDmitry Osipenko static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
5403dae08bcSDmitry Osipenko 					  struct tegra_plane_state *state)
5413dae08bcSDmitry Osipenko {
5423dae08bcSDmitry Osipenko 	struct tegra_plane_state *tegra_state;
5433dae08bcSDmitry Osipenko 	struct drm_plane_state *new;
5443dae08bcSDmitry Osipenko 	struct drm_plane *plane;
5453dae08bcSDmitry Osipenko 	int err;
546ebae8d07SThierry Reding 
547ebae8d07SThierry Reding 	/*
5483dae08bcSDmitry Osipenko 	 * If planes zpos / transparency changed, sibling planes blending
5493dae08bcSDmitry Osipenko 	 * state may require adjustment and in this case they will be included
5503dae08bcSDmitry Osipenko 	 * into this atom commit, otherwise blending state is unchanged.
551ebae8d07SThierry Reding 	 */
5523dae08bcSDmitry Osipenko 	err = tegra_plane_check_transparency(tegra, state);
5533dae08bcSDmitry Osipenko 	if (err <= 0)
5543dae08bcSDmitry Osipenko 		return err;
5553dae08bcSDmitry Osipenko 
5563dae08bcSDmitry Osipenko 	/*
5573dae08bcSDmitry Osipenko 	 * All planes are now in the atomic state, walk them up and update
5583dae08bcSDmitry Osipenko 	 * transparency state for each plane.
5593dae08bcSDmitry Osipenko 	 */
5603dae08bcSDmitry Osipenko 	drm_for_each_plane(plane, tegra->base.dev) {
5613dae08bcSDmitry Osipenko 		struct tegra_plane *p = to_tegra_plane(plane);
5623dae08bcSDmitry Osipenko 
5633dae08bcSDmitry Osipenko 		/* skip planes on different CRTCs */
5643dae08bcSDmitry Osipenko 		if (p->dc != tegra->dc)
5653dae08bcSDmitry Osipenko 			continue;
5663dae08bcSDmitry Osipenko 
5673dae08bcSDmitry Osipenko 		new = drm_atomic_get_new_plane_state(state->base.state, plane);
5683dae08bcSDmitry Osipenko 		tegra_state = to_tegra_plane_state(new);
5693dae08bcSDmitry Osipenko 
5703dae08bcSDmitry Osipenko 		/*
5713dae08bcSDmitry Osipenko 		 * There is no need to update blending state for the disabled
5723dae08bcSDmitry Osipenko 		 * plane.
5733dae08bcSDmitry Osipenko 		 */
5743dae08bcSDmitry Osipenko 		if (new->fb)
5753dae08bcSDmitry Osipenko 			tegra_plane_update_transparency(p, tegra_state);
576ebae8d07SThierry Reding 	}
5773dae08bcSDmitry Osipenko 
5783dae08bcSDmitry Osipenko 	return 0;
5793dae08bcSDmitry Osipenko }
5803dae08bcSDmitry Osipenko 
5813dae08bcSDmitry Osipenko int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
5823dae08bcSDmitry Osipenko 				   struct tegra_plane_state *state)
5833dae08bcSDmitry Osipenko {
5843dae08bcSDmitry Osipenko 	int err;
5853dae08bcSDmitry Osipenko 
5863dae08bcSDmitry Osipenko 	err = tegra_plane_setup_opacity(tegra, state);
5873dae08bcSDmitry Osipenko 	if (err < 0)
5883dae08bcSDmitry Osipenko 		return err;
5893dae08bcSDmitry Osipenko 
5903dae08bcSDmitry Osipenko 	err = tegra_plane_setup_transparency(tegra, state);
5913dae08bcSDmitry Osipenko 	if (err < 0)
5923dae08bcSDmitry Osipenko 		return err;
5933dae08bcSDmitry Osipenko 
5943dae08bcSDmitry Osipenko 	return 0;
595ebae8d07SThierry Reding }
596