blob: 32871bed5dcc311ef1f483338c042e00b24c1248 [file] [log] [blame]
Philipp Zabelb8d181e2013-10-10 16:18:45 +02001/*
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>
Liu Ying5f2f9112016-07-08 17:40:59 +080017#include <drm/drm_atomic.h>
Liu Ying255c35f2016-07-08 17:40:56 +080018#include <drm/drm_atomic_helper.h>
Philipp Zabelb8d181e2013-10-10 16:18:45 +020019#include <drm/drm_fb_cma_helper.h>
20#include <drm/drm_gem_cma_helper.h>
Liu Ying33f14232016-07-08 17:40:55 +080021#include <drm/drm_plane_helper.h>
Philipp Zabelb8d181e2013-10-10 16:18:45 +020022
Philipp Zabel39b90042013-09-30 16:13:39 +020023#include "video/imx-ipu-v3.h"
Philipp Zabelb8d181e2013-10-10 16:18:45 +020024#include "ipuv3-plane.h"
25
Philipp Zabel3df07392016-07-06 15:47:11 +020026static inline struct ipu_plane *to_ipu_plane(struct drm_plane *p)
27{
28 return container_of(p, struct ipu_plane, base);
29}
Philipp Zabelb8d181e2013-10-10 16:18:45 +020030
31static const uint32_t ipu_plane_formats[] = {
Philipp Zabelc639a1c2014-12-12 13:40:38 +010032 DRM_FORMAT_ARGB1555,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020033 DRM_FORMAT_XRGB1555,
Philipp Zabelc639a1c2014-12-12 13:40:38 +010034 DRM_FORMAT_ABGR1555,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020035 DRM_FORMAT_XBGR1555,
Philipp Zabelc639a1c2014-12-12 13:40:38 +010036 DRM_FORMAT_RGBA5551,
37 DRM_FORMAT_BGRA5551,
Lucas Stachcb166a32015-08-04 17:22:06 +020038 DRM_FORMAT_ARGB4444,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020039 DRM_FORMAT_ARGB8888,
40 DRM_FORMAT_XRGB8888,
41 DRM_FORMAT_ABGR8888,
42 DRM_FORMAT_XBGR8888,
Philipp Zabel59d6b712015-04-16 15:56:40 +020043 DRM_FORMAT_RGBA8888,
44 DRM_FORMAT_RGBX8888,
45 DRM_FORMAT_BGRA8888,
46 DRM_FORMAT_BGRA8888,
Philipp Zabel79321312016-02-12 14:35:55 +010047 DRM_FORMAT_UYVY,
48 DRM_FORMAT_VYUY,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020049 DRM_FORMAT_YUYV,
50 DRM_FORMAT_YVYU,
51 DRM_FORMAT_YUV420,
52 DRM_FORMAT_YVU420,
Enrico Jorns33bee522015-11-24 16:29:22 +010053 DRM_FORMAT_RGB565,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020054};
55
56int 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
Liu Ying33f14232016-07-08 17:40:55 +080062static inline unsigned long
63drm_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
76static inline unsigned long
77drm_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
91static inline unsigned long
92drm_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
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200106void ipu_plane_put_resources(struct ipu_plane *ipu_plane)
107{
108 if (!IS_ERR_OR_NULL(ipu_plane->dp))
109 ipu_dp_put(ipu_plane->dp);
110 if (!IS_ERR_OR_NULL(ipu_plane->dmfc))
111 ipu_dmfc_put(ipu_plane->dmfc);
112 if (!IS_ERR_OR_NULL(ipu_plane->ipu_ch))
113 ipu_idmac_put(ipu_plane->ipu_ch);
114}
115
116int ipu_plane_get_resources(struct ipu_plane *ipu_plane)
117{
118 int ret;
119
120 ipu_plane->ipu_ch = ipu_idmac_get(ipu_plane->ipu, ipu_plane->dma);
121 if (IS_ERR(ipu_plane->ipu_ch)) {
122 ret = PTR_ERR(ipu_plane->ipu_ch);
123 DRM_ERROR("failed to get idmac channel: %d\n", ret);
124 return ret;
125 }
126
127 ipu_plane->dmfc = ipu_dmfc_get(ipu_plane->ipu, ipu_plane->dma);
128 if (IS_ERR(ipu_plane->dmfc)) {
129 ret = PTR_ERR(ipu_plane->dmfc);
130 DRM_ERROR("failed to get dmfc: ret %d\n", ret);
131 goto err_out;
132 }
133
134 if (ipu_plane->dp_flow >= 0) {
135 ipu_plane->dp = ipu_dp_get(ipu_plane->ipu, ipu_plane->dp_flow);
136 if (IS_ERR(ipu_plane->dp)) {
137 ret = PTR_ERR(ipu_plane->dp);
138 DRM_ERROR("failed to get dp flow: %d\n", ret);
139 goto err_out;
140 }
141 }
142
143 return 0;
144err_out:
145 ipu_plane_put_resources(ipu_plane);
146
147 return ret;
148}
149
Liu Ying33f14232016-07-08 17:40:55 +0800150static void ipu_plane_enable(struct ipu_plane *ipu_plane)
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200151{
Philipp Zabel285bbb02014-04-14 23:53:20 +0200152 if (ipu_plane->dp)
153 ipu_dp_enable(ipu_plane->ipu);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200154 ipu_dmfc_enable_channel(ipu_plane->dmfc);
155 ipu_idmac_enable_channel(ipu_plane->ipu_ch);
156 if (ipu_plane->dp)
157 ipu_dp_enable_channel(ipu_plane->dp);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200158}
159
Lucas Stachdf4b2232016-08-11 11:18:50 +0200160static int ipu_disable_plane(struct drm_plane *plane)
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200161{
Lucas Stachdf4b2232016-08-11 11:18:50 +0200162 struct ipu_plane *ipu_plane = to_ipu_plane(plane);
163
164 DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
165
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200166 ipu_idmac_wait_busy(ipu_plane->ipu_ch, 50);
167
168 if (ipu_plane->dp)
169 ipu_dp_disable_channel(ipu_plane->dp);
170 ipu_idmac_disable_channel(ipu_plane->ipu_ch);
171 ipu_dmfc_disable_channel(ipu_plane->dmfc);
Philipp Zabel285bbb02014-04-14 23:53:20 +0200172 if (ipu_plane->dp)
173 ipu_dp_disable(ipu_plane->ipu);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200174
175 return 0;
176}
177
178static void ipu_plane_destroy(struct drm_plane *plane)
179{
180 struct ipu_plane *ipu_plane = to_ipu_plane(plane);
181
182 DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
183
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200184 drm_plane_cleanup(plane);
185 kfree(ipu_plane);
186}
187
Liu Ying8b3ce872016-05-24 18:10:40 +0800188static const struct drm_plane_funcs ipu_plane_funcs = {
Liu Ying5f2f9112016-07-08 17:40:59 +0800189 .update_plane = drm_atomic_helper_update_plane,
190 .disable_plane = drm_atomic_helper_disable_plane,
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200191 .destroy = ipu_plane_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800192 .reset = drm_atomic_helper_plane_reset,
193 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
194 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200195};
196
Liu Ying33f14232016-07-08 17:40:55 +0800197static int ipu_plane_atomic_check(struct drm_plane *plane,
198 struct drm_plane_state *state)
199{
200 struct drm_plane_state *old_state = plane->state;
201 struct drm_crtc_state *crtc_state;
202 struct device *dev = plane->dev->dev;
203 struct drm_framebuffer *fb = state->fb;
204 struct drm_framebuffer *old_fb = old_state->fb;
205 unsigned long eba, ubo, vbo, old_ubo, old_vbo;
Philipp Zabel5fb57ab2016-10-18 12:30:36 +0200206 int hsub, vsub;
Liu Ying33f14232016-07-08 17:40:55 +0800207
208 /* Ok to disable */
209 if (!fb)
Liu Ying5f2f9112016-07-08 17:40:59 +0800210 return 0;
211
212 if (!state->crtc)
213 return -EINVAL;
214
215 crtc_state =
216 drm_atomic_get_existing_crtc_state(state->state, state->crtc);
217 if (WARN_ON(!crtc_state))
218 return -EINVAL;
Liu Ying33f14232016-07-08 17:40:55 +0800219
220 /* CRTC should be enabled */
Liu Ying5f2f9112016-07-08 17:40:59 +0800221 if (!crtc_state->enable)
Liu Ying33f14232016-07-08 17:40:55 +0800222 return -EINVAL;
223
224 /* no scaling */
225 if (state->src_w >> 16 != state->crtc_w ||
226 state->src_h >> 16 != state->crtc_h)
227 return -EINVAL;
228
Liu Ying33f14232016-07-08 17:40:55 +0800229 switch (plane->type) {
230 case DRM_PLANE_TYPE_PRIMARY:
231 /* full plane doesn't support partial off screen */
232 if (state->crtc_x || state->crtc_y ||
233 state->crtc_w != crtc_state->adjusted_mode.hdisplay ||
234 state->crtc_h != crtc_state->adjusted_mode.vdisplay)
235 return -EINVAL;
236
237 /* full plane minimum width is 13 pixels */
238 if (state->crtc_w < 13)
239 return -EINVAL;
240 break;
241 case DRM_PLANE_TYPE_OVERLAY:
242 if (state->crtc_x < 0 || state->crtc_y < 0)
243 return -EINVAL;
244
245 if (state->crtc_x + state->crtc_w >
246 crtc_state->adjusted_mode.hdisplay)
247 return -EINVAL;
248 if (state->crtc_y + state->crtc_h >
249 crtc_state->adjusted_mode.vdisplay)
250 return -EINVAL;
251 break;
252 default:
253 dev_warn(dev, "Unsupported plane type\n");
254 return -EINVAL;
255 }
256
257 if (state->crtc_h < 2)
258 return -EINVAL;
259
260 /*
Liu Ying17809992016-08-26 15:30:44 +0800261 * We support resizing active plane or changing its format by
262 * forcing CRTC mode change in plane's ->atomic_check callback
263 * and disabling all affected active planes in CRTC's ->atomic_disable
264 * callback. The planes will be reenabled in plane's ->atomic_update
265 * callback.
Liu Ying33f14232016-07-08 17:40:55 +0800266 */
267 if (old_fb && (state->src_w != old_state->src_w ||
268 state->src_h != old_state->src_h ||
269 fb->pixel_format != old_fb->pixel_format))
Liu Ying17809992016-08-26 15:30:44 +0800270 crtc_state->mode_changed = true;
Liu Ying33f14232016-07-08 17:40:55 +0800271
272 eba = drm_plane_state_to_eba(state);
273
274 if (eba & 0x7)
275 return -EINVAL;
276
277 if (fb->pitches[0] < 1 || fb->pitches[0] > 16384)
278 return -EINVAL;
279
280 if (old_fb && fb->pitches[0] != old_fb->pitches[0])
Liu Ying17809992016-08-26 15:30:44 +0800281 crtc_state->mode_changed = true;
Liu Ying33f14232016-07-08 17:40:55 +0800282
283 switch (fb->pixel_format) {
284 case DRM_FORMAT_YUV420:
285 case DRM_FORMAT_YVU420:
286 /*
287 * Multiplanar formats have to meet the following restrictions:
288 * - The (up to) three plane addresses are EBA, EBA+UBO, EBA+VBO
289 * - EBA, UBO and VBO are a multiple of 8
290 * - UBO and VBO are unsigned and not larger than 0xfffff8
291 * - Only EBA may be changed while scanout is active
292 * - The strides of U and V planes must be identical.
293 */
294 ubo = drm_plane_state_to_ubo(state);
295 vbo = drm_plane_state_to_vbo(state);
296
297 if ((ubo & 0x7) || (vbo & 0x7))
298 return -EINVAL;
299
300 if ((ubo > 0xfffff8) || (vbo > 0xfffff8))
301 return -EINVAL;
302
Philipp Zabel181c9bf2016-10-18 12:13:15 +0200303 if (old_fb && (fb->pixel_format == old_fb->pixel_format)) {
Liu Ying33f14232016-07-08 17:40:55 +0800304 old_ubo = drm_plane_state_to_ubo(old_state);
305 old_vbo = drm_plane_state_to_vbo(old_state);
306 if (ubo != old_ubo || vbo != old_vbo)
Philipp Zabel181c9bf2016-10-18 12:13:15 +0200307 crtc_state->mode_changed = true;
Liu Ying33f14232016-07-08 17:40:55 +0800308 }
309
310 if (fb->pitches[1] != fb->pitches[2])
311 return -EINVAL;
312
313 if (fb->pitches[1] < 1 || fb->pitches[1] > 16384)
314 return -EINVAL;
315
316 if (old_fb && old_fb->pitches[1] != fb->pitches[1])
Liu Ying17809992016-08-26 15:30:44 +0800317 crtc_state->mode_changed = true;
Philipp Zabel5fb57ab2016-10-18 12:30:36 +0200318
319 /*
320 * The x/y offsets must be even in case of horizontal/vertical
321 * chroma subsampling.
322 */
323 hsub = drm_format_horz_chroma_subsampling(fb->pixel_format);
324 vsub = drm_format_vert_chroma_subsampling(fb->pixel_format);
325 if (((state->src_x >> 16) & (hsub - 1)) ||
326 ((state->src_y >> 16) & (vsub - 1)))
327 return -EINVAL;
Liu Ying33f14232016-07-08 17:40:55 +0800328 }
329
330 return 0;
331}
332
333static void ipu_plane_atomic_disable(struct drm_plane *plane,
334 struct drm_plane_state *old_state)
335{
336 ipu_disable_plane(plane);
337}
338
339static void ipu_plane_atomic_update(struct drm_plane *plane,
340 struct drm_plane_state *old_state)
341{
342 struct ipu_plane *ipu_plane = to_ipu_plane(plane);
343 struct drm_plane_state *state = plane->state;
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200344 struct drm_crtc_state *crtc_state = state->crtc->state;
345 struct drm_framebuffer *fb = state->fb;
346 unsigned long eba, ubo, vbo;
Liu Ying33f14232016-07-08 17:40:55 +0800347 enum ipu_color_space ics;
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200348 int active;
Liu Ying33f14232016-07-08 17:40:55 +0800349
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200350 eba = drm_plane_state_to_eba(state);
Liu Ying5f4df0c2016-08-26 15:30:43 +0800351
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200352 if (old_state->fb && !drm_atomic_crtc_needs_modeset(crtc_state)) {
353 active = ipu_idmac_get_current_buffer(ipu_plane->ipu_ch);
354 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, !active, eba);
355 ipu_idmac_select_buffer(ipu_plane->ipu_ch, !active);
356 return;
Liu Ying33f14232016-07-08 17:40:55 +0800357 }
358
359 switch (ipu_plane->dp_flow) {
360 case IPU_DP_FLOW_SYNC_BG:
361 ipu_dp_setup_channel(ipu_plane->dp,
362 IPUV3_COLORSPACE_RGB,
363 IPUV3_COLORSPACE_RGB);
364 ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
365 break;
366 case IPU_DP_FLOW_SYNC_FG:
367 ics = ipu_drm_fourcc_to_colorspace(state->fb->pixel_format);
368 ipu_dp_setup_channel(ipu_plane->dp, ics,
369 IPUV3_COLORSPACE_UNKNOWN);
370 ipu_dp_set_window_pos(ipu_plane->dp, state->crtc_x,
371 state->crtc_y);
372 /* Enable local alpha on partial plane */
373 switch (state->fb->pixel_format) {
374 case DRM_FORMAT_ARGB1555:
375 case DRM_FORMAT_ABGR1555:
376 case DRM_FORMAT_RGBA5551:
377 case DRM_FORMAT_BGRA5551:
378 case DRM_FORMAT_ARGB4444:
379 case DRM_FORMAT_ARGB8888:
380 case DRM_FORMAT_ABGR8888:
381 case DRM_FORMAT_RGBA8888:
382 case DRM_FORMAT_BGRA8888:
383 ipu_dp_set_global_alpha(ipu_plane->dp, false, 0, false);
384 break;
385 default:
Philipp Zabel86126742016-10-18 17:09:30 +0200386 ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
Liu Ying33f14232016-07-08 17:40:55 +0800387 break;
388 }
389 }
390
391 ipu_dmfc_config_wait4eot(ipu_plane->dmfc, state->crtc_w);
392
393 ipu_cpmem_zero(ipu_plane->ipu_ch);
394 ipu_cpmem_set_resolution(ipu_plane->ipu_ch, state->src_w >> 16,
395 state->src_h >> 16);
396 ipu_cpmem_set_fmt(ipu_plane->ipu_ch, state->fb->pixel_format);
397 ipu_cpmem_set_high_priority(ipu_plane->ipu_ch);
398 ipu_idmac_set_double_buffer(ipu_plane->ipu_ch, 1);
399 ipu_cpmem_set_stride(ipu_plane->ipu_ch, state->fb->pitches[0]);
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200400 switch (fb->pixel_format) {
401 case DRM_FORMAT_YUV420:
402 case DRM_FORMAT_YVU420:
403 ubo = drm_plane_state_to_ubo(state);
404 vbo = drm_plane_state_to_vbo(state);
405
406 if (fb->pixel_format == DRM_FORMAT_YUV420)
407 ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
408 fb->pitches[1], ubo, vbo);
409 else
410 ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
411 fb->pitches[1], vbo, ubo);
412
413 dev_dbg(ipu_plane->base.dev->dev,
414 "phy = %lu %lu %lu, x = %d, y = %d", eba, ubo, vbo,
415 state->src_x >> 16, state->src_y >> 16);
416 break;
417 default:
418 dev_dbg(ipu_plane->base.dev->dev, "phys = %lu, x = %d, y = %d",
419 eba, state->src_x >> 16, state->src_y >> 16);
420 break;
421 }
422 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 0, eba);
423 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 1, eba);
Liu Ying33f14232016-07-08 17:40:55 +0800424 ipu_plane_enable(ipu_plane);
425}
426
427static const struct drm_plane_helper_funcs ipu_plane_helper_funcs = {
428 .atomic_check = ipu_plane_atomic_check,
429 .atomic_disable = ipu_plane_atomic_disable,
430 .atomic_update = ipu_plane_atomic_update,
431};
432
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200433struct ipu_plane *ipu_plane_init(struct drm_device *dev, struct ipu_soc *ipu,
434 int dma, int dp, unsigned int possible_crtcs,
Philipp Zabel43895592015-11-06 11:08:02 +0100435 enum drm_plane_type type)
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200436{
437 struct ipu_plane *ipu_plane;
438 int ret;
439
440 DRM_DEBUG_KMS("channel %d, dp flow %d, possible_crtcs=0x%x\n",
441 dma, dp, possible_crtcs);
442
443 ipu_plane = kzalloc(sizeof(*ipu_plane), GFP_KERNEL);
444 if (!ipu_plane) {
445 DRM_ERROR("failed to allocate plane\n");
446 return ERR_PTR(-ENOMEM);
447 }
448
449 ipu_plane->ipu = ipu;
450 ipu_plane->dma = dma;
451 ipu_plane->dp_flow = dp;
452
Philipp Zabel43895592015-11-06 11:08:02 +0100453 ret = drm_universal_plane_init(dev, &ipu_plane->base, possible_crtcs,
454 &ipu_plane_funcs, ipu_plane_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +0200455 ARRAY_SIZE(ipu_plane_formats), type,
456 NULL);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200457 if (ret) {
458 DRM_ERROR("failed to initialize plane\n");
459 kfree(ipu_plane);
460 return ERR_PTR(ret);
461 }
462
Liu Ying33f14232016-07-08 17:40:55 +0800463 drm_plane_helper_add(&ipu_plane->base, &ipu_plane_helper_funcs);
464
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200465 return ipu_plane;
466}