Loading...
1/*
2 * drivers/gpu/drm/omapdrm/omap_plane.c
3 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob.clark@linaro.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <drm/drm_atomic.h>
21#include <drm/drm_atomic_helper.h>
22#include <drm/drm_plane_helper.h>
23
24#include "omap_dmm_tiler.h"
25#include "omap_drv.h"
26
27/* some hackery because omapdss has an 'enum omap_plane' (which would be
28 * better named omap_plane_id).. and compiler seems unhappy about having
29 * both a 'struct omap_plane' and 'enum omap_plane'
30 */
31#define omap_plane _omap_plane
32
33/*
34 * plane funcs
35 */
36
37#define to_omap_plane(x) container_of(x, struct omap_plane, base)
38
39struct omap_plane {
40 struct drm_plane base;
41 int id; /* TODO rename omap_plane -> omap_plane_id in omapdss so I can use the enum */
42 const char *name;
43
44 uint32_t nformats;
45 uint32_t formats[32];
46
47 struct omap_drm_irq error_irq;
48};
49
50struct omap_plane_state {
51 struct drm_plane_state base;
52
53 unsigned int zorder;
54};
55
56static inline struct omap_plane_state *
57to_omap_plane_state(struct drm_plane_state *state)
58{
59 return container_of(state, struct omap_plane_state, base);
60}
61
62static int omap_plane_prepare_fb(struct drm_plane *plane,
63 struct drm_plane_state *new_state)
64{
65 if (!new_state->fb)
66 return 0;
67
68 return omap_framebuffer_pin(new_state->fb);
69}
70
71static void omap_plane_cleanup_fb(struct drm_plane *plane,
72 struct drm_plane_state *old_state)
73{
74 if (old_state->fb)
75 omap_framebuffer_unpin(old_state->fb);
76}
77
78static void omap_plane_atomic_update(struct drm_plane *plane,
79 struct drm_plane_state *old_state)
80{
81 struct omap_plane *omap_plane = to_omap_plane(plane);
82 struct drm_plane_state *state = plane->state;
83 struct omap_plane_state *omap_state = to_omap_plane_state(state);
84 struct omap_overlay_info info;
85 struct omap_drm_window win;
86 int ret;
87
88 DBG("%s, crtc=%p fb=%p", omap_plane->name, state->crtc, state->fb);
89
90 memset(&info, 0, sizeof(info));
91 info.rotation_type = OMAP_DSS_ROT_DMA;
92 info.rotation = OMAP_DSS_ROT_0;
93 info.global_alpha = 0xff;
94 info.mirror = 0;
95 info.zorder = omap_state->zorder;
96
97 memset(&win, 0, sizeof(win));
98 win.rotation = state->rotation;
99 win.crtc_x = state->crtc_x;
100 win.crtc_y = state->crtc_y;
101 win.crtc_w = state->crtc_w;
102 win.crtc_h = state->crtc_h;
103
104 /*
105 * src values are in Q16 fixed point, convert to integer.
106 * omap_framebuffer_update_scanout() takes adjusted src.
107 */
108 win.src_x = state->src_x >> 16;
109 win.src_y = state->src_y >> 16;
110
111 if (drm_rotation_90_or_270(state->rotation)) {
112 win.src_w = state->src_h >> 16;
113 win.src_h = state->src_w >> 16;
114 } else {
115 win.src_w = state->src_w >> 16;
116 win.src_h = state->src_h >> 16;
117 }
118
119 /* update scanout: */
120 omap_framebuffer_update_scanout(state->fb, &win, &info);
121
122 DBG("%dx%d -> %dx%d (%d)", info.width, info.height,
123 info.out_width, info.out_height,
124 info.screen_width);
125 DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
126 &info.paddr, &info.p_uv_addr);
127
128 dispc_ovl_set_channel_out(omap_plane->id,
129 omap_crtc_channel(state->crtc));
130
131 /* and finally, update omapdss: */
132 ret = dispc_ovl_setup(omap_plane->id, &info, false,
133 omap_crtc_timings(state->crtc), false);
134 if (ret) {
135 dev_err(plane->dev->dev, "Failed to setup plane %s\n",
136 omap_plane->name);
137 dispc_ovl_enable(omap_plane->id, false);
138 return;
139 }
140
141 dispc_ovl_enable(omap_plane->id, true);
142}
143
144static void omap_plane_atomic_disable(struct drm_plane *plane,
145 struct drm_plane_state *old_state)
146{
147 struct omap_plane_state *omap_state = to_omap_plane_state(plane->state);
148 struct omap_plane *omap_plane = to_omap_plane(plane);
149
150 plane->state->rotation = DRM_ROTATE_0;
151 omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
152 ? 0 : omap_plane->id;
153
154 dispc_ovl_enable(omap_plane->id, false);
155}
156
157static int omap_plane_atomic_check(struct drm_plane *plane,
158 struct drm_plane_state *state)
159{
160 struct drm_crtc_state *crtc_state;
161
162 if (!state->fb)
163 return 0;
164
165 /* crtc should only be NULL when disabling (i.e., !state->fb) */
166 if (WARN_ON(!state->crtc))
167 return 0;
168
169 crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc);
170 /* we should have a crtc state if the plane is attached to a crtc */
171 if (WARN_ON(!crtc_state))
172 return 0;
173
174 if (!crtc_state->enable)
175 return 0;
176
177 if (state->crtc_x < 0 || state->crtc_y < 0)
178 return -EINVAL;
179
180 if (state->crtc_x + state->crtc_w > crtc_state->adjusted_mode.hdisplay)
181 return -EINVAL;
182
183 if (state->crtc_y + state->crtc_h > crtc_state->adjusted_mode.vdisplay)
184 return -EINVAL;
185
186 if (state->rotation != DRM_ROTATE_0 &&
187 !omap_framebuffer_supports_rotation(state->fb))
188 return -EINVAL;
189
190 return 0;
191}
192
193static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
194 .prepare_fb = omap_plane_prepare_fb,
195 .cleanup_fb = omap_plane_cleanup_fb,
196 .atomic_check = omap_plane_atomic_check,
197 .atomic_update = omap_plane_atomic_update,
198 .atomic_disable = omap_plane_atomic_disable,
199};
200
201static void omap_plane_destroy(struct drm_plane *plane)
202{
203 struct omap_plane *omap_plane = to_omap_plane(plane);
204
205 DBG("%s", omap_plane->name);
206
207 omap_irq_unregister(plane->dev, &omap_plane->error_irq);
208
209 drm_plane_cleanup(plane);
210
211 kfree(omap_plane);
212}
213
214/* helper to install properties which are common to planes and crtcs */
215void omap_plane_install_properties(struct drm_plane *plane,
216 struct drm_mode_object *obj)
217{
218 struct drm_device *dev = plane->dev;
219 struct omap_drm_private *priv = dev->dev_private;
220
221 if (priv->has_dmm) {
222 if (!plane->rotation_property)
223 drm_plane_create_rotation_property(plane,
224 DRM_ROTATE_0,
225 DRM_ROTATE_0 | DRM_ROTATE_90 |
226 DRM_ROTATE_180 | DRM_ROTATE_270 |
227 DRM_REFLECT_X | DRM_REFLECT_Y);
228
229 /* Attach the rotation property also to the crtc object */
230 if (plane->rotation_property && obj != &plane->base)
231 drm_object_attach_property(obj, plane->rotation_property,
232 DRM_ROTATE_0);
233 }
234
235 drm_object_attach_property(obj, priv->zorder_prop, 0);
236}
237
238static struct drm_plane_state *
239omap_plane_atomic_duplicate_state(struct drm_plane *plane)
240{
241 struct omap_plane_state *state;
242 struct omap_plane_state *copy;
243
244 if (WARN_ON(!plane->state))
245 return NULL;
246
247 state = to_omap_plane_state(plane->state);
248 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
249 if (copy == NULL)
250 return NULL;
251
252 __drm_atomic_helper_plane_duplicate_state(plane, ©->base);
253
254 return ©->base;
255}
256
257static void omap_plane_atomic_destroy_state(struct drm_plane *plane,
258 struct drm_plane_state *state)
259{
260 __drm_atomic_helper_plane_destroy_state(state);
261 kfree(to_omap_plane_state(state));
262}
263
264static void omap_plane_reset(struct drm_plane *plane)
265{
266 struct omap_plane *omap_plane = to_omap_plane(plane);
267 struct omap_plane_state *omap_state;
268
269 if (plane->state) {
270 omap_plane_atomic_destroy_state(plane, plane->state);
271 plane->state = NULL;
272 }
273
274 omap_state = kzalloc(sizeof(*omap_state), GFP_KERNEL);
275 if (omap_state == NULL)
276 return;
277
278 /*
279 * Set defaults depending on whether we are a primary or overlay
280 * plane.
281 */
282 omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
283 ? 0 : omap_plane->id;
284 omap_state->base.rotation = DRM_ROTATE_0;
285
286 plane->state = &omap_state->base;
287 plane->state->plane = plane;
288}
289
290static int omap_plane_atomic_set_property(struct drm_plane *plane,
291 struct drm_plane_state *state,
292 struct drm_property *property,
293 uint64_t val)
294{
295 struct omap_drm_private *priv = plane->dev->dev_private;
296 struct omap_plane_state *omap_state = to_omap_plane_state(state);
297
298 if (property == priv->zorder_prop)
299 omap_state->zorder = val;
300 else
301 return -EINVAL;
302
303 return 0;
304}
305
306static int omap_plane_atomic_get_property(struct drm_plane *plane,
307 const struct drm_plane_state *state,
308 struct drm_property *property,
309 uint64_t *val)
310{
311 struct omap_drm_private *priv = plane->dev->dev_private;
312 const struct omap_plane_state *omap_state =
313 container_of(state, const struct omap_plane_state, base);
314
315 if (property == priv->zorder_prop)
316 *val = omap_state->zorder;
317 else
318 return -EINVAL;
319
320 return 0;
321}
322
323static const struct drm_plane_funcs omap_plane_funcs = {
324 .update_plane = drm_atomic_helper_update_plane,
325 .disable_plane = drm_atomic_helper_disable_plane,
326 .reset = omap_plane_reset,
327 .destroy = omap_plane_destroy,
328 .set_property = drm_atomic_helper_plane_set_property,
329 .atomic_duplicate_state = omap_plane_atomic_duplicate_state,
330 .atomic_destroy_state = omap_plane_atomic_destroy_state,
331 .atomic_set_property = omap_plane_atomic_set_property,
332 .atomic_get_property = omap_plane_atomic_get_property,
333};
334
335static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
336{
337 struct omap_plane *omap_plane =
338 container_of(irq, struct omap_plane, error_irq);
339 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
340 irqstatus);
341}
342
343static const char *plane_names[] = {
344 [OMAP_DSS_GFX] = "gfx",
345 [OMAP_DSS_VIDEO1] = "vid1",
346 [OMAP_DSS_VIDEO2] = "vid2",
347 [OMAP_DSS_VIDEO3] = "vid3",
348};
349
350static const uint32_t error_irqs[] = {
351 [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
352 [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
353 [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
354 [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
355};
356
357/* initialize plane */
358struct drm_plane *omap_plane_init(struct drm_device *dev,
359 int id, enum drm_plane_type type,
360 u32 possible_crtcs)
361{
362 struct drm_plane *plane;
363 struct omap_plane *omap_plane;
364 int ret;
365
366 DBG("%s: type=%d", plane_names[id], type);
367
368 omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
369 if (!omap_plane)
370 return ERR_PTR(-ENOMEM);
371
372 omap_plane->nformats = omap_framebuffer_get_formats(
373 omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
374 dss_feat_get_supported_color_modes(id));
375 omap_plane->id = id;
376 omap_plane->name = plane_names[id];
377
378 plane = &omap_plane->base;
379
380 omap_plane->error_irq.irqmask = error_irqs[id];
381 omap_plane->error_irq.irq = omap_plane_error_irq;
382 omap_irq_register(dev, &omap_plane->error_irq);
383
384 ret = drm_universal_plane_init(dev, plane, possible_crtcs,
385 &omap_plane_funcs, omap_plane->formats,
386 omap_plane->nformats, type, NULL);
387 if (ret < 0)
388 goto error;
389
390 drm_plane_helper_add(plane, &omap_plane_helper_funcs);
391
392 omap_plane_install_properties(plane, &plane->base);
393
394 return plane;
395
396error:
397 omap_irq_unregister(plane->dev, &omap_plane->error_irq);
398 kfree(omap_plane);
399 return NULL;
400}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
4 * Author: Rob Clark <rob.clark@linaro.org>
5 */
6
7#include <drm/drm_atomic.h>
8#include <drm/drm_atomic_helper.h>
9#include <drm/drm_blend.h>
10#include <drm/drm_gem_atomic_helper.h>
11#include <drm/drm_fourcc.h>
12#include <drm/drm_framebuffer.h>
13
14#include "omap_dmm_tiler.h"
15#include "omap_drv.h"
16
17/*
18 * plane funcs
19 */
20
21#define to_omap_plane_state(x) container_of(x, struct omap_plane_state, base)
22
23struct omap_plane_state {
24 /* Must be first. */
25 struct drm_plane_state base;
26
27 struct omap_hw_overlay *overlay;
28 struct omap_hw_overlay *r_overlay; /* right overlay */
29};
30
31#define to_omap_plane(x) container_of(x, struct omap_plane, base)
32
33struct omap_plane {
34 struct drm_plane base;
35 enum omap_plane_id id;
36};
37
38bool is_omap_plane_dual_overlay(struct drm_plane_state *state)
39{
40 struct omap_plane_state *omap_state = to_omap_plane_state(state);
41
42 return !!omap_state->r_overlay;
43}
44
45static int omap_plane_prepare_fb(struct drm_plane *plane,
46 struct drm_plane_state *new_state)
47{
48 if (!new_state->fb)
49 return 0;
50
51 drm_gem_plane_helper_prepare_fb(plane, new_state);
52
53 return omap_framebuffer_pin(new_state->fb);
54}
55
56static void omap_plane_cleanup_fb(struct drm_plane *plane,
57 struct drm_plane_state *old_state)
58{
59 if (old_state->fb)
60 omap_framebuffer_unpin(old_state->fb);
61}
62
63static void omap_plane_atomic_update(struct drm_plane *plane,
64 struct drm_atomic_state *state)
65{
66 struct omap_drm_private *priv = plane->dev->dev_private;
67 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
68 plane);
69 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
70 plane);
71 struct omap_plane_state *new_omap_state;
72 struct omap_plane_state *old_omap_state;
73 struct omap_overlay_info info, r_info;
74 enum omap_plane_id ovl_id, r_ovl_id;
75 int ret;
76 bool dual_ovl;
77
78 new_omap_state = to_omap_plane_state(new_state);
79 old_omap_state = to_omap_plane_state(old_state);
80
81 dual_ovl = is_omap_plane_dual_overlay(new_state);
82
83 /* Cleanup previously held overlay if needed */
84 if (old_omap_state->overlay)
85 omap_overlay_update_state(priv, old_omap_state->overlay);
86 if (old_omap_state->r_overlay)
87 omap_overlay_update_state(priv, old_omap_state->r_overlay);
88
89 if (!new_omap_state->overlay) {
90 DBG("[PLANE:%d:%s] no overlay attached", plane->base.id, plane->name);
91 return;
92 }
93
94 ovl_id = new_omap_state->overlay->id;
95 DBG("%s, crtc=%p fb=%p", plane->name, new_state->crtc,
96 new_state->fb);
97
98 memset(&info, 0, sizeof(info));
99 info.rotation_type = OMAP_DSS_ROT_NONE;
100 info.rotation = DRM_MODE_ROTATE_0;
101 info.global_alpha = new_state->alpha >> 8;
102 info.zorder = new_state->normalized_zpos;
103 if (new_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
104 info.pre_mult_alpha = 1;
105 else
106 info.pre_mult_alpha = 0;
107 info.color_encoding = new_state->color_encoding;
108 info.color_range = new_state->color_range;
109
110 r_info = info;
111
112 /* update scanout: */
113 omap_framebuffer_update_scanout(new_state->fb, new_state, &info,
114 dual_ovl ? &r_info : NULL);
115
116 DBG("%s: %dx%d -> %dx%d (%d)",
117 new_omap_state->overlay->name, info.width, info.height,
118 info.out_width, info.out_height, info.screen_width);
119 DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
120 &info.paddr, &info.p_uv_addr);
121
122 if (dual_ovl) {
123 r_ovl_id = new_omap_state->r_overlay->id;
124 /*
125 * If the current plane uses 2 hw planes the very next
126 * zorder is used by the r_overlay so we just use the
127 * main overlay zorder + 1
128 */
129 r_info.zorder = info.zorder + 1;
130
131 DBG("%s: %dx%d -> %dx%d (%d)",
132 new_omap_state->r_overlay->name,
133 r_info.width, r_info.height,
134 r_info.out_width, r_info.out_height, r_info.screen_width);
135 DBG("%d,%d %pad %pad", r_info.pos_x, r_info.pos_y,
136 &r_info.paddr, &r_info.p_uv_addr);
137 }
138
139 /* and finally, update omapdss: */
140 ret = dispc_ovl_setup(priv->dispc, ovl_id, &info,
141 omap_crtc_timings(new_state->crtc), false,
142 omap_crtc_channel(new_state->crtc));
143 if (ret) {
144 dev_err(plane->dev->dev, "Failed to setup plane %s\n",
145 plane->name);
146 dispc_ovl_enable(priv->dispc, ovl_id, false);
147 return;
148 }
149
150 dispc_ovl_enable(priv->dispc, ovl_id, true);
151
152 if (dual_ovl) {
153 ret = dispc_ovl_setup(priv->dispc, r_ovl_id, &r_info,
154 omap_crtc_timings(new_state->crtc), false,
155 omap_crtc_channel(new_state->crtc));
156 if (ret) {
157 dev_err(plane->dev->dev, "Failed to setup plane right-overlay %s\n",
158 plane->name);
159 dispc_ovl_enable(priv->dispc, r_ovl_id, false);
160 dispc_ovl_enable(priv->dispc, ovl_id, false);
161 return;
162 }
163
164 dispc_ovl_enable(priv->dispc, r_ovl_id, true);
165 }
166}
167
168static void omap_plane_atomic_disable(struct drm_plane *plane,
169 struct drm_atomic_state *state)
170{
171 struct omap_drm_private *priv = plane->dev->dev_private;
172 struct omap_plane *omap_plane = to_omap_plane(plane);
173 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
174 plane);
175 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
176 plane);
177 struct omap_plane_state *new_omap_state;
178 struct omap_plane_state *old_omap_state;
179
180 new_omap_state = to_omap_plane_state(new_state);
181 old_omap_state = to_omap_plane_state(old_state);
182
183 if (!old_omap_state->overlay)
184 return;
185
186 new_state->rotation = DRM_MODE_ROTATE_0;
187 new_state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : omap_plane->id;
188
189 omap_overlay_update_state(priv, old_omap_state->overlay);
190 new_omap_state->overlay = NULL;
191
192 if (is_omap_plane_dual_overlay(old_state)) {
193 omap_overlay_update_state(priv, old_omap_state->r_overlay);
194 new_omap_state->r_overlay = NULL;
195 }
196}
197
198#define FRAC_16_16(mult, div) (((mult) << 16) / (div))
199
200static int omap_plane_atomic_check(struct drm_plane *plane,
201 struct drm_atomic_state *state)
202{
203 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
204 plane);
205 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state,
206 plane);
207 struct omap_drm_private *priv = plane->dev->dev_private;
208 struct omap_plane_state *omap_state = to_omap_plane_state(new_plane_state);
209 struct omap_global_state *omap_overlay_global_state;
210 struct drm_crtc_state *crtc_state;
211 bool new_r_hw_overlay = false;
212 bool new_hw_overlay = false;
213 u32 max_width, max_height;
214 struct drm_crtc *crtc;
215 u16 width, height;
216 u32 caps = 0;
217 u32 fourcc;
218 int ret;
219
220 omap_overlay_global_state = omap_get_global_state(state);
221 if (IS_ERR(omap_overlay_global_state))
222 return PTR_ERR(omap_overlay_global_state);
223
224 dispc_ovl_get_max_size(priv->dispc, &width, &height);
225 max_width = width << 16;
226 max_height = height << 16;
227
228 crtc = new_plane_state->crtc ? new_plane_state->crtc : plane->state->crtc;
229 if (!crtc)
230 return 0;
231
232 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
233 /* we should have a crtc state if the plane is attached to a crtc */
234 if (WARN_ON(!crtc_state))
235 return 0;
236
237 /*
238 * Note: these are just sanity checks to filter out totally bad scaling
239 * factors. The real limits must be calculated case by case, and
240 * unfortunately we currently do those checks only at the commit
241 * phase in dispc.
242 */
243 ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
244 FRAC_16_16(1, 8), FRAC_16_16(8, 1),
245 true, true);
246 if (ret)
247 return ret;
248
249 DBG("%s: visible %d -> %d", plane->name,
250 old_plane_state->visible, new_plane_state->visible);
251
252 if (!new_plane_state->visible) {
253 omap_overlay_release(state, omap_state->overlay);
254 omap_overlay_release(state, omap_state->r_overlay);
255 omap_state->overlay = NULL;
256 omap_state->r_overlay = NULL;
257 return 0;
258 }
259
260 if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0)
261 return -EINVAL;
262
263 if (new_plane_state->crtc_x + new_plane_state->crtc_w > crtc_state->adjusted_mode.hdisplay)
264 return -EINVAL;
265
266 if (new_plane_state->crtc_y + new_plane_state->crtc_h > crtc_state->adjusted_mode.vdisplay)
267 return -EINVAL;
268
269 /* Make sure dimensions are within bounds. */
270 if (new_plane_state->src_h > max_height || new_plane_state->crtc_h > height)
271 return -EINVAL;
272
273
274 if (new_plane_state->src_w > max_width || new_plane_state->crtc_w > width) {
275 bool is_fourcc_yuv = new_plane_state->fb->format->is_yuv;
276
277 if (is_fourcc_yuv && (((new_plane_state->src_w >> 16) / 2 & 1) ||
278 new_plane_state->crtc_w / 2 & 1)) {
279 /*
280 * When calculating the split overlay width
281 * and it yield an odd value we will need to adjust
282 * the indivual width +/- 1. So make sure it fits
283 */
284 if (new_plane_state->src_w <= ((2 * width - 1) << 16) &&
285 new_plane_state->crtc_w <= (2 * width - 1))
286 new_r_hw_overlay = true;
287 else
288 return -EINVAL;
289 } else {
290 if (new_plane_state->src_w <= (2 * max_width) &&
291 new_plane_state->crtc_w <= (2 * width))
292 new_r_hw_overlay = true;
293 else
294 return -EINVAL;
295 }
296 }
297
298 if (new_plane_state->rotation != DRM_MODE_ROTATE_0 &&
299 !omap_framebuffer_supports_rotation(new_plane_state->fb))
300 return -EINVAL;
301
302 if ((new_plane_state->src_w >> 16) != new_plane_state->crtc_w ||
303 (new_plane_state->src_h >> 16) != new_plane_state->crtc_h)
304 caps |= OMAP_DSS_OVL_CAP_SCALE;
305
306 fourcc = new_plane_state->fb->format->format;
307
308 /*
309 * (re)allocate hw overlay if we don't have one or
310 * there is a caps mismatch
311 */
312 if (!omap_state->overlay || (caps & ~omap_state->overlay->caps)) {
313 new_hw_overlay = true;
314 } else {
315 /* check supported format */
316 if (!dispc_ovl_color_mode_supported(priv->dispc, omap_state->overlay->id,
317 fourcc))
318 new_hw_overlay = true;
319 }
320
321 /*
322 * check if we need two overlays and only have 1 or
323 * if we had 2 overlays but will only need 1
324 */
325 if ((new_r_hw_overlay && !omap_state->r_overlay) ||
326 (!new_r_hw_overlay && omap_state->r_overlay))
327 new_hw_overlay = true;
328
329 if (new_hw_overlay) {
330 struct omap_hw_overlay *old_ovl = omap_state->overlay;
331 struct omap_hw_overlay *old_r_ovl = omap_state->r_overlay;
332 struct omap_hw_overlay *new_ovl = NULL;
333 struct omap_hw_overlay *new_r_ovl = NULL;
334
335 omap_overlay_release(state, old_ovl);
336 omap_overlay_release(state, old_r_ovl);
337
338 ret = omap_overlay_assign(state, plane, caps, fourcc, &new_ovl,
339 new_r_hw_overlay ? &new_r_ovl : NULL);
340 if (ret) {
341 DBG("%s: failed to assign hw_overlay", plane->name);
342 omap_state->overlay = NULL;
343 omap_state->r_overlay = NULL;
344 return ret;
345 }
346
347 omap_state->overlay = new_ovl;
348 if (new_r_hw_overlay)
349 omap_state->r_overlay = new_r_ovl;
350 else
351 omap_state->r_overlay = NULL;
352 }
353
354 DBG("plane: %s overlay_id: %d", plane->name, omap_state->overlay->id);
355
356 if (omap_state->r_overlay)
357 DBG("plane: %s r_overlay_id: %d", plane->name, omap_state->r_overlay->id);
358
359 return 0;
360}
361
362static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
363 .prepare_fb = omap_plane_prepare_fb,
364 .cleanup_fb = omap_plane_cleanup_fb,
365 .atomic_check = omap_plane_atomic_check,
366 .atomic_update = omap_plane_atomic_update,
367 .atomic_disable = omap_plane_atomic_disable,
368};
369
370static void omap_plane_destroy(struct drm_plane *plane)
371{
372 struct omap_plane *omap_plane = to_omap_plane(plane);
373
374 DBG("%s", plane->name);
375
376 drm_plane_cleanup(plane);
377
378 kfree(omap_plane);
379}
380
381/* helper to install properties which are common to planes and crtcs */
382void omap_plane_install_properties(struct drm_plane *plane,
383 struct drm_mode_object *obj)
384{
385 struct drm_device *dev = plane->dev;
386 struct omap_drm_private *priv = dev->dev_private;
387
388 if (priv->has_dmm) {
389 if (!plane->rotation_property)
390 drm_plane_create_rotation_property(plane,
391 DRM_MODE_ROTATE_0,
392 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
393 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
394 DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y);
395
396 /* Attach the rotation property also to the crtc object */
397 if (plane->rotation_property && obj != &plane->base)
398 drm_object_attach_property(obj, plane->rotation_property,
399 DRM_MODE_ROTATE_0);
400 }
401
402 drm_object_attach_property(obj, priv->zorder_prop, 0);
403}
404
405static void omap_plane_reset(struct drm_plane *plane)
406{
407 struct omap_plane_state *omap_state;
408
409 if (plane->state)
410 drm_atomic_helper_plane_destroy_state(plane, plane->state);
411
412 omap_state = kzalloc(sizeof(*omap_state), GFP_KERNEL);
413 if (!omap_state)
414 return;
415
416 __drm_atomic_helper_plane_reset(plane, &omap_state->base);
417}
418
419static struct drm_plane_state *
420omap_plane_atomic_duplicate_state(struct drm_plane *plane)
421{
422 struct omap_plane_state *state, *current_state;
423
424 if (WARN_ON(!plane->state))
425 return NULL;
426
427 current_state = to_omap_plane_state(plane->state);
428
429 state = kmalloc(sizeof(*state), GFP_KERNEL);
430 if (!state)
431 return NULL;
432
433 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
434
435 state->overlay = current_state->overlay;
436 state->r_overlay = current_state->r_overlay;
437
438 return &state->base;
439}
440
441static void omap_plane_atomic_print_state(struct drm_printer *p,
442 const struct drm_plane_state *state)
443{
444 struct omap_plane_state *omap_state = to_omap_plane_state(state);
445
446 if (omap_state->overlay)
447 drm_printf(p, "\toverlay=%s (caps=0x%x)\n",
448 omap_state->overlay->name,
449 omap_state->overlay->caps);
450 else
451 drm_printf(p, "\toverlay=None\n");
452 if (omap_state->r_overlay)
453 drm_printf(p, "\tr_overlay=%s (caps=0x%x)\n",
454 omap_state->r_overlay->name,
455 omap_state->r_overlay->caps);
456 else
457 drm_printf(p, "\tr_overlay=None\n");
458}
459
460static int omap_plane_atomic_set_property(struct drm_plane *plane,
461 struct drm_plane_state *state,
462 struct drm_property *property,
463 u64 val)
464{
465 struct omap_drm_private *priv = plane->dev->dev_private;
466
467 if (property == priv->zorder_prop)
468 state->zpos = val;
469 else
470 return -EINVAL;
471
472 return 0;
473}
474
475static int omap_plane_atomic_get_property(struct drm_plane *plane,
476 const struct drm_plane_state *state,
477 struct drm_property *property,
478 u64 *val)
479{
480 struct omap_drm_private *priv = plane->dev->dev_private;
481
482 if (property == priv->zorder_prop)
483 *val = state->zpos;
484 else
485 return -EINVAL;
486
487 return 0;
488}
489
490static const struct drm_plane_funcs omap_plane_funcs = {
491 .update_plane = drm_atomic_helper_update_plane,
492 .disable_plane = drm_atomic_helper_disable_plane,
493 .reset = omap_plane_reset,
494 .destroy = omap_plane_destroy,
495 .atomic_duplicate_state = omap_plane_atomic_duplicate_state,
496 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
497 .atomic_set_property = omap_plane_atomic_set_property,
498 .atomic_get_property = omap_plane_atomic_get_property,
499 .atomic_print_state = omap_plane_atomic_print_state,
500};
501
502static bool omap_plane_supports_yuv(struct drm_plane *plane)
503{
504 struct omap_drm_private *priv = plane->dev->dev_private;
505 struct omap_plane *omap_plane = to_omap_plane(plane);
506 const u32 *formats = dispc_ovl_get_color_modes(priv->dispc, omap_plane->id);
507 u32 i;
508
509 for (i = 0; formats[i]; i++)
510 if (formats[i] == DRM_FORMAT_YUYV ||
511 formats[i] == DRM_FORMAT_UYVY ||
512 formats[i] == DRM_FORMAT_NV12)
513 return true;
514
515 return false;
516}
517
518/* initialize plane */
519struct drm_plane *omap_plane_init(struct drm_device *dev,
520 int idx, enum drm_plane_type type,
521 u32 possible_crtcs)
522{
523 struct omap_drm_private *priv = dev->dev_private;
524 unsigned int num_planes = dispc_get_num_ovls(priv->dispc);
525 struct drm_plane *plane;
526 struct omap_plane *omap_plane;
527 unsigned int zpos;
528 int ret;
529 u32 nformats;
530 const u32 *formats;
531
532 if (WARN_ON(idx >= num_planes))
533 return ERR_PTR(-EINVAL);
534
535 omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
536 if (!omap_plane)
537 return ERR_PTR(-ENOMEM);
538
539 omap_plane->id = idx;
540
541 DBG("%d: type=%d", omap_plane->id, type);
542 DBG(" crtc_mask: 0x%04x", possible_crtcs);
543
544 formats = dispc_ovl_get_color_modes(priv->dispc, omap_plane->id);
545 for (nformats = 0; formats[nformats]; ++nformats)
546 ;
547
548 plane = &omap_plane->base;
549
550 ret = drm_universal_plane_init(dev, plane, possible_crtcs,
551 &omap_plane_funcs, formats,
552 nformats, NULL, type, NULL);
553 if (ret < 0)
554 goto error;
555
556 drm_plane_helper_add(plane, &omap_plane_helper_funcs);
557
558 omap_plane_install_properties(plane, &plane->base);
559
560 /*
561 * Set the zpos default depending on whether we are a primary or overlay
562 * plane.
563 */
564 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
565 zpos = 0;
566 else
567 zpos = omap_plane->id;
568 drm_plane_create_zpos_property(plane, zpos, 0, num_planes - 1);
569 drm_plane_create_alpha_property(plane);
570 drm_plane_create_blend_mode_property(plane, BIT(DRM_MODE_BLEND_PREMULTI) |
571 BIT(DRM_MODE_BLEND_COVERAGE));
572
573 if (omap_plane_supports_yuv(plane))
574 drm_plane_create_color_properties(plane,
575 BIT(DRM_COLOR_YCBCR_BT601) |
576 BIT(DRM_COLOR_YCBCR_BT709),
577 BIT(DRM_COLOR_YCBCR_FULL_RANGE) |
578 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE),
579 DRM_COLOR_YCBCR_BT601,
580 DRM_COLOR_YCBCR_FULL_RANGE);
581
582 return plane;
583
584error:
585 dev_err(dev->dev, "%s(): could not create plane: %d\n",
586 __func__, omap_plane->id);
587
588 kfree(omap_plane);
589 return NULL;
590}