blob: d63e853a030064e3719787b5ba04a1241bedaa28 [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
Lucas Stach00514e82017-03-08 12:13:21 +010026struct ipu_plane_state {
27 struct drm_plane_state base;
28 bool use_pre;
29};
30
31static inline struct ipu_plane_state *
32to_ipu_plane_state(struct drm_plane_state *p)
33{
34 return container_of(p, struct ipu_plane_state, base);
35}
36
Philipp Zabel3df07392016-07-06 15:47:11 +020037static inline struct ipu_plane *to_ipu_plane(struct drm_plane *p)
38{
39 return container_of(p, struct ipu_plane, base);
40}
Philipp Zabelb8d181e2013-10-10 16:18:45 +020041
42static const uint32_t ipu_plane_formats[] = {
Philipp Zabelc639a1c2014-12-12 13:40:38 +010043 DRM_FORMAT_ARGB1555,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020044 DRM_FORMAT_XRGB1555,
Philipp Zabelc639a1c2014-12-12 13:40:38 +010045 DRM_FORMAT_ABGR1555,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020046 DRM_FORMAT_XBGR1555,
Philipp Zabelc639a1c2014-12-12 13:40:38 +010047 DRM_FORMAT_RGBA5551,
48 DRM_FORMAT_BGRA5551,
Lucas Stachcb166a32015-08-04 17:22:06 +020049 DRM_FORMAT_ARGB4444,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020050 DRM_FORMAT_ARGB8888,
51 DRM_FORMAT_XRGB8888,
52 DRM_FORMAT_ABGR8888,
53 DRM_FORMAT_XBGR8888,
Philipp Zabel59d6b712015-04-16 15:56:40 +020054 DRM_FORMAT_RGBA8888,
55 DRM_FORMAT_RGBX8888,
56 DRM_FORMAT_BGRA8888,
57 DRM_FORMAT_BGRA8888,
Philipp Zabel79321312016-02-12 14:35:55 +010058 DRM_FORMAT_UYVY,
59 DRM_FORMAT_VYUY,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020060 DRM_FORMAT_YUYV,
61 DRM_FORMAT_YVYU,
62 DRM_FORMAT_YUV420,
63 DRM_FORMAT_YVU420,
Philipp Zabeleae13c92016-10-18 12:31:40 +020064 DRM_FORMAT_YUV422,
65 DRM_FORMAT_YVU422,
66 DRM_FORMAT_YUV444,
67 DRM_FORMAT_YVU444,
68 DRM_FORMAT_NV12,
69 DRM_FORMAT_NV16,
Enrico Jorns33bee522015-11-24 16:29:22 +010070 DRM_FORMAT_RGB565,
Philipp Zabelf6b50ef2016-07-22 12:02:45 +020071 DRM_FORMAT_RGB565_A8,
72 DRM_FORMAT_BGR565_A8,
73 DRM_FORMAT_RGB888_A8,
74 DRM_FORMAT_BGR888_A8,
75 DRM_FORMAT_RGBX8888_A8,
76 DRM_FORMAT_BGRX8888_A8,
Philipp Zabelb8d181e2013-10-10 16:18:45 +020077};
78
79int ipu_plane_irq(struct ipu_plane *ipu_plane)
80{
81 return ipu_idmac_channel_irq(ipu_plane->ipu, ipu_plane->ipu_ch,
82 IPU_IRQ_EOF);
83}
84
Liu Ying33f14232016-07-08 17:40:55 +080085static inline unsigned long
Philipp Zabel0bfd56f2016-07-22 12:01:11 +020086drm_plane_state_to_eba(struct drm_plane_state *state, int plane)
Liu Ying33f14232016-07-08 17:40:55 +080087{
88 struct drm_framebuffer *fb = state->fb;
89 struct drm_gem_cma_object *cma_obj;
Philipp Zabel03ee3da2016-10-19 10:50:26 +020090 int x = state->src.x1 >> 16;
91 int y = state->src.y1 >> 16;
Liu Ying33f14232016-07-08 17:40:55 +080092
Philipp Zabel0bfd56f2016-07-22 12:01:11 +020093 cma_obj = drm_fb_cma_get_gem_obj(fb, plane);
Liu Ying33f14232016-07-08 17:40:55 +080094 BUG_ON(!cma_obj);
95
Philipp Zabel0bfd56f2016-07-22 12:01:11 +020096 return cma_obj->paddr + fb->offsets[plane] + fb->pitches[plane] * y +
97 fb->format->cpp[plane] * x;
Liu Ying33f14232016-07-08 17:40:55 +080098}
99
100static inline unsigned long
101drm_plane_state_to_ubo(struct drm_plane_state *state)
102{
103 struct drm_framebuffer *fb = state->fb;
104 struct drm_gem_cma_object *cma_obj;
Philipp Zabel0bfd56f2016-07-22 12:01:11 +0200105 unsigned long eba = drm_plane_state_to_eba(state, 0);
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200106 int x = state->src.x1 >> 16;
107 int y = state->src.y1 >> 16;
Liu Ying33f14232016-07-08 17:40:55 +0800108
109 cma_obj = drm_fb_cma_get_gem_obj(fb, 1);
110 BUG_ON(!cma_obj);
111
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200112 x /= drm_format_horz_chroma_subsampling(fb->format->format);
113 y /= drm_format_vert_chroma_subsampling(fb->format->format);
Philipp Zabelf2fa3532016-10-18 12:26:19 +0200114
115 return cma_obj->paddr + fb->offsets[1] + fb->pitches[1] * y +
Ville Syrjälä353c8592016-12-14 23:30:57 +0200116 fb->format->cpp[1] * x - eba;
Liu Ying33f14232016-07-08 17:40:55 +0800117}
118
119static inline unsigned long
120drm_plane_state_to_vbo(struct drm_plane_state *state)
121{
122 struct drm_framebuffer *fb = state->fb;
123 struct drm_gem_cma_object *cma_obj;
Philipp Zabel0bfd56f2016-07-22 12:01:11 +0200124 unsigned long eba = drm_plane_state_to_eba(state, 0);
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200125 int x = state->src.x1 >> 16;
126 int y = state->src.y1 >> 16;
Liu Ying33f14232016-07-08 17:40:55 +0800127
128 cma_obj = drm_fb_cma_get_gem_obj(fb, 2);
129 BUG_ON(!cma_obj);
130
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200131 x /= drm_format_horz_chroma_subsampling(fb->format->format);
132 y /= drm_format_vert_chroma_subsampling(fb->format->format);
Philipp Zabelf2fa3532016-10-18 12:26:19 +0200133
134 return cma_obj->paddr + fb->offsets[2] + fb->pitches[2] * y +
Ville Syrjälä353c8592016-12-14 23:30:57 +0200135 fb->format->cpp[2] * x - eba;
Liu Ying33f14232016-07-08 17:40:55 +0800136}
137
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200138void ipu_plane_put_resources(struct ipu_plane *ipu_plane)
139{
140 if (!IS_ERR_OR_NULL(ipu_plane->dp))
141 ipu_dp_put(ipu_plane->dp);
142 if (!IS_ERR_OR_NULL(ipu_plane->dmfc))
143 ipu_dmfc_put(ipu_plane->dmfc);
144 if (!IS_ERR_OR_NULL(ipu_plane->ipu_ch))
145 ipu_idmac_put(ipu_plane->ipu_ch);
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200146 if (!IS_ERR_OR_NULL(ipu_plane->alpha_ch))
147 ipu_idmac_put(ipu_plane->alpha_ch);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200148}
149
150int ipu_plane_get_resources(struct ipu_plane *ipu_plane)
151{
152 int ret;
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200153 int alpha_ch;
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200154
155 ipu_plane->ipu_ch = ipu_idmac_get(ipu_plane->ipu, ipu_plane->dma);
156 if (IS_ERR(ipu_plane->ipu_ch)) {
157 ret = PTR_ERR(ipu_plane->ipu_ch);
158 DRM_ERROR("failed to get idmac channel: %d\n", ret);
159 return ret;
160 }
161
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200162 alpha_ch = ipu_channel_alpha_channel(ipu_plane->dma);
163 if (alpha_ch >= 0) {
164 ipu_plane->alpha_ch = ipu_idmac_get(ipu_plane->ipu, alpha_ch);
165 if (IS_ERR(ipu_plane->alpha_ch)) {
166 ret = PTR_ERR(ipu_plane->alpha_ch);
167 DRM_ERROR("failed to get alpha idmac channel %d: %d\n",
168 alpha_ch, ret);
169 return ret;
170 }
171 }
172
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200173 ipu_plane->dmfc = ipu_dmfc_get(ipu_plane->ipu, ipu_plane->dma);
174 if (IS_ERR(ipu_plane->dmfc)) {
175 ret = PTR_ERR(ipu_plane->dmfc);
176 DRM_ERROR("failed to get dmfc: ret %d\n", ret);
177 goto err_out;
178 }
179
180 if (ipu_plane->dp_flow >= 0) {
181 ipu_plane->dp = ipu_dp_get(ipu_plane->ipu, ipu_plane->dp_flow);
182 if (IS_ERR(ipu_plane->dp)) {
183 ret = PTR_ERR(ipu_plane->dp);
184 DRM_ERROR("failed to get dp flow: %d\n", ret);
185 goto err_out;
186 }
187 }
188
189 return 0;
190err_out:
191 ipu_plane_put_resources(ipu_plane);
192
193 return ret;
194}
195
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200196static bool ipu_plane_separate_alpha(struct ipu_plane *ipu_plane)
197{
198 switch (ipu_plane->base.state->fb->format->format) {
199 case DRM_FORMAT_RGB565_A8:
200 case DRM_FORMAT_BGR565_A8:
201 case DRM_FORMAT_RGB888_A8:
202 case DRM_FORMAT_BGR888_A8:
203 case DRM_FORMAT_RGBX8888_A8:
204 case DRM_FORMAT_BGRX8888_A8:
205 return true;
206 default:
207 return false;
208 }
209}
210
Liu Ying33f14232016-07-08 17:40:55 +0800211static void ipu_plane_enable(struct ipu_plane *ipu_plane)
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200212{
Philipp Zabel285bbb02014-04-14 23:53:20 +0200213 if (ipu_plane->dp)
214 ipu_dp_enable(ipu_plane->ipu);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200215 ipu_dmfc_enable_channel(ipu_plane->dmfc);
216 ipu_idmac_enable_channel(ipu_plane->ipu_ch);
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200217 if (ipu_plane_separate_alpha(ipu_plane))
218 ipu_idmac_enable_channel(ipu_plane->alpha_ch);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200219 if (ipu_plane->dp)
220 ipu_dp_enable_channel(ipu_plane->dp);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200221}
222
Philipp Zabeleb8c8882017-02-24 18:31:05 +0100223void ipu_plane_disable(struct ipu_plane *ipu_plane, bool disable_dp_channel)
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200224{
Lucas Stachdf4b2232016-08-11 11:18:50 +0200225 DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
226
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200227 ipu_idmac_wait_busy(ipu_plane->ipu_ch, 50);
228
Philipp Zabeleb8c8882017-02-24 18:31:05 +0100229 if (ipu_plane->dp && disable_dp_channel)
230 ipu_dp_disable_channel(ipu_plane->dp, false);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200231 ipu_idmac_disable_channel(ipu_plane->ipu_ch);
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200232 if (ipu_plane->alpha_ch)
233 ipu_idmac_disable_channel(ipu_plane->alpha_ch);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200234 ipu_dmfc_disable_channel(ipu_plane->dmfc);
Philipp Zabel285bbb02014-04-14 23:53:20 +0200235 if (ipu_plane->dp)
236 ipu_dp_disable(ipu_plane->ipu);
Lucas Stach00514e82017-03-08 12:13:21 +0100237 if (ipu_prg_present(ipu_plane->ipu))
238 ipu_prg_channel_disable(ipu_plane->ipu_ch);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200239}
240
Philipp Zabeleb8c8882017-02-24 18:31:05 +0100241void ipu_plane_disable_deferred(struct drm_plane *plane)
242{
243 struct ipu_plane *ipu_plane = to_ipu_plane(plane);
244
245 if (ipu_plane->disabling) {
246 ipu_plane->disabling = false;
247 ipu_plane_disable(ipu_plane, false);
248 }
249}
250EXPORT_SYMBOL_GPL(ipu_plane_disable_deferred);
251
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200252static void ipu_plane_destroy(struct drm_plane *plane)
253{
254 struct ipu_plane *ipu_plane = to_ipu_plane(plane);
255
256 DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
257
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200258 drm_plane_cleanup(plane);
259 kfree(ipu_plane);
260}
261
Lucas Stach00514e82017-03-08 12:13:21 +0100262void ipu_plane_state_reset(struct drm_plane *plane)
263{
264 struct ipu_plane_state *ipu_state;
265
266 if (plane->state) {
267 ipu_state = to_ipu_plane_state(plane->state);
268 __drm_atomic_helper_plane_destroy_state(plane->state);
269 kfree(ipu_state);
270 }
271
272 ipu_state = kzalloc(sizeof(*ipu_state), GFP_KERNEL);
273
274 if (ipu_state) {
275 ipu_state->base.plane = plane;
276 ipu_state->base.rotation = DRM_ROTATE_0;
277 }
278
279 plane->state = &ipu_state->base;
280}
281
282struct drm_plane_state *ipu_plane_duplicate_state(struct drm_plane *plane)
283{
284 struct ipu_plane_state *state;
285
286 if (WARN_ON(!plane->state))
287 return NULL;
288
289 state = kmalloc(sizeof(*state), GFP_KERNEL);
290 if (state)
291 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
292
293 return &state->base;
294}
295
296void ipu_plane_destroy_state(struct drm_plane *plane,
297 struct drm_plane_state *state)
298{
299 struct ipu_plane_state *ipu_state = to_ipu_plane_state(state);
300
301 __drm_atomic_helper_plane_destroy_state(state);
302 kfree(ipu_state);
303}
304
Liu Ying8b3ce872016-05-24 18:10:40 +0800305static const struct drm_plane_funcs ipu_plane_funcs = {
Liu Ying5f2f9112016-07-08 17:40:59 +0800306 .update_plane = drm_atomic_helper_update_plane,
307 .disable_plane = drm_atomic_helper_disable_plane,
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200308 .destroy = ipu_plane_destroy,
Lucas Stach00514e82017-03-08 12:13:21 +0100309 .reset = ipu_plane_state_reset,
310 .atomic_duplicate_state = ipu_plane_duplicate_state,
311 .atomic_destroy_state = ipu_plane_destroy_state,
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200312};
313
Liu Ying33f14232016-07-08 17:40:55 +0800314static int ipu_plane_atomic_check(struct drm_plane *plane,
315 struct drm_plane_state *state)
316{
317 struct drm_plane_state *old_state = plane->state;
318 struct drm_crtc_state *crtc_state;
319 struct device *dev = plane->dev->dev;
320 struct drm_framebuffer *fb = state->fb;
321 struct drm_framebuffer *old_fb = old_state->fb;
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200322 unsigned long eba, ubo, vbo, old_ubo, old_vbo, alpha_eba;
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200323 bool can_position = (plane->type == DRM_PLANE_TYPE_OVERLAY);
324 struct drm_rect clip;
Philipp Zabel5fb57ab2016-10-18 12:30:36 +0200325 int hsub, vsub;
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200326 int ret;
Liu Ying33f14232016-07-08 17:40:55 +0800327
328 /* Ok to disable */
329 if (!fb)
Liu Ying5f2f9112016-07-08 17:40:59 +0800330 return 0;
331
332 if (!state->crtc)
333 return -EINVAL;
334
335 crtc_state =
336 drm_atomic_get_existing_crtc_state(state->state, state->crtc);
337 if (WARN_ON(!crtc_state))
338 return -EINVAL;
Liu Ying33f14232016-07-08 17:40:55 +0800339
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200340 clip.x1 = 0;
341 clip.y1 = 0;
342 clip.x2 = crtc_state->adjusted_mode.hdisplay;
343 clip.y2 = crtc_state->adjusted_mode.vdisplay;
344 ret = drm_plane_helper_check_state(state, &clip,
345 DRM_PLANE_HELPER_NO_SCALING,
346 DRM_PLANE_HELPER_NO_SCALING,
347 can_position, true);
348 if (ret)
349 return ret;
350
Liu Ying33f14232016-07-08 17:40:55 +0800351 /* CRTC should be enabled */
Liu Ying5f2f9112016-07-08 17:40:59 +0800352 if (!crtc_state->enable)
Liu Ying33f14232016-07-08 17:40:55 +0800353 return -EINVAL;
354
Liu Ying33f14232016-07-08 17:40:55 +0800355 switch (plane->type) {
356 case DRM_PLANE_TYPE_PRIMARY:
Liu Ying33f14232016-07-08 17:40:55 +0800357 /* full plane minimum width is 13 pixels */
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200358 if (drm_rect_width(&state->dst) < 13)
Liu Ying33f14232016-07-08 17:40:55 +0800359 return -EINVAL;
360 break;
361 case DRM_PLANE_TYPE_OVERLAY:
Liu Ying33f14232016-07-08 17:40:55 +0800362 break;
363 default:
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200364 dev_warn(dev, "Unsupported plane type %d\n", plane->type);
Liu Ying33f14232016-07-08 17:40:55 +0800365 return -EINVAL;
366 }
367
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200368 if (drm_rect_height(&state->dst) < 2)
Liu Ying33f14232016-07-08 17:40:55 +0800369 return -EINVAL;
370
371 /*
Liu Ying17809992016-08-26 15:30:44 +0800372 * We support resizing active plane or changing its format by
373 * forcing CRTC mode change in plane's ->atomic_check callback
374 * and disabling all affected active planes in CRTC's ->atomic_disable
375 * callback. The planes will be reenabled in plane's ->atomic_update
376 * callback.
Liu Ying33f14232016-07-08 17:40:55 +0800377 */
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200378 if (old_fb &&
379 (drm_rect_width(&state->dst) != drm_rect_width(&old_state->dst) ||
380 drm_rect_height(&state->dst) != drm_rect_height(&old_state->dst) ||
381 fb->format != old_fb->format))
Liu Ying17809992016-08-26 15:30:44 +0800382 crtc_state->mode_changed = true;
Liu Ying33f14232016-07-08 17:40:55 +0800383
Philipp Zabel0bfd56f2016-07-22 12:01:11 +0200384 eba = drm_plane_state_to_eba(state, 0);
Liu Ying33f14232016-07-08 17:40:55 +0800385
386 if (eba & 0x7)
387 return -EINVAL;
388
389 if (fb->pitches[0] < 1 || fb->pitches[0] > 16384)
390 return -EINVAL;
391
392 if (old_fb && fb->pitches[0] != old_fb->pitches[0])
Liu Ying17809992016-08-26 15:30:44 +0800393 crtc_state->mode_changed = true;
Liu Ying33f14232016-07-08 17:40:55 +0800394
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200395 switch (fb->format->format) {
Liu Ying33f14232016-07-08 17:40:55 +0800396 case DRM_FORMAT_YUV420:
397 case DRM_FORMAT_YVU420:
Philipp Zabeleae13c92016-10-18 12:31:40 +0200398 case DRM_FORMAT_YUV422:
399 case DRM_FORMAT_YVU422:
400 case DRM_FORMAT_YUV444:
401 case DRM_FORMAT_YVU444:
Liu Ying33f14232016-07-08 17:40:55 +0800402 /*
403 * Multiplanar formats have to meet the following restrictions:
404 * - The (up to) three plane addresses are EBA, EBA+UBO, EBA+VBO
405 * - EBA, UBO and VBO are a multiple of 8
406 * - UBO and VBO are unsigned and not larger than 0xfffff8
407 * - Only EBA may be changed while scanout is active
408 * - The strides of U and V planes must be identical.
409 */
Liu Ying33f14232016-07-08 17:40:55 +0800410 vbo = drm_plane_state_to_vbo(state);
411
Philipp Zabeleae13c92016-10-18 12:31:40 +0200412 if (vbo & 0x7 || vbo > 0xfffff8)
Liu Ying33f14232016-07-08 17:40:55 +0800413 return -EINVAL;
414
Ville Syrjälädbd4d572016-11-18 21:53:10 +0200415 if (old_fb && (fb->format == old_fb->format)) {
Liu Ying33f14232016-07-08 17:40:55 +0800416 old_vbo = drm_plane_state_to_vbo(old_state);
Philipp Zabeleae13c92016-10-18 12:31:40 +0200417 if (vbo != old_vbo)
Philipp Zabel181c9bf2016-10-18 12:13:15 +0200418 crtc_state->mode_changed = true;
Liu Ying33f14232016-07-08 17:40:55 +0800419 }
420
421 if (fb->pitches[1] != fb->pitches[2])
422 return -EINVAL;
423
Philipp Zabeleae13c92016-10-18 12:31:40 +0200424 /* fall-through */
425 case DRM_FORMAT_NV12:
426 case DRM_FORMAT_NV16:
427 ubo = drm_plane_state_to_ubo(state);
428
429 if (ubo & 0x7 || ubo > 0xfffff8)
430 return -EINVAL;
431
Ville Syrjälädbd4d572016-11-18 21:53:10 +0200432 if (old_fb && (fb->format == old_fb->format)) {
Philipp Zabeleae13c92016-10-18 12:31:40 +0200433 old_ubo = drm_plane_state_to_ubo(old_state);
434 if (ubo != old_ubo)
435 crtc_state->mode_changed = true;
436 }
437
Liu Ying33f14232016-07-08 17:40:55 +0800438 if (fb->pitches[1] < 1 || fb->pitches[1] > 16384)
439 return -EINVAL;
440
441 if (old_fb && old_fb->pitches[1] != fb->pitches[1])
Liu Ying17809992016-08-26 15:30:44 +0800442 crtc_state->mode_changed = true;
Philipp Zabel5fb57ab2016-10-18 12:30:36 +0200443
444 /*
445 * The x/y offsets must be even in case of horizontal/vertical
446 * chroma subsampling.
447 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200448 hsub = drm_format_horz_chroma_subsampling(fb->format->format);
449 vsub = drm_format_vert_chroma_subsampling(fb->format->format);
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200450 if (((state->src.x1 >> 16) & (hsub - 1)) ||
451 ((state->src.y1 >> 16) & (vsub - 1)))
Philipp Zabel5fb57ab2016-10-18 12:30:36 +0200452 return -EINVAL;
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200453 break;
454 case DRM_FORMAT_RGB565_A8:
455 case DRM_FORMAT_BGR565_A8:
456 case DRM_FORMAT_RGB888_A8:
457 case DRM_FORMAT_BGR888_A8:
458 case DRM_FORMAT_RGBX8888_A8:
459 case DRM_FORMAT_BGRX8888_A8:
460 alpha_eba = drm_plane_state_to_eba(state, 1);
461 if (alpha_eba & 0x7)
462 return -EINVAL;
463
464 if (fb->pitches[1] < 1 || fb->pitches[1] > 16384)
465 return -EINVAL;
466
467 if (old_fb && old_fb->pitches[1] != fb->pitches[1])
468 crtc_state->mode_changed = true;
469 break;
Liu Ying33f14232016-07-08 17:40:55 +0800470 }
471
472 return 0;
473}
474
475static void ipu_plane_atomic_disable(struct drm_plane *plane,
476 struct drm_plane_state *old_state)
477{
Philipp Zabeleb8c8882017-02-24 18:31:05 +0100478 struct ipu_plane *ipu_plane = to_ipu_plane(plane);
479
480 if (ipu_plane->dp)
481 ipu_dp_disable_channel(ipu_plane->dp, true);
482 ipu_plane->disabling = true;
Liu Ying33f14232016-07-08 17:40:55 +0800483}
484
Lucas Stach00514e82017-03-08 12:13:21 +0100485static int ipu_chan_assign_axi_id(int ipu_chan)
486{
487 switch (ipu_chan) {
488 case IPUV3_CHANNEL_MEM_BG_SYNC:
489 return 1;
490 case IPUV3_CHANNEL_MEM_FG_SYNC:
491 return 2;
492 case IPUV3_CHANNEL_MEM_DC_SYNC:
493 return 3;
494 default:
495 return 0;
496 }
497}
498
Liu Ying33f14232016-07-08 17:40:55 +0800499static void ipu_plane_atomic_update(struct drm_plane *plane,
500 struct drm_plane_state *old_state)
501{
502 struct ipu_plane *ipu_plane = to_ipu_plane(plane);
503 struct drm_plane_state *state = plane->state;
Lucas Stach00514e82017-03-08 12:13:21 +0100504 struct ipu_plane_state *ipu_state = to_ipu_plane_state(state);
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200505 struct drm_crtc_state *crtc_state = state->crtc->state;
506 struct drm_framebuffer *fb = state->fb;
Philipp Zabel2e9a7122016-10-20 15:37:52 +0200507 struct drm_rect *dst = &state->dst;
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200508 unsigned long eba, ubo, vbo;
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200509 unsigned long alpha_eba = 0;
Liu Ying33f14232016-07-08 17:40:55 +0800510 enum ipu_color_space ics;
Lucas Stach00514e82017-03-08 12:13:21 +0100511 unsigned int axi_id = 0;
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200512 int active;
Liu Ying33f14232016-07-08 17:40:55 +0800513
Philipp Zabel2e9a7122016-10-20 15:37:52 +0200514 if (ipu_plane->dp_flow == IPU_DP_FLOW_SYNC_FG)
515 ipu_dp_set_window_pos(ipu_plane->dp, dst->x1, dst->y1);
516
Philipp Zabel0bfd56f2016-07-22 12:01:11 +0200517 eba = drm_plane_state_to_eba(state, 0);
Liu Ying5f4df0c2016-08-26 15:30:43 +0800518
Lucas Stach00514e82017-03-08 12:13:21 +0100519 /*
520 * Configure PRG channel and attached PRE, this changes the EBA to an
521 * internal SRAM location.
522 */
523 if (ipu_state->use_pre) {
524 axi_id = ipu_chan_assign_axi_id(ipu_plane->dma);
525 ipu_prg_channel_configure(ipu_plane->ipu_ch, axi_id,
526 drm_rect_width(&state->src) >> 16,
527 drm_rect_height(&state->src) >> 16,
528 state->fb->pitches[0],
529 state->fb->format->format, &eba);
530 }
531
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200532 if (old_state->fb && !drm_atomic_crtc_needs_modeset(crtc_state)) {
Lucas Stach00514e82017-03-08 12:13:21 +0100533 /* nothing to do if PRE is used */
534 if (ipu_state->use_pre)
535 return;
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200536 active = ipu_idmac_get_current_buffer(ipu_plane->ipu_ch);
537 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, !active, eba);
538 ipu_idmac_select_buffer(ipu_plane->ipu_ch, !active);
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200539 if (ipu_plane_separate_alpha(ipu_plane)) {
540 active = ipu_idmac_get_current_buffer(ipu_plane->alpha_ch);
541 ipu_cpmem_set_buffer(ipu_plane->alpha_ch, !active,
542 alpha_eba);
543 ipu_idmac_select_buffer(ipu_plane->alpha_ch, !active);
544 }
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200545 return;
Liu Ying33f14232016-07-08 17:40:55 +0800546 }
547
548 switch (ipu_plane->dp_flow) {
549 case IPU_DP_FLOW_SYNC_BG:
550 ipu_dp_setup_channel(ipu_plane->dp,
551 IPUV3_COLORSPACE_RGB,
552 IPUV3_COLORSPACE_RGB);
553 ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
554 break;
555 case IPU_DP_FLOW_SYNC_FG:
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200556 ics = ipu_drm_fourcc_to_colorspace(state->fb->format->format);
Liu Ying33f14232016-07-08 17:40:55 +0800557 ipu_dp_setup_channel(ipu_plane->dp, ics,
558 IPUV3_COLORSPACE_UNKNOWN);
Liu Ying33f14232016-07-08 17:40:55 +0800559 /* Enable local alpha on partial plane */
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200560 switch (state->fb->format->format) {
Liu Ying33f14232016-07-08 17:40:55 +0800561 case DRM_FORMAT_ARGB1555:
562 case DRM_FORMAT_ABGR1555:
563 case DRM_FORMAT_RGBA5551:
564 case DRM_FORMAT_BGRA5551:
565 case DRM_FORMAT_ARGB4444:
566 case DRM_FORMAT_ARGB8888:
567 case DRM_FORMAT_ABGR8888:
568 case DRM_FORMAT_RGBA8888:
569 case DRM_FORMAT_BGRA8888:
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200570 case DRM_FORMAT_RGB565_A8:
571 case DRM_FORMAT_BGR565_A8:
572 case DRM_FORMAT_RGB888_A8:
573 case DRM_FORMAT_BGR888_A8:
574 case DRM_FORMAT_RGBX8888_A8:
575 case DRM_FORMAT_BGRX8888_A8:
Liu Ying33f14232016-07-08 17:40:55 +0800576 ipu_dp_set_global_alpha(ipu_plane->dp, false, 0, false);
577 break;
578 default:
Philipp Zabel86126742016-10-18 17:09:30 +0200579 ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
Liu Ying33f14232016-07-08 17:40:55 +0800580 break;
581 }
582 }
583
Philipp Zabel2e9a7122016-10-20 15:37:52 +0200584 ipu_dmfc_config_wait4eot(ipu_plane->dmfc, drm_rect_width(dst));
Liu Ying33f14232016-07-08 17:40:55 +0800585
586 ipu_cpmem_zero(ipu_plane->ipu_ch);
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200587 ipu_cpmem_set_resolution(ipu_plane->ipu_ch,
588 drm_rect_width(&state->src) >> 16,
589 drm_rect_height(&state->src) >> 16);
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200590 ipu_cpmem_set_fmt(ipu_plane->ipu_ch, state->fb->format->format);
Liu Ying33f14232016-07-08 17:40:55 +0800591 ipu_cpmem_set_high_priority(ipu_plane->ipu_ch);
592 ipu_idmac_set_double_buffer(ipu_plane->ipu_ch, 1);
593 ipu_cpmem_set_stride(ipu_plane->ipu_ch, state->fb->pitches[0]);
Lucas Stach00514e82017-03-08 12:13:21 +0100594 ipu_cpmem_set_axi_id(ipu_plane->ipu_ch, axi_id);
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200595 switch (fb->format->format) {
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200596 case DRM_FORMAT_YUV420:
597 case DRM_FORMAT_YVU420:
Philipp Zabeleae13c92016-10-18 12:31:40 +0200598 case DRM_FORMAT_YUV422:
599 case DRM_FORMAT_YVU422:
600 case DRM_FORMAT_YUV444:
601 case DRM_FORMAT_YVU444:
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200602 ubo = drm_plane_state_to_ubo(state);
603 vbo = drm_plane_state_to_vbo(state);
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200604 if (fb->format->format == DRM_FORMAT_YVU420 ||
605 fb->format->format == DRM_FORMAT_YVU422 ||
606 fb->format->format == DRM_FORMAT_YVU444)
Philipp Zabeleae13c92016-10-18 12:31:40 +0200607 swap(ubo, vbo);
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200608
Philipp Zabeleae13c92016-10-18 12:31:40 +0200609 ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
610 fb->pitches[1], ubo, vbo);
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200611
612 dev_dbg(ipu_plane->base.dev->dev,
613 "phy = %lu %lu %lu, x = %d, y = %d", eba, ubo, vbo,
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200614 state->src.x1 >> 16, state->src.y1 >> 16);
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200615 break;
Philipp Zabeleae13c92016-10-18 12:31:40 +0200616 case DRM_FORMAT_NV12:
617 case DRM_FORMAT_NV16:
618 ubo = drm_plane_state_to_ubo(state);
619
620 ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
621 fb->pitches[1], ubo, ubo);
622
623 dev_dbg(ipu_plane->base.dev->dev,
624 "phy = %lu %lu, x = %d, y = %d", eba, ubo,
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200625 state->src.x1 >> 16, state->src.y1 >> 16);
Philipp Zabeleae13c92016-10-18 12:31:40 +0200626 break;
Philipp Zabelf6b50ef2016-07-22 12:02:45 +0200627 case DRM_FORMAT_RGB565_A8:
628 case DRM_FORMAT_BGR565_A8:
629 case DRM_FORMAT_RGB888_A8:
630 case DRM_FORMAT_BGR888_A8:
631 case DRM_FORMAT_RGBX8888_A8:
632 case DRM_FORMAT_BGRX8888_A8:
633 alpha_eba = drm_plane_state_to_eba(state, 1);
634
635 dev_dbg(ipu_plane->base.dev->dev, "phys = %lu %lu, x = %d, y = %d",
636 eba, alpha_eba, state->src.x1 >> 16, state->src.y1 >> 16);
637
638 ipu_cpmem_set_burstsize(ipu_plane->ipu_ch, 16);
639
640 ipu_cpmem_zero(ipu_plane->alpha_ch);
641 ipu_cpmem_set_resolution(ipu_plane->alpha_ch,
642 drm_rect_width(&state->src) >> 16,
643 drm_rect_height(&state->src) >> 16);
644 ipu_cpmem_set_format_passthrough(ipu_plane->alpha_ch, 8);
645 ipu_cpmem_set_high_priority(ipu_plane->alpha_ch);
646 ipu_idmac_set_double_buffer(ipu_plane->alpha_ch, 1);
647 ipu_cpmem_set_stride(ipu_plane->alpha_ch,
648 state->fb->pitches[1]);
649 ipu_cpmem_set_burstsize(ipu_plane->alpha_ch, 16);
650 ipu_cpmem_set_buffer(ipu_plane->alpha_ch, 0, alpha_eba);
651 ipu_cpmem_set_buffer(ipu_plane->alpha_ch, 1, alpha_eba);
652 break;
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200653 default:
654 dev_dbg(ipu_plane->base.dev->dev, "phys = %lu, x = %d, y = %d",
Philipp Zabel03ee3da2016-10-19 10:50:26 +0200655 eba, state->src.x1 >> 16, state->src.y1 >> 16);
Philipp Zabel3fd8b292016-10-18 11:40:25 +0200656 break;
657 }
658 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 0, eba);
659 ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 1, eba);
Liu Ying33f14232016-07-08 17:40:55 +0800660 ipu_plane_enable(ipu_plane);
661}
662
663static const struct drm_plane_helper_funcs ipu_plane_helper_funcs = {
Marek Vasut782ea2a2016-11-14 11:07:32 +0100664 .prepare_fb = drm_fb_cma_prepare_fb,
Liu Ying33f14232016-07-08 17:40:55 +0800665 .atomic_check = ipu_plane_atomic_check,
666 .atomic_disable = ipu_plane_atomic_disable,
667 .atomic_update = ipu_plane_atomic_update,
668};
669
Lucas Stach00514e82017-03-08 12:13:21 +0100670int ipu_planes_assign_pre(struct drm_device *dev,
671 struct drm_atomic_state *state)
672{
673 struct drm_plane_state *plane_state;
674 struct drm_plane *plane;
675 int available_pres = ipu_prg_max_active_channels();
676 int i;
677
678 for_each_plane_in_state(state, plane, plane_state, i) {
679 struct ipu_plane_state *ipu_state =
680 to_ipu_plane_state(plane_state);
681 struct ipu_plane *ipu_plane = to_ipu_plane(plane);
682
683 if (ipu_prg_present(ipu_plane->ipu) && available_pres &&
684 plane_state->fb &&
685 ipu_prg_format_supported(ipu_plane->ipu,
686 plane_state->fb->format->format,
687 plane_state->fb->modifier)) {
688 ipu_state->use_pre = true;
689 available_pres--;
690 } else {
691 ipu_state->use_pre = false;
692 }
693 }
694
695 return 0;
696}
697EXPORT_SYMBOL_GPL(ipu_planes_assign_pre);
698
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200699struct ipu_plane *ipu_plane_init(struct drm_device *dev, struct ipu_soc *ipu,
700 int dma, int dp, unsigned int possible_crtcs,
Philipp Zabel43895592015-11-06 11:08:02 +0100701 enum drm_plane_type type)
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200702{
703 struct ipu_plane *ipu_plane;
704 int ret;
705
706 DRM_DEBUG_KMS("channel %d, dp flow %d, possible_crtcs=0x%x\n",
707 dma, dp, possible_crtcs);
708
709 ipu_plane = kzalloc(sizeof(*ipu_plane), GFP_KERNEL);
710 if (!ipu_plane) {
711 DRM_ERROR("failed to allocate plane\n");
712 return ERR_PTR(-ENOMEM);
713 }
714
715 ipu_plane->ipu = ipu;
716 ipu_plane->dma = dma;
717 ipu_plane->dp_flow = dp;
718
Philipp Zabel43895592015-11-06 11:08:02 +0100719 ret = drm_universal_plane_init(dev, &ipu_plane->base, possible_crtcs,
720 &ipu_plane_funcs, ipu_plane_formats,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +0200721 ARRAY_SIZE(ipu_plane_formats), type,
722 NULL);
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200723 if (ret) {
724 DRM_ERROR("failed to initialize plane\n");
725 kfree(ipu_plane);
726 return ERR_PTR(ret);
727 }
728
Liu Ying33f14232016-07-08 17:40:55 +0800729 drm_plane_helper_add(&ipu_plane->base, &ipu_plane_helper_funcs);
730
Philipp Zabelb8d181e2013-10-10 16:18:45 +0200731 return ipu_plane;
732}