treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / tegra / plane.c
blobcadcdd9ea427bf7d16df10b8673130c967ee8579
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
4 */
6 #include <drm/drm_atomic.h>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_fourcc.h>
9 #include <drm/drm_gem_framebuffer_helper.h>
10 #include <drm/drm_plane_helper.h>
12 #include "dc.h"
13 #include "plane.h"
15 static void tegra_plane_destroy(struct drm_plane *plane)
17 struct tegra_plane *p = to_tegra_plane(plane);
19 drm_plane_cleanup(plane);
20 kfree(p);
23 static void tegra_plane_reset(struct drm_plane *plane)
25 struct tegra_plane *p = to_tegra_plane(plane);
26 struct tegra_plane_state *state;
27 unsigned int i;
29 if (plane->state)
30 __drm_atomic_helper_plane_destroy_state(plane->state);
32 kfree(plane->state);
33 plane->state = NULL;
35 state = kzalloc(sizeof(*state), GFP_KERNEL);
36 if (state) {
37 plane->state = &state->base;
38 plane->state->plane = plane;
39 plane->state->zpos = p->index;
40 plane->state->normalized_zpos = p->index;
42 for (i = 0; i < 3; i++)
43 state->iova[i] = DMA_MAPPING_ERROR;
47 static struct drm_plane_state *
48 tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
50 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
51 struct tegra_plane_state *copy;
52 unsigned int i;
54 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
55 if (!copy)
56 return NULL;
58 __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
59 copy->tiling = state->tiling;
60 copy->format = state->format;
61 copy->swap = state->swap;
62 copy->bottom_up = state->bottom_up;
63 copy->opaque = state->opaque;
65 for (i = 0; i < 2; i++)
66 copy->blending[i] = state->blending[i];
68 for (i = 0; i < 3; i++) {
69 copy->iova[i] = DMA_MAPPING_ERROR;
70 copy->sgt[i] = NULL;
73 return &copy->base;
76 static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
77 struct drm_plane_state *state)
79 __drm_atomic_helper_plane_destroy_state(state);
80 kfree(state);
83 static bool tegra_plane_format_mod_supported(struct drm_plane *plane,
84 uint32_t format,
85 uint64_t modifier)
87 const struct drm_format_info *info = drm_format_info(format);
89 if (modifier == DRM_FORMAT_MOD_LINEAR)
90 return true;
92 if (info->num_planes == 1)
93 return true;
95 return false;
98 const struct drm_plane_funcs tegra_plane_funcs = {
99 .update_plane = drm_atomic_helper_update_plane,
100 .disable_plane = drm_atomic_helper_disable_plane,
101 .destroy = tegra_plane_destroy,
102 .reset = tegra_plane_reset,
103 .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
104 .atomic_destroy_state = tegra_plane_atomic_destroy_state,
105 .format_mod_supported = tegra_plane_format_mod_supported,
108 static int tegra_dc_pin(struct tegra_dc *dc, struct tegra_plane_state *state)
110 unsigned int i;
111 int err;
113 for (i = 0; i < state->base.fb->format->num_planes; i++) {
114 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
116 if (!dc->client.group) {
117 struct sg_table *sgt;
119 sgt = host1x_bo_pin(dc->dev, &bo->base, NULL);
120 if (IS_ERR(sgt)) {
121 err = PTR_ERR(sgt);
122 goto unpin;
125 err = dma_map_sg(dc->dev, sgt->sgl, sgt->nents,
126 DMA_TO_DEVICE);
127 if (err == 0) {
128 err = -ENOMEM;
129 goto unpin;
133 * The display controller needs contiguous memory, so
134 * fail if the buffer is discontiguous and we fail to
135 * map its SG table to a single contiguous chunk of
136 * I/O virtual memory.
138 if (err > 1) {
139 err = -EINVAL;
140 goto unpin;
143 state->iova[i] = sg_dma_address(sgt->sgl);
144 state->sgt[i] = sgt;
145 } else {
146 state->iova[i] = bo->iova;
150 return 0;
152 unpin:
153 dev_err(dc->dev, "failed to map plane %u: %d\n", i, err);
155 while (i--) {
156 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
157 struct sg_table *sgt = state->sgt[i];
159 dma_unmap_sg(dc->dev, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
160 host1x_bo_unpin(dc->dev, &bo->base, sgt);
162 state->iova[i] = DMA_MAPPING_ERROR;
163 state->sgt[i] = NULL;
166 return err;
169 static void tegra_dc_unpin(struct tegra_dc *dc, struct tegra_plane_state *state)
171 unsigned int i;
173 for (i = 0; i < state->base.fb->format->num_planes; i++) {
174 struct tegra_bo *bo = tegra_fb_get_plane(state->base.fb, i);
176 if (!dc->client.group) {
177 struct sg_table *sgt = state->sgt[i];
179 if (sgt) {
180 dma_unmap_sg(dc->dev, sgt->sgl, sgt->nents,
181 DMA_TO_DEVICE);
182 host1x_bo_unpin(dc->dev, &bo->base, sgt);
186 state->iova[i] = DMA_MAPPING_ERROR;
187 state->sgt[i] = NULL;
191 int tegra_plane_prepare_fb(struct drm_plane *plane,
192 struct drm_plane_state *state)
194 struct tegra_dc *dc = to_tegra_dc(state->crtc);
196 if (!state->fb)
197 return 0;
199 drm_gem_fb_prepare_fb(plane, state);
201 return tegra_dc_pin(dc, to_tegra_plane_state(state));
204 void tegra_plane_cleanup_fb(struct drm_plane *plane,
205 struct drm_plane_state *state)
207 struct tegra_dc *dc = to_tegra_dc(state->crtc);
209 if (dc)
210 tegra_dc_unpin(dc, to_tegra_plane_state(state));
213 int tegra_plane_state_add(struct tegra_plane *plane,
214 struct drm_plane_state *state)
216 struct drm_crtc_state *crtc_state;
217 struct tegra_dc_state *tegra;
218 int err;
220 /* Propagate errors from allocation or locking failures. */
221 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
222 if (IS_ERR(crtc_state))
223 return PTR_ERR(crtc_state);
225 /* Check plane state for visibility and calculate clipping bounds */
226 err = drm_atomic_helper_check_plane_state(state, crtc_state,
227 0, INT_MAX, true, true);
228 if (err < 0)
229 return err;
231 tegra = to_dc_state(crtc_state);
233 tegra->planes |= WIN_A_ACT_REQ << plane->index;
235 return 0;
238 int tegra_plane_format(u32 fourcc, u32 *format, u32 *swap)
240 /* assume no swapping of fetched data */
241 if (swap)
242 *swap = BYTE_SWAP_NOSWAP;
244 switch (fourcc) {
245 case DRM_FORMAT_ARGB4444:
246 *format = WIN_COLOR_DEPTH_B4G4R4A4;
247 break;
249 case DRM_FORMAT_ARGB1555:
250 *format = WIN_COLOR_DEPTH_B5G5R5A1;
251 break;
253 case DRM_FORMAT_RGB565:
254 *format = WIN_COLOR_DEPTH_B5G6R5;
255 break;
257 case DRM_FORMAT_RGBA5551:
258 *format = WIN_COLOR_DEPTH_A1B5G5R5;
259 break;
261 case DRM_FORMAT_ARGB8888:
262 *format = WIN_COLOR_DEPTH_B8G8R8A8;
263 break;
265 case DRM_FORMAT_ABGR8888:
266 *format = WIN_COLOR_DEPTH_R8G8B8A8;
267 break;
269 case DRM_FORMAT_ABGR4444:
270 *format = WIN_COLOR_DEPTH_R4G4B4A4;
271 break;
273 case DRM_FORMAT_ABGR1555:
274 *format = WIN_COLOR_DEPTH_R5G5B5A;
275 break;
277 case DRM_FORMAT_BGRA5551:
278 *format = WIN_COLOR_DEPTH_AR5G5B5;
279 break;
281 case DRM_FORMAT_XRGB1555:
282 *format = WIN_COLOR_DEPTH_B5G5R5X1;
283 break;
285 case DRM_FORMAT_RGBX5551:
286 *format = WIN_COLOR_DEPTH_X1B5G5R5;
287 break;
289 case DRM_FORMAT_XBGR1555:
290 *format = WIN_COLOR_DEPTH_R5G5B5X1;
291 break;
293 case DRM_FORMAT_BGRX5551:
294 *format = WIN_COLOR_DEPTH_X1R5G5B5;
295 break;
297 case DRM_FORMAT_BGR565:
298 *format = WIN_COLOR_DEPTH_R5G6B5;
299 break;
301 case DRM_FORMAT_BGRA8888:
302 *format = WIN_COLOR_DEPTH_A8R8G8B8;
303 break;
305 case DRM_FORMAT_RGBA8888:
306 *format = WIN_COLOR_DEPTH_A8B8G8R8;
307 break;
309 case DRM_FORMAT_XRGB8888:
310 *format = WIN_COLOR_DEPTH_B8G8R8X8;
311 break;
313 case DRM_FORMAT_XBGR8888:
314 *format = WIN_COLOR_DEPTH_R8G8B8X8;
315 break;
317 case DRM_FORMAT_UYVY:
318 *format = WIN_COLOR_DEPTH_YCbCr422;
319 break;
321 case DRM_FORMAT_YUYV:
322 if (!swap)
323 return -EINVAL;
325 *format = WIN_COLOR_DEPTH_YCbCr422;
326 *swap = BYTE_SWAP_SWAP2;
327 break;
329 case DRM_FORMAT_YUV420:
330 *format = WIN_COLOR_DEPTH_YCbCr420P;
331 break;
333 case DRM_FORMAT_YUV422:
334 *format = WIN_COLOR_DEPTH_YCbCr422P;
335 break;
337 default:
338 return -EINVAL;
341 return 0;
344 bool tegra_plane_format_is_yuv(unsigned int format, bool *planar)
346 switch (format) {
347 case WIN_COLOR_DEPTH_YCbCr422:
348 case WIN_COLOR_DEPTH_YUV422:
349 if (planar)
350 *planar = false;
352 return true;
354 case WIN_COLOR_DEPTH_YCbCr420P:
355 case WIN_COLOR_DEPTH_YUV420P:
356 case WIN_COLOR_DEPTH_YCbCr422P:
357 case WIN_COLOR_DEPTH_YUV422P:
358 case WIN_COLOR_DEPTH_YCbCr422R:
359 case WIN_COLOR_DEPTH_YUV422R:
360 case WIN_COLOR_DEPTH_YCbCr422RA:
361 case WIN_COLOR_DEPTH_YUV422RA:
362 if (planar)
363 *planar = true;
365 return true;
368 if (planar)
369 *planar = false;
371 return false;
374 static bool __drm_format_has_alpha(u32 format)
376 switch (format) {
377 case DRM_FORMAT_ARGB1555:
378 case DRM_FORMAT_RGBA5551:
379 case DRM_FORMAT_ABGR8888:
380 case DRM_FORMAT_ARGB8888:
381 return true;
384 return false;
387 static int tegra_plane_format_get_alpha(unsigned int opaque,
388 unsigned int *alpha)
390 if (tegra_plane_format_is_yuv(opaque, NULL)) {
391 *alpha = opaque;
392 return 0;
395 switch (opaque) {
396 case WIN_COLOR_DEPTH_B5G5R5X1:
397 *alpha = WIN_COLOR_DEPTH_B5G5R5A1;
398 return 0;
400 case WIN_COLOR_DEPTH_X1B5G5R5:
401 *alpha = WIN_COLOR_DEPTH_A1B5G5R5;
402 return 0;
404 case WIN_COLOR_DEPTH_R8G8B8X8:
405 *alpha = WIN_COLOR_DEPTH_R8G8B8A8;
406 return 0;
408 case WIN_COLOR_DEPTH_B8G8R8X8:
409 *alpha = WIN_COLOR_DEPTH_B8G8R8A8;
410 return 0;
412 case WIN_COLOR_DEPTH_B5G6R5:
413 *alpha = opaque;
414 return 0;
417 return -EINVAL;
421 * This is applicable to Tegra20 and Tegra30 only where the opaque formats can
422 * be emulated using the alpha formats and alpha blending disabled.
424 static int tegra_plane_setup_opacity(struct tegra_plane *tegra,
425 struct tegra_plane_state *state)
427 unsigned int format;
428 int err;
430 switch (state->format) {
431 case WIN_COLOR_DEPTH_B5G5R5A1:
432 case WIN_COLOR_DEPTH_A1B5G5R5:
433 case WIN_COLOR_DEPTH_R8G8B8A8:
434 case WIN_COLOR_DEPTH_B8G8R8A8:
435 state->opaque = false;
436 break;
438 default:
439 err = tegra_plane_format_get_alpha(state->format, &format);
440 if (err < 0)
441 return err;
443 state->format = format;
444 state->opaque = true;
445 break;
448 return 0;
451 static int tegra_plane_check_transparency(struct tegra_plane *tegra,
452 struct tegra_plane_state *state)
454 struct drm_plane_state *old, *plane_state;
455 struct drm_plane *plane;
457 old = drm_atomic_get_old_plane_state(state->base.state, &tegra->base);
459 /* check if zpos / transparency changed */
460 if (old->normalized_zpos == state->base.normalized_zpos &&
461 to_tegra_plane_state(old)->opaque == state->opaque)
462 return 0;
464 /* include all sibling planes into this commit */
465 drm_for_each_plane(plane, tegra->base.dev) {
466 struct tegra_plane *p = to_tegra_plane(plane);
468 /* skip this plane and planes on different CRTCs */
469 if (p == tegra || p->dc != tegra->dc)
470 continue;
472 plane_state = drm_atomic_get_plane_state(state->base.state,
473 plane);
474 if (IS_ERR(plane_state))
475 return PTR_ERR(plane_state);
478 return 1;
481 static unsigned int tegra_plane_get_overlap_index(struct tegra_plane *plane,
482 struct tegra_plane *other)
484 unsigned int index = 0, i;
486 WARN_ON(plane == other);
488 for (i = 0; i < 3; i++) {
489 if (i == plane->index)
490 continue;
492 if (i == other->index)
493 break;
495 index++;
498 return index;
501 static void tegra_plane_update_transparency(struct tegra_plane *tegra,
502 struct tegra_plane_state *state)
504 struct drm_plane_state *new;
505 struct drm_plane *plane;
506 unsigned int i;
508 for_each_new_plane_in_state(state->base.state, plane, new, i) {
509 struct tegra_plane *p = to_tegra_plane(plane);
510 unsigned index;
512 /* skip this plane and planes on different CRTCs */
513 if (p == tegra || p->dc != tegra->dc)
514 continue;
516 index = tegra_plane_get_overlap_index(tegra, p);
518 if (new->fb && __drm_format_has_alpha(new->fb->format->format))
519 state->blending[index].alpha = true;
520 else
521 state->blending[index].alpha = false;
523 if (new->normalized_zpos > state->base.normalized_zpos)
524 state->blending[index].top = true;
525 else
526 state->blending[index].top = false;
529 * Missing framebuffer means that plane is disabled, in this
530 * case mark B / C window as top to be able to differentiate
531 * windows indices order in regards to zPos for the middle
532 * window X / Y registers programming.
534 if (!new->fb)
535 state->blending[index].top = (index == 1);
539 static int tegra_plane_setup_transparency(struct tegra_plane *tegra,
540 struct tegra_plane_state *state)
542 struct tegra_plane_state *tegra_state;
543 struct drm_plane_state *new;
544 struct drm_plane *plane;
545 int err;
548 * If planes zpos / transparency changed, sibling planes blending
549 * state may require adjustment and in this case they will be included
550 * into this atom commit, otherwise blending state is unchanged.
552 err = tegra_plane_check_transparency(tegra, state);
553 if (err <= 0)
554 return err;
557 * All planes are now in the atomic state, walk them up and update
558 * transparency state for each plane.
560 drm_for_each_plane(plane, tegra->base.dev) {
561 struct tegra_plane *p = to_tegra_plane(plane);
563 /* skip planes on different CRTCs */
564 if (p->dc != tegra->dc)
565 continue;
567 new = drm_atomic_get_new_plane_state(state->base.state, plane);
568 tegra_state = to_tegra_plane_state(new);
571 * There is no need to update blending state for the disabled
572 * plane.
574 if (new->fb)
575 tegra_plane_update_transparency(p, tegra_state);
578 return 0;
581 int tegra_plane_setup_legacy_state(struct tegra_plane *tegra,
582 struct tegra_plane_state *state)
584 int err;
586 err = tegra_plane_setup_opacity(tegra, state);
587 if (err < 0)
588 return err;
590 err = tegra_plane_setup_transparency(tegra, state);
591 if (err < 0)
592 return err;
594 return 0;