GNU Linux-libre 4.9.288-gnu1
[releases.git] / drivers / gpu / drm / imx / ipuv3-plane.c
1 /*
2  * i.MX IPUv3 DP Overlay Planes
3  *
4  * Copyright (C) 2013 Philipp Zabel, Pengutronix
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <drm/drmP.h>
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_plane_helper.h>
22
23 #include "video/imx-ipu-v3.h"
24 #include "ipuv3-plane.h"
25
26 static inline struct ipu_plane *to_ipu_plane(struct drm_plane *p)
27 {
28         return container_of(p, struct ipu_plane, base);
29 }
30
31 static const uint32_t ipu_plane_formats[] = {
32         DRM_FORMAT_ARGB1555,
33         DRM_FORMAT_XRGB1555,
34         DRM_FORMAT_ABGR1555,
35         DRM_FORMAT_XBGR1555,
36         DRM_FORMAT_RGBA5551,
37         DRM_FORMAT_BGRA5551,
38         DRM_FORMAT_ARGB4444,
39         DRM_FORMAT_ARGB8888,
40         DRM_FORMAT_XRGB8888,
41         DRM_FORMAT_ABGR8888,
42         DRM_FORMAT_XBGR8888,
43         DRM_FORMAT_RGBA8888,
44         DRM_FORMAT_RGBX8888,
45         DRM_FORMAT_BGRA8888,
46         DRM_FORMAT_BGRA8888,
47         DRM_FORMAT_UYVY,
48         DRM_FORMAT_VYUY,
49         DRM_FORMAT_YUYV,
50         DRM_FORMAT_YVYU,
51         DRM_FORMAT_YUV420,
52         DRM_FORMAT_YVU420,
53         DRM_FORMAT_RGB565,
54 };
55
56 int ipu_plane_irq(struct ipu_plane *ipu_plane)
57 {
58         return ipu_idmac_channel_irq(ipu_plane->ipu, ipu_plane->ipu_ch,
59                                      IPU_IRQ_EOF);
60 }
61
62 static inline unsigned long
63 drm_plane_state_to_eba(struct drm_plane_state *state)
64 {
65         struct drm_framebuffer *fb = state->fb;
66         struct drm_gem_cma_object *cma_obj;
67
68         cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
69         BUG_ON(!cma_obj);
70
71         return cma_obj->paddr + fb->offsets[0] +
72                fb->pitches[0] * (state->src_y >> 16) +
73                (fb->bits_per_pixel >> 3) * (state->src_x >> 16);
74 }
75
76 static inline unsigned long
77 drm_plane_state_to_ubo(struct drm_plane_state *state)
78 {
79         struct drm_framebuffer *fb = state->fb;
80         struct drm_gem_cma_object *cma_obj;
81         unsigned long eba = drm_plane_state_to_eba(state);
82
83         cma_obj = drm_fb_cma_get_gem_obj(fb, 1);
84         BUG_ON(!cma_obj);
85
86         return cma_obj->paddr + fb->offsets[1] +
87                fb->pitches[1] * (state->src_y >> 16) / 2 +
88                (state->src_x >> 16) / 2 - eba;
89 }
90
91 static inline unsigned long
92 drm_plane_state_to_vbo(struct drm_plane_state *state)
93 {
94         struct drm_framebuffer *fb = state->fb;
95         struct drm_gem_cma_object *cma_obj;
96         unsigned long eba = drm_plane_state_to_eba(state);
97
98         cma_obj = drm_fb_cma_get_gem_obj(fb, 2);
99         BUG_ON(!cma_obj);
100
101         return cma_obj->paddr + fb->offsets[2] +
102                fb->pitches[2] * (state->src_y >> 16) / 2 +
103                (state->src_x >> 16) / 2 - eba;
104 }
105
106 static void ipu_plane_atomic_set_base(struct ipu_plane *ipu_plane)
107 {
108         struct drm_plane *plane = &ipu_plane->base;
109         struct drm_plane_state *state = plane->state;
110         struct drm_crtc_state *crtc_state = state->crtc->state;
111         struct drm_framebuffer *fb = state->fb;
112         unsigned long eba, ubo, vbo;
113         int active;
114
115         eba = drm_plane_state_to_eba(state);
116
117         switch (fb->pixel_format) {
118         case DRM_FORMAT_YUV420:
119         case DRM_FORMAT_YVU420:
120                 if (!drm_atomic_crtc_needs_modeset(crtc_state))
121                         break;
122
123                 /*
124                  * Multiplanar formats have to meet the following restrictions:
125                  * - The (up to) three plane addresses are EBA, EBA+UBO, EBA+VBO
126                  * - EBA, UBO and VBO are a multiple of 8
127                  * - UBO and VBO are unsigned and not larger than 0xfffff8
128                  * - Only EBA may be changed while scanout is active
129                  * - The strides of U and V planes must be identical.
130                  */
131                 ubo = drm_plane_state_to_ubo(state);
132                 vbo = drm_plane_state_to_vbo(state);
133
134                 if (fb->pixel_format == DRM_FORMAT_YUV420)
135                         ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
136                                                       fb->pitches[1], ubo, vbo);
137                 else
138                         ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
139                                                       fb->pitches[1], vbo, ubo);
140
141                 dev_dbg(ipu_plane->base.dev->dev,
142                         "phy = %lu %lu %lu, x = %d, y = %d", eba, ubo, vbo,
143                         state->src_x >> 16, state->src_y >> 16);
144                 break;
145         default:
146                 dev_dbg(ipu_plane->base.dev->dev, "phys = %lu, x = %d, y = %d",
147                         eba, state->src_x >> 16, state->src_y >> 16);
148
149                 break;
150         }
151
152         if (!drm_atomic_crtc_needs_modeset(crtc_state)) {
153                 active = ipu_idmac_get_current_buffer(ipu_plane->ipu_ch);
154                 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, !active, eba);
155                 ipu_idmac_select_buffer(ipu_plane->ipu_ch, !active);
156         } else {
157                 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 0, eba);
158                 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 1, eba);
159         }
160 }
161
162 void ipu_plane_put_resources(struct ipu_plane *ipu_plane)
163 {
164         if (!IS_ERR_OR_NULL(ipu_plane->dp))
165                 ipu_dp_put(ipu_plane->dp);
166         if (!IS_ERR_OR_NULL(ipu_plane->dmfc))
167                 ipu_dmfc_put(ipu_plane->dmfc);
168         if (!IS_ERR_OR_NULL(ipu_plane->ipu_ch))
169                 ipu_idmac_put(ipu_plane->ipu_ch);
170 }
171
172 int ipu_plane_get_resources(struct ipu_plane *ipu_plane)
173 {
174         int ret;
175
176         ipu_plane->ipu_ch = ipu_idmac_get(ipu_plane->ipu, ipu_plane->dma);
177         if (IS_ERR(ipu_plane->ipu_ch)) {
178                 ret = PTR_ERR(ipu_plane->ipu_ch);
179                 DRM_ERROR("failed to get idmac channel: %d\n", ret);
180                 return ret;
181         }
182
183         ipu_plane->dmfc = ipu_dmfc_get(ipu_plane->ipu, ipu_plane->dma);
184         if (IS_ERR(ipu_plane->dmfc)) {
185                 ret = PTR_ERR(ipu_plane->dmfc);
186                 DRM_ERROR("failed to get dmfc: ret %d\n", ret);
187                 goto err_out;
188         }
189
190         if (ipu_plane->dp_flow >= 0) {
191                 ipu_plane->dp = ipu_dp_get(ipu_plane->ipu, ipu_plane->dp_flow);
192                 if (IS_ERR(ipu_plane->dp)) {
193                         ret = PTR_ERR(ipu_plane->dp);
194                         DRM_ERROR("failed to get dp flow: %d\n", ret);
195                         goto err_out;
196                 }
197         }
198
199         return 0;
200 err_out:
201         ipu_plane_put_resources(ipu_plane);
202
203         return ret;
204 }
205
206 static void ipu_plane_enable(struct ipu_plane *ipu_plane)
207 {
208         if (ipu_plane->dp)
209                 ipu_dp_enable(ipu_plane->ipu);
210         ipu_dmfc_enable_channel(ipu_plane->dmfc);
211         ipu_idmac_enable_channel(ipu_plane->ipu_ch);
212         if (ipu_plane->dp)
213                 ipu_dp_enable_channel(ipu_plane->dp);
214 }
215
216 static int ipu_disable_plane(struct drm_plane *plane)
217 {
218         struct ipu_plane *ipu_plane = to_ipu_plane(plane);
219
220         DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
221
222         ipu_idmac_wait_busy(ipu_plane->ipu_ch, 50);
223
224         if (ipu_plane->dp)
225                 ipu_dp_disable_channel(ipu_plane->dp);
226         ipu_idmac_disable_channel(ipu_plane->ipu_ch);
227         ipu_dmfc_disable_channel(ipu_plane->dmfc);
228         if (ipu_plane->dp)
229                 ipu_dp_disable(ipu_plane->ipu);
230
231         return 0;
232 }
233
234 static void ipu_plane_destroy(struct drm_plane *plane)
235 {
236         struct ipu_plane *ipu_plane = to_ipu_plane(plane);
237
238         DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
239
240         drm_plane_cleanup(plane);
241         kfree(ipu_plane);
242 }
243
244 static const struct drm_plane_funcs ipu_plane_funcs = {
245         .update_plane   = drm_atomic_helper_update_plane,
246         .disable_plane  = drm_atomic_helper_disable_plane,
247         .destroy        = ipu_plane_destroy,
248         .reset          = drm_atomic_helper_plane_reset,
249         .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
250         .atomic_destroy_state   = drm_atomic_helper_plane_destroy_state,
251 };
252
253 static int ipu_plane_atomic_check(struct drm_plane *plane,
254                                   struct drm_plane_state *state)
255 {
256         struct drm_plane_state *old_state = plane->state;
257         struct drm_crtc_state *crtc_state;
258         struct device *dev = plane->dev->dev;
259         struct drm_framebuffer *fb = state->fb;
260         struct drm_framebuffer *old_fb = old_state->fb;
261         unsigned long eba, ubo, vbo, old_ubo, old_vbo;
262         int hsub, vsub;
263
264         /* Ok to disable */
265         if (!fb)
266                 return 0;
267
268         if (!state->crtc)
269                 return -EINVAL;
270
271         crtc_state =
272                 drm_atomic_get_existing_crtc_state(state->state, state->crtc);
273         if (WARN_ON(!crtc_state))
274                 return -EINVAL;
275
276         /* CRTC should be enabled */
277         if (!crtc_state->enable)
278                 return -EINVAL;
279
280         /* no scaling */
281         if (state->src_w >> 16 != state->crtc_w ||
282             state->src_h >> 16 != state->crtc_h)
283                 return -EINVAL;
284
285         switch (plane->type) {
286         case DRM_PLANE_TYPE_PRIMARY:
287                 /* full plane doesn't support partial off screen */
288                 if (state->crtc_x || state->crtc_y ||
289                     state->crtc_w != crtc_state->adjusted_mode.hdisplay ||
290                     state->crtc_h != crtc_state->adjusted_mode.vdisplay)
291                         return -EINVAL;
292
293                 /* full plane minimum width is 13 pixels */
294                 if (state->crtc_w < 13)
295                         return -EINVAL;
296                 break;
297         case DRM_PLANE_TYPE_OVERLAY:
298                 if (state->crtc_x < 0 || state->crtc_y < 0)
299                         return -EINVAL;
300
301                 if (state->crtc_x + state->crtc_w >
302                     crtc_state->adjusted_mode.hdisplay)
303                         return -EINVAL;
304                 if (state->crtc_y + state->crtc_h >
305                     crtc_state->adjusted_mode.vdisplay)
306                         return -EINVAL;
307                 break;
308         default:
309                 dev_warn(dev, "Unsupported plane type\n");
310                 return -EINVAL;
311         }
312
313         if (state->crtc_h < 2)
314                 return -EINVAL;
315
316         /*
317          * We support resizing active plane or changing its format by
318          * forcing CRTC mode change in plane's ->atomic_check callback
319          * and disabling all affected active planes in CRTC's ->atomic_disable
320          * callback.  The planes will be reenabled in plane's ->atomic_update
321          * callback.
322          */
323         if (old_fb && (state->src_w != old_state->src_w ||
324                               state->src_h != old_state->src_h ||
325                               fb->pixel_format != old_fb->pixel_format))
326                 crtc_state->mode_changed = true;
327
328         eba = drm_plane_state_to_eba(state);
329
330         if (eba & 0x7)
331                 return -EINVAL;
332
333         if (fb->pitches[0] < 1 || fb->pitches[0] > 16384)
334                 return -EINVAL;
335
336         if (old_fb && fb->pitches[0] != old_fb->pitches[0])
337                 crtc_state->mode_changed = true;
338
339         switch (fb->pixel_format) {
340         case DRM_FORMAT_YUV420:
341         case DRM_FORMAT_YVU420:
342                 /*
343                  * Multiplanar formats have to meet the following restrictions:
344                  * - The (up to) three plane addresses are EBA, EBA+UBO, EBA+VBO
345                  * - EBA, UBO and VBO are a multiple of 8
346                  * - UBO and VBO are unsigned and not larger than 0xfffff8
347                  * - Only EBA may be changed while scanout is active
348                  * - The strides of U and V planes must be identical.
349                  */
350                 ubo = drm_plane_state_to_ubo(state);
351                 vbo = drm_plane_state_to_vbo(state);
352
353                 if ((ubo & 0x7) || (vbo & 0x7))
354                         return -EINVAL;
355
356                 if ((ubo > 0xfffff8) || (vbo > 0xfffff8))
357                         return -EINVAL;
358
359                 if (old_fb &&
360                     (old_fb->pixel_format == DRM_FORMAT_YUV420 ||
361                      old_fb->pixel_format == DRM_FORMAT_YVU420)) {
362                         old_ubo = drm_plane_state_to_ubo(old_state);
363                         old_vbo = drm_plane_state_to_vbo(old_state);
364                         if (ubo != old_ubo || vbo != old_vbo)
365                                 return -EINVAL;
366                 }
367
368                 if (fb->pitches[1] != fb->pitches[2])
369                         return -EINVAL;
370
371                 if (fb->pitches[1] < 1 || fb->pitches[1] > 16384)
372                         return -EINVAL;
373
374                 if (old_fb && old_fb->pitches[1] != fb->pitches[1])
375                         crtc_state->mode_changed = true;
376
377                 /*
378                  * The x/y offsets must be even in case of horizontal/vertical
379                  * chroma subsampling.
380                  */
381                 hsub = drm_format_horz_chroma_subsampling(fb->pixel_format);
382                 vsub = drm_format_vert_chroma_subsampling(fb->pixel_format);
383                 if (((state->src_x >> 16) & (hsub - 1)) ||
384                     ((state->src_y >> 16) & (vsub - 1)))
385                         return -EINVAL;
386         }
387
388         return 0;
389 }
390
391 static void ipu_plane_atomic_disable(struct drm_plane *plane,
392                                      struct drm_plane_state *old_state)
393 {
394         ipu_disable_plane(plane);
395 }
396
397 static void ipu_plane_atomic_update(struct drm_plane *plane,
398                                     struct drm_plane_state *old_state)
399 {
400         struct ipu_plane *ipu_plane = to_ipu_plane(plane);
401         struct drm_plane_state *state = plane->state;
402         enum ipu_color_space ics;
403
404         if (old_state->fb) {
405                 struct drm_crtc_state *crtc_state = state->crtc->state;
406
407                 if (!drm_atomic_crtc_needs_modeset(crtc_state)) {
408                         ipu_plane_atomic_set_base(ipu_plane);
409                         return;
410                 }
411         }
412
413         switch (ipu_plane->dp_flow) {
414         case IPU_DP_FLOW_SYNC_BG:
415                 ipu_dp_setup_channel(ipu_plane->dp,
416                                         IPUV3_COLORSPACE_RGB,
417                                         IPUV3_COLORSPACE_RGB);
418                 ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
419                 break;
420         case IPU_DP_FLOW_SYNC_FG:
421                 ics = ipu_drm_fourcc_to_colorspace(state->fb->pixel_format);
422                 ipu_dp_setup_channel(ipu_plane->dp, ics,
423                                         IPUV3_COLORSPACE_UNKNOWN);
424                 ipu_dp_set_window_pos(ipu_plane->dp, state->crtc_x,
425                                         state->crtc_y);
426                 /* Enable local alpha on partial plane */
427                 switch (state->fb->pixel_format) {
428                 case DRM_FORMAT_ARGB1555:
429                 case DRM_FORMAT_ABGR1555:
430                 case DRM_FORMAT_RGBA5551:
431                 case DRM_FORMAT_BGRA5551:
432                 case DRM_FORMAT_ARGB4444:
433                 case DRM_FORMAT_ARGB8888:
434                 case DRM_FORMAT_ABGR8888:
435                 case DRM_FORMAT_RGBA8888:
436                 case DRM_FORMAT_BGRA8888:
437                         ipu_dp_set_global_alpha(ipu_plane->dp, false, 0, false);
438                         break;
439                 default:
440                         ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
441                         break;
442                 }
443         }
444
445         ipu_dmfc_config_wait4eot(ipu_plane->dmfc, state->crtc_w);
446
447         ipu_cpmem_zero(ipu_plane->ipu_ch);
448         ipu_cpmem_set_resolution(ipu_plane->ipu_ch, state->src_w >> 16,
449                                         state->src_h >> 16);
450         ipu_cpmem_set_fmt(ipu_plane->ipu_ch, state->fb->pixel_format);
451         ipu_cpmem_set_high_priority(ipu_plane->ipu_ch);
452         ipu_idmac_set_double_buffer(ipu_plane->ipu_ch, 1);
453         ipu_cpmem_set_stride(ipu_plane->ipu_ch, state->fb->pitches[0]);
454         ipu_plane_atomic_set_base(ipu_plane);
455         ipu_plane_enable(ipu_plane);
456 }
457
458 static const struct drm_plane_helper_funcs ipu_plane_helper_funcs = {
459         .atomic_check = ipu_plane_atomic_check,
460         .atomic_disable = ipu_plane_atomic_disable,
461         .atomic_update = ipu_plane_atomic_update,
462 };
463
464 struct ipu_plane *ipu_plane_init(struct drm_device *dev, struct ipu_soc *ipu,
465                                  int dma, int dp, unsigned int possible_crtcs,
466                                  enum drm_plane_type type)
467 {
468         struct ipu_plane *ipu_plane;
469         int ret;
470
471         DRM_DEBUG_KMS("channel %d, dp flow %d, possible_crtcs=0x%x\n",
472                       dma, dp, possible_crtcs);
473
474         ipu_plane = kzalloc(sizeof(*ipu_plane), GFP_KERNEL);
475         if (!ipu_plane) {
476                 DRM_ERROR("failed to allocate plane\n");
477                 return ERR_PTR(-ENOMEM);
478         }
479
480         ipu_plane->ipu = ipu;
481         ipu_plane->dma = dma;
482         ipu_plane->dp_flow = dp;
483
484         ret = drm_universal_plane_init(dev, &ipu_plane->base, possible_crtcs,
485                                        &ipu_plane_funcs, ipu_plane_formats,
486                                        ARRAY_SIZE(ipu_plane_formats), type,
487                                        NULL);
488         if (ret) {
489                 DRM_ERROR("failed to initialize plane\n");
490                 kfree(ipu_plane);
491                 return ERR_PTR(ret);
492         }
493
494         drm_plane_helper_add(&ipu_plane->base, &ipu_plane_helper_funcs);
495
496         return ipu_plane;
497 }