1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
6 #include <drm/drm_atomic.h>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_fourcc.h>
9 #include <drm/drm_plane_helper.h>
14 static void tegra_plane_destroy(struct drm_plane *plane)
16 struct tegra_plane *p = to_tegra_plane(plane);
18 drm_plane_cleanup(plane);
22 static void tegra_plane_reset(struct drm_plane *plane)
24 struct tegra_plane *p = to_tegra_plane(plane);
25 struct tegra_plane_state *state;
28 __drm_atomic_helper_plane_destroy_state(plane->state);
33 state = kzalloc(sizeof(*state), GFP_KERNEL);
35 plane->state = &state->base;
36 plane->state->plane = plane;
37 plane->state->zpos = p->index;
38 plane->state->normalized_zpos = p->index;
42 static struct drm_plane_state *
43 tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
45 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
46 struct tegra_plane_state *copy;
49 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
53 __drm_atomic_helper_plane_duplicate_state(plane, ©->base);
54 copy->tiling = state->tiling;
55 copy->format = state->format;
56 copy->swap = state->swap;
57 copy->bottom_up = state->bottom_up;
58 copy->opaque = state->opaque;
60 for (i = 0; i < 2; i++)
61 copy->blending[i] = state->blending[i];
66 static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
67 struct drm_plane_state *state)
69 __drm_atomic_helper_plane_destroy_state(state);
73 static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
77 const struct drm_format_info *info = drm_format_info(format);
79 if (modifier == DRM_FORMAT_MOD_LINEAR)
82 if (info->num_planes == 1)
88 const struct drm_plane_funcs tegra_plane_funcs = {
89 .update_plane = drm_atomic_helper_update_plane,
90 .disable_plane = drm_atomic_helper_disable_plane,
91 .destroy = tegra_plane_destroy,
92 .reset = tegra_plane_reset,
93 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
94 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
95 .format_mod_supported = tegra_plane_format_mod_supported,
98 int tegra_plane_state_add(struct tegra_plane *plane,
99 struct drm_plane_state *state)
101 struct drm_crtc_state *crtc_state;
102 struct tegra_dc_state *tegra;
105 /* Propagate errors from allocation or locking failures. */
106 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
107 if (IS_ERR(crtc_state))
108 return PTR_ERR(crtc_state);
110 /* Check plane state for visibility and calculate clipping bounds */
111 err = drm_atomic_helper_check_plane_state(state, crtc_state,
112 0, INT_MAX, true, true);
116 tegra = to_dc_state(crtc_state);
118 tegra->planes |= WIN_A_ACT_REQ << plane->index;
123 int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
125 /* assume no swapping of fetched data */
127 *swap = BYTE_SWAP_NOSWAP;
130 case DRM_FORMAT_ARGB4444:
131 *format = WIN_COLOR_DEPTH_B4G4R4A4;
134 case DRM_FORMAT_ARGB1555:
135 *format = WIN_COLOR_DEPTH_B5G5R5A1;
138 case DRM_FORMAT_RGB565:
139 *format = WIN_COLOR_DEPTH_B5G6R5;
142 case DRM_FORMAT_RGBA5551:
143 *format = WIN_COLOR_DEPTH_A1B5G5R5;
146 case DRM_FORMAT_ARGB8888:
147 *format = WIN_COLOR_DEPTH_B8G8R8A8;
150 case DRM_FORMAT_ABGR8888:
151 *format = WIN_COLOR_DEPTH_R8G8B8A8;
154 case DRM_FORMAT_ABGR4444:
155 *format = WIN_COLOR_DEPTH_R4G4B4A4;
158 case DRM_FORMAT_ABGR1555:
159 *format = WIN_COLOR_DEPTH_R5G5B5A;
162 case DRM_FORMAT_BGRA5551:
163 *format = WIN_COLOR_DEPTH_AR5G5B5;
166 case DRM_FORMAT_XRGB1555:
167 *format = WIN_COLOR_DEPTH_B5G5R5X1;
170 case DRM_FORMAT_RGBX5551:
171 *format = WIN_COLOR_DEPTH_X1B5G5R5;
174 case DRM_FORMAT_XBGR1555:
175 *format = WIN_COLOR_DEPTH_R5G5B5X1;
178 case DRM_FORMAT_BGRX5551:
179 *format = WIN_COLOR_DEPTH_X1R5G5B5;
182 case DRM_FORMAT_BGR565:
183 *format = WIN_COLOR_DEPTH_R5G6B5;
186 case DRM_FORMAT_BGRA8888:
187 *format = WIN_COLOR_DEPTH_A8R8G8B8;
190 case DRM_FORMAT_RGBA8888:
191 *format = WIN_COLOR_DEPTH_A8B8G8R8;
194 case DRM_FORMAT_XRGB8888:
195 *format = WIN_COLOR_DEPTH_B8G8R8X8;
198 case DRM_FORMAT_XBGR8888:
199 *format = WIN_COLOR_DEPTH_R8G8B8X8;
202 case DRM_FORMAT_UYVY:
203 *format = WIN_COLOR_DEPTH_YCbCr422;
206 case DRM_FORMAT_YUYV:
210 *format = WIN_COLOR_DEPTH_YCbCr422;
211 *swap = BYTE_SWAP_SWAP2;
214 case DRM_FORMAT_YUV420:
215 *format = WIN_COLOR_DEPTH_YCbCr420P;
218 case DRM_FORMAT_YUV422:
219 *format = WIN_COLOR_DEPTH_YCbCr422P;
229 bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
232 case WIN_COLOR_DEPTH_YCbCr422:
233 case WIN_COLOR_DEPTH_YUV422:
239 case WIN_COLOR_DEPTH_YCbCr420P:
240 case WIN_COLOR_DEPTH_YUV420P:
241 case WIN_COLOR_DEPTH_YCbCr422P:
242 case WIN_COLOR_DEPTH_YUV422P:
243 case WIN_COLOR_DEPTH_YCbCr422R:
244 case WIN_COLOR_DEPTH_YUV422R:
245 case WIN_COLOR_DEPTH_YCbCr422RA:
246 case WIN_COLOR_DEPTH_YUV422RA:
259 static bool __drm_format_has_alpha(u32 format)
262 case DRM_FORMAT_ARGB1555:
263 case DRM_FORMAT_RGBA5551:
264 case DRM_FORMAT_ABGR8888:
265 case DRM_FORMAT_ARGB8888:
272 static int tegra_plane_format_get_alpha(unsigned int opaque,
275 if (tegra_plane_format_is_yuv(opaque, NULL)) {
281 case WIN_COLOR_DEPTH_B5G5R5X1:
282 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
285 case WIN_COLOR_DEPTH_X1B5G5R5:
286 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
289 case WIN_COLOR_DEPTH_R8G8B8X8:
290 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
293 case WIN_COLOR_DEPTH_B8G8R8X8:
294 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
297 case WIN_COLOR_DEPTH_B5G6R5:
306 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
307 * be emulated using the alpha formats and alpha blending disabled.
309 static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
310 struct tegra_plane_state *state)
315 switch (state->format) {
316 case WIN_COLOR_DEPTH_B5G5R5A1:
317 case WIN_COLOR_DEPTH_A1B5G5R5:
318 case WIN_COLOR_DEPTH_R8G8B8A8:
319 case WIN_COLOR_DEPTH_B8G8R8A8:
320 state->opaque = false;
324 err = tegra_plane_format_get_alpha(state->format, &format);
328 state->format = format;
329 state->opaque = true;
336 static int tegra_plane_check_transparency(struct tegra_plane *tegra,
337 struct tegra_plane_state *state)
339 struct drm_plane_state *old, *plane_state;
340 struct drm_plane *plane;
342 old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
344 /* check if zpos / transparency changed */
345 if (old->normalized_zpos == state->base.normalized_zpos &&
346 to_tegra_plane_state(old)->opaque == state->opaque)
349 /* include all sibling planes into this commit */
350 drm_for_each_plane(plane, tegra->base.dev) {
351 struct tegra_plane *p = to_tegra_plane(plane);
353 /* skip this plane and planes on different CRTCs */
354 if (p == tegra || p->dc != tegra->dc)
357 plane_state = drm_atomic_get_plane_state(state->base.state,
359 if (IS_ERR(plane_state))
360 return PTR_ERR(plane_state);
366 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
367 struct tegra_plane *other)
369 unsigned int index = 0, i;
371 WARN_ON(plane == other);
373 for (i = 0; i < 3; i++) {
374 if (i == plane->index)
377 if (i == other->index)
386 static void tegra_plane_update_transparency(struct tegra_plane *tegra,
387 struct tegra_plane_state *state)
389 struct drm_plane_state *new;
390 struct drm_plane *plane;
393 for_each_new_plane_in_state(state->base.state, plane, new, i) {
394 struct tegra_plane *p = to_tegra_plane(plane);
397 /* skip this plane and planes on different CRTCs */
398 if (p == tegra || p->dc != tegra->dc)
401 index = tegra_plane_get_overlap_index(tegra, p);
403 if (new->fb && __drm_format_has_alpha(new->fb->format->format))
404 state->blending[index].alpha = true;
406 state->blending[index].alpha = false;
408 if (new->normalized_zpos > state->base.normalized_zpos)
409 state->blending[index].top = true;
411 state->blending[index].top = false;
414 * Missing framebuffer means that plane is disabled, in this
415 * case mark B / C window as top to be able to differentiate
416 * windows indices order in regards to zPos for the middle
417 * window X / Y registers programming.
420 state->blending[index].top = (index == 1);
424 static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
425 struct tegra_plane_state *state)
427 struct tegra_plane_state *tegra_state;
428 struct drm_plane_state *new;
429 struct drm_plane *plane;
433 * If planes zpos / transparency changed, sibling planes blending
434 * state may require adjustment and in this case they will be included
435 * into this atom commit, otherwise blending state is unchanged.
437 err = tegra_plane_check_transparency(tegra, state);
442 * All planes are now in the atomic state, walk them up and update
443 * transparency state for each plane.
445 drm_for_each_plane(plane, tegra->base.dev) {
446 struct tegra_plane *p = to_tegra_plane(plane);
448 /* skip planes on different CRTCs */
449 if (p->dc != tegra->dc)
452 new = drm_atomic_get_new_plane_state(state->base.state, plane);
453 tegra_state = to_tegra_plane_state(new);
456 * There is no need to update blending state for the disabled
460 tegra_plane_update_transparency(p, tegra_state);
466 int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
467 struct tegra_plane_state *state)
471 err = tegra_plane_setup_opacity(tegra, state);
475 err = tegra_plane_setup_transparency(tegra, state);