xref: /linux/drivers/gpu/drm/tegra/plane.c (revision 2e8d8749f6f9bb35b947228271dc9ec31be93335)
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>
9*2e8d8749SThierry 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;
27*2e8d8749SThierry 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;
41*2e8d8749SThierry Reding 
42*2e8d8749SThierry Reding 		for (i = 0; i < 3; i++)
43*2e8d8749SThierry 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 
68*2e8d8749SThierry Reding 	for (i = 0; i < 3; i++) {
69*2e8d8749SThierry Reding 		copy->iova[i] = DMA_MAPPING_ERROR;
70*2e8d8749SThierry Reding 		copy->sgt[i] = NULL;
71*2e8d8749SThierry Reding 	}
72*2e8d8749SThierry 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 
108*2e8d8749SThierry Reding static int tegra_dc_pin(struct tegra_dc *dc, struct tegra_plane_state *state)
109*2e8d8749SThierry Reding {
110*2e8d8749SThierry Reding 	unsigned int i;
111*2e8d8749SThierry Reding 	int err;
112*2e8d8749SThierry Reding 
113*2e8d8749SThierry Reding 	for (i = 0; i < state->base.fb->format->num_planes; i++) {
114*2e8d8749SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
115*2e8d8749SThierry Reding 
116*2e8d8749SThierry Reding 		if (!dc->client.group) {
117*2e8d8749SThierry Reding 			struct sg_table *sgt;
118*2e8d8749SThierry Reding 
119*2e8d8749SThierry Reding 			sgt = host1x_bo_pin(dc->dev, &bo->base, NULL);
120*2e8d8749SThierry Reding 			if (IS_ERR(sgt)) {
121*2e8d8749SThierry Reding 				err = PTR_ERR(sgt);
122*2e8d8749SThierry Reding 				goto unpin;
123*2e8d8749SThierry Reding 			}
124*2e8d8749SThierry Reding 
125*2e8d8749SThierry Reding 			err = dma_map_sg(dc->dev, sgt->sgl, sgt->nents,
126*2e8d8749SThierry Reding 					 DMA_TO_DEVICE);
127*2e8d8749SThierry Reding 			if (err == 0) {
128*2e8d8749SThierry Reding 				err = -ENOMEM;
129*2e8d8749SThierry Reding 				goto unpin;
130*2e8d8749SThierry Reding 			}
131*2e8d8749SThierry Reding 
132*2e8d8749SThierry Reding 			state->iova[i] = sg_dma_address(sgt->sgl);
133*2e8d8749SThierry Reding 			state->sgt[i] = sgt;
134*2e8d8749SThierry Reding 		} else {
135*2e8d8749SThierry Reding 			state->iova[i] = bo->iova;
136*2e8d8749SThierry Reding 		}
137*2e8d8749SThierry Reding 	}
138*2e8d8749SThierry Reding 
139*2e8d8749SThierry Reding 	return 0;
140*2e8d8749SThierry Reding 
141*2e8d8749SThierry Reding unpin:
142*2e8d8749SThierry Reding 	dev_err(dc->dev, "failed to map plane %u: %d\n", i, err);
143*2e8d8749SThierry Reding 
144*2e8d8749SThierry Reding 	while (i--) {
145*2e8d8749SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
146*2e8d8749SThierry Reding 		struct sg_table *sgt = state->sgt[i];
147*2e8d8749SThierry Reding 
148*2e8d8749SThierry Reding 		dma_unmap_sg(dc->dev, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
149*2e8d8749SThierry Reding 		host1x_bo_unpin(dc->dev, &bo->base, sgt);
150*2e8d8749SThierry Reding 
151*2e8d8749SThierry Reding 		state->iova[i] = DMA_MAPPING_ERROR;
152*2e8d8749SThierry Reding 		state->sgt[i] = NULL;
153*2e8d8749SThierry Reding 	}
154*2e8d8749SThierry Reding 
155*2e8d8749SThierry Reding 	return err;
156*2e8d8749SThierry Reding }
157*2e8d8749SThierry Reding 
158*2e8d8749SThierry Reding static void tegra_dc_unpin(struct tegra_dc *dc, struct tegra_plane_state *state)
159*2e8d8749SThierry Reding {
160*2e8d8749SThierry Reding 	unsigned int i;
161*2e8d8749SThierry Reding 
162*2e8d8749SThierry Reding 	for (i = 0; i < state->base.fb->format->num_planes; i++) {
163*2e8d8749SThierry Reding 		struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
164*2e8d8749SThierry Reding 
165*2e8d8749SThierry Reding 		if (!dc->client.group) {
166*2e8d8749SThierry Reding 			struct sg_table *sgt = state->sgt[i];
167*2e8d8749SThierry Reding 
168*2e8d8749SThierry Reding 			if (sgt) {
169*2e8d8749SThierry Reding 				dma_unmap_sg(dc->dev, sgt->sgl, sgt->nents,
170*2e8d8749SThierry Reding 					     DMA_TO_DEVICE);
171*2e8d8749SThierry Reding 				host1x_bo_unpin(dc->dev, &bo->base, sgt);
172*2e8d8749SThierry Reding 			}
173*2e8d8749SThierry Reding 		}
174*2e8d8749SThierry Reding 
175*2e8d8749SThierry Reding 		state->iova[i] = DMA_MAPPING_ERROR;
176*2e8d8749SThierry Reding 		state->sgt[i] = NULL;
177*2e8d8749SThierry Reding 	}
178*2e8d8749SThierry Reding }
179*2e8d8749SThierry Reding 
180*2e8d8749SThierry Reding int tegra_plane_prepare_fb(struct drm_plane *plane,
181*2e8d8749SThierry Reding 			   struct drm_plane_state *state)
182*2e8d8749SThierry Reding {
183*2e8d8749SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
184*2e8d8749SThierry Reding 
185*2e8d8749SThierry Reding 	if (!state->fb)
186*2e8d8749SThierry Reding 		return 0;
187*2e8d8749SThierry Reding 
188*2e8d8749SThierry Reding 	drm_gem_fb_prepare_fb(plane, state);
189*2e8d8749SThierry Reding 
190*2e8d8749SThierry Reding 	return tegra_dc_pin(dc, to_tegra_plane_state(state));
191*2e8d8749SThierry Reding }
192*2e8d8749SThierry Reding 
193*2e8d8749SThierry Reding void tegra_plane_cleanup_fb(struct drm_plane *plane,
194*2e8d8749SThierry Reding 			    struct drm_plane_state *state)
195*2e8d8749SThierry Reding {
196*2e8d8749SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(state->crtc);
197*2e8d8749SThierry Reding 
198*2e8d8749SThierry Reding 	if (dc)
199*2e8d8749SThierry Reding 		tegra_dc_unpin(dc, to_tegra_plane_state(state));
200*2e8d8749SThierry Reding }
201*2e8d8749SThierry Reding 
2025acd3514SThierry Reding int tegra_plane_state_add(struct tegra_plane *plane,
2035acd3514SThierry Reding 			  struct drm_plane_state *state)
2045acd3514SThierry Reding {
2055acd3514SThierry Reding 	struct drm_crtc_state *crtc_state;
2065acd3514SThierry Reding 	struct tegra_dc_state *tegra;
2075acd3514SThierry Reding 	int err;
2085acd3514SThierry Reding 
2095acd3514SThierry Reding 	/* Propagate errors from allocation or locking failures. */
2105acd3514SThierry Reding 	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
2115acd3514SThierry Reding 	if (IS_ERR(crtc_state))
2125acd3514SThierry Reding 		return PTR_ERR(crtc_state);
2135acd3514SThierry Reding 
2145acd3514SThierry Reding 	/* Check plane state for visibility and calculate clipping bounds */
21581af63a4SVille Syrjälä 	err = drm_atomic_helper_check_plane_state(state, crtc_state,
2165acd3514SThierry Reding 						  0, INT_MAX, true, true);
2175acd3514SThierry Reding 	if (err < 0)
2185acd3514SThierry Reding 		return err;
2195acd3514SThierry Reding 
2205acd3514SThierry Reding 	tegra = to_dc_state(crtc_state);
2215acd3514SThierry Reding 
2225acd3514SThierry Reding 	tegra->planes |= WIN_A_ACT_REQ << plane->index;
2235acd3514SThierry Reding 
2245acd3514SThierry Reding 	return 0;
2255acd3514SThierry Reding }
2265acd3514SThierry Reding 
2275acd3514SThierry Reding int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
2285acd3514SThierry Reding {
2295acd3514SThierry Reding 	/* assume no swapping of fetched data */
2305acd3514SThierry Reding 	if (swap)
2315acd3514SThierry Reding 		*swap = BYTE_SWAP_NOSWAP;
2325acd3514SThierry Reding 
2335acd3514SThierry Reding 	switch (fourcc) {
234511c7023SThierry Reding 	case DRM_FORMAT_ARGB4444:
235511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B4G4R4A4;
2367772fdaeSThierry Reding 		break;
2377772fdaeSThierry Reding 
238511c7023SThierry Reding 	case DRM_FORMAT_ARGB1555:
239511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B5G5R5A1;
2405acd3514SThierry Reding 		break;
2415acd3514SThierry Reding 
242511c7023SThierry Reding 	case DRM_FORMAT_RGB565:
243511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B5G6R5;
244511c7023SThierry Reding 		break;
245511c7023SThierry Reding 
246511c7023SThierry Reding 	case DRM_FORMAT_RGBA5551:
247511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_A1B5G5R5;
2487772fdaeSThierry Reding 		break;
2497772fdaeSThierry Reding 
2507772fdaeSThierry Reding 	case DRM_FORMAT_ARGB8888:
2515acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8A8;
2525acd3514SThierry Reding 		break;
2535acd3514SThierry Reding 
254511c7023SThierry Reding 	case DRM_FORMAT_ABGR8888:
255511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8A8;
256511c7023SThierry Reding 		break;
257511c7023SThierry Reding 
258511c7023SThierry Reding 	case DRM_FORMAT_ABGR4444:
259511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R4G4B4A4;
260511c7023SThierry Reding 		break;
261511c7023SThierry Reding 
262511c7023SThierry Reding 	case DRM_FORMAT_ABGR1555:
263511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R5G5B5A;
264511c7023SThierry Reding 		break;
265511c7023SThierry Reding 
266511c7023SThierry Reding 	case DRM_FORMAT_BGRA5551:
267511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_AR5G5B5;
268511c7023SThierry Reding 		break;
269511c7023SThierry Reding 
270511c7023SThierry Reding 	case DRM_FORMAT_XRGB1555:
271511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B5G5R5X1;
272511c7023SThierry Reding 		break;
273511c7023SThierry Reding 
274511c7023SThierry Reding 	case DRM_FORMAT_RGBX5551:
275511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_X1B5G5R5;
276511c7023SThierry Reding 		break;
277511c7023SThierry Reding 
278511c7023SThierry Reding 	case DRM_FORMAT_XBGR1555:
279511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R5G5B5X1;
280511c7023SThierry Reding 		break;
281511c7023SThierry Reding 
282511c7023SThierry Reding 	case DRM_FORMAT_BGRX5551:
283511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_X1R5G5B5;
284511c7023SThierry Reding 		break;
285511c7023SThierry Reding 
286511c7023SThierry Reding 	case DRM_FORMAT_BGR565:
287511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R5G6B5;
288511c7023SThierry Reding 		break;
289511c7023SThierry Reding 
290511c7023SThierry Reding 	case DRM_FORMAT_BGRA8888:
291511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_A8R8G8B8;
292511c7023SThierry Reding 		break;
293511c7023SThierry Reding 
294511c7023SThierry Reding 	case DRM_FORMAT_RGBA8888:
295511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_A8B8G8R8;
296511c7023SThierry Reding 		break;
297511c7023SThierry Reding 
298511c7023SThierry Reding 	case DRM_FORMAT_XRGB8888:
299511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_B8G8R8X8;
300511c7023SThierry Reding 		break;
301511c7023SThierry Reding 
302511c7023SThierry Reding 	case DRM_FORMAT_XBGR8888:
303511c7023SThierry Reding 		*format = WIN_COLOR_DEPTH_R8G8B8X8;
3045acd3514SThierry Reding 		break;
3055acd3514SThierry Reding 
3065acd3514SThierry Reding 	case DRM_FORMAT_UYVY:
3075acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
3085acd3514SThierry Reding 		break;
3095acd3514SThierry Reding 
3105acd3514SThierry Reding 	case DRM_FORMAT_YUYV:
3115acd3514SThierry Reding 		if (!swap)
3125acd3514SThierry Reding 			return -EINVAL;
3135acd3514SThierry Reding 
3145acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422;
3155acd3514SThierry Reding 		*swap = BYTE_SWAP_SWAP2;
3165acd3514SThierry Reding 		break;
3175acd3514SThierry Reding 
3185acd3514SThierry Reding 	case DRM_FORMAT_YUV420:
3195acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr420P;
3205acd3514SThierry Reding 		break;
3215acd3514SThierry Reding 
3225acd3514SThierry Reding 	case DRM_FORMAT_YUV422:
3235acd3514SThierry Reding 		*format = WIN_COLOR_DEPTH_YCbCr422P;
3245acd3514SThierry Reding 		break;
3255acd3514SThierry Reding 
3265acd3514SThierry Reding 	default:
3275acd3514SThierry Reding 		return -EINVAL;
3285acd3514SThierry Reding 	}
3295acd3514SThierry Reding 
3305acd3514SThierry Reding 	return 0;
3315acd3514SThierry Reding }
3325acd3514SThierry Reding 
3335acd3514SThierry Reding bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
3345acd3514SThierry Reding {
3355acd3514SThierry Reding 	switch (format) {
3365acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422:
3375acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422:
3385acd3514SThierry Reding 		if (planar)
3395acd3514SThierry Reding 			*planar = false;
3405acd3514SThierry Reding 
3415acd3514SThierry Reding 		return true;
3425acd3514SThierry Reding 
3435acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr420P:
3445acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV420P:
3455acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422P:
3465acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422P:
3475acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422R:
3485acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422R:
3495acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YCbCr422RA:
3505acd3514SThierry Reding 	case WIN_COLOR_DEPTH_YUV422RA:
3515acd3514SThierry Reding 		if (planar)
3525acd3514SThierry Reding 			*planar = true;
3535acd3514SThierry Reding 
3545acd3514SThierry Reding 		return true;
3555acd3514SThierry Reding 	}
3565acd3514SThierry Reding 
3575acd3514SThierry Reding 	if (planar)
3585acd3514SThierry Reding 		*planar = false;
3595acd3514SThierry Reding 
3605acd3514SThierry Reding 	return false;
3615acd3514SThierry Reding }
362ebae8d07SThierry Reding 
363ebae8d07SThierry Reding static bool __drm_format_has_alpha(u32 format)
364ebae8d07SThierry Reding {
365ebae8d07SThierry Reding 	switch (format) {
366ebae8d07SThierry Reding 	case DRM_FORMAT_ARGB1555:
367ebae8d07SThierry Reding 	case DRM_FORMAT_RGBA5551:
368ebae8d07SThierry Reding 	case DRM_FORMAT_ABGR8888:
369ebae8d07SThierry Reding 	case DRM_FORMAT_ARGB8888:
370ebae8d07SThierry Reding 		return true;
371ebae8d07SThierry Reding 	}
372ebae8d07SThierry Reding 
373ebae8d07SThierry Reding 	return false;
374ebae8d07SThierry Reding }
375ebae8d07SThierry Reding 
3763dae08bcSDmitry Osipenko static int tegra_plane_format_get_alpha(unsigned int opaque,
3773dae08bcSDmitry Osipenko 					unsigned int *alpha)
378ebae8d07SThierry Reding {
3795467a8b8SThierry Reding 	if (tegra_plane_format_is_yuv(opaque, NULL)) {
3805467a8b8SThierry Reding 		*alpha = opaque;
3815467a8b8SThierry Reding 		return 0;
3825467a8b8SThierry Reding 	}
3835467a8b8SThierry Reding 
384ebae8d07SThierry Reding 	switch (opaque) {
385ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_B5G5R5X1:
386ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_B5G5R5A1;
387ebae8d07SThierry Reding 		return 0;
388ebae8d07SThierry Reding 
389ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_X1B5G5R5:
390ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_A1B5G5R5;
391ebae8d07SThierry Reding 		return 0;
392ebae8d07SThierry Reding 
393ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_R8G8B8X8:
394ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_R8G8B8A8;
395ebae8d07SThierry Reding 		return 0;
396ebae8d07SThierry Reding 
397ebae8d07SThierry Reding 	case WIN_COLOR_DEPTH_B8G8R8X8:
398ebae8d07SThierry Reding 		*alpha = WIN_COLOR_DEPTH_B8G8R8A8;
399ebae8d07SThierry Reding 		return 0;
4008a927d64SThierry Reding 
4018a927d64SThierry Reding 	case WIN_COLOR_DEPTH_B5G6R5:
4028a927d64SThierry Reding 		*alpha = opaque;
4038a927d64SThierry Reding 		return 0;
404ebae8d07SThierry Reding 	}
405ebae8d07SThierry Reding 
406ebae8d07SThierry Reding 	return -EINVAL;
407ebae8d07SThierry Reding }
408ebae8d07SThierry Reding 
4093dae08bcSDmitry Osipenko /*
4103dae08bcSDmitry Osipenko  * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
4113dae08bcSDmitry Osipenko  * be emulated using the alpha formats and alpha blending disabled.
4123dae08bcSDmitry Osipenko  */
4133dae08bcSDmitry Osipenko static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
4143dae08bcSDmitry Osipenko 				     struct tegra_plane_state *state)
4153dae08bcSDmitry Osipenko {
4163dae08bcSDmitry Osipenko 	unsigned int format;
4173dae08bcSDmitry Osipenko 	int err;
4183dae08bcSDmitry Osipenko 
4193dae08bcSDmitry Osipenko 	switch (state->format) {
4203dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_B5G5R5A1:
4213dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_A1B5G5R5:
4223dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_R8G8B8A8:
4233dae08bcSDmitry Osipenko 	case WIN_COLOR_DEPTH_B8G8R8A8:
4243dae08bcSDmitry Osipenko 		state->opaque = false;
4253dae08bcSDmitry Osipenko 		break;
4263dae08bcSDmitry Osipenko 
4273dae08bcSDmitry Osipenko 	default:
4283dae08bcSDmitry Osipenko 		err = tegra_plane_format_get_alpha(state->format, &format);
4293dae08bcSDmitry Osipenko 		if (err < 0)
4303dae08bcSDmitry Osipenko 			return err;
4313dae08bcSDmitry Osipenko 
4323dae08bcSDmitry Osipenko 		state->format = format;
4333dae08bcSDmitry Osipenko 		state->opaque = true;
4343dae08bcSDmitry Osipenko 		break;
4353dae08bcSDmitry Osipenko 	}
4363dae08bcSDmitry Osipenko 
4373dae08bcSDmitry Osipenko 	return 0;
4383dae08bcSDmitry Osipenko }
4393dae08bcSDmitry Osipenko 
4403dae08bcSDmitry Osipenko static int tegra_plane_check_transparency(struct tegra_plane *tegra,
4413dae08bcSDmitry Osipenko 					  struct tegra_plane_state *state)
4423dae08bcSDmitry Osipenko {
4433dae08bcSDmitry Osipenko 	struct drm_plane_state *old, *plane_state;
4443dae08bcSDmitry Osipenko 	struct drm_plane *plane;
4453dae08bcSDmitry Osipenko 
4463dae08bcSDmitry Osipenko 	old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
4473dae08bcSDmitry Osipenko 
4483dae08bcSDmitry Osipenko 	/* check if zpos / transparency changed */
4493dae08bcSDmitry Osipenko 	if (old->normalized_zpos == state->base.normalized_zpos &&
4503dae08bcSDmitry Osipenko 	    to_tegra_plane_state(old)->opaque == state->opaque)
4513dae08bcSDmitry Osipenko 		return 0;
4523dae08bcSDmitry Osipenko 
4533dae08bcSDmitry Osipenko 	/* include all sibling planes into this commit */
4543dae08bcSDmitry Osipenko 	drm_for_each_plane(plane, tegra->base.dev) {
4553dae08bcSDmitry Osipenko 		struct tegra_plane *p = to_tegra_plane(plane);
4563dae08bcSDmitry Osipenko 
4573dae08bcSDmitry Osipenko 		/* skip this plane and planes on different CRTCs */
4583dae08bcSDmitry Osipenko 		if (p == tegra || p->dc != tegra->dc)
4593dae08bcSDmitry Osipenko 			continue;
4603dae08bcSDmitry Osipenko 
4613dae08bcSDmitry Osipenko 		plane_state = drm_atomic_get_plane_state(state->base.state,
4623dae08bcSDmitry Osipenko 							 plane);
4633dae08bcSDmitry Osipenko 		if (IS_ERR(plane_state))
4643dae08bcSDmitry Osipenko 			return PTR_ERR(plane_state);
4653dae08bcSDmitry Osipenko 	}
4663dae08bcSDmitry Osipenko 
4673dae08bcSDmitry Osipenko 	return 1;
4683dae08bcSDmitry Osipenko }
4693dae08bcSDmitry Osipenko 
4705e2e86f1SDmitry Osipenko static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
471ebae8d07SThierry Reding 						  struct tegra_plane *other)
472ebae8d07SThierry Reding {
473ebae8d07SThierry Reding 	unsigned int index = 0, i;
474ebae8d07SThierry Reding 
475ebae8d07SThierry Reding 	WARN_ON(plane == other);
476ebae8d07SThierry Reding 
477ebae8d07SThierry Reding 	for (i = 0; i < 3; i++) {
478ebae8d07SThierry Reding 		if (i == plane->index)
479ebae8d07SThierry Reding 			continue;
480ebae8d07SThierry Reding 
481ebae8d07SThierry Reding 		if (i == other->index)
482ebae8d07SThierry Reding 			break;
483ebae8d07SThierry Reding 
484ebae8d07SThierry Reding 		index++;
485ebae8d07SThierry Reding 	}
486ebae8d07SThierry Reding 
487ebae8d07SThierry Reding 	return index;
488ebae8d07SThierry Reding }
489ebae8d07SThierry Reding 
4903dae08bcSDmitry Osipenko static void tegra_plane_update_transparency(struct tegra_plane *tegra,
491ebae8d07SThierry Reding 					    struct tegra_plane_state *state)
492ebae8d07SThierry Reding {
4933dae08bcSDmitry Osipenko 	struct drm_plane_state *new;
494ebae8d07SThierry Reding 	struct drm_plane *plane;
495ebae8d07SThierry Reding 	unsigned int i;
496ebae8d07SThierry Reding 
4973dae08bcSDmitry Osipenko 	for_each_new_plane_in_state(state->base.state, plane, new, i) {
498ebae8d07SThierry Reding 		struct tegra_plane *p = to_tegra_plane(plane);
499ebae8d07SThierry Reding 		unsigned index;
500ebae8d07SThierry Reding 
501ebae8d07SThierry Reding 		/* skip this plane and planes on different CRTCs */
5023dae08bcSDmitry Osipenko 		if (p == tegra || p->dc != tegra->dc)
503ebae8d07SThierry Reding 			continue;
504ebae8d07SThierry Reding 
505ebae8d07SThierry Reding 		index = tegra_plane_get_overlap_index(tegra, p);
506ebae8d07SThierry Reding 
5073dae08bcSDmitry Osipenko 		if (new->fb && __drm_format_has_alpha(new->fb->format->format))
5083dae08bcSDmitry Osipenko 			state->blending[index].alpha = true;
5093dae08bcSDmitry Osipenko 		else
5103dae08bcSDmitry Osipenko 			state->blending[index].alpha = false;
5113dae08bcSDmitry Osipenko 
5123dae08bcSDmitry Osipenko 		if (new->normalized_zpos > state->base.normalized_zpos)
5133dae08bcSDmitry Osipenko 			state->blending[index].top = true;
5143dae08bcSDmitry Osipenko 		else
5153dae08bcSDmitry Osipenko 			state->blending[index].top = false;
51648519232SDmitry Osipenko 
517ebae8d07SThierry Reding 		/*
5183dae08bcSDmitry Osipenko 		 * Missing framebuffer means that plane is disabled, in this
5193dae08bcSDmitry Osipenko 		 * case mark B / C window as top to be able to differentiate
5203dae08bcSDmitry Osipenko 		 * windows indices order in regards to zPos for the middle
5213dae08bcSDmitry Osipenko 		 * window X / Y registers programming.
522ebae8d07SThierry Reding 		 */
5233dae08bcSDmitry Osipenko 		if (!new->fb)
5243dae08bcSDmitry Osipenko 			state->blending[index].top = (index == 1);
525ebae8d07SThierry Reding 	}
526ebae8d07SThierry Reding }
527ebae8d07SThierry Reding 
5283dae08bcSDmitry Osipenko static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
5293dae08bcSDmitry Osipenko 					  struct tegra_plane_state *state)
5303dae08bcSDmitry Osipenko {
5313dae08bcSDmitry Osipenko 	struct tegra_plane_state *tegra_state;
5323dae08bcSDmitry Osipenko 	struct drm_plane_state *new;
5333dae08bcSDmitry Osipenko 	struct drm_plane *plane;
5343dae08bcSDmitry Osipenko 	int err;
535ebae8d07SThierry Reding 
536ebae8d07SThierry Reding 	/*
5373dae08bcSDmitry Osipenko 	 * If planes zpos / transparency changed, sibling planes blending
5383dae08bcSDmitry Osipenko 	 * state may require adjustment and in this case they will be included
5393dae08bcSDmitry Osipenko 	 * into this atom commit, otherwise blending state is unchanged.
540ebae8d07SThierry Reding 	 */
5413dae08bcSDmitry Osipenko 	err = tegra_plane_check_transparency(tegra, state);
5423dae08bcSDmitry Osipenko 	if (err <= 0)
5433dae08bcSDmitry Osipenko 		return err;
5443dae08bcSDmitry Osipenko 
5453dae08bcSDmitry Osipenko 	/*
5463dae08bcSDmitry Osipenko 	 * All planes are now in the atomic state, walk them up and update
5473dae08bcSDmitry Osipenko 	 * transparency state for each plane.
5483dae08bcSDmitry Osipenko 	 */
5493dae08bcSDmitry Osipenko 	drm_for_each_plane(plane, tegra->base.dev) {
5503dae08bcSDmitry Osipenko 		struct tegra_plane *p = to_tegra_plane(plane);
5513dae08bcSDmitry Osipenko 
5523dae08bcSDmitry Osipenko 		/* skip planes on different CRTCs */
5533dae08bcSDmitry Osipenko 		if (p->dc != tegra->dc)
5543dae08bcSDmitry Osipenko 			continue;
5553dae08bcSDmitry Osipenko 
5563dae08bcSDmitry Osipenko 		new = drm_atomic_get_new_plane_state(state->base.state, plane);
5573dae08bcSDmitry Osipenko 		tegra_state = to_tegra_plane_state(new);
5583dae08bcSDmitry Osipenko 
5593dae08bcSDmitry Osipenko 		/*
5603dae08bcSDmitry Osipenko 		 * There is no need to update blending state for the disabled
5613dae08bcSDmitry Osipenko 		 * plane.
5623dae08bcSDmitry Osipenko 		 */
5633dae08bcSDmitry Osipenko 		if (new->fb)
5643dae08bcSDmitry Osipenko 			tegra_plane_update_transparency(p, tegra_state);
565ebae8d07SThierry Reding 	}
5663dae08bcSDmitry Osipenko 
5673dae08bcSDmitry Osipenko 	return 0;
5683dae08bcSDmitry Osipenko }
5693dae08bcSDmitry Osipenko 
5703dae08bcSDmitry Osipenko int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
5713dae08bcSDmitry Osipenko 				   struct tegra_plane_state *state)
5723dae08bcSDmitry Osipenko {
5733dae08bcSDmitry Osipenko 	int err;
5743dae08bcSDmitry Osipenko 
5753dae08bcSDmitry Osipenko 	err = tegra_plane_setup_opacity(tegra, state);
5763dae08bcSDmitry Osipenko 	if (err < 0)
5773dae08bcSDmitry Osipenko 		return err;
5783dae08bcSDmitry Osipenko 
5793dae08bcSDmitry Osipenko 	err = tegra_plane_setup_transparency(tegra, state);
5803dae08bcSDmitry Osipenko 	if (err < 0)
5813dae08bcSDmitry Osipenko 		return err;
5823dae08bcSDmitry Osipenko 
5833dae08bcSDmitry Osipenko 	return 0;
584ebae8d07SThierry Reding }
585