blob: fcf7b8e3032d3dbb80af0012292c72310eff8f70 [file] [log] [blame]
Thierry Reding5acd3512017-11-10 15:27:25 +01001/*
2 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
3 *
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.
7 */
8
9#include <drm/drm_atomic.h>
10#include <drm/drm_atomic_helper.h>
11#include <drm/drm_plane_helper.h>
12
13#include "dc.h"
14#include "plane.h"
15
16static void tegra_plane_destroy(struct drm_plane *plane)
17{
18 struct tegra_plane *p = to_tegra_plane(plane);
19
20 drm_plane_cleanup(plane);
21 kfree(p);
22}
23
24static void tegra_plane_reset(struct drm_plane *plane)
25{
26 struct tegra_plane_state *state;
27
28 if (plane->state)
29 __drm_atomic_helper_plane_destroy_state(plane->state);
30
31 kfree(plane->state);
32 plane->state = NULL;
33
34 state = kzalloc(sizeof(*state), GFP_KERNEL);
35 if (state) {
36 plane->state = &state->base;
37 plane->state->plane = plane;
38 }
39}
40
41static struct drm_plane_state *
42tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
43{
44 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
45 struct tegra_plane_state *copy;
Thierry Redingebae8d02017-12-20 09:39:14 +010046 unsigned int i;
Thierry Reding5acd3512017-11-10 15:27:25 +010047
48 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
49 if (!copy)
50 return NULL;
51
52 __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
53 copy->tiling = state->tiling;
54 copy->format = state->format;
55 copy->swap = state->swap;
Thierry Redingebae8d02017-12-20 09:39:14 +010056 copy->opaque = state->opaque;
57
58 for (i = 0; i < 3; i++)
59 copy->dependent[i] = state->dependent[i];
Thierry Reding5acd3512017-11-10 15:27:25 +010060
61 return &copy->base;
62}
63
64static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
65 struct drm_plane_state *state)
66{
67 __drm_atomic_helper_plane_destroy_state(state);
68 kfree(state);
69}
70
Thierry Redinge90124c2018-03-15 16:44:04 +010071static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
72 uint32_t format,
73 uint64_t modifier)
74{
75 const struct drm_format_info *info = drm_format_info(format);
76
77 if (modifier == DRM_FORMAT_MOD_LINEAR)
78 return true;
79
80 if (info->num_planes == 1)
81 return true;
82
83 return false;
84}
85
Thierry Reding5acd3512017-11-10 15:27:25 +010086const struct drm_plane_funcs tegra_plane_funcs = {
87 .update_plane = drm_atomic_helper_update_plane,
88 .disable_plane = drm_atomic_helper_disable_plane,
89 .destroy = tegra_plane_destroy,
90 .reset = tegra_plane_reset,
91 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
92 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
Thierry Redinge90124c2018-03-15 16:44:04 +010093 .format_mod_supported = tegra_plane_format_mod_supported,
Thierry Reding5acd3512017-11-10 15:27:25 +010094};
95
96int tegra_plane_state_add(struct tegra_plane *plane,
97 struct drm_plane_state *state)
98{
99 struct drm_crtc_state *crtc_state;
100 struct tegra_dc_state *tegra;
101 struct drm_rect clip;
102 int err;
103
104 /* Propagate errors from allocation or locking failures. */
105 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
106 if (IS_ERR(crtc_state))
107 return PTR_ERR(crtc_state);
108
109 clip.x1 = 0;
110 clip.y1 = 0;
111 clip.x2 = crtc_state->mode.hdisplay;
112 clip.y2 = crtc_state->mode.vdisplay;
113
114 /* Check plane state for visibility and calculate clipping bounds */
115 err = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
116 0, INT_MAX, true, true);
117 if (err < 0)
118 return err;
119
120 tegra = to_dc_state(crtc_state);
121
122 tegra->planes |= WIN_A_ACT_REQ << plane->index;
123
124 return 0;
125}
126
127int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
128{
129 /* assume no swapping of fetched data */
130 if (swap)
131 *swap = BYTE_SWAP_NOSWAP;
132
133 switch (fourcc) {
Thierry Reding511c7022017-11-14 16:07:40 +0100134 case DRM_FORMAT_ARGB4444:
135 *format = WIN_COLOR_DEPTH_B4G4R4A4;
Thierry Reding7772fda2017-10-12 17:30:55 +0200136 break;
137
Thierry Reding511c7022017-11-14 16:07:40 +0100138 case DRM_FORMAT_ARGB1555:
139 *format = WIN_COLOR_DEPTH_B5G5R5A1;
Thierry Reding5acd3512017-11-10 15:27:25 +0100140 break;
141
Thierry Reding511c7022017-11-14 16:07:40 +0100142 case DRM_FORMAT_RGB565:
143 *format = WIN_COLOR_DEPTH_B5G6R5;
144 break;
145
146 case DRM_FORMAT_RGBA5551:
147 *format = WIN_COLOR_DEPTH_A1B5G5R5;
Thierry Reding7772fda2017-10-12 17:30:55 +0200148 break;
149
150 case DRM_FORMAT_ARGB8888:
Thierry Reding5acd3512017-11-10 15:27:25 +0100151 *format = WIN_COLOR_DEPTH_B8G8R8A8;
152 break;
153
Thierry Reding511c7022017-11-14 16:07:40 +0100154 case DRM_FORMAT_ABGR8888:
155 *format = WIN_COLOR_DEPTH_R8G8B8A8;
156 break;
157
158 case DRM_FORMAT_ABGR4444:
159 *format = WIN_COLOR_DEPTH_R4G4B4A4;
160 break;
161
162 case DRM_FORMAT_ABGR1555:
163 *format = WIN_COLOR_DEPTH_R5G5B5A;
164 break;
165
166 case DRM_FORMAT_BGRA5551:
167 *format = WIN_COLOR_DEPTH_AR5G5B5;
168 break;
169
170 case DRM_FORMAT_XRGB1555:
171 *format = WIN_COLOR_DEPTH_B5G5R5X1;
172 break;
173
174 case DRM_FORMAT_RGBX5551:
175 *format = WIN_COLOR_DEPTH_X1B5G5R5;
176 break;
177
178 case DRM_FORMAT_XBGR1555:
179 *format = WIN_COLOR_DEPTH_R5G5B5X1;
180 break;
181
182 case DRM_FORMAT_BGRX5551:
183 *format = WIN_COLOR_DEPTH_X1R5G5B5;
184 break;
185
186 case DRM_FORMAT_BGR565:
187 *format = WIN_COLOR_DEPTH_R5G6B5;
188 break;
189
190 case DRM_FORMAT_BGRA8888:
191 *format = WIN_COLOR_DEPTH_A8R8G8B8;
192 break;
193
194 case DRM_FORMAT_RGBA8888:
195 *format = WIN_COLOR_DEPTH_A8B8G8R8;
196 break;
197
198 case DRM_FORMAT_XRGB8888:
199 *format = WIN_COLOR_DEPTH_B8G8R8X8;
200 break;
201
202 case DRM_FORMAT_XBGR8888:
203 *format = WIN_COLOR_DEPTH_R8G8B8X8;
Thierry Reding5acd3512017-11-10 15:27:25 +0100204 break;
205
206 case DRM_FORMAT_UYVY:
207 *format = WIN_COLOR_DEPTH_YCbCr422;
208 break;
209
210 case DRM_FORMAT_YUYV:
211 if (!swap)
212 return -EINVAL;
213
214 *format = WIN_COLOR_DEPTH_YCbCr422;
215 *swap = BYTE_SWAP_SWAP2;
216 break;
217
218 case DRM_FORMAT_YUV420:
219 *format = WIN_COLOR_DEPTH_YCbCr420P;
220 break;
221
222 case DRM_FORMAT_YUV422:
223 *format = WIN_COLOR_DEPTH_YCbCr422P;
224 break;
225
226 default:
227 return -EINVAL;
228 }
229
230 return 0;
231}
232
233bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
234{
235 switch (format) {
236 case WIN_COLOR_DEPTH_YCbCr422:
237 case WIN_COLOR_DEPTH_YUV422:
238 if (planar)
239 *planar = false;
240
241 return true;
242
243 case WIN_COLOR_DEPTH_YCbCr420P:
244 case WIN_COLOR_DEPTH_YUV420P:
245 case WIN_COLOR_DEPTH_YCbCr422P:
246 case WIN_COLOR_DEPTH_YUV422P:
247 case WIN_COLOR_DEPTH_YCbCr422R:
248 case WIN_COLOR_DEPTH_YUV422R:
249 case WIN_COLOR_DEPTH_YCbCr422RA:
250 case WIN_COLOR_DEPTH_YUV422RA:
251 if (planar)
252 *planar = true;
253
254 return true;
255 }
256
257 if (planar)
258 *planar = false;
259
260 return false;
261}
Thierry Redingebae8d02017-12-20 09:39:14 +0100262
263static bool __drm_format_has_alpha(u32 format)
264{
265 switch (format) {
266 case DRM_FORMAT_ARGB1555:
267 case DRM_FORMAT_RGBA5551:
268 case DRM_FORMAT_ABGR8888:
269 case DRM_FORMAT_ARGB8888:
270 return true;
271 }
272
273 return false;
274}
275
276/*
277 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
278 * be emulated using the alpha formats and alpha blending disabled.
279 */
280bool tegra_plane_format_has_alpha(unsigned int format)
281{
282 switch (format) {
283 case WIN_COLOR_DEPTH_B5G5R5A1:
284 case WIN_COLOR_DEPTH_A1B5G5R5:
285 case WIN_COLOR_DEPTH_R8G8B8A8:
286 case WIN_COLOR_DEPTH_B8G8R8A8:
287 return true;
288 }
289
290 return false;
291}
292
293int tegra_plane_format_get_alpha(unsigned int opaque, unsigned int *alpha)
294{
Thierry Reding5467a8b2018-01-08 13:40:44 +0100295 if (tegra_plane_format_is_yuv(opaque, NULL)) {
296 *alpha = opaque;
297 return 0;
298 }
299
Thierry Redingebae8d02017-12-20 09:39:14 +0100300 switch (opaque) {
301 case WIN_COLOR_DEPTH_B5G5R5X1:
302 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
303 return 0;
304
305 case WIN_COLOR_DEPTH_X1B5G5R5:
306 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
307 return 0;
308
309 case WIN_COLOR_DEPTH_R8G8B8X8:
310 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
311 return 0;
312
313 case WIN_COLOR_DEPTH_B8G8R8X8:
314 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
315 return 0;
316 }
317
318 return -EINVAL;
319}
320
Dmitry Osipenko5e2e86f2018-03-15 11:37:05 +0100321static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
322 struct tegra_plane *other)
Thierry Redingebae8d02017-12-20 09:39:14 +0100323{
324 unsigned int index = 0, i;
325
326 WARN_ON(plane == other);
327
328 for (i = 0; i < 3; i++) {
329 if (i == plane->index)
330 continue;
331
332 if (i == other->index)
333 break;
334
335 index++;
336 }
337
338 return index;
339}
340
341void tegra_plane_check_dependent(struct tegra_plane *tegra,
342 struct tegra_plane_state *state)
343{
344 struct drm_plane_state *old, *new;
345 struct drm_plane *plane;
346 unsigned int zpos[2];
347 unsigned int i;
348
349 for (i = 0; i < 3; i++)
350 state->dependent[i] = false;
351
352 for (i = 0; i < 2; i++)
353 zpos[i] = 0;
354
355 for_each_oldnew_plane_in_state(state->base.state, plane, old, new, i) {
356 struct tegra_plane *p = to_tegra_plane(plane);
357 unsigned index;
358
359 /* skip this plane and planes on different CRTCs */
360 if (p == tegra || new->crtc != state->base.crtc)
361 continue;
362
363 index = tegra_plane_get_overlap_index(tegra, p);
364
365 /*
366 * If any of the other planes is on top of this plane and uses
367 * a format with an alpha component, mark this plane as being
368 * dependent, meaning it's alpha value will be 1 minus the sum
369 * of alpha components of the overlapping planes.
370 */
371 if (p->index > tegra->index) {
372 if (__drm_format_has_alpha(new->fb->format->format))
373 state->dependent[index] = true;
374
375 /* keep track of the Z position */
376 zpos[index] = p->index;
377 }
378 }
379
380 /*
381 * The region where three windows overlap is the intersection of the
382 * two regions where two windows overlap. It contributes to the area
383 * if any of the windows on top of it have an alpha component.
384 */
385 for (i = 0; i < 2; i++)
386 state->dependent[2] = state->dependent[2] ||
387 state->dependent[i];
388
389 /*
390 * However, if any of the windows on top of this window is opaque, it
391 * will completely conceal this window within that area, so avoid the
392 * window from contributing to the area.
393 */
394 for (i = 0; i < 2; i++) {
395 if (zpos[i] > tegra->index)
396 state->dependent[2] = state->dependent[2] &&
397 state->dependent[i];
398 }
399}