2 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <drm/drm_atomic.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_plane_helper.h>
16 static void tegra_plane_destroy(struct drm_plane *plane)
18 struct tegra_plane *p = to_tegra_plane(plane);
20 drm_plane_cleanup(plane);
24 static void tegra_plane_reset(struct drm_plane *plane)
26 struct tegra_plane *p = to_tegra_plane(plane);
27 struct tegra_plane_state *state;
30 __drm_atomic_helper_plane_destroy_state(plane->state);
35 state = kzalloc(sizeof(*state), GFP_KERNEL);
37 plane->state = &state->base;
38 plane->state->plane = plane;
39 plane->state->zpos = p->index;
40 plane->state->normalized_zpos = p->index;
44 static struct drm_plane_state *
45 tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
47 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
48 struct tegra_plane_state *copy;
51 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
55 __drm_atomic_helper_plane_duplicate_state(plane, ©->base);
56 copy->tiling = state->tiling;
57 copy->format = state->format;
58 copy->swap = state->swap;
59 copy->bottom_up = state->bottom_up;
60 copy->opaque = state->opaque;
62 for (i = 0; i < 2; i++)
63 copy->blending[i] = state->blending[i];
68 static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
69 struct drm_plane_state *state)
71 __drm_atomic_helper_plane_destroy_state(state);
75 static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
79 const struct drm_format_info *info = drm_format_info(format);
81 if (modifier == DRM_FORMAT_MOD_LINEAR)
84 if (info->num_planes == 1)
90 const struct drm_plane_funcs tegra_plane_funcs = {
91 .update_plane = drm_atomic_helper_update_plane,
92 .disable_plane = drm_atomic_helper_disable_plane,
93 .destroy = tegra_plane_destroy,
94 .reset = tegra_plane_reset,
95 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
96 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
97 .format_mod_supported = tegra_plane_format_mod_supported,
100 int tegra_plane_state_add(struct tegra_plane *plane,
101 struct drm_plane_state *state)
103 struct drm_crtc_state *crtc_state;
104 struct tegra_dc_state *tegra;
107 /* Propagate errors from allocation or locking failures. */
108 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
109 if (IS_ERR(crtc_state))
110 return PTR_ERR(crtc_state);
112 /* Check plane state for visibility and calculate clipping bounds */
113 err = drm_atomic_helper_check_plane_state(state, crtc_state,
114 0, INT_MAX, true, true);
118 tegra = to_dc_state(crtc_state);
120 tegra->planes |= WIN_A_ACT_REQ << plane->index;
125 int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
127 /* assume no swapping of fetched data */
129 *swap = BYTE_SWAP_NOSWAP;
132 case DRM_FORMAT_ARGB4444:
133 *format = WIN_COLOR_DEPTH_B4G4R4A4;
136 case DRM_FORMAT_ARGB1555:
137 *format = WIN_COLOR_DEPTH_B5G5R5A1;
140 case DRM_FORMAT_RGB565:
141 *format = WIN_COLOR_DEPTH_B5G6R5;
144 case DRM_FORMAT_RGBA5551:
145 *format = WIN_COLOR_DEPTH_A1B5G5R5;
148 case DRM_FORMAT_ARGB8888:
149 *format = WIN_COLOR_DEPTH_B8G8R8A8;
152 case DRM_FORMAT_ABGR8888:
153 *format = WIN_COLOR_DEPTH_R8G8B8A8;
156 case DRM_FORMAT_ABGR4444:
157 *format = WIN_COLOR_DEPTH_R4G4B4A4;
160 case DRM_FORMAT_ABGR1555:
161 *format = WIN_COLOR_DEPTH_R5G5B5A;
164 case DRM_FORMAT_BGRA5551:
165 *format = WIN_COLOR_DEPTH_AR5G5B5;
168 case DRM_FORMAT_XRGB1555:
169 *format = WIN_COLOR_DEPTH_B5G5R5X1;
172 case DRM_FORMAT_RGBX5551:
173 *format = WIN_COLOR_DEPTH_X1B5G5R5;
176 case DRM_FORMAT_XBGR1555:
177 *format = WIN_COLOR_DEPTH_R5G5B5X1;
180 case DRM_FORMAT_BGRX5551:
181 *format = WIN_COLOR_DEPTH_X1R5G5B5;
184 case DRM_FORMAT_BGR565:
185 *format = WIN_COLOR_DEPTH_R5G6B5;
188 case DRM_FORMAT_BGRA8888:
189 *format = WIN_COLOR_DEPTH_A8R8G8B8;
192 case DRM_FORMAT_RGBA8888:
193 *format = WIN_COLOR_DEPTH_A8B8G8R8;
196 case DRM_FORMAT_XRGB8888:
197 *format = WIN_COLOR_DEPTH_B8G8R8X8;
200 case DRM_FORMAT_XBGR8888:
201 *format = WIN_COLOR_DEPTH_R8G8B8X8;
204 case DRM_FORMAT_UYVY:
205 *format = WIN_COLOR_DEPTH_YCbCr422;
208 case DRM_FORMAT_YUYV:
212 *format = WIN_COLOR_DEPTH_YCbCr422;
213 *swap = BYTE_SWAP_SWAP2;
216 case DRM_FORMAT_YUV420:
217 *format = WIN_COLOR_DEPTH_YCbCr420P;
220 case DRM_FORMAT_YUV422:
221 *format = WIN_COLOR_DEPTH_YCbCr422P;
231 bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
234 case WIN_COLOR_DEPTH_YCbCr422:
235 case WIN_COLOR_DEPTH_YUV422:
241 case WIN_COLOR_DEPTH_YCbCr420P:
242 case WIN_COLOR_DEPTH_YUV420P:
243 case WIN_COLOR_DEPTH_YCbCr422P:
244 case WIN_COLOR_DEPTH_YUV422P:
245 case WIN_COLOR_DEPTH_YCbCr422R:
246 case WIN_COLOR_DEPTH_YUV422R:
247 case WIN_COLOR_DEPTH_YCbCr422RA:
248 case WIN_COLOR_DEPTH_YUV422RA:
261 static bool __drm_format_has_alpha(u32 format)
264 case DRM_FORMAT_ARGB1555:
265 case DRM_FORMAT_RGBA5551:
266 case DRM_FORMAT_ABGR8888:
267 case DRM_FORMAT_ARGB8888:
274 static int tegra_plane_format_get_alpha(unsigned int opaque,
277 if (tegra_plane_format_is_yuv(opaque, NULL)) {
283 case WIN_COLOR_DEPTH_B5G5R5X1:
284 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
287 case WIN_COLOR_DEPTH_X1B5G5R5:
288 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
291 case WIN_COLOR_DEPTH_R8G8B8X8:
292 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
295 case WIN_COLOR_DEPTH_B8G8R8X8:
296 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
299 case WIN_COLOR_DEPTH_B5G6R5:
308 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
309 * be emulated using the alpha formats and alpha blending disabled.
311 static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
312 struct tegra_plane_state *state)
317 switch (state->format) {
318 case WIN_COLOR_DEPTH_B5G5R5A1:
319 case WIN_COLOR_DEPTH_A1B5G5R5:
320 case WIN_COLOR_DEPTH_R8G8B8A8:
321 case WIN_COLOR_DEPTH_B8G8R8A8:
322 state->opaque = false;
326 err = tegra_plane_format_get_alpha(state->format, &format);
330 state->format = format;
331 state->opaque = true;
338 static int tegra_plane_check_transparency(struct tegra_plane *tegra,
339 struct tegra_plane_state *state)
341 struct drm_plane_state *old, *plane_state;
342 struct drm_plane *plane;
344 old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
346 /* check if zpos / transparency changed */
347 if (old->normalized_zpos == state->base.normalized_zpos &&
348 to_tegra_plane_state(old)->opaque == state->opaque)
351 /* include all sibling planes into this commit */
352 drm_for_each_plane(plane, tegra->base.dev) {
353 struct tegra_plane *p = to_tegra_plane(plane);
355 /* skip this plane and planes on different CRTCs */
356 if (p == tegra || p->dc != tegra->dc)
359 plane_state = drm_atomic_get_plane_state(state->base.state,
361 if (IS_ERR(plane_state))
362 return PTR_ERR(plane_state);
368 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
369 struct tegra_plane *other)
371 unsigned int index = 0, i;
373 WARN_ON(plane == other);
375 for (i = 0; i < 3; i++) {
376 if (i == plane->index)
379 if (i == other->index)
388 static void tegra_plane_update_transparency(struct tegra_plane *tegra,
389 struct tegra_plane_state *state)
391 struct drm_plane_state *new;
392 struct drm_plane *plane;
395 for_each_new_plane_in_state(state->base.state, plane, new, i) {
396 struct tegra_plane *p = to_tegra_plane(plane);
399 /* skip this plane and planes on different CRTCs */
400 if (p == tegra || p->dc != tegra->dc)
403 index = tegra_plane_get_overlap_index(tegra, p);
405 if (new->fb && __drm_format_has_alpha(new->fb->format->format))
406 state->blending[index].alpha = true;
408 state->blending[index].alpha = false;
410 if (new->normalized_zpos > state->base.normalized_zpos)
411 state->blending[index].top = true;
413 state->blending[index].top = false;
416 * Missing framebuffer means that plane is disabled, in this
417 * case mark B / C window as top to be able to differentiate
418 * windows indices order in regards to zPos for the middle
419 * window X / Y registers programming.
422 state->blending[index].top = (index == 1);
426 static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
427 struct tegra_plane_state *state)
429 struct tegra_plane_state *tegra_state;
430 struct drm_plane_state *new;
431 struct drm_plane *plane;
435 * If planes zpos / transparency changed, sibling planes blending
436 * state may require adjustment and in this case they will be included
437 * into this atom commit, otherwise blending state is unchanged.
439 err = tegra_plane_check_transparency(tegra, state);
444 * All planes are now in the atomic state, walk them up and update
445 * transparency state for each plane.
447 drm_for_each_plane(plane, tegra->base.dev) {
448 struct tegra_plane *p = to_tegra_plane(plane);
450 /* skip planes on different CRTCs */
451 if (p->dc != tegra->dc)
454 new = drm_atomic_get_new_plane_state(state->base.state, plane);
455 tegra_state = to_tegra_plane_state(new);
458 * There is no need to update blending state for the disabled
462 tegra_plane_update_transparency(p, tegra_state);
468 int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
469 struct tegra_plane_state *state)
473 err = tegra_plane_setup_opacity(tegra, state);
477 err = tegra_plane_setup_transparency(tegra, state);