1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
6 #include <linux/dma-mapping.h>
7 #include <linux/iommu.h>
8 #include <linux/interconnect.h>
10 #include <drm/drm_atomic.h>
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_fourcc.h>
13 #include <drm/drm_framebuffer.h>
14 #include <drm/drm_gem_atomic_helper.h>
15 #include <drm/drm_plane_helper.h>
20 static void tegra_plane_destroy(struct drm_plane *plane)
22 struct tegra_plane *p = to_tegra_plane(plane);
24 drm_plane_cleanup(plane);
28 static void tegra_plane_reset(struct drm_plane *plane)
30 struct tegra_plane *p = to_tegra_plane(plane);
31 struct tegra_plane_state *state;
35 __drm_atomic_helper_plane_destroy_state(plane->state);
40 state = kzalloc(sizeof(*state), GFP_KERNEL);
42 plane->state = &state->base;
43 plane->state->plane = plane;
44 plane->state->zpos = p->index;
45 plane->state->normalized_zpos = p->index;
47 for (i = 0; i < 3; i++)
48 state->iova[i] = DMA_MAPPING_ERROR;
52 static struct drm_plane_state *
53 tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
55 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
56 struct tegra_plane_state *copy;
59 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
63 __drm_atomic_helper_plane_duplicate_state(plane, ©->base);
64 copy->tiling = state->tiling;
65 copy->format = state->format;
66 copy->swap = state->swap;
67 copy->reflect_x = state->reflect_x;
68 copy->reflect_y = state->reflect_y;
69 copy->opaque = state->opaque;
70 copy->total_peak_memory_bandwidth = state->total_peak_memory_bandwidth;
71 copy->peak_memory_bandwidth = state->peak_memory_bandwidth;
72 copy->avg_memory_bandwidth = state->avg_memory_bandwidth;
74 for (i = 0; i < 2; i++)
75 copy->blending[i] = state->blending[i];
77 for (i = 0; i < 3; i++) {
78 copy->iova[i] = DMA_MAPPING_ERROR;
85 static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
86 struct drm_plane_state *state)
88 __drm_atomic_helper_plane_destroy_state(state);
92 static bool tegra_plane_supports_sector_layout(struct drm_plane *plane)
94 struct drm_crtc *crtc;
96 drm_for_each_crtc(crtc, plane->dev) {
97 if (plane->possible_crtcs & drm_crtc_mask(crtc)) {
98 struct tegra_dc *dc = to_tegra_dc(crtc);
100 if (!dc->soc->supports_sector_layout)
108 static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
112 const struct drm_format_info *info = drm_format_info(format);
114 if (modifier == DRM_FORMAT_MOD_LINEAR)
117 /* check for the sector layout bit */
118 if (fourcc_mod_is_vendor(modifier, NVIDIA)) {
119 if (modifier & DRM_FORMAT_MOD_NVIDIA_SECTOR_LAYOUT) {
120 if (!tegra_plane_supports_sector_layout(plane))
125 if (info->num_planes == 1)
131 const struct drm_plane_funcs tegra_plane_funcs = {
132 .update_plane = drm_atomic_helper_update_plane,
133 .disable_plane = drm_atomic_helper_disable_plane,
134 .destroy = tegra_plane_destroy,
135 .reset = tegra_plane_reset,
136 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
137 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
138 .format_mod_supported = tegra_plane_format_mod_supported,
141 static int tegra_dc_pin(struct tegra_dc *dc, struct tegra_plane_state *state)
146 for (i = 0; i < state->base.fb->format->num_planes; i++) {
147 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
148 struct host1x_bo_mapping *map;
150 map = host1x_bo_pin(dc->dev, &bo->base, DMA_TO_DEVICE, &dc->client.cache);
156 if (!dc->client.group) {
158 * The display controller needs contiguous memory, so
159 * fail if the buffer is discontiguous and we fail to
160 * map its SG table to a single contiguous chunk of
161 * I/O virtual memory.
163 if (map->chunks > 1) {
168 state->iova[i] = map->phys;
170 state->iova[i] = bo->iova;
179 dev_err(dc->dev, "failed to map plane %u: %d\n", i, err);
182 host1x_bo_unpin(state->map[i]);
183 state->iova[i] = DMA_MAPPING_ERROR;
184 state->map[i] = NULL;
190 static void tegra_dc_unpin(struct tegra_dc *dc, struct tegra_plane_state *state)
194 for (i = 0; i < state->base.fb->format->num_planes; i++) {
195 host1x_bo_unpin(state->map[i]);
196 state->iova[i] = DMA_MAPPING_ERROR;
197 state->map[i] = NULL;
201 int tegra_plane_prepare_fb(struct drm_plane *plane,
202 struct drm_plane_state *state)
204 struct tegra_dc *dc = to_tegra_dc(state->crtc);
210 err = drm_gem_plane_helper_prepare_fb(plane, state);
214 return tegra_dc_pin(dc, to_tegra_plane_state(state));
217 void tegra_plane_cleanup_fb(struct drm_plane *plane,
218 struct drm_plane_state *state)
220 struct tegra_dc *dc = to_tegra_dc(state->crtc);
223 tegra_dc_unpin(dc, to_tegra_plane_state(state));
226 static int tegra_plane_calculate_memory_bandwidth(struct drm_plane_state *state)
228 struct tegra_plane_state *tegra_state = to_tegra_plane_state(state);
229 unsigned int i, bpp, dst_w, dst_h, src_w, src_h, mul;
230 const struct tegra_dc_soc_info *soc;
231 const struct drm_format_info *fmt;
232 struct drm_crtc_state *crtc_state;
233 u64 avg_bandwidth, peak_bandwidth;
238 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
242 src_w = drm_rect_width(&state->src) >> 16;
243 src_h = drm_rect_height(&state->src) >> 16;
244 dst_w = drm_rect_width(&state->dst);
245 dst_h = drm_rect_height(&state->dst);
247 fmt = state->fb->format;
248 soc = to_tegra_dc(state->crtc)->soc;
251 * Note that real memory bandwidth vary depending on format and
252 * memory layout, we are not taking that into account because small
253 * estimation error isn't important since bandwidth is rounded up
256 for (i = 0, bpp = 0; i < fmt->num_planes; i++) {
257 unsigned int bpp_plane = fmt->cpp[i] * 8;
260 * Sub-sampling is relevant for chroma planes only and vertical
261 * readouts are not cached, hence only horizontal sub-sampling
265 bpp_plane /= fmt->hsub;
270 /* average bandwidth in kbytes/sec */
271 avg_bandwidth = min(src_w, dst_w) * min(src_h, dst_h);
272 avg_bandwidth *= drm_mode_vrefresh(&crtc_state->adjusted_mode);
273 avg_bandwidth = DIV_ROUND_UP(avg_bandwidth * bpp, 8) + 999;
274 do_div(avg_bandwidth, 1000);
276 /* mode.clock in kHz, peak bandwidth in kbytes/sec */
277 peak_bandwidth = DIV_ROUND_UP(crtc_state->adjusted_mode.clock * bpp, 8);
280 * Tegra30/114 Memory Controller can't interleave DC memory requests
281 * for the tiled windows because DC uses 16-bytes atom, while DDR3
282 * uses 32-bytes atom. Hence there is x2 memory overfetch for tiled
283 * framebuffer and DDR3 on these SoCs.
285 if (soc->plane_tiled_memory_bandwidth_x2 &&
286 tegra_state->tiling.mode == TEGRA_BO_TILING_MODE_TILED)
291 /* ICC bandwidth in kbytes/sec */
292 tegra_state->peak_memory_bandwidth = kBps_to_icc(peak_bandwidth) * mul;
293 tegra_state->avg_memory_bandwidth = kBps_to_icc(avg_bandwidth) * mul;
298 int tegra_plane_state_add(struct tegra_plane *plane,
299 struct drm_plane_state *state)
301 struct drm_crtc_state *crtc_state;
302 struct tegra_dc_state *tegra;
305 /* Propagate errors from allocation or locking failures. */
306 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
307 if (IS_ERR(crtc_state))
308 return PTR_ERR(crtc_state);
310 /* Check plane state for visibility and calculate clipping bounds */
311 err = drm_atomic_helper_check_plane_state(state, crtc_state,
312 0, INT_MAX, true, true);
316 err = tegra_plane_calculate_memory_bandwidth(state);
320 tegra = to_dc_state(crtc_state);
322 tegra->planes |= WIN_A_ACT_REQ << plane->index;
327 int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
329 /* assume no swapping of fetched data */
331 *swap = BYTE_SWAP_NOSWAP;
334 case DRM_FORMAT_ARGB4444:
335 *format = WIN_COLOR_DEPTH_B4G4R4A4;
338 case DRM_FORMAT_ARGB1555:
339 *format = WIN_COLOR_DEPTH_B5G5R5A1;
342 case DRM_FORMAT_RGB565:
343 *format = WIN_COLOR_DEPTH_B5G6R5;
346 case DRM_FORMAT_RGBA5551:
347 *format = WIN_COLOR_DEPTH_A1B5G5R5;
350 case DRM_FORMAT_ARGB8888:
351 *format = WIN_COLOR_DEPTH_B8G8R8A8;
354 case DRM_FORMAT_ABGR8888:
355 *format = WIN_COLOR_DEPTH_R8G8B8A8;
358 case DRM_FORMAT_ABGR4444:
359 *format = WIN_COLOR_DEPTH_R4G4B4A4;
362 case DRM_FORMAT_ABGR1555:
363 *format = WIN_COLOR_DEPTH_R5G5B5A;
366 case DRM_FORMAT_BGRA5551:
367 *format = WIN_COLOR_DEPTH_AR5G5B5;
370 case DRM_FORMAT_XRGB1555:
371 *format = WIN_COLOR_DEPTH_B5G5R5X1;
374 case DRM_FORMAT_RGBX5551:
375 *format = WIN_COLOR_DEPTH_X1B5G5R5;
378 case DRM_FORMAT_XBGR1555:
379 *format = WIN_COLOR_DEPTH_R5G5B5X1;
382 case DRM_FORMAT_BGRX5551:
383 *format = WIN_COLOR_DEPTH_X1R5G5B5;
386 case DRM_FORMAT_BGR565:
387 *format = WIN_COLOR_DEPTH_R5G6B5;
390 case DRM_FORMAT_BGRA8888:
391 *format = WIN_COLOR_DEPTH_A8R8G8B8;
394 case DRM_FORMAT_RGBA8888:
395 *format = WIN_COLOR_DEPTH_A8B8G8R8;
398 case DRM_FORMAT_XRGB8888:
399 *format = WIN_COLOR_DEPTH_B8G8R8X8;
402 case DRM_FORMAT_XBGR8888:
403 *format = WIN_COLOR_DEPTH_R8G8B8X8;
406 case DRM_FORMAT_UYVY:
407 *format = WIN_COLOR_DEPTH_YCbCr422;
410 case DRM_FORMAT_YUYV:
414 *format = WIN_COLOR_DEPTH_YCbCr422;
415 *swap = BYTE_SWAP_SWAP2;
418 case DRM_FORMAT_YVYU:
422 *format = WIN_COLOR_DEPTH_YCbCr422;
423 *swap = BYTE_SWAP_SWAP4;
426 case DRM_FORMAT_VYUY:
430 *format = WIN_COLOR_DEPTH_YCbCr422;
431 *swap = BYTE_SWAP_SWAP4HW;
434 case DRM_FORMAT_YUV420:
435 *format = WIN_COLOR_DEPTH_YCbCr420P;
438 case DRM_FORMAT_YUV422:
439 *format = WIN_COLOR_DEPTH_YCbCr422P;
442 case DRM_FORMAT_YUV444:
443 *format = WIN_COLOR_DEPTH_YCbCr444P;
446 case DRM_FORMAT_NV12:
447 *format = WIN_COLOR_DEPTH_YCbCr420SP;
450 case DRM_FORMAT_NV21:
451 *format = WIN_COLOR_DEPTH_YCrCb420SP;
454 case DRM_FORMAT_NV16:
455 *format = WIN_COLOR_DEPTH_YCbCr422SP;
458 case DRM_FORMAT_NV61:
459 *format = WIN_COLOR_DEPTH_YCrCb422SP;
462 case DRM_FORMAT_NV24:
463 *format = WIN_COLOR_DEPTH_YCbCr444SP;
466 case DRM_FORMAT_NV42:
467 *format = WIN_COLOR_DEPTH_YCrCb444SP;
477 bool tegra_plane_format_is_indexed(unsigned int format)
480 case WIN_COLOR_DEPTH_P1:
481 case WIN_COLOR_DEPTH_P2:
482 case WIN_COLOR_DEPTH_P4:
483 case WIN_COLOR_DEPTH_P8:
490 bool tegra_plane_format_is_yuv(unsigned int format, unsigned int *planes, unsigned int *bpc)
493 case WIN_COLOR_DEPTH_YCbCr422:
494 case WIN_COLOR_DEPTH_YUV422:
503 case WIN_COLOR_DEPTH_YCbCr420P:
504 case WIN_COLOR_DEPTH_YUV420P:
505 case WIN_COLOR_DEPTH_YCbCr422P:
506 case WIN_COLOR_DEPTH_YUV422P:
507 case WIN_COLOR_DEPTH_YCbCr422R:
508 case WIN_COLOR_DEPTH_YUV422R:
509 case WIN_COLOR_DEPTH_YCbCr422RA:
510 case WIN_COLOR_DEPTH_YUV422RA:
511 case WIN_COLOR_DEPTH_YCbCr444P:
520 case WIN_COLOR_DEPTH_YCrCb420SP:
521 case WIN_COLOR_DEPTH_YCbCr420SP:
522 case WIN_COLOR_DEPTH_YCrCb422SP:
523 case WIN_COLOR_DEPTH_YCbCr422SP:
524 case WIN_COLOR_DEPTH_YCrCb444SP:
525 case WIN_COLOR_DEPTH_YCbCr444SP:
541 static bool __drm_format_has_alpha(u32 format)
544 case DRM_FORMAT_ARGB1555:
545 case DRM_FORMAT_RGBA5551:
546 case DRM_FORMAT_ABGR8888:
547 case DRM_FORMAT_ARGB8888:
554 static int tegra_plane_format_get_alpha(unsigned int opaque,
557 if (tegra_plane_format_is_yuv(opaque, NULL, NULL)) {
563 case WIN_COLOR_DEPTH_B5G5R5X1:
564 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
567 case WIN_COLOR_DEPTH_X1B5G5R5:
568 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
571 case WIN_COLOR_DEPTH_R8G8B8X8:
572 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
575 case WIN_COLOR_DEPTH_B8G8R8X8:
576 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
579 case WIN_COLOR_DEPTH_B5G6R5:
588 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
589 * be emulated using the alpha formats and alpha blending disabled.
591 static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
592 struct tegra_plane_state *state)
597 switch (state->format) {
598 case WIN_COLOR_DEPTH_B5G5R5A1:
599 case WIN_COLOR_DEPTH_A1B5G5R5:
600 case WIN_COLOR_DEPTH_R8G8B8A8:
601 case WIN_COLOR_DEPTH_B8G8R8A8:
602 state->opaque = false;
606 err = tegra_plane_format_get_alpha(state->format, &format);
610 state->format = format;
611 state->opaque = true;
618 static int tegra_plane_check_transparency(struct tegra_plane *tegra,
619 struct tegra_plane_state *state)
621 struct drm_plane_state *old, *plane_state;
622 struct drm_plane *plane;
624 old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
626 /* check if zpos / transparency changed */
627 if (old->normalized_zpos == state->base.normalized_zpos &&
628 to_tegra_plane_state(old)->opaque == state->opaque)
631 /* include all sibling planes into this commit */
632 drm_for_each_plane(plane, tegra->base.dev) {
633 struct tegra_plane *p = to_tegra_plane(plane);
635 /* skip this plane and planes on different CRTCs */
636 if (p == tegra || p->dc != tegra->dc)
639 plane_state = drm_atomic_get_plane_state(state->base.state,
641 if (IS_ERR(plane_state))
642 return PTR_ERR(plane_state);
648 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
649 struct tegra_plane *other)
651 unsigned int index = 0, i;
653 WARN_ON(plane == other);
655 for (i = 0; i < 3; i++) {
656 if (i == plane->index)
659 if (i == other->index)
668 static void tegra_plane_update_transparency(struct tegra_plane *tegra,
669 struct tegra_plane_state *state)
671 struct drm_plane_state *new;
672 struct drm_plane *plane;
675 for_each_new_plane_in_state(state->base.state, plane, new, i) {
676 struct tegra_plane *p = to_tegra_plane(plane);
679 /* skip this plane and planes on different CRTCs */
680 if (p == tegra || p->dc != tegra->dc)
683 index = tegra_plane_get_overlap_index(tegra, p);
685 if (new->fb && __drm_format_has_alpha(new->fb->format->format))
686 state->blending[index].alpha = true;
688 state->blending[index].alpha = false;
690 if (new->normalized_zpos > state->base.normalized_zpos)
691 state->blending[index].top = true;
693 state->blending[index].top = false;
696 * Missing framebuffer means that plane is disabled, in this
697 * case mark B / C window as top to be able to differentiate
698 * windows indices order in regards to zPos for the middle
699 * window X / Y registers programming.
702 state->blending[index].top = (index == 1);
706 static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
707 struct tegra_plane_state *state)
709 struct tegra_plane_state *tegra_state;
710 struct drm_plane_state *new;
711 struct drm_plane *plane;
715 * If planes zpos / transparency changed, sibling planes blending
716 * state may require adjustment and in this case they will be included
717 * into this atom commit, otherwise blending state is unchanged.
719 err = tegra_plane_check_transparency(tegra, state);
724 * All planes are now in the atomic state, walk them up and update
725 * transparency state for each plane.
727 drm_for_each_plane(plane, tegra->base.dev) {
728 struct tegra_plane *p = to_tegra_plane(plane);
730 /* skip planes on different CRTCs */
731 if (p->dc != tegra->dc)
734 new = drm_atomic_get_new_plane_state(state->base.state, plane);
735 tegra_state = to_tegra_plane_state(new);
738 * There is no need to update blending state for the disabled
742 tegra_plane_update_transparency(p, tegra_state);
748 int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
749 struct tegra_plane_state *state)
753 err = tegra_plane_setup_opacity(tegra, state);
757 err = tegra_plane_setup_transparency(tegra, state);
764 static const char * const tegra_plane_icc_names[TEGRA_DC_LEGACY_PLANES_NUM] = {
765 "wina", "winb", "winc", NULL, NULL, NULL, "cursor",
768 int tegra_plane_interconnect_init(struct tegra_plane *plane)
770 const char *icc_name = tegra_plane_icc_names[plane->index];
771 struct device *dev = plane->dc->dev;
772 struct tegra_dc *dc = plane->dc;
775 if (WARN_ON(plane->index >= TEGRA_DC_LEGACY_PLANES_NUM) ||
776 WARN_ON(!tegra_plane_icc_names[plane->index]))
779 plane->icc_mem = devm_of_icc_get(dev, icc_name);
780 err = PTR_ERR_OR_ZERO(plane->icc_mem);
782 dev_err_probe(dev, err, "failed to get %s interconnect\n",
787 /* plane B on T20/30 has a dedicated memory client for a 6-tap vertical filter */
788 if (plane->index == 1 && dc->soc->has_win_b_vfilter_mem_client) {
789 plane->icc_mem_vfilter = devm_of_icc_get(dev, "winb-vfilter");
790 err = PTR_ERR_OR_ZERO(plane->icc_mem_vfilter);
792 dev_err_probe(dev, err, "failed to get %s interconnect\n",