1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
6 #include <linux/iommu.h>
7 #include <linux/interconnect.h>
9 #include <drm/drm_atomic.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_fourcc.h>
12 #include <drm/drm_gem_atomic_helper.h>
13 #include <drm/drm_plane_helper.h>
18 static void tegra_plane_destroy(struct drm_plane *plane)
20 struct tegra_plane *p = to_tegra_plane(plane);
22 drm_plane_cleanup(plane);
26 static void tegra_plane_reset(struct drm_plane *plane)
28 struct tegra_plane *p = to_tegra_plane(plane);
29 struct tegra_plane_state *state;
33 __drm_atomic_helper_plane_destroy_state(plane->state);
38 state = kzalloc(sizeof(*state), GFP_KERNEL);
40 plane->state = &state->base;
41 plane->state->plane = plane;
42 plane->state->zpos = p->index;
43 plane->state->normalized_zpos = p->index;
45 for (i = 0; i < 3; i++)
46 state->iova[i] = DMA_MAPPING_ERROR;
50 static struct drm_plane_state *
51 tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
53 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
54 struct tegra_plane_state *copy;
57 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
61 __drm_atomic_helper_plane_duplicate_state(plane, ©->base);
62 copy->tiling = state->tiling;
63 copy->format = state->format;
64 copy->swap = state->swap;
65 copy->reflect_x = state->reflect_x;
66 copy->reflect_y = state->reflect_y;
67 copy->opaque = state->opaque;
68 copy->total_peak_memory_bandwidth = state->total_peak_memory_bandwidth;
69 copy->peak_memory_bandwidth = state->peak_memory_bandwidth;
70 copy->avg_memory_bandwidth = state->avg_memory_bandwidth;
72 for (i = 0; i < 2; i++)
73 copy->blending[i] = state->blending[i];
75 for (i = 0; i < 3; i++) {
76 copy->iova[i] = DMA_MAPPING_ERROR;
83 static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
84 struct drm_plane_state *state)
86 __drm_atomic_helper_plane_destroy_state(state);
90 static bool tegra_plane_supports_sector_layout(struct drm_plane *plane)
92 struct drm_crtc *crtc;
94 drm_for_each_crtc(crtc, plane->dev) {
95 if (plane->possible_crtcs & drm_crtc_mask(crtc)) {
96 struct tegra_dc *dc = to_tegra_dc(crtc);
98 if (!dc->soc->supports_sector_layout)
106 static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
110 const struct drm_format_info *info = drm_format_info(format);
112 if (modifier == DRM_FORMAT_MOD_LINEAR)
115 /* check for the sector layout bit */
116 if ((modifier >> 56) == DRM_FORMAT_MOD_VENDOR_NVIDIA) {
117 if (modifier & DRM_FORMAT_MOD_NVIDIA_SECTOR_LAYOUT) {
118 if (!tegra_plane_supports_sector_layout(plane))
123 if (info->num_planes == 1)
129 const struct drm_plane_funcs tegra_plane_funcs = {
130 .update_plane = drm_atomic_helper_update_plane,
131 .disable_plane = drm_atomic_helper_disable_plane,
132 .destroy = tegra_plane_destroy,
133 .reset = tegra_plane_reset,
134 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
135 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
136 .format_mod_supported = tegra_plane_format_mod_supported,
139 static int tegra_dc_pin(struct tegra_dc *dc, struct tegra_plane_state *state)
141 struct iommu_domain *domain = iommu_get_domain_for_dev(dc->dev);
145 for (i = 0; i < state->base.fb->format->num_planes; i++) {
146 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
147 dma_addr_t phys_addr, *phys;
148 struct sg_table *sgt;
151 * If we're not attached to a domain, we already stored the
152 * physical address when the buffer was allocated. If we're
153 * part of a group that's shared between all display
154 * controllers, we've also already mapped the framebuffer
155 * through the SMMU. In both cases we can short-circuit the
156 * code below and retrieve the stored IOV address.
158 if (!domain || dc->client.group)
163 sgt = host1x_bo_pin(dc->dev, &bo->base, phys);
170 err = dma_map_sgtable(dc->dev, sgt, DMA_TO_DEVICE, 0);
175 * The display controller needs contiguous memory, so
176 * fail if the buffer is discontiguous and we fail to
177 * map its SG table to a single contiguous chunk of
178 * I/O virtual memory.
180 if (sgt->nents > 1) {
185 state->iova[i] = sg_dma_address(sgt->sgl);
188 state->iova[i] = phys_addr;
195 dev_err(dc->dev, "failed to map plane %u: %d\n", i, err);
198 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
199 struct sg_table *sgt = state->sgt[i];
202 dma_unmap_sgtable(dc->dev, sgt, DMA_TO_DEVICE, 0);
204 host1x_bo_unpin(dc->dev, &bo->base, sgt);
205 state->iova[i] = DMA_MAPPING_ERROR;
206 state->sgt[i] = NULL;
212 static void tegra_dc_unpin(struct tegra_dc *dc, struct tegra_plane_state *state)
216 for (i = 0; i < state->base.fb->format->num_planes; i++) {
217 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
218 struct sg_table *sgt = state->sgt[i];
221 dma_unmap_sgtable(dc->dev, sgt, DMA_TO_DEVICE, 0);
223 host1x_bo_unpin(dc->dev, &bo->base, sgt);
224 state->iova[i] = DMA_MAPPING_ERROR;
225 state->sgt[i] = NULL;
229 int tegra_plane_prepare_fb(struct drm_plane *plane,
230 struct drm_plane_state *state)
232 struct tegra_dc *dc = to_tegra_dc(state->crtc);
237 drm_gem_plane_helper_prepare_fb(plane, state);
239 return tegra_dc_pin(dc, to_tegra_plane_state(state));
242 void tegra_plane_cleanup_fb(struct drm_plane *plane,
243 struct drm_plane_state *state)
245 struct tegra_dc *dc = to_tegra_dc(state->crtc);
248 tegra_dc_unpin(dc, to_tegra_plane_state(state));
251 static int tegra_plane_calculate_memory_bandwidth(struct drm_plane_state *state)
253 struct tegra_plane_state *tegra_state = to_tegra_plane_state(state);
254 unsigned int i, bpp, dst_w, dst_h, src_w, src_h, mul;
255 const struct tegra_dc_soc_info *soc;
256 const struct drm_format_info *fmt;
257 struct drm_crtc_state *crtc_state;
258 u64 avg_bandwidth, peak_bandwidth;
263 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
267 src_w = drm_rect_width(&state->src) >> 16;
268 src_h = drm_rect_height(&state->src) >> 16;
269 dst_w = drm_rect_width(&state->dst);
270 dst_h = drm_rect_height(&state->dst);
272 fmt = state->fb->format;
273 soc = to_tegra_dc(state->crtc)->soc;
276 * Note that real memory bandwidth vary depending on format and
277 * memory layout, we are not taking that into account because small
278 * estimation error isn't important since bandwidth is rounded up
281 for (i = 0, bpp = 0; i < fmt->num_planes; i++) {
282 unsigned int bpp_plane = fmt->cpp[i] * 8;
285 * Sub-sampling is relevant for chroma planes only and vertical
286 * readouts are not cached, hence only horizontal sub-sampling
290 bpp_plane /= fmt->hsub;
295 /* average bandwidth in kbytes/sec */
296 avg_bandwidth = min(src_w, dst_w) * min(src_h, dst_h);
297 avg_bandwidth *= drm_mode_vrefresh(&crtc_state->adjusted_mode);
298 avg_bandwidth = DIV_ROUND_UP(avg_bandwidth * bpp, 8) + 999;
299 do_div(avg_bandwidth, 1000);
301 /* mode.clock in kHz, peak bandwidth in kbytes/sec */
302 peak_bandwidth = DIV_ROUND_UP(crtc_state->adjusted_mode.clock * bpp, 8);
305 * Tegra30/114 Memory Controller can't interleave DC memory requests
306 * for the tiled windows because DC uses 16-bytes atom, while DDR3
307 * uses 32-bytes atom. Hence there is x2 memory overfetch for tiled
308 * framebuffer and DDR3 on these SoCs.
310 if (soc->plane_tiled_memory_bandwidth_x2 &&
311 tegra_state->tiling.mode == TEGRA_BO_TILING_MODE_TILED)
316 /* ICC bandwidth in kbytes/sec */
317 tegra_state->peak_memory_bandwidth = kBps_to_icc(peak_bandwidth) * mul;
318 tegra_state->avg_memory_bandwidth = kBps_to_icc(avg_bandwidth) * mul;
323 int tegra_plane_state_add(struct tegra_plane *plane,
324 struct drm_plane_state *state)
326 struct drm_crtc_state *crtc_state;
327 struct tegra_dc_state *tegra;
330 /* Propagate errors from allocation or locking failures. */
331 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
332 if (IS_ERR(crtc_state))
333 return PTR_ERR(crtc_state);
335 /* Check plane state for visibility and calculate clipping bounds */
336 err = drm_atomic_helper_check_plane_state(state, crtc_state,
337 0, INT_MAX, true, true);
341 err = tegra_plane_calculate_memory_bandwidth(state);
345 tegra = to_dc_state(crtc_state);
347 tegra->planes |= WIN_A_ACT_REQ << plane->index;
352 int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
354 /* assume no swapping of fetched data */
356 *swap = BYTE_SWAP_NOSWAP;
359 case DRM_FORMAT_ARGB4444:
360 *format = WIN_COLOR_DEPTH_B4G4R4A4;
363 case DRM_FORMAT_ARGB1555:
364 *format = WIN_COLOR_DEPTH_B5G5R5A1;
367 case DRM_FORMAT_RGB565:
368 *format = WIN_COLOR_DEPTH_B5G6R5;
371 case DRM_FORMAT_RGBA5551:
372 *format = WIN_COLOR_DEPTH_A1B5G5R5;
375 case DRM_FORMAT_ARGB8888:
376 *format = WIN_COLOR_DEPTH_B8G8R8A8;
379 case DRM_FORMAT_ABGR8888:
380 *format = WIN_COLOR_DEPTH_R8G8B8A8;
383 case DRM_FORMAT_ABGR4444:
384 *format = WIN_COLOR_DEPTH_R4G4B4A4;
387 case DRM_FORMAT_ABGR1555:
388 *format = WIN_COLOR_DEPTH_R5G5B5A;
391 case DRM_FORMAT_BGRA5551:
392 *format = WIN_COLOR_DEPTH_AR5G5B5;
395 case DRM_FORMAT_XRGB1555:
396 *format = WIN_COLOR_DEPTH_B5G5R5X1;
399 case DRM_FORMAT_RGBX5551:
400 *format = WIN_COLOR_DEPTH_X1B5G5R5;
403 case DRM_FORMAT_XBGR1555:
404 *format = WIN_COLOR_DEPTH_R5G5B5X1;
407 case DRM_FORMAT_BGRX5551:
408 *format = WIN_COLOR_DEPTH_X1R5G5B5;
411 case DRM_FORMAT_BGR565:
412 *format = WIN_COLOR_DEPTH_R5G6B5;
415 case DRM_FORMAT_BGRA8888:
416 *format = WIN_COLOR_DEPTH_A8R8G8B8;
419 case DRM_FORMAT_RGBA8888:
420 *format = WIN_COLOR_DEPTH_A8B8G8R8;
423 case DRM_FORMAT_XRGB8888:
424 *format = WIN_COLOR_DEPTH_B8G8R8X8;
427 case DRM_FORMAT_XBGR8888:
428 *format = WIN_COLOR_DEPTH_R8G8B8X8;
431 case DRM_FORMAT_UYVY:
432 *format = WIN_COLOR_DEPTH_YCbCr422;
435 case DRM_FORMAT_YUYV:
439 *format = WIN_COLOR_DEPTH_YCbCr422;
440 *swap = BYTE_SWAP_SWAP2;
443 case DRM_FORMAT_YUV420:
444 *format = WIN_COLOR_DEPTH_YCbCr420P;
447 case DRM_FORMAT_YUV422:
448 *format = WIN_COLOR_DEPTH_YCbCr422P;
458 bool tegra_plane_format_is_indexed(unsigned int format)
461 case WIN_COLOR_DEPTH_P1:
462 case WIN_COLOR_DEPTH_P2:
463 case WIN_COLOR_DEPTH_P4:
464 case WIN_COLOR_DEPTH_P8:
471 bool tegra_plane_format_is_yuv(unsigned int format, bool *planar, unsigned int *bpc)
474 case WIN_COLOR_DEPTH_YCbCr422:
475 case WIN_COLOR_DEPTH_YUV422:
484 case WIN_COLOR_DEPTH_YCbCr420P:
485 case WIN_COLOR_DEPTH_YUV420P:
486 case WIN_COLOR_DEPTH_YCbCr422P:
487 case WIN_COLOR_DEPTH_YUV422P:
488 case WIN_COLOR_DEPTH_YCbCr422R:
489 case WIN_COLOR_DEPTH_YUV422R:
490 case WIN_COLOR_DEPTH_YCbCr422RA:
491 case WIN_COLOR_DEPTH_YUV422RA:
507 static bool __drm_format_has_alpha(u32 format)
510 case DRM_FORMAT_ARGB1555:
511 case DRM_FORMAT_RGBA5551:
512 case DRM_FORMAT_ABGR8888:
513 case DRM_FORMAT_ARGB8888:
520 static int tegra_plane_format_get_alpha(unsigned int opaque,
523 if (tegra_plane_format_is_yuv(opaque, NULL, NULL)) {
529 case WIN_COLOR_DEPTH_B5G5R5X1:
530 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
533 case WIN_COLOR_DEPTH_X1B5G5R5:
534 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
537 case WIN_COLOR_DEPTH_R8G8B8X8:
538 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
541 case WIN_COLOR_DEPTH_B8G8R8X8:
542 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
545 case WIN_COLOR_DEPTH_B5G6R5:
554 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
555 * be emulated using the alpha formats and alpha blending disabled.
557 static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
558 struct tegra_plane_state *state)
563 switch (state->format) {
564 case WIN_COLOR_DEPTH_B5G5R5A1:
565 case WIN_COLOR_DEPTH_A1B5G5R5:
566 case WIN_COLOR_DEPTH_R8G8B8A8:
567 case WIN_COLOR_DEPTH_B8G8R8A8:
568 state->opaque = false;
572 err = tegra_plane_format_get_alpha(state->format, &format);
576 state->format = format;
577 state->opaque = true;
584 static int tegra_plane_check_transparency(struct tegra_plane *tegra,
585 struct tegra_plane_state *state)
587 struct drm_plane_state *old, *plane_state;
588 struct drm_plane *plane;
590 old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
592 /* check if zpos / transparency changed */
593 if (old->normalized_zpos == state->base.normalized_zpos &&
594 to_tegra_plane_state(old)->opaque == state->opaque)
597 /* include all sibling planes into this commit */
598 drm_for_each_plane(plane, tegra->base.dev) {
599 struct tegra_plane *p = to_tegra_plane(plane);
601 /* skip this plane and planes on different CRTCs */
602 if (p == tegra || p->dc != tegra->dc)
605 plane_state = drm_atomic_get_plane_state(state->base.state,
607 if (IS_ERR(plane_state))
608 return PTR_ERR(plane_state);
614 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
615 struct tegra_plane *other)
617 unsigned int index = 0, i;
619 WARN_ON(plane == other);
621 for (i = 0; i < 3; i++) {
622 if (i == plane->index)
625 if (i == other->index)
634 static void tegra_plane_update_transparency(struct tegra_plane *tegra,
635 struct tegra_plane_state *state)
637 struct drm_plane_state *new;
638 struct drm_plane *plane;
641 for_each_new_plane_in_state(state->base.state, plane, new, i) {
642 struct tegra_plane *p = to_tegra_plane(plane);
645 /* skip this plane and planes on different CRTCs */
646 if (p == tegra || p->dc != tegra->dc)
649 index = tegra_plane_get_overlap_index(tegra, p);
651 if (new->fb && __drm_format_has_alpha(new->fb->format->format))
652 state->blending[index].alpha = true;
654 state->blending[index].alpha = false;
656 if (new->normalized_zpos > state->base.normalized_zpos)
657 state->blending[index].top = true;
659 state->blending[index].top = false;
662 * Missing framebuffer means that plane is disabled, in this
663 * case mark B / C window as top to be able to differentiate
664 * windows indices order in regards to zPos for the middle
665 * window X / Y registers programming.
668 state->blending[index].top = (index == 1);
672 static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
673 struct tegra_plane_state *state)
675 struct tegra_plane_state *tegra_state;
676 struct drm_plane_state *new;
677 struct drm_plane *plane;
681 * If planes zpos / transparency changed, sibling planes blending
682 * state may require adjustment and in this case they will be included
683 * into this atom commit, otherwise blending state is unchanged.
685 err = tegra_plane_check_transparency(tegra, state);
690 * All planes are now in the atomic state, walk them up and update
691 * transparency state for each plane.
693 drm_for_each_plane(plane, tegra->base.dev) {
694 struct tegra_plane *p = to_tegra_plane(plane);
696 /* skip planes on different CRTCs */
697 if (p->dc != tegra->dc)
700 new = drm_atomic_get_new_plane_state(state->base.state, plane);
701 tegra_state = to_tegra_plane_state(new);
704 * There is no need to update blending state for the disabled
708 tegra_plane_update_transparency(p, tegra_state);
714 int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
715 struct tegra_plane_state *state)
719 err = tegra_plane_setup_opacity(tegra, state);
723 err = tegra_plane_setup_transparency(tegra, state);
730 static const char * const tegra_plane_icc_names[TEGRA_DC_LEGACY_PLANES_NUM] = {
731 "wina", "winb", "winc", NULL, NULL, NULL, "cursor",
734 int tegra_plane_interconnect_init(struct tegra_plane *plane)
736 const char *icc_name = tegra_plane_icc_names[plane->index];
737 struct device *dev = plane->dc->dev;
738 struct tegra_dc *dc = plane->dc;
741 if (WARN_ON(plane->index >= TEGRA_DC_LEGACY_PLANES_NUM) ||
742 WARN_ON(!tegra_plane_icc_names[plane->index]))
745 plane->icc_mem = devm_of_icc_get(dev, icc_name);
746 err = PTR_ERR_OR_ZERO(plane->icc_mem);
748 dev_err_probe(dev, err, "failed to get %s interconnect\n",
753 /* plane B on T20/30 has a dedicated memory client for a 6-tap vertical filter */
754 if (plane->index == 1 && dc->soc->has_win_b_vfilter_mem_client) {
755 plane->icc_mem_vfilter = devm_of_icc_get(dev, "winb-vfilter");
756 err = PTR_ERR_OR_ZERO(plane->icc_mem_vfilter);
758 dev_err_probe(dev, err, "failed to get %s interconnect\n",