]> Git Repo - linux.git/blob - drivers/gpu/drm/imx/ipuv3-plane.c
Merge drm/drm-next into drm-intel-gt-next
[linux.git] / drivers / gpu / drm / imx / ipuv3-plane.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * i.MX IPUv3 DP Overlay Planes
4  *
5  * Copyright (C) 2013 Philipp Zabel, Pengutronix
6  */
7
8 #include <drm/drm_atomic.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_fb_cma_helper.h>
11 #include <drm/drm_fourcc.h>
12 #include <drm/drm_gem_cma_helper.h>
13 #include <drm/drm_gem_framebuffer_helper.h>
14 #include <drm/drm_managed.h>
15 #include <drm/drm_plane_helper.h>
16
17 #include <video/imx-ipu-v3.h>
18
19 #include "imx-drm.h"
20 #include "ipuv3-plane.h"
21
22 struct ipu_plane_state {
23         struct drm_plane_state base;
24         bool use_pre;
25 };
26
27 static inline struct ipu_plane_state *
28 to_ipu_plane_state(struct drm_plane_state *p)
29 {
30         return container_of(p, struct ipu_plane_state, base);
31 }
32
33 static inline struct ipu_plane *to_ipu_plane(struct drm_plane *p)
34 {
35         return container_of(p, struct ipu_plane, base);
36 }
37
38 static const uint32_t ipu_plane_formats[] = {
39         DRM_FORMAT_ARGB1555,
40         DRM_FORMAT_XRGB1555,
41         DRM_FORMAT_ABGR1555,
42         DRM_FORMAT_XBGR1555,
43         DRM_FORMAT_RGBA5551,
44         DRM_FORMAT_BGRA5551,
45         DRM_FORMAT_ARGB4444,
46         DRM_FORMAT_ARGB8888,
47         DRM_FORMAT_XRGB8888,
48         DRM_FORMAT_ABGR8888,
49         DRM_FORMAT_XBGR8888,
50         DRM_FORMAT_RGBA8888,
51         DRM_FORMAT_RGBX8888,
52         DRM_FORMAT_BGRA8888,
53         DRM_FORMAT_BGRX8888,
54         DRM_FORMAT_UYVY,
55         DRM_FORMAT_VYUY,
56         DRM_FORMAT_YUYV,
57         DRM_FORMAT_YVYU,
58         DRM_FORMAT_YUV420,
59         DRM_FORMAT_YVU420,
60         DRM_FORMAT_YUV422,
61         DRM_FORMAT_YVU422,
62         DRM_FORMAT_YUV444,
63         DRM_FORMAT_YVU444,
64         DRM_FORMAT_NV12,
65         DRM_FORMAT_NV16,
66         DRM_FORMAT_RGB565,
67         DRM_FORMAT_RGB565_A8,
68         DRM_FORMAT_BGR565_A8,
69         DRM_FORMAT_RGB888_A8,
70         DRM_FORMAT_BGR888_A8,
71         DRM_FORMAT_RGBX8888_A8,
72         DRM_FORMAT_BGRX8888_A8,
73 };
74
75 static const uint64_t ipu_format_modifiers[] = {
76         DRM_FORMAT_MOD_LINEAR,
77         DRM_FORMAT_MOD_INVALID
78 };
79
80 static const uint64_t pre_format_modifiers[] = {
81         DRM_FORMAT_MOD_LINEAR,
82         DRM_FORMAT_MOD_VIVANTE_TILED,
83         DRM_FORMAT_MOD_VIVANTE_SUPER_TILED,
84         DRM_FORMAT_MOD_INVALID
85 };
86
87 int ipu_plane_irq(struct ipu_plane *ipu_plane)
88 {
89         return ipu_idmac_channel_irq(ipu_plane->ipu, ipu_plane->ipu_ch,
90                                      IPU_IRQ_EOF);
91 }
92
93 static inline unsigned long
94 drm_plane_state_to_eba(struct drm_plane_state *state, int plane)
95 {
96         struct drm_framebuffer *fb = state->fb;
97         struct drm_gem_cma_object *cma_obj;
98         int x = state->src.x1 >> 16;
99         int y = state->src.y1 >> 16;
100
101         cma_obj = drm_fb_cma_get_gem_obj(fb, plane);
102         BUG_ON(!cma_obj);
103
104         return cma_obj->paddr + fb->offsets[plane] + fb->pitches[plane] * y +
105                fb->format->cpp[plane] * x;
106 }
107
108 static inline unsigned long
109 drm_plane_state_to_ubo(struct drm_plane_state *state)
110 {
111         struct drm_framebuffer *fb = state->fb;
112         struct drm_gem_cma_object *cma_obj;
113         unsigned long eba = drm_plane_state_to_eba(state, 0);
114         int x = state->src.x1 >> 16;
115         int y = state->src.y1 >> 16;
116
117         cma_obj = drm_fb_cma_get_gem_obj(fb, 1);
118         BUG_ON(!cma_obj);
119
120         x /= fb->format->hsub;
121         y /= fb->format->vsub;
122
123         return cma_obj->paddr + fb->offsets[1] + fb->pitches[1] * y +
124                fb->format->cpp[1] * x - eba;
125 }
126
127 static inline unsigned long
128 drm_plane_state_to_vbo(struct drm_plane_state *state)
129 {
130         struct drm_framebuffer *fb = state->fb;
131         struct drm_gem_cma_object *cma_obj;
132         unsigned long eba = drm_plane_state_to_eba(state, 0);
133         int x = state->src.x1 >> 16;
134         int y = state->src.y1 >> 16;
135
136         cma_obj = drm_fb_cma_get_gem_obj(fb, 2);
137         BUG_ON(!cma_obj);
138
139         x /= fb->format->hsub;
140         y /= fb->format->vsub;
141
142         return cma_obj->paddr + fb->offsets[2] + fb->pitches[2] * y +
143                fb->format->cpp[2] * x - eba;
144 }
145
146 static void ipu_plane_put_resources(struct drm_device *dev, void *ptr)
147 {
148         struct ipu_plane *ipu_plane = ptr;
149
150         if (!IS_ERR_OR_NULL(ipu_plane->dp))
151                 ipu_dp_put(ipu_plane->dp);
152         if (!IS_ERR_OR_NULL(ipu_plane->dmfc))
153                 ipu_dmfc_put(ipu_plane->dmfc);
154         if (!IS_ERR_OR_NULL(ipu_plane->ipu_ch))
155                 ipu_idmac_put(ipu_plane->ipu_ch);
156         if (!IS_ERR_OR_NULL(ipu_plane->alpha_ch))
157                 ipu_idmac_put(ipu_plane->alpha_ch);
158 }
159
160 static int ipu_plane_get_resources(struct drm_device *dev,
161                                    struct ipu_plane *ipu_plane)
162 {
163         int ret;
164         int alpha_ch;
165
166         ipu_plane->ipu_ch = ipu_idmac_get(ipu_plane->ipu, ipu_plane->dma);
167         if (IS_ERR(ipu_plane->ipu_ch)) {
168                 ret = PTR_ERR(ipu_plane->ipu_ch);
169                 DRM_ERROR("failed to get idmac channel: %d\n", ret);
170                 return ret;
171         }
172
173         ret = drmm_add_action_or_reset(dev, ipu_plane_put_resources, ipu_plane);
174         if (ret)
175                 return ret;
176
177         alpha_ch = ipu_channel_alpha_channel(ipu_plane->dma);
178         if (alpha_ch >= 0) {
179                 ipu_plane->alpha_ch = ipu_idmac_get(ipu_plane->ipu, alpha_ch);
180                 if (IS_ERR(ipu_plane->alpha_ch)) {
181                         ret = PTR_ERR(ipu_plane->alpha_ch);
182                         DRM_ERROR("failed to get alpha idmac channel %d: %d\n",
183                                   alpha_ch, ret);
184                         return ret;
185                 }
186         }
187
188         ipu_plane->dmfc = ipu_dmfc_get(ipu_plane->ipu, ipu_plane->dma);
189         if (IS_ERR(ipu_plane->dmfc)) {
190                 ret = PTR_ERR(ipu_plane->dmfc);
191                 DRM_ERROR("failed to get dmfc: ret %d\n", ret);
192                 return ret;
193         }
194
195         if (ipu_plane->dp_flow >= 0) {
196                 ipu_plane->dp = ipu_dp_get(ipu_plane->ipu, ipu_plane->dp_flow);
197                 if (IS_ERR(ipu_plane->dp)) {
198                         ret = PTR_ERR(ipu_plane->dp);
199                         DRM_ERROR("failed to get dp flow: %d\n", ret);
200                         return ret;
201                 }
202         }
203
204         return 0;
205 }
206
207 static bool ipu_plane_separate_alpha(struct ipu_plane *ipu_plane)
208 {
209         switch (ipu_plane->base.state->fb->format->format) {
210         case DRM_FORMAT_RGB565_A8:
211         case DRM_FORMAT_BGR565_A8:
212         case DRM_FORMAT_RGB888_A8:
213         case DRM_FORMAT_BGR888_A8:
214         case DRM_FORMAT_RGBX8888_A8:
215         case DRM_FORMAT_BGRX8888_A8:
216                 return true;
217         default:
218                 return false;
219         }
220 }
221
222 static void ipu_plane_enable(struct ipu_plane *ipu_plane)
223 {
224         if (ipu_plane->dp)
225                 ipu_dp_enable(ipu_plane->ipu);
226         ipu_dmfc_enable_channel(ipu_plane->dmfc);
227         ipu_idmac_enable_channel(ipu_plane->ipu_ch);
228         if (ipu_plane_separate_alpha(ipu_plane))
229                 ipu_idmac_enable_channel(ipu_plane->alpha_ch);
230         if (ipu_plane->dp)
231                 ipu_dp_enable_channel(ipu_plane->dp);
232 }
233
234 void ipu_plane_disable(struct ipu_plane *ipu_plane, bool disable_dp_channel)
235 {
236         int ret;
237
238         DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
239
240         ret = ipu_idmac_wait_busy(ipu_plane->ipu_ch, 50);
241         if (ret == -ETIMEDOUT) {
242                 DRM_ERROR("[PLANE:%d] IDMAC timeout\n",
243                           ipu_plane->base.base.id);
244         }
245
246         if (ipu_plane->dp && disable_dp_channel)
247                 ipu_dp_disable_channel(ipu_plane->dp, false);
248         ipu_idmac_disable_channel(ipu_plane->ipu_ch);
249         if (ipu_plane->alpha_ch)
250                 ipu_idmac_disable_channel(ipu_plane->alpha_ch);
251         ipu_dmfc_disable_channel(ipu_plane->dmfc);
252         if (ipu_plane->dp)
253                 ipu_dp_disable(ipu_plane->ipu);
254         if (ipu_prg_present(ipu_plane->ipu))
255                 ipu_prg_channel_disable(ipu_plane->ipu_ch);
256 }
257
258 void ipu_plane_disable_deferred(struct drm_plane *plane)
259 {
260         struct ipu_plane *ipu_plane = to_ipu_plane(plane);
261
262         if (ipu_plane->disabling) {
263                 ipu_plane->disabling = false;
264                 ipu_plane_disable(ipu_plane, false);
265         }
266 }
267 EXPORT_SYMBOL_GPL(ipu_plane_disable_deferred);
268
269 static void ipu_plane_state_reset(struct drm_plane *plane)
270 {
271         unsigned int zpos = (plane->type == DRM_PLANE_TYPE_PRIMARY) ? 0 : 1;
272         struct ipu_plane_state *ipu_state;
273
274         if (plane->state) {
275                 ipu_state = to_ipu_plane_state(plane->state);
276                 __drm_atomic_helper_plane_destroy_state(plane->state);
277                 kfree(ipu_state);
278                 plane->state = NULL;
279         }
280
281         ipu_state = kzalloc(sizeof(*ipu_state), GFP_KERNEL);
282
283         if (ipu_state) {
284                 __drm_atomic_helper_plane_reset(plane, &ipu_state->base);
285                 ipu_state->base.zpos = zpos;
286                 ipu_state->base.normalized_zpos = zpos;
287         }
288 }
289
290 static struct drm_plane_state *
291 ipu_plane_duplicate_state(struct drm_plane *plane)
292 {
293         struct ipu_plane_state *state;
294
295         if (WARN_ON(!plane->state))
296                 return NULL;
297
298         state = kmalloc(sizeof(*state), GFP_KERNEL);
299         if (state)
300                 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
301
302         return &state->base;
303 }
304
305 static void ipu_plane_destroy_state(struct drm_plane *plane,
306                                     struct drm_plane_state *state)
307 {
308         struct ipu_plane_state *ipu_state = to_ipu_plane_state(state);
309
310         __drm_atomic_helper_plane_destroy_state(state);
311         kfree(ipu_state);
312 }
313
314 static bool ipu_plane_format_mod_supported(struct drm_plane *plane,
315                                            uint32_t format, uint64_t modifier)
316 {
317         struct ipu_soc *ipu = to_ipu_plane(plane)->ipu;
318
319         /* linear is supported for all planes and formats */
320         if (modifier == DRM_FORMAT_MOD_LINEAR)
321                 return true;
322
323         /* without a PRG there are no supported modifiers */
324         if (!ipu_prg_present(ipu))
325                 return false;
326
327         return ipu_prg_format_supported(ipu, format, modifier);
328 }
329
330 static const struct drm_plane_funcs ipu_plane_funcs = {
331         .update_plane   = drm_atomic_helper_update_plane,
332         .disable_plane  = drm_atomic_helper_disable_plane,
333         .reset          = ipu_plane_state_reset,
334         .atomic_duplicate_state = ipu_plane_duplicate_state,
335         .atomic_destroy_state   = ipu_plane_destroy_state,
336         .format_mod_supported = ipu_plane_format_mod_supported,
337 };
338
339 static int ipu_plane_atomic_check(struct drm_plane *plane,
340                                   struct drm_plane_state *state)
341 {
342         struct drm_plane_state *old_state = plane->state;
343         struct drm_crtc_state *crtc_state;
344         struct device *dev = plane->dev->dev;
345         struct drm_framebuffer *fb = state->fb;
346         struct drm_framebuffer *old_fb = old_state->fb;
347         unsigned long eba, ubo, vbo, old_ubo, old_vbo, alpha_eba;
348         bool can_position = (plane->type == DRM_PLANE_TYPE_OVERLAY);
349         int ret;
350
351         /* Ok to disable */
352         if (!fb)
353                 return 0;
354
355         if (WARN_ON(!state->crtc))
356                 return -EINVAL;
357
358         crtc_state =
359                 drm_atomic_get_existing_crtc_state(state->state, state->crtc);
360         if (WARN_ON(!crtc_state))
361                 return -EINVAL;
362
363         ret = drm_atomic_helper_check_plane_state(state, crtc_state,
364                                                   DRM_PLANE_HELPER_NO_SCALING,
365                                                   DRM_PLANE_HELPER_NO_SCALING,
366                                                   can_position, true);
367         if (ret)
368                 return ret;
369
370         /* nothing to check when disabling or disabled */
371         if (!crtc_state->enable)
372                 return 0;
373
374         switch (plane->type) {
375         case DRM_PLANE_TYPE_PRIMARY:
376                 /* full plane minimum width is 13 pixels */
377                 if (drm_rect_width(&state->dst) < 13)
378                         return -EINVAL;
379                 break;
380         case DRM_PLANE_TYPE_OVERLAY:
381                 break;
382         default:
383                 dev_warn(dev, "Unsupported plane type %d\n", plane->type);
384                 return -EINVAL;
385         }
386
387         if (drm_rect_height(&state->dst) < 2)
388                 return -EINVAL;
389
390         /*
391          * We support resizing active plane or changing its format by
392          * forcing CRTC mode change in plane's ->atomic_check callback
393          * and disabling all affected active planes in CRTC's ->atomic_disable
394          * callback.  The planes will be reenabled in plane's ->atomic_update
395          * callback.
396          */
397         if (old_fb &&
398             (drm_rect_width(&state->dst) != drm_rect_width(&old_state->dst) ||
399              drm_rect_height(&state->dst) != drm_rect_height(&old_state->dst) ||
400              fb->format != old_fb->format))
401                 crtc_state->mode_changed = true;
402
403         eba = drm_plane_state_to_eba(state, 0);
404
405         if (eba & 0x7)
406                 return -EINVAL;
407
408         if (fb->pitches[0] < 1 || fb->pitches[0] > 16384)
409                 return -EINVAL;
410
411         if (old_fb && fb->pitches[0] != old_fb->pitches[0])
412                 crtc_state->mode_changed = true;
413
414         switch (fb->format->format) {
415         case DRM_FORMAT_YUV420:
416         case DRM_FORMAT_YVU420:
417         case DRM_FORMAT_YUV422:
418         case DRM_FORMAT_YVU422:
419         case DRM_FORMAT_YUV444:
420         case DRM_FORMAT_YVU444:
421                 /*
422                  * Multiplanar formats have to meet the following restrictions:
423                  * - The (up to) three plane addresses are EBA, EBA+UBO, EBA+VBO
424                  * - EBA, UBO and VBO are a multiple of 8
425                  * - UBO and VBO are unsigned and not larger than 0xfffff8
426                  * - Only EBA may be changed while scanout is active
427                  * - The strides of U and V planes must be identical.
428                  */
429                 vbo = drm_plane_state_to_vbo(state);
430
431                 if (vbo & 0x7 || vbo > 0xfffff8)
432                         return -EINVAL;
433
434                 if (old_fb && (fb->format == old_fb->format)) {
435                         old_vbo = drm_plane_state_to_vbo(old_state);
436                         if (vbo != old_vbo)
437                                 crtc_state->mode_changed = true;
438                 }
439
440                 if (fb->pitches[1] != fb->pitches[2])
441                         return -EINVAL;
442
443                 fallthrough;
444         case DRM_FORMAT_NV12:
445         case DRM_FORMAT_NV16:
446                 ubo = drm_plane_state_to_ubo(state);
447
448                 if (ubo & 0x7 || ubo > 0xfffff8)
449                         return -EINVAL;
450
451                 if (old_fb && (fb->format == old_fb->format)) {
452                         old_ubo = drm_plane_state_to_ubo(old_state);
453                         if (ubo != old_ubo)
454                                 crtc_state->mode_changed = true;
455                 }
456
457                 if (fb->pitches[1] < 1 || fb->pitches[1] > 16384)
458                         return -EINVAL;
459
460                 if (old_fb && old_fb->pitches[1] != fb->pitches[1])
461                         crtc_state->mode_changed = true;
462
463                 /*
464                  * The x/y offsets must be even in case of horizontal/vertical
465                  * chroma subsampling.
466                  */
467                 if (((state->src.x1 >> 16) & (fb->format->hsub - 1)) ||
468                     ((state->src.y1 >> 16) & (fb->format->vsub - 1)))
469                         return -EINVAL;
470                 break;
471         case DRM_FORMAT_RGB565_A8:
472         case DRM_FORMAT_BGR565_A8:
473         case DRM_FORMAT_RGB888_A8:
474         case DRM_FORMAT_BGR888_A8:
475         case DRM_FORMAT_RGBX8888_A8:
476         case DRM_FORMAT_BGRX8888_A8:
477                 alpha_eba = drm_plane_state_to_eba(state, 1);
478                 if (alpha_eba & 0x7)
479                         return -EINVAL;
480
481                 if (fb->pitches[1] < 1 || fb->pitches[1] > 16384)
482                         return -EINVAL;
483
484                 if (old_fb && old_fb->pitches[1] != fb->pitches[1])
485                         crtc_state->mode_changed = true;
486                 break;
487         }
488
489         return 0;
490 }
491
492 static void ipu_plane_atomic_disable(struct drm_plane *plane,
493                                      struct drm_plane_state *old_state)
494 {
495         struct ipu_plane *ipu_plane = to_ipu_plane(plane);
496
497         if (ipu_plane->dp)
498                 ipu_dp_disable_channel(ipu_plane->dp, true);
499         ipu_plane->disabling = true;
500 }
501
502 static int ipu_chan_assign_axi_id(int ipu_chan)
503 {
504         switch (ipu_chan) {
505         case IPUV3_CHANNEL_MEM_BG_SYNC:
506                 return 1;
507         case IPUV3_CHANNEL_MEM_FG_SYNC:
508                 return 2;
509         case IPUV3_CHANNEL_MEM_DC_SYNC:
510                 return 3;
511         default:
512                 return 0;
513         }
514 }
515
516 static void ipu_calculate_bursts(u32 width, u32 cpp, u32 stride,
517                                  u8 *burstsize, u8 *num_bursts)
518 {
519         const unsigned int width_bytes = width * cpp;
520         unsigned int npb, bursts;
521
522         /* Maximum number of pixels per burst without overshooting stride */
523         for (npb = 64 / cpp; npb > 0; --npb) {
524                 if (round_up(width_bytes, npb * cpp) <= stride)
525                         break;
526         }
527         *burstsize = npb;
528
529         /* Maximum number of consecutive bursts without overshooting stride */
530         for (bursts = 8; bursts > 1; bursts /= 2) {
531                 if (round_up(width_bytes, npb * cpp * bursts) <= stride)
532                         break;
533         }
534         *num_bursts = bursts;
535 }
536
537 static void ipu_plane_atomic_update(struct drm_plane *plane,
538                                     struct drm_plane_state *old_state)
539 {
540         struct ipu_plane *ipu_plane = to_ipu_plane(plane);
541         struct drm_plane_state *state = plane->state;
542         struct ipu_plane_state *ipu_state = to_ipu_plane_state(state);
543         struct drm_crtc_state *crtc_state = state->crtc->state;
544         struct drm_framebuffer *fb = state->fb;
545         struct drm_rect *dst = &state->dst;
546         unsigned long eba, ubo, vbo;
547         unsigned long alpha_eba = 0;
548         enum ipu_color_space ics;
549         unsigned int axi_id = 0;
550         const struct drm_format_info *info;
551         u8 burstsize, num_bursts;
552         u32 width, height;
553         int active;
554
555         if (ipu_plane->dp_flow == IPU_DP_FLOW_SYNC_FG)
556                 ipu_dp_set_window_pos(ipu_plane->dp, dst->x1, dst->y1);
557
558         switch (ipu_plane->dp_flow) {
559         case IPU_DP_FLOW_SYNC_BG:
560                 if (state->normalized_zpos == 1) {
561                         ipu_dp_set_global_alpha(ipu_plane->dp,
562                                                 !fb->format->has_alpha, 0xff,
563                                                 true);
564                 } else {
565                         ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
566                 }
567                 break;
568         case IPU_DP_FLOW_SYNC_FG:
569                 if (state->normalized_zpos == 1) {
570                         ipu_dp_set_global_alpha(ipu_plane->dp,
571                                                 !fb->format->has_alpha, 0xff,
572                                                 false);
573                 }
574                 break;
575         }
576
577         eba = drm_plane_state_to_eba(state, 0);
578
579         /*
580          * Configure PRG channel and attached PRE, this changes the EBA to an
581          * internal SRAM location.
582          */
583         if (ipu_state->use_pre) {
584                 axi_id = ipu_chan_assign_axi_id(ipu_plane->dma);
585                 ipu_prg_channel_configure(ipu_plane->ipu_ch, axi_id,
586                                           drm_rect_width(&state->src) >> 16,
587                                           drm_rect_height(&state->src) >> 16,
588                                           fb->pitches[0], fb->format->format,
589                                           fb->modifier, &eba);
590         }
591
592         if (old_state->fb && !drm_atomic_crtc_needs_modeset(crtc_state)) {
593                 /* nothing to do if PRE is used */
594                 if (ipu_state->use_pre)
595                         return;
596                 active = ipu_idmac_get_current_buffer(ipu_plane->ipu_ch);
597                 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, !active, eba);
598                 ipu_idmac_select_buffer(ipu_plane->ipu_ch, !active);
599                 if (ipu_plane_separate_alpha(ipu_plane)) {
600                         active = ipu_idmac_get_current_buffer(ipu_plane->alpha_ch);
601                         ipu_cpmem_set_buffer(ipu_plane->alpha_ch, !active,
602                                              alpha_eba);
603                         ipu_idmac_select_buffer(ipu_plane->alpha_ch, !active);
604                 }
605                 return;
606         }
607
608         ics = ipu_drm_fourcc_to_colorspace(fb->format->format);
609         switch (ipu_plane->dp_flow) {
610         case IPU_DP_FLOW_SYNC_BG:
611                 ipu_dp_setup_channel(ipu_plane->dp, ics, IPUV3_COLORSPACE_RGB);
612                 break;
613         case IPU_DP_FLOW_SYNC_FG:
614                 ipu_dp_setup_channel(ipu_plane->dp, ics,
615                                         IPUV3_COLORSPACE_UNKNOWN);
616                 break;
617         }
618
619         ipu_dmfc_config_wait4eot(ipu_plane->dmfc, drm_rect_width(dst));
620
621         width = drm_rect_width(&state->src) >> 16;
622         height = drm_rect_height(&state->src) >> 16;
623         info = drm_format_info(fb->format->format);
624         ipu_calculate_bursts(width, info->cpp[0], fb->pitches[0],
625                              &burstsize, &num_bursts);
626
627         ipu_cpmem_zero(ipu_plane->ipu_ch);
628         ipu_cpmem_set_resolution(ipu_plane->ipu_ch, width, height);
629         ipu_cpmem_set_fmt(ipu_plane->ipu_ch, fb->format->format);
630         ipu_cpmem_set_burstsize(ipu_plane->ipu_ch, burstsize);
631         ipu_cpmem_set_high_priority(ipu_plane->ipu_ch);
632         ipu_idmac_enable_watermark(ipu_plane->ipu_ch, true);
633         ipu_idmac_set_double_buffer(ipu_plane->ipu_ch, 1);
634         ipu_cpmem_set_stride(ipu_plane->ipu_ch, fb->pitches[0]);
635         ipu_cpmem_set_axi_id(ipu_plane->ipu_ch, axi_id);
636
637         switch (fb->format->format) {
638         case DRM_FORMAT_YUV420:
639         case DRM_FORMAT_YVU420:
640         case DRM_FORMAT_YUV422:
641         case DRM_FORMAT_YVU422:
642         case DRM_FORMAT_YUV444:
643         case DRM_FORMAT_YVU444:
644                 ubo = drm_plane_state_to_ubo(state);
645                 vbo = drm_plane_state_to_vbo(state);
646                 if (fb->format->format == DRM_FORMAT_YVU420 ||
647                     fb->format->format == DRM_FORMAT_YVU422 ||
648                     fb->format->format == DRM_FORMAT_YVU444)
649                         swap(ubo, vbo);
650
651                 ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
652                                               fb->pitches[1], ubo, vbo);
653
654                 dev_dbg(ipu_plane->base.dev->dev,
655                         "phy = %lu %lu %lu, x = %d, y = %d", eba, ubo, vbo,
656                         state->src.x1 >> 16, state->src.y1 >> 16);
657                 break;
658         case DRM_FORMAT_NV12:
659         case DRM_FORMAT_NV16:
660                 ubo = drm_plane_state_to_ubo(state);
661
662                 ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
663                                               fb->pitches[1], ubo, ubo);
664
665                 dev_dbg(ipu_plane->base.dev->dev,
666                         "phy = %lu %lu, x = %d, y = %d", eba, ubo,
667                         state->src.x1 >> 16, state->src.y1 >> 16);
668                 break;
669         case DRM_FORMAT_RGB565_A8:
670         case DRM_FORMAT_BGR565_A8:
671         case DRM_FORMAT_RGB888_A8:
672         case DRM_FORMAT_BGR888_A8:
673         case DRM_FORMAT_RGBX8888_A8:
674         case DRM_FORMAT_BGRX8888_A8:
675                 alpha_eba = drm_plane_state_to_eba(state, 1);
676                 num_bursts = 0;
677
678                 dev_dbg(ipu_plane->base.dev->dev, "phys = %lu %lu, x = %d, y = %d",
679                         eba, alpha_eba, state->src.x1 >> 16, state->src.y1 >> 16);
680
681                 ipu_cpmem_set_burstsize(ipu_plane->ipu_ch, 16);
682
683                 ipu_cpmem_zero(ipu_plane->alpha_ch);
684                 ipu_cpmem_set_resolution(ipu_plane->alpha_ch,
685                                          drm_rect_width(&state->src) >> 16,
686                                          drm_rect_height(&state->src) >> 16);
687                 ipu_cpmem_set_format_passthrough(ipu_plane->alpha_ch, 8);
688                 ipu_cpmem_set_high_priority(ipu_plane->alpha_ch);
689                 ipu_idmac_set_double_buffer(ipu_plane->alpha_ch, 1);
690                 ipu_cpmem_set_stride(ipu_plane->alpha_ch, fb->pitches[1]);
691                 ipu_cpmem_set_burstsize(ipu_plane->alpha_ch, 16);
692                 ipu_cpmem_set_buffer(ipu_plane->alpha_ch, 0, alpha_eba);
693                 ipu_cpmem_set_buffer(ipu_plane->alpha_ch, 1, alpha_eba);
694                 break;
695         default:
696                 dev_dbg(ipu_plane->base.dev->dev, "phys = %lu, x = %d, y = %d",
697                         eba, state->src.x1 >> 16, state->src.y1 >> 16);
698                 break;
699         }
700         ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 0, eba);
701         ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 1, eba);
702         ipu_idmac_lock_enable(ipu_plane->ipu_ch, num_bursts);
703         ipu_plane_enable(ipu_plane);
704 }
705
706 static const struct drm_plane_helper_funcs ipu_plane_helper_funcs = {
707         .prepare_fb = drm_gem_fb_prepare_fb,
708         .atomic_check = ipu_plane_atomic_check,
709         .atomic_disable = ipu_plane_atomic_disable,
710         .atomic_update = ipu_plane_atomic_update,
711 };
712
713 bool ipu_plane_atomic_update_pending(struct drm_plane *plane)
714 {
715         struct ipu_plane *ipu_plane = to_ipu_plane(plane);
716         struct drm_plane_state *state = plane->state;
717         struct ipu_plane_state *ipu_state = to_ipu_plane_state(state);
718
719         /* disabled crtcs must not block the update */
720         if (!state->crtc)
721                 return false;
722
723         if (ipu_state->use_pre)
724                 return ipu_prg_channel_configure_pending(ipu_plane->ipu_ch);
725
726         /*
727          * Pretend no update is pending in the non-PRE/PRG case. For this to
728          * happen, an atomic update would have to be deferred until after the
729          * start of the next frame and simultaneously interrupt latency would
730          * have to be high enough to let the atomic update finish and issue an
731          * event before the previous end of frame interrupt handler can be
732          * executed.
733          */
734         return false;
735 }
736 int ipu_planes_assign_pre(struct drm_device *dev,
737                           struct drm_atomic_state *state)
738 {
739         struct drm_crtc_state *old_crtc_state, *crtc_state;
740         struct drm_plane_state *plane_state;
741         struct ipu_plane_state *ipu_state;
742         struct ipu_plane *ipu_plane;
743         struct drm_plane *plane;
744         struct drm_crtc *crtc;
745         int available_pres = ipu_prg_max_active_channels();
746         int ret, i;
747
748         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
749                 ret = drm_atomic_add_affected_planes(state, crtc);
750                 if (ret)
751                         return ret;
752         }
753
754         /*
755          * We are going over the planes in 2 passes: first we assign PREs to
756          * planes with a tiling modifier, which need the PREs to resolve into
757          * linear. Any failure to assign a PRE there is fatal. In the second
758          * pass we try to assign PREs to linear FBs, to improve memory access
759          * patterns for them. Failure at this point is non-fatal, as we can
760          * scan out linear FBs without a PRE.
761          */
762         for_each_new_plane_in_state(state, plane, plane_state, i) {
763                 ipu_state = to_ipu_plane_state(plane_state);
764                 ipu_plane = to_ipu_plane(plane);
765
766                 if (!plane_state->fb) {
767                         ipu_state->use_pre = false;
768                         continue;
769                 }
770
771                 if (!(plane_state->fb->flags & DRM_MODE_FB_MODIFIERS) ||
772                     plane_state->fb->modifier == DRM_FORMAT_MOD_LINEAR)
773                         continue;
774
775                 if (!ipu_prg_present(ipu_plane->ipu) || !available_pres)
776                         return -EINVAL;
777
778                 if (!ipu_prg_format_supported(ipu_plane->ipu,
779                                               plane_state->fb->format->format,
780                                               plane_state->fb->modifier))
781                         return -EINVAL;
782
783                 ipu_state->use_pre = true;
784                 available_pres--;
785         }
786
787         for_each_new_plane_in_state(state, plane, plane_state, i) {
788                 ipu_state = to_ipu_plane_state(plane_state);
789                 ipu_plane = to_ipu_plane(plane);
790
791                 if (!plane_state->fb) {
792                         ipu_state->use_pre = false;
793                         continue;
794                 }
795
796                 if ((plane_state->fb->flags & DRM_MODE_FB_MODIFIERS) &&
797                     plane_state->fb->modifier != DRM_FORMAT_MOD_LINEAR)
798                         continue;
799
800                 /* make sure that modifier is initialized */
801                 plane_state->fb->modifier = DRM_FORMAT_MOD_LINEAR;
802
803                 if (ipu_prg_present(ipu_plane->ipu) && available_pres &&
804                     ipu_prg_format_supported(ipu_plane->ipu,
805                                              plane_state->fb->format->format,
806                                              plane_state->fb->modifier)) {
807                         ipu_state->use_pre = true;
808                         available_pres--;
809                 } else {
810                         ipu_state->use_pre = false;
811                 }
812         }
813
814         return 0;
815 }
816 EXPORT_SYMBOL_GPL(ipu_planes_assign_pre);
817
818 struct ipu_plane *ipu_plane_init(struct drm_device *dev, struct ipu_soc *ipu,
819                                  int dma, int dp, unsigned int possible_crtcs,
820                                  enum drm_plane_type type)
821 {
822         struct ipu_plane *ipu_plane;
823         const uint64_t *modifiers = ipu_format_modifiers;
824         unsigned int zpos = (type == DRM_PLANE_TYPE_PRIMARY) ? 0 : 1;
825         int ret;
826
827         DRM_DEBUG_KMS("channel %d, dp flow %d, possible_crtcs=0x%x\n",
828                       dma, dp, possible_crtcs);
829
830         ipu_plane = drmm_universal_plane_alloc(dev, struct ipu_plane, base,
831                                                possible_crtcs, &ipu_plane_funcs,
832                                                ipu_plane_formats,
833                                                ARRAY_SIZE(ipu_plane_formats),
834                                                modifiers, type, NULL);
835         if (IS_ERR(ipu_plane)) {
836                 DRM_ERROR("failed to allocate and initialize %s plane\n",
837                           zpos ? "overlay" : "primary");
838                 return ipu_plane;
839         }
840
841         ipu_plane->ipu = ipu;
842         ipu_plane->dma = dma;
843         ipu_plane->dp_flow = dp;
844
845         if (ipu_prg_present(ipu))
846                 modifiers = pre_format_modifiers;
847
848         drm_plane_helper_add(&ipu_plane->base, &ipu_plane_helper_funcs);
849
850         if (dp == IPU_DP_FLOW_SYNC_BG || dp == IPU_DP_FLOW_SYNC_FG)
851                 ret = drm_plane_create_zpos_property(&ipu_plane->base, zpos, 0,
852                                                      1);
853         else
854                 ret = drm_plane_create_zpos_immutable_property(&ipu_plane->base,
855                                                                0);
856         if (ret)
857                 return ERR_PTR(ret);
858
859         ret = ipu_plane_get_resources(dev, ipu_plane);
860         if (ret) {
861                 DRM_ERROR("failed to get %s plane resources: %pe\n",
862                           zpos ? "overlay" : "primary", &ret);
863                 return ERR_PTR(ret);
864         }
865
866         return ipu_plane;
867 }
This page took 0.088914 seconds and 4 git commands to generate.