blob: 99663ec011b7a649f5902a2077a44bdff89040c9 [file] [log] [blame]
Rob Clarkbb5c2d92012-01-16 12:51:16 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_plane.c
Rob Clarkbb5c2d92012-01-16 12:51:16 -06003 *
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 "omap_drv.h"
Rob Clark3c810c62012-08-15 15:18:01 -050021#include "omap_dmm_tiler.h"
Rob Clarkbb5c2d92012-01-16 12:51:16 -060022
23/* some hackery because omapdss has an 'enum omap_plane' (which would be
24 * better named omap_plane_id).. and compiler seems unhappy about having
25 * both a 'struct omap_plane' and 'enum omap_plane'
26 */
27#define omap_plane _omap_plane
28
29/*
30 * plane funcs
31 */
32
33#define to_omap_plane(x) container_of(x, struct omap_plane, base)
34
35struct omap_plane {
36 struct drm_plane base;
Rob Clarkf5f94542012-12-04 13:59:12 -060037 int id; /* TODO rename omap_plane -> omap_plane_id in omapdss so I can use the enum */
38 const char *name;
Rob Clarkbb5c2d92012-01-16 12:51:16 -060039 struct omap_overlay_info info;
40
Rob Clark3c810c62012-08-15 15:18:01 -050041 /* position/orientation of scanout within the fb: */
42 struct omap_drm_window win;
Rob Clarkf5f94542012-12-04 13:59:12 -060043 bool enabled;
Rob Clark9a0774e2012-01-16 12:51:17 -060044
45 /* last fb that we pinned: */
46 struct drm_framebuffer *pinned_fb;
Rob Clarkbb5c2d92012-01-16 12:51:16 -060047
Rob Clarka890e662012-03-05 10:48:31 -060048 uint32_t nformats;
49 uint32_t formats[32];
Rob Clarkb33f34d2012-03-05 10:48:35 -060050
Rob Clarkf5f94542012-12-04 13:59:12 -060051 struct omap_drm_irq error_irq;
Rob Clarka890e662012-03-05 10:48:31 -060052};
Rob Clarkbb5c2d92012-01-16 12:51:16 -060053
Rob Clark9a0774e2012-01-16 12:51:17 -060054/* update which fb (if any) is pinned for scanout */
Laurent Pincharta42133a2015-01-17 19:09:26 +020055static int omap_plane_update_pin(struct drm_plane *plane)
Rob Clark9a0774e2012-01-16 12:51:17 -060056{
57 struct omap_plane *omap_plane = to_omap_plane(plane);
Rob Clarkb33f34d2012-03-05 10:48:35 -060058 struct drm_framebuffer *pinned_fb = omap_plane->pinned_fb;
Laurent Pincharta42133a2015-01-17 19:09:26 +020059 struct drm_framebuffer *fb = omap_plane->enabled ? plane->fb : NULL;
60 int ret = 0;
Rob Clark9a0774e2012-01-16 12:51:17 -060061
Laurent Pincharta42133a2015-01-17 19:09:26 +020062 if (pinned_fb == fb)
63 return 0;
Rob Clarkb33f34d2012-03-05 10:48:35 -060064
Laurent Pincharta42133a2015-01-17 19:09:26 +020065 DBG("%p -> %p", pinned_fb, fb);
Rob Clarkb33f34d2012-03-05 10:48:35 -060066
Laurent Pincharta42133a2015-01-17 19:09:26 +020067 if (fb) {
68 drm_framebuffer_reference(fb);
69 ret = omap_framebuffer_pin(fb);
Rob Clark9a0774e2012-01-16 12:51:17 -060070 }
71
Laurent Pincharta42133a2015-01-17 19:09:26 +020072 if (pinned_fb)
73 omap_crtc_queue_unpin(plane->crtc, pinned_fb);
74
75 if (ret) {
76 dev_err(plane->dev->dev, "could not swap %p -> %p\n",
77 omap_plane->pinned_fb, fb);
78 drm_framebuffer_unreference(fb);
79 omap_plane->pinned_fb = NULL;
80 return ret;
81 }
82
83 omap_plane->pinned_fb = fb;
84
Rob Clarkb33f34d2012-03-05 10:48:35 -060085 return 0;
Rob Clark9a0774e2012-01-16 12:51:17 -060086}
87
Laurent Pincharta42133a2015-01-17 19:09:26 +020088static int omap_plane_setup(struct omap_plane *omap_plane)
Rob Clarkbb5c2d92012-01-16 12:51:16 -060089{
Laurent Pincharta42133a2015-01-17 19:09:26 +020090 struct omap_overlay_info *info = &omap_plane->info;
Rob Clarkf5f94542012-12-04 13:59:12 -060091 struct drm_plane *plane = &omap_plane->base;
92 struct drm_device *dev = plane->dev;
Rob Clarkf5f94542012-12-04 13:59:12 -060093 struct drm_crtc *crtc = plane->crtc;
Rob Clark9a0774e2012-01-16 12:51:17 -060094 int ret;
Rob Clarkbb5c2d92012-01-16 12:51:16 -060095
Laurent Pincharta42133a2015-01-17 19:09:26 +020096 DBG("%s, enabled=%d", omap_plane->name, omap_plane->enabled);
Rob Clarkf5f94542012-12-04 13:59:12 -060097
98 /* if fb has changed, pin new fb: */
Laurent Pincharta42133a2015-01-17 19:09:26 +020099 ret = omap_plane_update_pin(plane);
100 if (ret)
101 return ret;
Rob Clarkf5f94542012-12-04 13:59:12 -0600102
Laurent Pincharta42133a2015-01-17 19:09:26 +0200103 dispc_runtime_get();
104
105 if (!omap_plane->enabled) {
Rob Clarkf5f94542012-12-04 13:59:12 -0600106 dispc_ovl_enable(omap_plane->id, false);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200107 goto done;
Rob Clark9a0774e2012-01-16 12:51:17 -0600108 }
109
Rob Clarkf5f94542012-12-04 13:59:12 -0600110 /* update scanout: */
Laurent Pincharta42133a2015-01-17 19:09:26 +0200111 omap_framebuffer_update_scanout(plane->fb, &omap_plane->win, info);
Rob Clark9a0774e2012-01-16 12:51:17 -0600112
Rob Clarkf5f94542012-12-04 13:59:12 -0600113 DBG("%dx%d -> %dx%d (%d)", info->width, info->height,
114 info->out_width, info->out_height,
Rob Clark9a0774e2012-01-16 12:51:17 -0600115 info->screen_width);
Russell King2d31ca32014-07-12 10:53:41 +0100116 DBG("%d,%d %pad %pad", info->pos_x, info->pos_y,
117 &info->paddr, &info->p_uv_addr);
Rob Clarkf5f94542012-12-04 13:59:12 -0600118
Laurent Pincharta42133a2015-01-17 19:09:26 +0200119 dispc_ovl_set_channel_out(omap_plane->id,
120 omap_crtc_channel(crtc));
Tomi Valkeinen2dd38872014-10-03 15:14:08 +0000121
Rob Clarkf5f94542012-12-04 13:59:12 -0600122 /* and finally, update omapdss: */
Laurent Pinchart9c660b72015-01-12 16:44:03 +0200123 ret = dispc_ovl_setup(omap_plane->id, info, false,
124 omap_crtc_timings(crtc), false);
Rob Clarkf5f94542012-12-04 13:59:12 -0600125 if (ret) {
126 dev_err(dev->dev, "dispc_ovl_setup failed: %d\n", ret);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200127 goto done;
Rob Clarkf5f94542012-12-04 13:59:12 -0600128 }
129
130 dispc_ovl_enable(omap_plane->id, true);
Rob Clarkf5f94542012-12-04 13:59:12 -0600131
Laurent Pincharta42133a2015-01-17 19:09:26 +0200132done:
133 dispc_runtime_put();
134 return ret;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600135}
136
Rob Clark2f537002012-01-16 12:51:18 -0600137int omap_plane_mode_set(struct drm_plane *plane,
Laurent Pincharta350da82015-01-17 22:31:42 +0200138 struct drm_crtc *crtc, struct drm_framebuffer *fb,
139 int crtc_x, int crtc_y,
140 unsigned int crtc_w, unsigned int crtc_h,
141 unsigned int src_x, unsigned int src_y,
Laurent Pincharta42133a2015-01-17 19:09:26 +0200142 unsigned int src_w, unsigned int src_h)
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600143{
144 struct omap_plane *omap_plane = to_omap_plane(plane);
Rob Clark3c810c62012-08-15 15:18:01 -0500145 struct omap_drm_window *win = &omap_plane->win;
146
147 win->crtc_x = crtc_x;
148 win->crtc_y = crtc_y;
149 win->crtc_w = crtc_w;
150 win->crtc_h = crtc_h;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600151
Laurent Pincharta350da82015-01-17 22:31:42 +0200152 win->src_x = src_x;
153 win->src_y = src_y;
154 win->src_w = src_w;
155 win->src_h = src_h;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600156
Laurent Pincharta42133a2015-01-17 19:09:26 +0200157 return omap_plane_setup(omap_plane);
158}
Rob Clarkf5f94542012-12-04 13:59:12 -0600159
Laurent Pincharta42133a2015-01-17 19:09:26 +0200160int omap_plane_set_enable(struct drm_plane *plane, bool enable)
161{
162 struct omap_plane *omap_plane = to_omap_plane(plane);
Rob Clarkf5f94542012-12-04 13:59:12 -0600163
Laurent Pincharta42133a2015-01-17 19:09:26 +0200164 if (enable == omap_plane->enabled)
165 return 0;
166
167 omap_plane->enabled = enable;
168 return omap_plane_setup(omap_plane);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600169}
170
Rob Clark2f537002012-01-16 12:51:18 -0600171static int omap_plane_update(struct drm_plane *plane,
172 struct drm_crtc *crtc, struct drm_framebuffer *fb,
173 int crtc_x, int crtc_y,
174 unsigned int crtc_w, unsigned int crtc_h,
175 uint32_t src_x, uint32_t src_y,
176 uint32_t src_w, uint32_t src_h)
177{
Rob Clarkf5f94542012-12-04 13:59:12 -0600178 struct omap_plane *omap_plane = to_omap_plane(plane);
Laurent Pincharta42133a2015-01-17 19:09:26 +0200179 int ret;
180
Rob Clarkf5f94542012-12-04 13:59:12 -0600181 omap_plane->enabled = true;
Archit Tanejab03e14f2013-04-09 15:26:00 +0300182
Grazvydas Ignotasd4586602014-04-05 21:33:51 +0300183 /* omap_plane_mode_set() takes adjusted src */
184 switch (omap_plane->win.rotation & 0xf) {
185 case BIT(DRM_ROTATE_90):
186 case BIT(DRM_ROTATE_270):
187 swap(src_w, src_h);
188 break;
189 }
190
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200191 /*
192 * We don't need to take a reference to the framebuffer as the DRM core
193 * has already done so for the purpose of setting plane->fb.
194 */
195 plane->fb = fb;
196 plane->crtc = crtc;
197
Laurent Pincharta350da82015-01-17 22:31:42 +0200198 /* src values are in Q16 fixed point, convert to integer: */
Laurent Pincharta42133a2015-01-17 19:09:26 +0200199 ret = omap_plane_mode_set(plane, crtc, fb,
200 crtc_x, crtc_y, crtc_w, crtc_h,
201 src_x >> 16, src_y >> 16,
202 src_w >> 16, src_h >> 16);
203 if (ret < 0)
204 return ret;
205
206 return omap_crtc_flush(plane->crtc);
Rob Clark2f537002012-01-16 12:51:18 -0600207}
208
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600209static int omap_plane_disable(struct drm_plane *plane)
210{
Rob Clark3c810c62012-08-15 15:18:01 -0500211 struct omap_plane *omap_plane = to_omap_plane(plane);
Laurent Pinchart2debab92015-01-12 22:38:16 +0200212
Rob Clark3c810c62012-08-15 15:18:01 -0500213 omap_plane->win.rotation = BIT(DRM_ROTATE_0);
Laurent Pinchart82e588552015-01-12 23:56:57 +0200214 omap_plane->info.zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
215 ? 0 : omap_plane->id;
216
Laurent Pincharta42133a2015-01-17 19:09:26 +0200217 if (!omap_plane->enabled)
218 return 0;
219
220 /* Disabling a plane never fails. */
221 omap_plane->enabled = false;
222 omap_plane_setup(omap_plane);
223
224 return omap_crtc_flush(plane->crtc);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600225}
226
227static void omap_plane_destroy(struct drm_plane *plane)
228{
229 struct omap_plane *omap_plane = to_omap_plane(plane);
Rob Clarkf5f94542012-12-04 13:59:12 -0600230
231 DBG("%s", omap_plane->name);
232
233 omap_irq_unregister(plane->dev, &omap_plane->error_irq);
234
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600235 drm_plane_cleanup(plane);
Rob Clarkf5f94542012-12-04 13:59:12 -0600236
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600237 kfree(omap_plane);
238}
239
Rob Clark3c810c62012-08-15 15:18:01 -0500240/* helper to install properties which are common to planes and crtcs */
241void omap_plane_install_properties(struct drm_plane *plane,
242 struct drm_mode_object *obj)
243{
244 struct drm_device *dev = plane->dev;
245 struct omap_drm_private *priv = dev->dev_private;
Rob Clark3c810c62012-08-15 15:18:01 -0500246
Rob Clarkc2a6a552012-10-25 17:14:13 -0500247 if (priv->has_dmm) {
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200248 struct drm_property *prop = dev->mode_config.rotation_property;
249
Rob Clarkc2a6a552012-10-25 17:14:13 -0500250 drm_object_attach_property(obj, prop, 0);
Rob Clark3c810c62012-08-15 15:18:01 -0500251 }
Andre Renaud8451b5a2012-08-15 15:18:02 -0500252
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200253 drm_object_attach_property(obj, priv->zorder_prop, 0);
Rob Clark3c810c62012-08-15 15:18:01 -0500254}
255
256int omap_plane_set_property(struct drm_plane *plane,
257 struct drm_property *property, uint64_t val)
258{
259 struct omap_plane *omap_plane = to_omap_plane(plane);
260 struct omap_drm_private *priv = plane->dev->dev_private;
Laurent Pincharta42133a2015-01-17 19:09:26 +0200261 int ret;
Rob Clark3c810c62012-08-15 15:18:01 -0500262
Laurent Pincharte2cd09b2015-03-06 17:16:43 +0200263 if (property == plane->dev->mode_config.rotation_property) {
Rob Clarkf5f94542012-12-04 13:59:12 -0600264 DBG("%s: rotation: %02x", omap_plane->name, (uint32_t)val);
Rob Clark3c810c62012-08-15 15:18:01 -0500265 omap_plane->win.rotation = val;
Andre Renaud8451b5a2012-08-15 15:18:02 -0500266 } else if (property == priv->zorder_prop) {
Rob Clarkf5f94542012-12-04 13:59:12 -0600267 DBG("%s: zorder: %02x", omap_plane->name, (uint32_t)val);
Andre Renaud8451b5a2012-08-15 15:18:02 -0500268 omap_plane->info.zorder = val;
Laurent Pincharta42133a2015-01-17 19:09:26 +0200269 } else {
270 return -EINVAL;
Rob Clark3c810c62012-08-15 15:18:01 -0500271 }
272
Laurent Pincharta42133a2015-01-17 19:09:26 +0200273 /*
274 * We're done if the plane is disabled, properties will be applied the
275 * next time it becomes enabled.
276 */
277 if (!omap_plane->enabled)
278 return 0;
279
280 ret = omap_plane_setup(omap_plane);
281 if (ret < 0)
282 return ret;
283
284 return omap_crtc_flush(plane->crtc);
Rob Clark3c810c62012-08-15 15:18:01 -0500285}
286
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600287static const struct drm_plane_funcs omap_plane_funcs = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200288 .update_plane = omap_plane_update,
289 .disable_plane = omap_plane_disable,
290 .destroy = omap_plane_destroy,
291 .set_property = omap_plane_set_property,
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600292};
293
Rob Clarkf5f94542012-12-04 13:59:12 -0600294static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
295{
296 struct omap_plane *omap_plane =
297 container_of(irq, struct omap_plane, error_irq);
Tomi Valkeinen3b143fc2014-11-19 12:50:13 +0200298 DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
299 irqstatus);
Rob Clarkf5f94542012-12-04 13:59:12 -0600300}
301
302static const char *plane_names[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200303 [OMAP_DSS_GFX] = "gfx",
304 [OMAP_DSS_VIDEO1] = "vid1",
305 [OMAP_DSS_VIDEO2] = "vid2",
306 [OMAP_DSS_VIDEO3] = "vid3",
Rob Clarkf5f94542012-12-04 13:59:12 -0600307};
308
309static const uint32_t error_irqs[] = {
Laurent Pinchart222025e2015-01-11 00:02:07 +0200310 [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
311 [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
312 [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
313 [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
Rob Clarkf5f94542012-12-04 13:59:12 -0600314};
315
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600316/* initialize plane */
317struct drm_plane *omap_plane_init(struct drm_device *dev,
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200318 int id, enum drm_plane_type type)
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600319{
Rob Clarkf5f94542012-12-04 13:59:12 -0600320 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200321 struct drm_plane *plane;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600322 struct omap_plane *omap_plane;
Rob Clarkf5f94542012-12-04 13:59:12 -0600323 struct omap_overlay_info *info;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200324 int ret;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600325
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200326 DBG("%s: type=%d", plane_names[id], type);
Rob Clarkb33f34d2012-03-05 10:48:35 -0600327
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600328 omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800329 if (!omap_plane)
Laurent Pinchartfb9a35f2015-01-11 16:30:44 +0200330 return ERR_PTR(-ENOMEM);
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600331
Rob Clarka890e662012-03-05 10:48:31 -0600332 omap_plane->nformats = omap_framebuffer_get_formats(
333 omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
Rob Clarkf5f94542012-12-04 13:59:12 -0600334 dss_feat_get_supported_color_modes(id));
335 omap_plane->id = id;
336 omap_plane->name = plane_names[id];
337
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600338 plane = &omap_plane->base;
339
Rob Clarkf5f94542012-12-04 13:59:12 -0600340 omap_plane->error_irq.irqmask = error_irqs[id];
341 omap_plane->error_irq.irq = omap_plane_error_irq;
342 omap_irq_register(dev, &omap_plane->error_irq);
343
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200344 ret = drm_universal_plane_init(dev, plane, (1 << priv->num_crtcs) - 1,
345 &omap_plane_funcs, omap_plane->formats,
346 omap_plane->nformats, type);
347 if (ret < 0)
348 goto error;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600349
Rob Clark3c810c62012-08-15 15:18:01 -0500350 omap_plane_install_properties(plane, &plane->base);
351
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600352 /* get our starting configuration, set defaults for parameters
353 * we don't currently use, etc:
354 */
Rob Clarkf5f94542012-12-04 13:59:12 -0600355 info = &omap_plane->info;
356 info->rotation_type = OMAP_DSS_ROT_DMA;
357 info->rotation = OMAP_DSS_ROT_0;
358 info->global_alpha = 0xff;
359 info->mirror = 0;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600360
361 /* Set defaults depending on whether we are a CRTC or overlay
362 * layer.
363 * TODO add ioctl to give userspace an API to change this.. this
364 * will come in a subsequent patch.
365 */
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200366 if (type == DRM_PLANE_TYPE_PRIMARY)
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600367 omap_plane->info.zorder = 0;
368 else
Rob Clarkf5f94542012-12-04 13:59:12 -0600369 omap_plane->info.zorder = id;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600370
371 return plane;
Laurent Pinchartef6b0e02015-01-11 00:11:18 +0200372
373error:
374 omap_irq_unregister(plane->dev, &omap_plane->error_irq);
375 kfree(omap_plane);
376 return NULL;
Rob Clarkbb5c2d92012-01-16 12:51:16 -0600377}