1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
6 #include <linux/iommu.h>
8 #include <drm/drm_atomic.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_fourcc.h>
11 #include <drm/drm_gem_framebuffer_helper.h>
12 #include <drm/drm_plane_helper.h>
17 static void tegra_plane_destroy(struct drm_plane *plane)
19 struct tegra_plane *p = to_tegra_plane(plane);
21 drm_plane_cleanup(plane);
25 static void tegra_plane_reset(struct drm_plane *plane)
27 struct tegra_plane *p = to_tegra_plane(plane);
28 struct tegra_plane_state *state;
32 __drm_atomic_helper_plane_destroy_state(plane->state);
37 state = kzalloc(sizeof(*state), GFP_KERNEL);
39 plane->state = &state->base;
40 plane->state->plane = plane;
41 plane->state->zpos = p->index;
42 plane->state->normalized_zpos = p->index;
44 for (i = 0; i < 3; i++)
45 state->iova[i] = DMA_MAPPING_ERROR;
49 static struct drm_plane_state *
50 tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
52 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
53 struct tegra_plane_state *copy;
56 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
60 __drm_atomic_helper_plane_duplicate_state(plane, ©->base);
61 copy->tiling = state->tiling;
62 copy->format = state->format;
63 copy->swap = state->swap;
64 copy->reflect_x = state->reflect_x;
65 copy->reflect_y = state->reflect_y;
66 copy->opaque = state->opaque;
68 for (i = 0; i < 2; i++)
69 copy->blending[i] = state->blending[i];
71 for (i = 0; i < 3; i++) {
72 copy->iova[i] = DMA_MAPPING_ERROR;
79 static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
80 struct drm_plane_state *state)
82 __drm_atomic_helper_plane_destroy_state(state);
86 static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
90 const struct drm_format_info *info = drm_format_info(format);
92 if (modifier == DRM_FORMAT_MOD_LINEAR)
95 if (info->num_planes == 1)
101 const struct drm_plane_funcs tegra_plane_funcs = {
102 .update_plane = drm_atomic_helper_update_plane,
103 .disable_plane = drm_atomic_helper_disable_plane,
104 .destroy = tegra_plane_destroy,
105 .reset = tegra_plane_reset,
106 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
107 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
108 .format_mod_supported = tegra_plane_format_mod_supported,
111 static int tegra_dc_pin(struct tegra_dc *dc, struct tegra_plane_state *state)
113 struct iommu_domain *domain = iommu_get_domain_for_dev(dc->dev);
117 for (i = 0; i < state->base.fb->format->num_planes; i++) {
118 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
119 dma_addr_t phys_addr, *phys;
120 struct sg_table *sgt;
122 if (!domain || dc->client.group)
127 sgt = host1x_bo_pin(dc->dev, &bo->base, phys);
134 err = dma_map_sg(dc->dev, sgt->sgl, sgt->nents,
142 * The display controller needs contiguous memory, so
143 * fail if the buffer is discontiguous and we fail to
144 * map its SG table to a single contiguous chunk of
145 * I/O virtual memory.
152 state->iova[i] = sg_dma_address(sgt->sgl);
155 state->iova[i] = phys_addr;
162 dev_err(dc->dev, "failed to map plane %u: %d\n", i, err);
165 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
166 struct sg_table *sgt = state->sgt[i];
169 dma_unmap_sg(dc->dev, sgt->sgl, sgt->nents,
172 host1x_bo_unpin(dc->dev, &bo->base, sgt);
173 state->iova[i] = DMA_MAPPING_ERROR;
174 state->sgt[i] = NULL;
180 static void tegra_dc_unpin(struct tegra_dc *dc, struct tegra_plane_state *state)
184 for (i = 0; i < state->base.fb->format->num_planes; i++) {
185 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
186 struct sg_table *sgt = state->sgt[i];
189 dma_unmap_sg(dc->dev, sgt->sgl, sgt->nents,
192 host1x_bo_unpin(dc->dev, &bo->base, sgt);
193 state->iova[i] = DMA_MAPPING_ERROR;
194 state->sgt[i] = NULL;
198 int tegra_plane_prepare_fb(struct drm_plane *plane,
199 struct drm_plane_state *state)
201 struct tegra_dc *dc = to_tegra_dc(state->crtc);
206 drm_gem_fb_prepare_fb(plane, state);
208 return tegra_dc_pin(dc, to_tegra_plane_state(state));
211 void tegra_plane_cleanup_fb(struct drm_plane *plane,
212 struct drm_plane_state *state)
214 struct tegra_dc *dc = to_tegra_dc(state->crtc);
217 tegra_dc_unpin(dc, to_tegra_plane_state(state));
220 int tegra_plane_state_add(struct tegra_plane *plane,
221 struct drm_plane_state *state)
223 struct drm_crtc_state *crtc_state;
224 struct tegra_dc_state *tegra;
227 /* Propagate errors from allocation or locking failures. */
228 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
229 if (IS_ERR(crtc_state))
230 return PTR_ERR(crtc_state);
232 /* Check plane state for visibility and calculate clipping bounds */
233 err = drm_atomic_helper_check_plane_state(state, crtc_state,
234 0, INT_MAX, true, true);
238 tegra = to_dc_state(crtc_state);
240 tegra->planes |= WIN_A_ACT_REQ << plane->index;
245 int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
247 /* assume no swapping of fetched data */
249 *swap = BYTE_SWAP_NOSWAP;
252 case DRM_FORMAT_ARGB4444:
253 *format = WIN_COLOR_DEPTH_B4G4R4A4;
256 case DRM_FORMAT_ARGB1555:
257 *format = WIN_COLOR_DEPTH_B5G5R5A1;
260 case DRM_FORMAT_RGB565:
261 *format = WIN_COLOR_DEPTH_B5G6R5;
264 case DRM_FORMAT_RGBA5551:
265 *format = WIN_COLOR_DEPTH_A1B5G5R5;
268 case DRM_FORMAT_ARGB8888:
269 *format = WIN_COLOR_DEPTH_B8G8R8A8;
272 case DRM_FORMAT_ABGR8888:
273 *format = WIN_COLOR_DEPTH_R8G8B8A8;
276 case DRM_FORMAT_ABGR4444:
277 *format = WIN_COLOR_DEPTH_R4G4B4A4;
280 case DRM_FORMAT_ABGR1555:
281 *format = WIN_COLOR_DEPTH_R5G5B5A;
284 case DRM_FORMAT_BGRA5551:
285 *format = WIN_COLOR_DEPTH_AR5G5B5;
288 case DRM_FORMAT_XRGB1555:
289 *format = WIN_COLOR_DEPTH_B5G5R5X1;
292 case DRM_FORMAT_RGBX5551:
293 *format = WIN_COLOR_DEPTH_X1B5G5R5;
296 case DRM_FORMAT_XBGR1555:
297 *format = WIN_COLOR_DEPTH_R5G5B5X1;
300 case DRM_FORMAT_BGRX5551:
301 *format = WIN_COLOR_DEPTH_X1R5G5B5;
304 case DRM_FORMAT_BGR565:
305 *format = WIN_COLOR_DEPTH_R5G6B5;
308 case DRM_FORMAT_BGRA8888:
309 *format = WIN_COLOR_DEPTH_A8R8G8B8;
312 case DRM_FORMAT_RGBA8888:
313 *format = WIN_COLOR_DEPTH_A8B8G8R8;
316 case DRM_FORMAT_XRGB8888:
317 *format = WIN_COLOR_DEPTH_B8G8R8X8;
320 case DRM_FORMAT_XBGR8888:
321 *format = WIN_COLOR_DEPTH_R8G8B8X8;
324 case DRM_FORMAT_UYVY:
325 *format = WIN_COLOR_DEPTH_YCbCr422;
328 case DRM_FORMAT_YUYV:
332 *format = WIN_COLOR_DEPTH_YCbCr422;
333 *swap = BYTE_SWAP_SWAP2;
336 case DRM_FORMAT_YUV420:
337 *format = WIN_COLOR_DEPTH_YCbCr420P;
340 case DRM_FORMAT_YUV422:
341 *format = WIN_COLOR_DEPTH_YCbCr422P;
351 bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
354 case WIN_COLOR_DEPTH_YCbCr422:
355 case WIN_COLOR_DEPTH_YUV422:
361 case WIN_COLOR_DEPTH_YCbCr420P:
362 case WIN_COLOR_DEPTH_YUV420P:
363 case WIN_COLOR_DEPTH_YCbCr422P:
364 case WIN_COLOR_DEPTH_YUV422P:
365 case WIN_COLOR_DEPTH_YCbCr422R:
366 case WIN_COLOR_DEPTH_YUV422R:
367 case WIN_COLOR_DEPTH_YCbCr422RA:
368 case WIN_COLOR_DEPTH_YUV422RA:
381 static bool __drm_format_has_alpha(u32 format)
384 case DRM_FORMAT_ARGB1555:
385 case DRM_FORMAT_RGBA5551:
386 case DRM_FORMAT_ABGR8888:
387 case DRM_FORMAT_ARGB8888:
394 static int tegra_plane_format_get_alpha(unsigned int opaque,
397 if (tegra_plane_format_is_yuv(opaque, NULL)) {
403 case WIN_COLOR_DEPTH_B5G5R5X1:
404 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
407 case WIN_COLOR_DEPTH_X1B5G5R5:
408 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
411 case WIN_COLOR_DEPTH_R8G8B8X8:
412 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
415 case WIN_COLOR_DEPTH_B8G8R8X8:
416 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
419 case WIN_COLOR_DEPTH_B5G6R5:
428 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
429 * be emulated using the alpha formats and alpha blending disabled.
431 static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
432 struct tegra_plane_state *state)
437 switch (state->format) {
438 case WIN_COLOR_DEPTH_B5G5R5A1:
439 case WIN_COLOR_DEPTH_A1B5G5R5:
440 case WIN_COLOR_DEPTH_R8G8B8A8:
441 case WIN_COLOR_DEPTH_B8G8R8A8:
442 state->opaque = false;
446 err = tegra_plane_format_get_alpha(state->format, &format);
450 state->format = format;
451 state->opaque = true;
458 static int tegra_plane_check_transparency(struct tegra_plane *tegra,
459 struct tegra_plane_state *state)
461 struct drm_plane_state *old, *plane_state;
462 struct drm_plane *plane;
464 old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
466 /* check if zpos / transparency changed */
467 if (old->normalized_zpos == state->base.normalized_zpos &&
468 to_tegra_plane_state(old)->opaque == state->opaque)
471 /* include all sibling planes into this commit */
472 drm_for_each_plane(plane, tegra->base.dev) {
473 struct tegra_plane *p = to_tegra_plane(plane);
475 /* skip this plane and planes on different CRTCs */
476 if (p == tegra || p->dc != tegra->dc)
479 plane_state = drm_atomic_get_plane_state(state->base.state,
481 if (IS_ERR(plane_state))
482 return PTR_ERR(plane_state);
488 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
489 struct tegra_plane *other)
491 unsigned int index = 0, i;
493 WARN_ON(plane == other);
495 for (i = 0; i < 3; i++) {
496 if (i == plane->index)
499 if (i == other->index)
508 static void tegra_plane_update_transparency(struct tegra_plane *tegra,
509 struct tegra_plane_state *state)
511 struct drm_plane_state *new;
512 struct drm_plane *plane;
515 for_each_new_plane_in_state(state->base.state, plane, new, i) {
516 struct tegra_plane *p = to_tegra_plane(plane);
519 /* skip this plane and planes on different CRTCs */
520 if (p == tegra || p->dc != tegra->dc)
523 index = tegra_plane_get_overlap_index(tegra, p);
525 if (new->fb && __drm_format_has_alpha(new->fb->format->format))
526 state->blending[index].alpha = true;
528 state->blending[index].alpha = false;
530 if (new->normalized_zpos > state->base.normalized_zpos)
531 state->blending[index].top = true;
533 state->blending[index].top = false;
536 * Missing framebuffer means that plane is disabled, in this
537 * case mark B / C window as top to be able to differentiate
538 * windows indices order in regards to zPos for the middle
539 * window X / Y registers programming.
542 state->blending[index].top = (index == 1);
546 static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
547 struct tegra_plane_state *state)
549 struct tegra_plane_state *tegra_state;
550 struct drm_plane_state *new;
551 struct drm_plane *plane;
555 * If planes zpos / transparency changed, sibling planes blending
556 * state may require adjustment and in this case they will be included
557 * into this atom commit, otherwise blending state is unchanged.
559 err = tegra_plane_check_transparency(tegra, state);
564 * All planes are now in the atomic state, walk them up and update
565 * transparency state for each plane.
567 drm_for_each_plane(plane, tegra->base.dev) {
568 struct tegra_plane *p = to_tegra_plane(plane);
570 /* skip planes on different CRTCs */
571 if (p->dc != tegra->dc)
574 new = drm_atomic_get_new_plane_state(state->base.state, plane);
575 tegra_state = to_tegra_plane_state(new);
578 * There is no need to update blending state for the disabled
582 tegra_plane_update_transparency(p, tegra_state);
588 int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
589 struct tegra_plane_state *state)
593 err = tegra_plane_setup_opacity(tegra, state);
597 err = tegra_plane_setup_transparency(tegra, state);