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 (fourcc_mod_is_vendor(modifier, 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)
144 for (i = 0; i < state->base.fb->format->num_planes; i++) {
145 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
146 struct host1x_bo_mapping *map;
148 map = host1x_bo_pin(dc->dev, &bo->base, DMA_TO_DEVICE, &dc->client.cache);
154 if (!dc->client.group) {
156 * The display controller needs contiguous memory, so
157 * fail if the buffer is discontiguous and we fail to
158 * map its SG table to a single contiguous chunk of
159 * I/O virtual memory.
161 if (map->chunks > 1) {
166 state->iova[i] = map->phys;
168 state->iova[i] = bo->iova;
177 dev_err(dc->dev, "failed to map plane %u: %d\n", i, err);
180 host1x_bo_unpin(state->map[i]);
181 state->iova[i] = DMA_MAPPING_ERROR;
182 state->map[i] = NULL;
188 static void tegra_dc_unpin(struct tegra_dc *dc, struct tegra_plane_state *state)
192 for (i = 0; i < state->base.fb->format->num_planes; i++) {
193 host1x_bo_unpin(state->map[i]);
194 state->iova[i] = DMA_MAPPING_ERROR;
195 state->map[i] = NULL;
199 int tegra_plane_prepare_fb(struct drm_plane *plane,
200 struct drm_plane_state *state)
202 struct tegra_dc *dc = to_tegra_dc(state->crtc);
208 err = drm_gem_plane_helper_prepare_fb(plane, state);
212 return tegra_dc_pin(dc, to_tegra_plane_state(state));
215 void tegra_plane_cleanup_fb(struct drm_plane *plane,
216 struct drm_plane_state *state)
218 struct tegra_dc *dc = to_tegra_dc(state->crtc);
221 tegra_dc_unpin(dc, to_tegra_plane_state(state));
224 static int tegra_plane_calculate_memory_bandwidth(struct drm_plane_state *state)
226 struct tegra_plane_state *tegra_state = to_tegra_plane_state(state);
227 unsigned int i, bpp, dst_w, dst_h, src_w, src_h, mul;
228 const struct tegra_dc_soc_info *soc;
229 const struct drm_format_info *fmt;
230 struct drm_crtc_state *crtc_state;
231 u64 avg_bandwidth, peak_bandwidth;
236 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
240 src_w = drm_rect_width(&state->src) >> 16;
241 src_h = drm_rect_height(&state->src) >> 16;
242 dst_w = drm_rect_width(&state->dst);
243 dst_h = drm_rect_height(&state->dst);
245 fmt = state->fb->format;
246 soc = to_tegra_dc(state->crtc)->soc;
249 * Note that real memory bandwidth vary depending on format and
250 * memory layout, we are not taking that into account because small
251 * estimation error isn't important since bandwidth is rounded up
254 for (i = 0, bpp = 0; i < fmt->num_planes; i++) {
255 unsigned int bpp_plane = fmt->cpp[i] * 8;
258 * Sub-sampling is relevant for chroma planes only and vertical
259 * readouts are not cached, hence only horizontal sub-sampling
263 bpp_plane /= fmt->hsub;
268 /* average bandwidth in kbytes/sec */
269 avg_bandwidth = min(src_w, dst_w) * min(src_h, dst_h);
270 avg_bandwidth *= drm_mode_vrefresh(&crtc_state->adjusted_mode);
271 avg_bandwidth = DIV_ROUND_UP(avg_bandwidth * bpp, 8) + 999;
272 do_div(avg_bandwidth, 1000);
274 /* mode.clock in kHz, peak bandwidth in kbytes/sec */
275 peak_bandwidth = DIV_ROUND_UP(crtc_state->adjusted_mode.clock * bpp, 8);
278 * Tegra30/114 Memory Controller can't interleave DC memory requests
279 * for the tiled windows because DC uses 16-bytes atom, while DDR3
280 * uses 32-bytes atom. Hence there is x2 memory overfetch for tiled
281 * framebuffer and DDR3 on these SoCs.
283 if (soc->plane_tiled_memory_bandwidth_x2 &&
284 tegra_state->tiling.mode == TEGRA_BO_TILING_MODE_TILED)
289 /* ICC bandwidth in kbytes/sec */
290 tegra_state->peak_memory_bandwidth = kBps_to_icc(peak_bandwidth) * mul;
291 tegra_state->avg_memory_bandwidth = kBps_to_icc(avg_bandwidth) * mul;
296 int tegra_plane_state_add(struct tegra_plane *plane,
297 struct drm_plane_state *state)
299 struct drm_crtc_state *crtc_state;
300 struct tegra_dc_state *tegra;
303 /* Propagate errors from allocation or locking failures. */
304 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
305 if (IS_ERR(crtc_state))
306 return PTR_ERR(crtc_state);
308 /* Check plane state for visibility and calculate clipping bounds */
309 err = drm_atomic_helper_check_plane_state(state, crtc_state,
310 0, INT_MAX, true, true);
314 err = tegra_plane_calculate_memory_bandwidth(state);
318 tegra = to_dc_state(crtc_state);
320 tegra->planes |= WIN_A_ACT_REQ << plane->index;
325 int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
327 /* assume no swapping of fetched data */
329 *swap = BYTE_SWAP_NOSWAP;
332 case DRM_FORMAT_ARGB4444:
333 *format = WIN_COLOR_DEPTH_B4G4R4A4;
336 case DRM_FORMAT_ARGB1555:
337 *format = WIN_COLOR_DEPTH_B5G5R5A1;
340 case DRM_FORMAT_RGB565:
341 *format = WIN_COLOR_DEPTH_B5G6R5;
344 case DRM_FORMAT_RGBA5551:
345 *format = WIN_COLOR_DEPTH_A1B5G5R5;
348 case DRM_FORMAT_ARGB8888:
349 *format = WIN_COLOR_DEPTH_B8G8R8A8;
352 case DRM_FORMAT_ABGR8888:
353 *format = WIN_COLOR_DEPTH_R8G8B8A8;
356 case DRM_FORMAT_ABGR4444:
357 *format = WIN_COLOR_DEPTH_R4G4B4A4;
360 case DRM_FORMAT_ABGR1555:
361 *format = WIN_COLOR_DEPTH_R5G5B5A;
364 case DRM_FORMAT_BGRA5551:
365 *format = WIN_COLOR_DEPTH_AR5G5B5;
368 case DRM_FORMAT_XRGB1555:
369 *format = WIN_COLOR_DEPTH_B5G5R5X1;
372 case DRM_FORMAT_RGBX5551:
373 *format = WIN_COLOR_DEPTH_X1B5G5R5;
376 case DRM_FORMAT_XBGR1555:
377 *format = WIN_COLOR_DEPTH_R5G5B5X1;
380 case DRM_FORMAT_BGRX5551:
381 *format = WIN_COLOR_DEPTH_X1R5G5B5;
384 case DRM_FORMAT_BGR565:
385 *format = WIN_COLOR_DEPTH_R5G6B5;
388 case DRM_FORMAT_BGRA8888:
389 *format = WIN_COLOR_DEPTH_A8R8G8B8;
392 case DRM_FORMAT_RGBA8888:
393 *format = WIN_COLOR_DEPTH_A8B8G8R8;
396 case DRM_FORMAT_XRGB8888:
397 *format = WIN_COLOR_DEPTH_B8G8R8X8;
400 case DRM_FORMAT_XBGR8888:
401 *format = WIN_COLOR_DEPTH_R8G8B8X8;
404 case DRM_FORMAT_UYVY:
405 *format = WIN_COLOR_DEPTH_YCbCr422;
408 case DRM_FORMAT_YUYV:
412 *format = WIN_COLOR_DEPTH_YCbCr422;
413 *swap = BYTE_SWAP_SWAP2;
416 case DRM_FORMAT_YVYU:
420 *format = WIN_COLOR_DEPTH_YCbCr422;
421 *swap = BYTE_SWAP_SWAP4;
424 case DRM_FORMAT_VYUY:
428 *format = WIN_COLOR_DEPTH_YCbCr422;
429 *swap = BYTE_SWAP_SWAP4HW;
432 case DRM_FORMAT_YUV420:
433 *format = WIN_COLOR_DEPTH_YCbCr420P;
436 case DRM_FORMAT_YUV422:
437 *format = WIN_COLOR_DEPTH_YCbCr422P;
440 case DRM_FORMAT_YUV444:
441 *format = WIN_COLOR_DEPTH_YCbCr444P;
444 case DRM_FORMAT_NV12:
445 *format = WIN_COLOR_DEPTH_YCbCr420SP;
448 case DRM_FORMAT_NV21:
449 *format = WIN_COLOR_DEPTH_YCrCb420SP;
452 case DRM_FORMAT_NV16:
453 *format = WIN_COLOR_DEPTH_YCbCr422SP;
456 case DRM_FORMAT_NV61:
457 *format = WIN_COLOR_DEPTH_YCrCb422SP;
460 case DRM_FORMAT_NV24:
461 *format = WIN_COLOR_DEPTH_YCbCr444SP;
464 case DRM_FORMAT_NV42:
465 *format = WIN_COLOR_DEPTH_YCrCb444SP;
475 bool tegra_plane_format_is_indexed(unsigned int format)
478 case WIN_COLOR_DEPTH_P1:
479 case WIN_COLOR_DEPTH_P2:
480 case WIN_COLOR_DEPTH_P4:
481 case WIN_COLOR_DEPTH_P8:
488 bool tegra_plane_format_is_yuv(unsigned int format, unsigned int *planes, unsigned int *bpc)
491 case WIN_COLOR_DEPTH_YCbCr422:
492 case WIN_COLOR_DEPTH_YUV422:
501 case WIN_COLOR_DEPTH_YCbCr420P:
502 case WIN_COLOR_DEPTH_YUV420P:
503 case WIN_COLOR_DEPTH_YCbCr422P:
504 case WIN_COLOR_DEPTH_YUV422P:
505 case WIN_COLOR_DEPTH_YCbCr422R:
506 case WIN_COLOR_DEPTH_YUV422R:
507 case WIN_COLOR_DEPTH_YCbCr422RA:
508 case WIN_COLOR_DEPTH_YUV422RA:
509 case WIN_COLOR_DEPTH_YCbCr444P:
518 case WIN_COLOR_DEPTH_YCrCb420SP:
519 case WIN_COLOR_DEPTH_YCbCr420SP:
520 case WIN_COLOR_DEPTH_YCrCb422SP:
521 case WIN_COLOR_DEPTH_YCbCr422SP:
522 case WIN_COLOR_DEPTH_YCrCb444SP:
523 case WIN_COLOR_DEPTH_YCbCr444SP:
539 static bool __drm_format_has_alpha(u32 format)
542 case DRM_FORMAT_ARGB1555:
543 case DRM_FORMAT_RGBA5551:
544 case DRM_FORMAT_ABGR8888:
545 case DRM_FORMAT_ARGB8888:
552 static int tegra_plane_format_get_alpha(unsigned int opaque,
555 if (tegra_plane_format_is_yuv(opaque, NULL, NULL)) {
561 case WIN_COLOR_DEPTH_B5G5R5X1:
562 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
565 case WIN_COLOR_DEPTH_X1B5G5R5:
566 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
569 case WIN_COLOR_DEPTH_R8G8B8X8:
570 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
573 case WIN_COLOR_DEPTH_B8G8R8X8:
574 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
577 case WIN_COLOR_DEPTH_B5G6R5:
586 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
587 * be emulated using the alpha formats and alpha blending disabled.
589 static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
590 struct tegra_plane_state *state)
595 switch (state->format) {
596 case WIN_COLOR_DEPTH_B5G5R5A1:
597 case WIN_COLOR_DEPTH_A1B5G5R5:
598 case WIN_COLOR_DEPTH_R8G8B8A8:
599 case WIN_COLOR_DEPTH_B8G8R8A8:
600 state->opaque = false;
604 err = tegra_plane_format_get_alpha(state->format, &format);
608 state->format = format;
609 state->opaque = true;
616 static int tegra_plane_check_transparency(struct tegra_plane *tegra,
617 struct tegra_plane_state *state)
619 struct drm_plane_state *old, *plane_state;
620 struct drm_plane *plane;
622 old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
624 /* check if zpos / transparency changed */
625 if (old->normalized_zpos == state->base.normalized_zpos &&
626 to_tegra_plane_state(old)->opaque == state->opaque)
629 /* include all sibling planes into this commit */
630 drm_for_each_plane(plane, tegra->base.dev) {
631 struct tegra_plane *p = to_tegra_plane(plane);
633 /* skip this plane and planes on different CRTCs */
634 if (p == tegra || p->dc != tegra->dc)
637 plane_state = drm_atomic_get_plane_state(state->base.state,
639 if (IS_ERR(plane_state))
640 return PTR_ERR(plane_state);
646 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
647 struct tegra_plane *other)
649 unsigned int index = 0, i;
651 WARN_ON(plane == other);
653 for (i = 0; i < 3; i++) {
654 if (i == plane->index)
657 if (i == other->index)
666 static void tegra_plane_update_transparency(struct tegra_plane *tegra,
667 struct tegra_plane_state *state)
669 struct drm_plane_state *new;
670 struct drm_plane *plane;
673 for_each_new_plane_in_state(state->base.state, plane, new, i) {
674 struct tegra_plane *p = to_tegra_plane(plane);
677 /* skip this plane and planes on different CRTCs */
678 if (p == tegra || p->dc != tegra->dc)
681 index = tegra_plane_get_overlap_index(tegra, p);
683 if (new->fb && __drm_format_has_alpha(new->fb->format->format))
684 state->blending[index].alpha = true;
686 state->blending[index].alpha = false;
688 if (new->normalized_zpos > state->base.normalized_zpos)
689 state->blending[index].top = true;
691 state->blending[index].top = false;
694 * Missing framebuffer means that plane is disabled, in this
695 * case mark B / C window as top to be able to differentiate
696 * windows indices order in regards to zPos for the middle
697 * window X / Y registers programming.
700 state->blending[index].top = (index == 1);
704 static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
705 struct tegra_plane_state *state)
707 struct tegra_plane_state *tegra_state;
708 struct drm_plane_state *new;
709 struct drm_plane *plane;
713 * If planes zpos / transparency changed, sibling planes blending
714 * state may require adjustment and in this case they will be included
715 * into this atom commit, otherwise blending state is unchanged.
717 err = tegra_plane_check_transparency(tegra, state);
722 * All planes are now in the atomic state, walk them up and update
723 * transparency state for each plane.
725 drm_for_each_plane(plane, tegra->base.dev) {
726 struct tegra_plane *p = to_tegra_plane(plane);
728 /* skip planes on different CRTCs */
729 if (p->dc != tegra->dc)
732 new = drm_atomic_get_new_plane_state(state->base.state, plane);
733 tegra_state = to_tegra_plane_state(new);
736 * There is no need to update blending state for the disabled
740 tegra_plane_update_transparency(p, tegra_state);
746 int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
747 struct tegra_plane_state *state)
751 err = tegra_plane_setup_opacity(tegra, state);
755 err = tegra_plane_setup_transparency(tegra, state);
762 static const char * const tegra_plane_icc_names[TEGRA_DC_LEGACY_PLANES_NUM] = {
763 "wina", "winb", "winc", NULL, NULL, NULL, "cursor",
766 int tegra_plane_interconnect_init(struct tegra_plane *plane)
768 const char *icc_name = tegra_plane_icc_names[plane->index];
769 struct device *dev = plane->dc->dev;
770 struct tegra_dc *dc = plane->dc;
773 if (WARN_ON(plane->index >= TEGRA_DC_LEGACY_PLANES_NUM) ||
774 WARN_ON(!tegra_plane_icc_names[plane->index]))
777 plane->icc_mem = devm_of_icc_get(dev, icc_name);
778 err = PTR_ERR_OR_ZERO(plane->icc_mem);
780 dev_err_probe(dev, err, "failed to get %s interconnect\n",
785 /* plane B on T20/30 has a dedicated memory client for a 6-tap vertical filter */
786 if (plane->index == 1 && dc->soc->has_win_b_vfilter_mem_client) {
787 plane->icc_mem_vfilter = devm_of_icc_get(dev, "winb-vfilter");
788 err = PTR_ERR_OR_ZERO(plane->icc_mem_vfilter);
790 dev_err_probe(dev, err, "failed to get %s interconnect\n",