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_gem_framebuffer_helper.h>
10 #include <drm/drm_plane_helper.h>
15 static void tegra_plane_destroy(struct drm_plane
*plane
)
17 struct tegra_plane
*p
= to_tegra_plane(plane
);
19 drm_plane_cleanup(plane
);
23 static void tegra_plane_reset(struct drm_plane
*plane
)
25 struct tegra_plane
*p
= to_tegra_plane(plane
);
26 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
;
42 for (i
= 0; i
< 3; i
++)
43 state
->iova
[i
] = DMA_MAPPING_ERROR
;
47 static struct drm_plane_state
*
48 tegra_plane_atomic_duplicate_state(struct drm_plane
*plane
)
50 struct tegra_plane_state
*state
= to_tegra_plane_state(plane
->state
);
51 struct tegra_plane_state
*copy
;
54 copy
= kmalloc(sizeof(*copy
), GFP_KERNEL
);
58 __drm_atomic_helper_plane_duplicate_state(plane
, ©
->base
);
59 copy
->tiling
= state
->tiling
;
60 copy
->format
= state
->format
;
61 copy
->swap
= state
->swap
;
62 copy
->bottom_up
= state
->bottom_up
;
63 copy
->opaque
= state
->opaque
;
65 for (i
= 0; i
< 2; i
++)
66 copy
->blending
[i
] = state
->blending
[i
];
68 for (i
= 0; i
< 3; i
++) {
69 copy
->iova
[i
] = DMA_MAPPING_ERROR
;
76 static void tegra_plane_atomic_destroy_state(struct drm_plane
*plane
,
77 struct drm_plane_state
*state
)
79 __drm_atomic_helper_plane_destroy_state(state
);
83 static bool tegra_plane_format_mod_supported(struct drm_plane
*plane
,
87 const struct drm_format_info
*info
= drm_format_info(format
);
89 if (modifier
== DRM_FORMAT_MOD_LINEAR
)
92 if (info
->num_planes
== 1)
98 const struct drm_plane_funcs tegra_plane_funcs
= {
99 .update_plane
= drm_atomic_helper_update_plane
,
100 .disable_plane
= drm_atomic_helper_disable_plane
,
101 .destroy
= tegra_plane_destroy
,
102 .reset
= tegra_plane_reset
,
103 .atomic_duplicate_state
= tegra_plane_atomic_duplicate_state
,
104 .atomic_destroy_state
= tegra_plane_atomic_destroy_state
,
105 .format_mod_supported
= tegra_plane_format_mod_supported
,
108 static int tegra_dc_pin(struct tegra_dc
*dc
, struct tegra_plane_state
*state
)
113 for (i
= 0; i
< state
->base
.fb
->format
->num_planes
; i
++) {
114 struct tegra_bo
*bo
= tegra_fb_get_plane(state
->base
.fb
, i
);
116 if (!dc
->client
.group
) {
117 struct sg_table
*sgt
;
119 sgt
= host1x_bo_pin(dc
->dev
, &bo
->base
, NULL
);
125 err
= dma_map_sg(dc
->dev
, sgt
->sgl
, sgt
->nents
,
133 * The display controller needs contiguous memory, so
134 * fail if the buffer is discontiguous and we fail to
135 * map its SG table to a single contiguous chunk of
136 * I/O virtual memory.
143 state
->iova
[i
] = sg_dma_address(sgt
->sgl
);
146 state
->iova
[i
] = bo
->iova
;
153 dev_err(dc
->dev
, "failed to map plane %u: %d\n", i
, err
);
156 struct tegra_bo
*bo
= tegra_fb_get_plane(state
->base
.fb
, i
);
157 struct sg_table
*sgt
= state
->sgt
[i
];
159 dma_unmap_sg(dc
->dev
, sgt
->sgl
, sgt
->nents
, DMA_TO_DEVICE
);
160 host1x_bo_unpin(dc
->dev
, &bo
->base
, sgt
);
162 state
->iova
[i
] = DMA_MAPPING_ERROR
;
163 state
->sgt
[i
] = NULL
;
169 static void tegra_dc_unpin(struct tegra_dc
*dc
, struct tegra_plane_state
*state
)
173 for (i
= 0; i
< state
->base
.fb
->format
->num_planes
; i
++) {
174 struct tegra_bo
*bo
= tegra_fb_get_plane(state
->base
.fb
, i
);
176 if (!dc
->client
.group
) {
177 struct sg_table
*sgt
= state
->sgt
[i
];
180 dma_unmap_sg(dc
->dev
, sgt
->sgl
, sgt
->nents
,
182 host1x_bo_unpin(dc
->dev
, &bo
->base
, sgt
);
186 state
->iova
[i
] = DMA_MAPPING_ERROR
;
187 state
->sgt
[i
] = NULL
;
191 int tegra_plane_prepare_fb(struct drm_plane
*plane
,
192 struct drm_plane_state
*state
)
194 struct tegra_dc
*dc
= to_tegra_dc(state
->crtc
);
199 drm_gem_fb_prepare_fb(plane
, state
);
201 return tegra_dc_pin(dc
, to_tegra_plane_state(state
));
204 void tegra_plane_cleanup_fb(struct drm_plane
*plane
,
205 struct drm_plane_state
*state
)
207 struct tegra_dc
*dc
= to_tegra_dc(state
->crtc
);
210 tegra_dc_unpin(dc
, to_tegra_plane_state(state
));
213 int tegra_plane_state_add(struct tegra_plane
*plane
,
214 struct drm_plane_state
*state
)
216 struct drm_crtc_state
*crtc_state
;
217 struct tegra_dc_state
*tegra
;
220 /* Propagate errors from allocation or locking failures. */
221 crtc_state
= drm_atomic_get_crtc_state(state
->state
, state
->crtc
);
222 if (IS_ERR(crtc_state
))
223 return PTR_ERR(crtc_state
);
225 /* Check plane state for visibility and calculate clipping bounds */
226 err
= drm_atomic_helper_check_plane_state(state
, crtc_state
,
227 0, INT_MAX
, true, true);
231 tegra
= to_dc_state(crtc_state
);
233 tegra
->planes
|= WIN_A_ACT_REQ
<< plane
->index
;
238 int tegra_plane_format(u32 fourcc
, u32
*format
, u32
*swap
)
240 /* assume no swapping of fetched data */
242 *swap
= BYTE_SWAP_NOSWAP
;
245 case DRM_FORMAT_ARGB4444
:
246 *format
= WIN_COLOR_DEPTH_B4G4R4A4
;
249 case DRM_FORMAT_ARGB1555
:
250 *format
= WIN_COLOR_DEPTH_B5G5R5A1
;
253 case DRM_FORMAT_RGB565
:
254 *format
= WIN_COLOR_DEPTH_B5G6R5
;
257 case DRM_FORMAT_RGBA5551
:
258 *format
= WIN_COLOR_DEPTH_A1B5G5R5
;
261 case DRM_FORMAT_ARGB8888
:
262 *format
= WIN_COLOR_DEPTH_B8G8R8A8
;
265 case DRM_FORMAT_ABGR8888
:
266 *format
= WIN_COLOR_DEPTH_R8G8B8A8
;
269 case DRM_FORMAT_ABGR4444
:
270 *format
= WIN_COLOR_DEPTH_R4G4B4A4
;
273 case DRM_FORMAT_ABGR1555
:
274 *format
= WIN_COLOR_DEPTH_R5G5B5A
;
277 case DRM_FORMAT_BGRA5551
:
278 *format
= WIN_COLOR_DEPTH_AR5G5B5
;
281 case DRM_FORMAT_XRGB1555
:
282 *format
= WIN_COLOR_DEPTH_B5G5R5X1
;
285 case DRM_FORMAT_RGBX5551
:
286 *format
= WIN_COLOR_DEPTH_X1B5G5R5
;
289 case DRM_FORMAT_XBGR1555
:
290 *format
= WIN_COLOR_DEPTH_R5G5B5X1
;
293 case DRM_FORMAT_BGRX5551
:
294 *format
= WIN_COLOR_DEPTH_X1R5G5B5
;
297 case DRM_FORMAT_BGR565
:
298 *format
= WIN_COLOR_DEPTH_R5G6B5
;
301 case DRM_FORMAT_BGRA8888
:
302 *format
= WIN_COLOR_DEPTH_A8R8G8B8
;
305 case DRM_FORMAT_RGBA8888
:
306 *format
= WIN_COLOR_DEPTH_A8B8G8R8
;
309 case DRM_FORMAT_XRGB8888
:
310 *format
= WIN_COLOR_DEPTH_B8G8R8X8
;
313 case DRM_FORMAT_XBGR8888
:
314 *format
= WIN_COLOR_DEPTH_R8G8B8X8
;
317 case DRM_FORMAT_UYVY
:
318 *format
= WIN_COLOR_DEPTH_YCbCr422
;
321 case DRM_FORMAT_YUYV
:
325 *format
= WIN_COLOR_DEPTH_YCbCr422
;
326 *swap
= BYTE_SWAP_SWAP2
;
329 case DRM_FORMAT_YUV420
:
330 *format
= WIN_COLOR_DEPTH_YCbCr420P
;
333 case DRM_FORMAT_YUV422
:
334 *format
= WIN_COLOR_DEPTH_YCbCr422P
;
344 bool tegra_plane_format_is_yuv(unsigned int format
, bool *planar
)
347 case WIN_COLOR_DEPTH_YCbCr422
:
348 case WIN_COLOR_DEPTH_YUV422
:
354 case WIN_COLOR_DEPTH_YCbCr420P
:
355 case WIN_COLOR_DEPTH_YUV420P
:
356 case WIN_COLOR_DEPTH_YCbCr422P
:
357 case WIN_COLOR_DEPTH_YUV422P
:
358 case WIN_COLOR_DEPTH_YCbCr422R
:
359 case WIN_COLOR_DEPTH_YUV422R
:
360 case WIN_COLOR_DEPTH_YCbCr422RA
:
361 case WIN_COLOR_DEPTH_YUV422RA
:
374 static bool __drm_format_has_alpha(u32 format
)
377 case DRM_FORMAT_ARGB1555
:
378 case DRM_FORMAT_RGBA5551
:
379 case DRM_FORMAT_ABGR8888
:
380 case DRM_FORMAT_ARGB8888
:
387 static int tegra_plane_format_get_alpha(unsigned int opaque
,
390 if (tegra_plane_format_is_yuv(opaque
, NULL
)) {
396 case WIN_COLOR_DEPTH_B5G5R5X1
:
397 *alpha
= WIN_COLOR_DEPTH_B5G5R5A1
;
400 case WIN_COLOR_DEPTH_X1B5G5R5
:
401 *alpha
= WIN_COLOR_DEPTH_A1B5G5R5
;
404 case WIN_COLOR_DEPTH_R8G8B8X8
:
405 *alpha
= WIN_COLOR_DEPTH_R8G8B8A8
;
408 case WIN_COLOR_DEPTH_B8G8R8X8
:
409 *alpha
= WIN_COLOR_DEPTH_B8G8R8A8
;
412 case WIN_COLOR_DEPTH_B5G6R5
:
421 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
422 * be emulated using the alpha formats and alpha blending disabled.
424 static int tegra_plane_setup_opacity(struct tegra_plane
*tegra
,
425 struct tegra_plane_state
*state
)
430 switch (state
->format
) {
431 case WIN_COLOR_DEPTH_B5G5R5A1
:
432 case WIN_COLOR_DEPTH_A1B5G5R5
:
433 case WIN_COLOR_DEPTH_R8G8B8A8
:
434 case WIN_COLOR_DEPTH_B8G8R8A8
:
435 state
->opaque
= false;
439 err
= tegra_plane_format_get_alpha(state
->format
, &format
);
443 state
->format
= format
;
444 state
->opaque
= true;
451 static int tegra_plane_check_transparency(struct tegra_plane
*tegra
,
452 struct tegra_plane_state
*state
)
454 struct drm_plane_state
*old
, *plane_state
;
455 struct drm_plane
*plane
;
457 old
= drm_atomic_get_old_plane_state(state
->base
.state
, &tegra
->base
);
459 /* check if zpos / transparency changed */
460 if (old
->normalized_zpos
== state
->base
.normalized_zpos
&&
461 to_tegra_plane_state(old
)->opaque
== state
->opaque
)
464 /* include all sibling planes into this commit */
465 drm_for_each_plane(plane
, tegra
->base
.dev
) {
466 struct tegra_plane
*p
= to_tegra_plane(plane
);
468 /* skip this plane and planes on different CRTCs */
469 if (p
== tegra
|| p
->dc
!= tegra
->dc
)
472 plane_state
= drm_atomic_get_plane_state(state
->base
.state
,
474 if (IS_ERR(plane_state
))
475 return PTR_ERR(plane_state
);
481 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane
*plane
,
482 struct tegra_plane
*other
)
484 unsigned int index
= 0, i
;
486 WARN_ON(plane
== other
);
488 for (i
= 0; i
< 3; i
++) {
489 if (i
== plane
->index
)
492 if (i
== other
->index
)
501 static void tegra_plane_update_transparency(struct tegra_plane
*tegra
,
502 struct tegra_plane_state
*state
)
504 struct drm_plane_state
*new;
505 struct drm_plane
*plane
;
508 for_each_new_plane_in_state(state
->base
.state
, plane
, new, i
) {
509 struct tegra_plane
*p
= to_tegra_plane(plane
);
512 /* skip this plane and planes on different CRTCs */
513 if (p
== tegra
|| p
->dc
!= tegra
->dc
)
516 index
= tegra_plane_get_overlap_index(tegra
, p
);
518 if (new->fb
&& __drm_format_has_alpha(new->fb
->format
->format
))
519 state
->blending
[index
].alpha
= true;
521 state
->blending
[index
].alpha
= false;
523 if (new->normalized_zpos
> state
->base
.normalized_zpos
)
524 state
->blending
[index
].top
= true;
526 state
->blending
[index
].top
= false;
529 * Missing framebuffer means that plane is disabled, in this
530 * case mark B / C window as top to be able to differentiate
531 * windows indices order in regards to zPos for the middle
532 * window X / Y registers programming.
535 state
->blending
[index
].top
= (index
== 1);
539 static int tegra_plane_setup_transparency(struct tegra_plane
*tegra
,
540 struct tegra_plane_state
*state
)
542 struct tegra_plane_state
*tegra_state
;
543 struct drm_plane_state
*new;
544 struct drm_plane
*plane
;
548 * If planes zpos / transparency changed, sibling planes blending
549 * state may require adjustment and in this case they will be included
550 * into this atom commit, otherwise blending state is unchanged.
552 err
= tegra_plane_check_transparency(tegra
, state
);
557 * All planes are now in the atomic state, walk them up and update
558 * transparency state for each plane.
560 drm_for_each_plane(plane
, tegra
->base
.dev
) {
561 struct tegra_plane
*p
= to_tegra_plane(plane
);
563 /* skip planes on different CRTCs */
564 if (p
->dc
!= tegra
->dc
)
567 new = drm_atomic_get_new_plane_state(state
->base
.state
, plane
);
568 tegra_state
= to_tegra_plane_state(new);
571 * There is no need to update blending state for the disabled
575 tegra_plane_update_transparency(p
, tegra_state
);
581 int tegra_plane_setup_legacy_state(struct tegra_plane
*tegra
,
582 struct tegra_plane_state
*state
)
586 err
= tegra_plane_setup_opacity(tegra
, state
);
590 err
= tegra_plane_setup_transparency(tegra
, state
);