Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / tegra / plane.c
blob539d14935728ed7e80405268f416c6eea6adfd55
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
4 */
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>
14 #include "dc.h"
15 #include "plane.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);
22 kfree(p);
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;
29 unsigned int i;
31 if (plane->state)
32 __drm_atomic_helper_plane_destroy_state(plane->state);
34 kfree(plane->state);
35 plane->state = NULL;
37 state = kzalloc(sizeof(*state), GFP_KERNEL);
38 if (state) {
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;
54 unsigned int i;
56 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
57 if (!copy)
58 return NULL;
60 __drm_atomic_helper_plane_duplicate_state(plane, &copy->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;
73 copy->sgt[i] = NULL;
76 return &copy->base;
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);
83 kfree(state);
86 static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
87 uint32_t format,
88 uint64_t modifier)
90 const struct drm_format_info *info = drm_format_info(format);
92 if (modifier == DRM_FORMAT_MOD_LINEAR)
93 return true;
95 if (info->num_planes == 1)
96 return true;
98 return false;
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);
114 unsigned int i;
115 int err;
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)
123 phys = &phys_addr;
124 else
125 phys = NULL;
127 sgt = host1x_bo_pin(dc->dev, &bo->base, phys);
128 if (IS_ERR(sgt)) {
129 err = PTR_ERR(sgt);
130 goto unpin;
133 if (sgt) {
134 err = dma_map_sgtable(dc->dev, sgt, DMA_TO_DEVICE, 0);
135 if (err)
136 goto unpin;
139 * The display controller needs contiguous memory, so
140 * fail if the buffer is discontiguous and we fail to
141 * map its SG table to a single contiguous chunk of
142 * I/O virtual memory.
144 if (sgt->nents > 1) {
145 err = -EINVAL;
146 goto unpin;
149 state->iova[i] = sg_dma_address(sgt->sgl);
150 state->sgt[i] = sgt;
151 } else {
152 state->iova[i] = phys_addr;
156 return 0;
158 unpin:
159 dev_err(dc->dev, "failed to map plane %u: %d\n", i, err);
161 while (i--) {
162 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
163 struct sg_table *sgt = state->sgt[i];
165 if (sgt)
166 dma_unmap_sgtable(dc->dev, sgt, DMA_TO_DEVICE, 0);
168 host1x_bo_unpin(dc->dev, &bo->base, sgt);
169 state->iova[i] = DMA_MAPPING_ERROR;
170 state->sgt[i] = NULL;
173 return err;
176 static void tegra_dc_unpin(struct tegra_dc *dc, struct tegra_plane_state *state)
178 unsigned int i;
180 for (i = 0; i < state->base.fb->format->num_planes; i++) {
181 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
182 struct sg_table *sgt = state->sgt[i];
184 if (sgt)
185 dma_unmap_sgtable(dc->dev, sgt, DMA_TO_DEVICE, 0);
187 host1x_bo_unpin(dc->dev, &bo->base, sgt);
188 state->iova[i] = DMA_MAPPING_ERROR;
189 state->sgt[i] = NULL;
193 int tegra_plane_prepare_fb(struct drm_plane *plane,
194 struct drm_plane_state *state)
196 struct tegra_dc *dc = to_tegra_dc(state->crtc);
198 if (!state->fb)
199 return 0;
201 drm_gem_fb_prepare_fb(plane, state);
203 return tegra_dc_pin(dc, to_tegra_plane_state(state));
206 void tegra_plane_cleanup_fb(struct drm_plane *plane,
207 struct drm_plane_state *state)
209 struct tegra_dc *dc = to_tegra_dc(state->crtc);
211 if (dc)
212 tegra_dc_unpin(dc, to_tegra_plane_state(state));
215 int tegra_plane_state_add(struct tegra_plane *plane,
216 struct drm_plane_state *state)
218 struct drm_crtc_state *crtc_state;
219 struct tegra_dc_state *tegra;
220 int err;
222 /* Propagate errors from allocation or locking failures. */
223 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
224 if (IS_ERR(crtc_state))
225 return PTR_ERR(crtc_state);
227 /* Check plane state for visibility and calculate clipping bounds */
228 err = drm_atomic_helper_check_plane_state(state, crtc_state,
229 0, INT_MAX, true, true);
230 if (err < 0)
231 return err;
233 tegra = to_dc_state(crtc_state);
235 tegra->planes |= WIN_A_ACT_REQ << plane->index;
237 return 0;
240 int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
242 /* assume no swapping of fetched data */
243 if (swap)
244 *swap = BYTE_SWAP_NOSWAP;
246 switch (fourcc) {
247 case DRM_FORMAT_ARGB4444:
248 *format = WIN_COLOR_DEPTH_B4G4R4A4;
249 break;
251 case DRM_FORMAT_ARGB1555:
252 *format = WIN_COLOR_DEPTH_B5G5R5A1;
253 break;
255 case DRM_FORMAT_RGB565:
256 *format = WIN_COLOR_DEPTH_B5G6R5;
257 break;
259 case DRM_FORMAT_RGBA5551:
260 *format = WIN_COLOR_DEPTH_A1B5G5R5;
261 break;
263 case DRM_FORMAT_ARGB8888:
264 *format = WIN_COLOR_DEPTH_B8G8R8A8;
265 break;
267 case DRM_FORMAT_ABGR8888:
268 *format = WIN_COLOR_DEPTH_R8G8B8A8;
269 break;
271 case DRM_FORMAT_ABGR4444:
272 *format = WIN_COLOR_DEPTH_R4G4B4A4;
273 break;
275 case DRM_FORMAT_ABGR1555:
276 *format = WIN_COLOR_DEPTH_R5G5B5A;
277 break;
279 case DRM_FORMAT_BGRA5551:
280 *format = WIN_COLOR_DEPTH_AR5G5B5;
281 break;
283 case DRM_FORMAT_XRGB1555:
284 *format = WIN_COLOR_DEPTH_B5G5R5X1;
285 break;
287 case DRM_FORMAT_RGBX5551:
288 *format = WIN_COLOR_DEPTH_X1B5G5R5;
289 break;
291 case DRM_FORMAT_XBGR1555:
292 *format = WIN_COLOR_DEPTH_R5G5B5X1;
293 break;
295 case DRM_FORMAT_BGRX5551:
296 *format = WIN_COLOR_DEPTH_X1R5G5B5;
297 break;
299 case DRM_FORMAT_BGR565:
300 *format = WIN_COLOR_DEPTH_R5G6B5;
301 break;
303 case DRM_FORMAT_BGRA8888:
304 *format = WIN_COLOR_DEPTH_A8R8G8B8;
305 break;
307 case DRM_FORMAT_RGBA8888:
308 *format = WIN_COLOR_DEPTH_A8B8G8R8;
309 break;
311 case DRM_FORMAT_XRGB8888:
312 *format = WIN_COLOR_DEPTH_B8G8R8X8;
313 break;
315 case DRM_FORMAT_XBGR8888:
316 *format = WIN_COLOR_DEPTH_R8G8B8X8;
317 break;
319 case DRM_FORMAT_UYVY:
320 *format = WIN_COLOR_DEPTH_YCbCr422;
321 break;
323 case DRM_FORMAT_YUYV:
324 if (!swap)
325 return -EINVAL;
327 *format = WIN_COLOR_DEPTH_YCbCr422;
328 *swap = BYTE_SWAP_SWAP2;
329 break;
331 case DRM_FORMAT_YUV420:
332 *format = WIN_COLOR_DEPTH_YCbCr420P;
333 break;
335 case DRM_FORMAT_YUV422:
336 *format = WIN_COLOR_DEPTH_YCbCr422P;
337 break;
339 default:
340 return -EINVAL;
343 return 0;
346 bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
348 switch (format) {
349 case WIN_COLOR_DEPTH_YCbCr422:
350 case WIN_COLOR_DEPTH_YUV422:
351 if (planar)
352 *planar = false;
354 return true;
356 case WIN_COLOR_DEPTH_YCbCr420P:
357 case WIN_COLOR_DEPTH_YUV420P:
358 case WIN_COLOR_DEPTH_YCbCr422P:
359 case WIN_COLOR_DEPTH_YUV422P:
360 case WIN_COLOR_DEPTH_YCbCr422R:
361 case WIN_COLOR_DEPTH_YUV422R:
362 case WIN_COLOR_DEPTH_YCbCr422RA:
363 case WIN_COLOR_DEPTH_YUV422RA:
364 if (planar)
365 *planar = true;
367 return true;
370 if (planar)
371 *planar = false;
373 return false;
376 static bool __drm_format_has_alpha(u32 format)
378 switch (format) {
379 case DRM_FORMAT_ARGB1555:
380 case DRM_FORMAT_RGBA5551:
381 case DRM_FORMAT_ABGR8888:
382 case DRM_FORMAT_ARGB8888:
383 return true;
386 return false;
389 static int tegra_plane_format_get_alpha(unsigned int opaque,
390 unsigned int *alpha)
392 if (tegra_plane_format_is_yuv(opaque, NULL)) {
393 *alpha = opaque;
394 return 0;
397 switch (opaque) {
398 case WIN_COLOR_DEPTH_B5G5R5X1:
399 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
400 return 0;
402 case WIN_COLOR_DEPTH_X1B5G5R5:
403 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
404 return 0;
406 case WIN_COLOR_DEPTH_R8G8B8X8:
407 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
408 return 0;
410 case WIN_COLOR_DEPTH_B8G8R8X8:
411 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
412 return 0;
414 case WIN_COLOR_DEPTH_B5G6R5:
415 *alpha = opaque;
416 return 0;
419 return -EINVAL;
423 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
424 * be emulated using the alpha formats and alpha blending disabled.
426 static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
427 struct tegra_plane_state *state)
429 unsigned int format;
430 int err;
432 switch (state->format) {
433 case WIN_COLOR_DEPTH_B5G5R5A1:
434 case WIN_COLOR_DEPTH_A1B5G5R5:
435 case WIN_COLOR_DEPTH_R8G8B8A8:
436 case WIN_COLOR_DEPTH_B8G8R8A8:
437 state->opaque = false;
438 break;
440 default:
441 err = tegra_plane_format_get_alpha(state->format, &format);
442 if (err < 0)
443 return err;
445 state->format = format;
446 state->opaque = true;
447 break;
450 return 0;
453 static int tegra_plane_check_transparency(struct tegra_plane *tegra,
454 struct tegra_plane_state *state)
456 struct drm_plane_state *old, *plane_state;
457 struct drm_plane *plane;
459 old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
461 /* check if zpos / transparency changed */
462 if (old->normalized_zpos == state->base.normalized_zpos &&
463 to_tegra_plane_state(old)->opaque == state->opaque)
464 return 0;
466 /* include all sibling planes into this commit */
467 drm_for_each_plane(plane, tegra->base.dev) {
468 struct tegra_plane *p = to_tegra_plane(plane);
470 /* skip this plane and planes on different CRTCs */
471 if (p == tegra || p->dc != tegra->dc)
472 continue;
474 plane_state = drm_atomic_get_plane_state(state->base.state,
475 plane);
476 if (IS_ERR(plane_state))
477 return PTR_ERR(plane_state);
480 return 1;
483 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
484 struct tegra_plane *other)
486 unsigned int index = 0, i;
488 WARN_ON(plane == other);
490 for (i = 0; i < 3; i++) {
491 if (i == plane->index)
492 continue;
494 if (i == other->index)
495 break;
497 index++;
500 return index;
503 static void tegra_plane_update_transparency(struct tegra_plane *tegra,
504 struct tegra_plane_state *state)
506 struct drm_plane_state *new;
507 struct drm_plane *plane;
508 unsigned int i;
510 for_each_new_plane_in_state(state->base.state, plane, new, i) {
511 struct tegra_plane *p = to_tegra_plane(plane);
512 unsigned index;
514 /* skip this plane and planes on different CRTCs */
515 if (p == tegra || p->dc != tegra->dc)
516 continue;
518 index = tegra_plane_get_overlap_index(tegra, p);
520 if (new->fb && __drm_format_has_alpha(new->fb->format->format))
521 state->blending[index].alpha = true;
522 else
523 state->blending[index].alpha = false;
525 if (new->normalized_zpos > state->base.normalized_zpos)
526 state->blending[index].top = true;
527 else
528 state->blending[index].top = false;
531 * Missing framebuffer means that plane is disabled, in this
532 * case mark B / C window as top to be able to differentiate
533 * windows indices order in regards to zPos for the middle
534 * window X / Y registers programming.
536 if (!new->fb)
537 state->blending[index].top = (index == 1);
541 static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
542 struct tegra_plane_state *state)
544 struct tegra_plane_state *tegra_state;
545 struct drm_plane_state *new;
546 struct drm_plane *plane;
547 int err;
550 * If planes zpos / transparency changed, sibling planes blending
551 * state may require adjustment and in this case they will be included
552 * into this atom commit, otherwise blending state is unchanged.
554 err = tegra_plane_check_transparency(tegra, state);
555 if (err <= 0)
556 return err;
559 * All planes are now in the atomic state, walk them up and update
560 * transparency state for each plane.
562 drm_for_each_plane(plane, tegra->base.dev) {
563 struct tegra_plane *p = to_tegra_plane(plane);
565 /* skip planes on different CRTCs */
566 if (p->dc != tegra->dc)
567 continue;
569 new = drm_atomic_get_new_plane_state(state->base.state, plane);
570 tegra_state = to_tegra_plane_state(new);
573 * There is no need to update blending state for the disabled
574 * plane.
576 if (new->fb)
577 tegra_plane_update_transparency(p, tegra_state);
580 return 0;
583 int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
584 struct tegra_plane_state *state)
586 int err;
588 err = tegra_plane_setup_opacity(tegra, state);
589 if (err < 0)
590 return err;
592 err = tegra_plane_setup_transparency(tegra, state);
593 if (err < 0)
594 return err;
596 return 0;