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 const 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 const 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 switch (state->rotation & DRM_ROTATE_MASK) {
112 case BIT(DRM_ROTATE_90):
113 case BIT(DRM_ROTATE_270):
114 win.src_w = state->src_h >> 16;
115 win.src_h = state->src_w >> 16;
116 break;
117 default:
118 win.src_w = state->src_w >> 16;
119 win.src_h = state->src_h >> 16;
120 break;
121 }
122
123 /* update scanout: */
124 omap_framebuffer_update_scanout(state->fb, &win, &info);
125
126 DBG("%dx%d -> %dx%d (%d)", info.width, info.height,
127 info.out_width, info.out_height,
128 info.screen_width);
129 DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
130 &info.paddr, &info.p_uv_addr);
131
132 dispc_ovl_set_channel_out(omap_plane->id,
133 omap_crtc_channel(state->crtc));
134
135 /* and finally, update omapdss: */
136 ret = dispc_ovl_setup(omap_plane->id, &info, false,
137 omap_crtc_timings(state->crtc), false);
138 if (WARN_ON(ret)) {
139 dispc_ovl_enable(omap_plane->id, false);
140 return;
141 }
142
143 dispc_ovl_enable(omap_plane->id, true);
144}
145
146static void omap_plane_atomic_disable(struct drm_plane *plane,
147 struct drm_plane_state *old_state)
148{
149 struct omap_plane_state *omap_state = to_omap_plane_state(plane->state);
150 struct omap_plane *omap_plane = to_omap_plane(plane);
151
152 plane->state->rotation = BIT(DRM_ROTATE_0);
153 omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
154 ? 0 : omap_plane->id;
155
156 dispc_ovl_enable(omap_plane->id, false);
157}
158
159static int omap_plane_atomic_check(struct drm_plane *plane,
160 struct drm_plane_state *state)
161{
162 struct drm_crtc_state *crtc_state;
163
164 if (!state->crtc)
165 return 0;
166
167 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
168 if (IS_ERR(crtc_state))
169 return PTR_ERR(crtc_state);
170
171 if (state->crtc_x < 0 || state->crtc_y < 0)
172 return -EINVAL;
173
174 if (state->crtc_x + state->crtc_w > crtc_state->adjusted_mode.hdisplay)
175 return -EINVAL;
176
177 if (state->crtc_y + state->crtc_h > crtc_state->adjusted_mode.vdisplay)
178 return -EINVAL;
179
180 if (state->fb) {
181 if (state->rotation != BIT(DRM_ROTATE_0) &&
182 !omap_framebuffer_supports_rotation(state->fb))
183 return -EINVAL;
184 }
185
186 return 0;
187}
188
189static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
190 .prepare_fb = omap_plane_prepare_fb,
191 .cleanup_fb = omap_plane_cleanup_fb,
192 .atomic_check = omap_plane_atomic_check,
193 .atomic_update = omap_plane_atomic_update,
194 .atomic_disable = omap_plane_atomic_disable,
195};
196
197static void omap_plane_destroy(struct drm_plane *plane)
198{
199 struct omap_plane *omap_plane = to_omap_plane(plane);
200
201 DBG("%s", omap_plane->name);
202
203 omap_irq_unregister(plane->dev, &omap_plane->error_irq);
204
205 drm_plane_cleanup(plane);
206
207 kfree(omap_plane);
208}
209
210/* helper to install properties which are common to planes and crtcs */
211void omap_plane_install_properties(struct drm_plane *plane,
212 struct drm_mode_object *obj)
213{
214 struct drm_device *dev = plane->dev;
215 struct omap_drm_private *priv = dev->dev_private;
216
217 if (priv->has_dmm) {
218 struct drm_property *prop = dev->mode_config.rotation_property;
219
220 drm_object_attach_property(obj, prop, 0);
221 }
222
223 drm_object_attach_property(obj, priv->zorder_prop, 0);
224}
225
226static struct drm_plane_state *
227omap_plane_atomic_duplicate_state(struct drm_plane *plane)
228{
229 struct omap_plane_state *state;
230 struct omap_plane_state *copy;
231
232 if (WARN_ON(!plane->state))
233 return NULL;
234
235 state = to_omap_plane_state(plane->state);
236 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
237 if (copy == NULL)
238 return NULL;
239
240 __drm_atomic_helper_plane_duplicate_state(plane, ©->base);
241
242 return ©->base;
243}
244
245static void omap_plane_atomic_destroy_state(struct drm_plane *plane,
246 struct drm_plane_state *state)
247{
248 __drm_atomic_helper_plane_destroy_state(plane, state);
249 kfree(to_omap_plane_state(state));
250}
251
252static void omap_plane_reset(struct drm_plane *plane)
253{
254 struct omap_plane *omap_plane = to_omap_plane(plane);
255 struct omap_plane_state *omap_state;
256
257 if (plane->state) {
258 omap_plane_atomic_destroy_state(plane, plane->state);
259 plane->state = NULL;
260 }
261
262 omap_state = kzalloc(sizeof(*omap_state), GFP_KERNEL);
263 if (omap_state == NULL)
264 return;
265
266 /*
267 * Set defaults depending on whether we are a primary or overlay
268 * plane.
269 */
270 omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
271 ? 0 : omap_plane->id;
272 omap_state->base.rotation = BIT(DRM_ROTATE_0);
273
274 plane->state = &omap_state->base;
275 plane->state->plane = plane;
276}
277
278static int omap_plane_atomic_set_property(struct drm_plane *plane,
279 struct drm_plane_state *state,
280 struct drm_property *property,
281 uint64_t val)
282{
283 struct omap_drm_private *priv = plane->dev->dev_private;
284 struct omap_plane_state *omap_state = to_omap_plane_state(state);
285
286 if (property == priv->zorder_prop)
287 omap_state->zorder = val;
288 else
289 return -EINVAL;
290
291 return 0;
292}
293
294static int omap_plane_atomic_get_property(struct drm_plane *plane,
295 const struct drm_plane_state *state,
296 struct drm_property *property,
297 uint64_t *val)
298{
299 struct omap_drm_private *priv = plane->dev->dev_private;
300 const struct omap_plane_state *omap_state =
301 container_of(state, const struct omap_plane_state, base);
302
303 if (property == priv->zorder_prop)
304 *val = omap_state->zorder;
305 else
306 return -EINVAL;
307
308 return 0;
309}
310
311static const struct drm_plane_funcs omap_plane_funcs = {
312 .update_plane = drm_atomic_helper_update_plane,
313 .disable_plane = drm_atomic_helper_disable_plane,
314 .reset = omap_plane_reset,
315 .destroy = omap_plane_destroy,
316 .set_property = drm_atomic_helper_plane_set_property,
317 .atomic_duplicate_state = omap_plane_atomic_duplicate_state,
318 .atomic_destroy_state = omap_plane_atomic_destroy_state,
319 .atomic_set_property = omap_plane_atomic_set_property,
320 .atomic_get_property = omap_plane_atomic_get_property,
321};
322
323static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
324{
325 struct omap_plane *omap_plane =
326 container_of(irq, struct omap_plane, error_irq);
327 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
328 irqstatus);
329}
330
331static const char *plane_names[] = {
332 [OMAP_DSS_GFX] = "gfx",
333 [OMAP_DSS_VIDEO1] = "vid1",
334 [OMAP_DSS_VIDEO2] = "vid2",
335 [OMAP_DSS_VIDEO3] = "vid3",
336};
337
338static const uint32_t error_irqs[] = {
339 [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
340 [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
341 [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
342 [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
343};
344
345/* initialize plane */
346struct drm_plane *omap_plane_init(struct drm_device *dev,
347 int id, enum drm_plane_type type)
348{
349 struct omap_drm_private *priv = dev->dev_private;
350 struct drm_plane *plane;
351 struct omap_plane *omap_plane;
352 int ret;
353
354 DBG("%s: type=%d", plane_names[id], type);
355
356 omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
357 if (!omap_plane)
358 return ERR_PTR(-ENOMEM);
359
360 omap_plane->nformats = omap_framebuffer_get_formats(
361 omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
362 dss_feat_get_supported_color_modes(id));
363 omap_plane->id = id;
364 omap_plane->name = plane_names[id];
365
366 plane = &omap_plane->base;
367
368 omap_plane->error_irq.irqmask = error_irqs[id];
369 omap_plane->error_irq.irq = omap_plane_error_irq;
370 omap_irq_register(dev, &omap_plane->error_irq);
371
372 ret = drm_universal_plane_init(dev, plane, (1 << priv->num_crtcs) - 1,
373 &omap_plane_funcs, omap_plane->formats,
374 omap_plane->nformats, type, NULL);
375 if (ret < 0)
376 goto error;
377
378 drm_plane_helper_add(plane, &omap_plane_helper_funcs);
379
380 omap_plane_install_properties(plane, &plane->base);
381
382 return plane;
383
384error:
385 omap_irq_unregister(plane->dev, &omap_plane->error_irq);
386 kfree(omap_plane);
387 return NULL;
388}
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}