blob: 45dd9eed9c57a80ea0b74986d1b962e94548dae6 [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_fb.c
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
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 Clarkcd5351f2011-11-12 12:09:40 -060022
23#include "drm_crtc.h"
24#include "drm_crtc_helper.h"
25
Rob Clarkcd5351f2011-11-12 12:09:40 -060026/*
27 * framebuffer funcs
28 */
29
Rob Clarkae43d7c2012-01-16 12:51:15 -060030/* per-format info: */
31struct format {
32 enum omap_color_mode dss_format;
33 uint32_t pixel_format;
34 struct {
35 int stride_bpp; /* this times width is stride */
36 int sub_y; /* sub-sample in y dimension */
37 } planes[4];
38 bool yuv;
39};
40
41static const struct format formats[] = {
42 /* 16bpp [A]RGB: */
43 { OMAP_DSS_COLOR_RGB16, DRM_FORMAT_RGB565, {{2, 1}}, false }, /* RGB16-565 */
44 { OMAP_DSS_COLOR_RGB12U, DRM_FORMAT_RGBX4444, {{2, 1}}, false }, /* RGB12x-4444 */
45 { OMAP_DSS_COLOR_RGBX16, DRM_FORMAT_XRGB4444, {{2, 1}}, false }, /* xRGB12-4444 */
46 { OMAP_DSS_COLOR_RGBA16, DRM_FORMAT_RGBA4444, {{2, 1}}, false }, /* RGBA12-4444 */
47 { OMAP_DSS_COLOR_ARGB16, DRM_FORMAT_ARGB4444, {{2, 1}}, false }, /* ARGB16-4444 */
48 { OMAP_DSS_COLOR_XRGB16_1555, DRM_FORMAT_XRGB1555, {{2, 1}}, false }, /* xRGB15-1555 */
49 { OMAP_DSS_COLOR_ARGB16_1555, DRM_FORMAT_ARGB1555, {{2, 1}}, false }, /* ARGB16-1555 */
50 /* 24bpp RGB: */
51 { OMAP_DSS_COLOR_RGB24P, DRM_FORMAT_RGB888, {{3, 1}}, false }, /* RGB24-888 */
52 /* 32bpp [A]RGB: */
53 { OMAP_DSS_COLOR_RGBX32, DRM_FORMAT_RGBX8888, {{4, 1}}, false }, /* RGBx24-8888 */
54 { OMAP_DSS_COLOR_RGB24U, DRM_FORMAT_XRGB8888, {{4, 1}}, false }, /* xRGB24-8888 */
55 { OMAP_DSS_COLOR_RGBA32, DRM_FORMAT_RGBA8888, {{4, 1}}, false }, /* RGBA32-8888 */
56 { OMAP_DSS_COLOR_ARGB32, DRM_FORMAT_ARGB8888, {{4, 1}}, false }, /* ARGB32-8888 */
57 /* YUV: */
58 { OMAP_DSS_COLOR_NV12, DRM_FORMAT_NV12, {{1, 1}, {1, 2}}, true },
59 { OMAP_DSS_COLOR_YUV2, DRM_FORMAT_YUYV, {{2, 1}}, true },
60 { OMAP_DSS_COLOR_UYVY, DRM_FORMAT_UYVY, {{2, 1}}, true },
61};
62
Rob Clarka890e662012-03-05 10:48:31 -060063/* convert from overlay's pixel formats bitmask to an array of fourcc's */
64uint32_t omap_framebuffer_get_formats(uint32_t *pixel_formats,
65 uint32_t max_formats, enum omap_color_mode supported_modes)
66{
67 uint32_t nformats = 0;
68 int i = 0;
69
70 for (i = 0; i < ARRAY_SIZE(formats) && nformats < max_formats; i++)
71 if (formats[i].dss_format & supported_modes)
72 pixel_formats[nformats++] = formats[i].pixel_format;
73
74 return nformats;
75}
76
Rob Clark9a0774e2012-01-16 12:51:17 -060077/* per-plane info for the fb: */
78struct plane {
79 struct drm_gem_object *bo;
80 uint32_t pitch;
81 uint32_t offset;
82 dma_addr_t paddr;
83};
84
Rob Clarkcd5351f2011-11-12 12:09:40 -060085#define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
86
87struct omap_framebuffer {
88 struct drm_framebuffer base;
Rob Clarkae43d7c2012-01-16 12:51:15 -060089 const struct format *format;
Rob Clark9a0774e2012-01-16 12:51:17 -060090 struct plane planes[4];
Rob Clarkcd5351f2011-11-12 12:09:40 -060091};
92
93static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
94 struct drm_file *file_priv,
95 unsigned int *handle)
96{
97 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
Rob Clark9a0774e2012-01-16 12:51:17 -060098 return drm_gem_handle_create(file_priv,
99 omap_fb->planes[0].bo, handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600100}
101
102static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
103{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600104 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
Rob Clark9f18c952012-03-05 10:48:34 -0600105 int i, n = drm_format_num_planes(fb->pixel_format);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600106
107 DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
108
109 drm_framebuffer_cleanup(fb);
110
Rob Clark9a0774e2012-01-16 12:51:17 -0600111 for (i = 0; i < n; i++) {
112 struct plane *plane = &omap_fb->planes[i];
113 if (plane->bo)
114 drm_gem_object_unreference_unlocked(plane->bo);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600115 }
116
117 kfree(omap_fb);
118}
119
120static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
121 struct drm_file *file_priv, unsigned flags, unsigned color,
122 struct drm_clip_rect *clips, unsigned num_clips)
123{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600124 return 0;
125}
126
127static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
128 .create_handle = omap_framebuffer_create_handle,
129 .destroy = omap_framebuffer_destroy,
130 .dirty = omap_framebuffer_dirty,
131};
132
Rob Clark3c810c62012-08-15 15:18:01 -0500133static uint32_t get_linear_addr(struct plane *plane,
134 const struct format *format, int n, int x, int y)
135{
136 uint32_t offset;
137
138 offset = plane->offset +
139 (x * format->planes[n].stride_bpp) +
140 (y * plane->pitch / format->planes[n].sub_y);
141
142 return plane->paddr + offset;
143}
144
Rob Clark9a0774e2012-01-16 12:51:17 -0600145/* update ovl info for scanout, handles cases of multi-planar fb's, etc.
146 */
Rob Clark3c810c62012-08-15 15:18:01 -0500147void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
148 struct omap_drm_window *win, struct omap_overlay_info *info)
Rob Clark9a0774e2012-01-16 12:51:17 -0600149{
150 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
151 const struct format *format = omap_fb->format;
152 struct plane *plane = &omap_fb->planes[0];
Rob Clark3c810c62012-08-15 15:18:01 -0500153 uint32_t x, y, orient = 0;
Rob Clark9a0774e2012-01-16 12:51:17 -0600154
Rob Clark3c810c62012-08-15 15:18:01 -0500155 info->color_mode = format->dss_format;
Rob Clark9a0774e2012-01-16 12:51:17 -0600156
Rob Clark3c810c62012-08-15 15:18:01 -0500157 info->pos_x = win->crtc_x;
158 info->pos_y = win->crtc_y;
159 info->out_width = win->crtc_w;
160 info->out_height = win->crtc_h;
161 info->width = win->src_w;
162 info->height = win->src_h;
163
164 x = win->src_x;
165 y = win->src_y;
166
167 if (omap_gem_flags(plane->bo) & OMAP_BO_TILED) {
168 uint32_t w = win->src_w;
169 uint32_t h = win->src_h;
170
171 switch (win->rotation & 0xf) {
172 default:
173 dev_err(fb->dev->dev, "invalid rotation: %02x",
174 (uint32_t)win->rotation);
175 /* fallthru to default to no rotation */
176 case 0:
177 case BIT(DRM_ROTATE_0):
178 orient = 0;
179 break;
180 case BIT(DRM_ROTATE_90):
181 orient = MASK_XY_FLIP | MASK_X_INVERT;
182 break;
183 case BIT(DRM_ROTATE_180):
184 orient = MASK_X_INVERT | MASK_Y_INVERT;
185 break;
186 case BIT(DRM_ROTATE_270):
187 orient = MASK_XY_FLIP | MASK_Y_INVERT;
188 break;
189 }
190
191 if (win->rotation & BIT(DRM_REFLECT_X))
192 orient ^= MASK_X_INVERT;
193
194 if (win->rotation & BIT(DRM_REFLECT_Y))
195 orient ^= MASK_Y_INVERT;
196
197 /* adjust x,y offset for flip/invert: */
198 if (orient & MASK_XY_FLIP)
199 swap(w, h);
200 if (orient & MASK_Y_INVERT)
201 y += h - 1;
202 if (orient & MASK_X_INVERT)
203 x += w - 1;
204
205 omap_gem_rotated_paddr(plane->bo, orient, x, y, &info->paddr);
206 info->rotation_type = OMAP_DSS_ROT_TILER;
207 info->screen_width = omap_gem_tiled_stride(plane->bo, orient);
208 } else {
Tomi Valkeinen5ac96342014-04-08 16:18:41 +0300209 switch (win->rotation & 0xf) {
210 case 0:
211 case BIT(DRM_ROTATE_0):
212 /* OK */
213 break;
214
215 default:
216 dev_warn(fb->dev->dev,
217 "rotation '%d' ignored for non-tiled fb\n",
218 win->rotation);
219 win->rotation = 0;
220 break;
221 }
222
Rob Clark3c810c62012-08-15 15:18:01 -0500223 info->paddr = get_linear_addr(plane, format, 0, x, y);
224 info->rotation_type = OMAP_DSS_ROT_DMA;
225 info->screen_width = plane->pitch;
226 }
227
228 /* convert to pixels: */
229 info->screen_width /= format->planes[0].stride_bpp;
Rob Clark9a0774e2012-01-16 12:51:17 -0600230
231 if (format->dss_format == OMAP_DSS_COLOR_NV12) {
232 plane = &omap_fb->planes[1];
Rob Clark3c810c62012-08-15 15:18:01 -0500233
234 if (info->rotation_type == OMAP_DSS_ROT_TILER) {
235 WARN_ON(!(omap_gem_flags(plane->bo) & OMAP_BO_TILED));
236 omap_gem_rotated_paddr(plane->bo, orient,
237 x/2, y/2, &info->p_uv_addr);
238 } else {
239 info->p_uv_addr = get_linear_addr(plane, format, 1, x, y);
240 }
Rob Clark9a0774e2012-01-16 12:51:17 -0600241 } else {
242 info->p_uv_addr = 0;
243 }
244}
245
Rob Clark5833bd22013-08-07 13:41:21 -0400246/* pin, prepare for scanout: */
247int omap_framebuffer_pin(struct drm_framebuffer *fb)
Rob Clarkb33f34d2012-03-05 10:48:35 -0600248{
Rob Clark5833bd22013-08-07 13:41:21 -0400249 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
250 int ret, i, n = drm_format_num_planes(fb->pixel_format);
Rob Clarkb33f34d2012-03-05 10:48:35 -0600251
Rob Clark5833bd22013-08-07 13:41:21 -0400252 for (i = 0; i < n; i++) {
253 struct plane *plane = &omap_fb->planes[i];
254 ret = omap_gem_get_paddr(plane->bo, &plane->paddr, true);
255 if (ret)
256 goto fail;
257 omap_gem_dma_sync(plane->bo, DMA_TO_DEVICE);
Rob Clarkb33f34d2012-03-05 10:48:35 -0600258 }
259
Rob Clark5833bd22013-08-07 13:41:21 -0400260 return 0;
261
262fail:
263 for (i--; i >= 0; i--) {
264 struct plane *plane = &omap_fb->planes[i];
265 omap_gem_put_paddr(plane->bo);
266 plane->paddr = 0;
Rob Clarkb33f34d2012-03-05 10:48:35 -0600267 }
268
269 return ret;
270}
271
Rob Clark5833bd22013-08-07 13:41:21 -0400272/* unpin, no longer being scanned out: */
273int omap_framebuffer_unpin(struct drm_framebuffer *fb)
274{
275 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
276 int ret, i, n = drm_format_num_planes(fb->pixel_format);
277
278 for (i = 0; i < n; i++) {
279 struct plane *plane = &omap_fb->planes[i];
280 ret = omap_gem_put_paddr(plane->bo);
281 if (ret)
282 goto fail;
283 plane->paddr = 0;
284 }
285
286 return 0;
287
288fail:
289 return ret;
290}
291
Rob Clark9a0774e2012-01-16 12:51:17 -0600292struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb, int p)
293{
294 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
Rob Clark9f18c952012-03-05 10:48:34 -0600295 if (p >= drm_format_num_planes(fb->pixel_format))
Rob Clark9a0774e2012-01-16 12:51:17 -0600296 return NULL;
297 return omap_fb->planes[p].bo;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600298}
299
300/* iterate thru all the connectors, returning ones that are attached
301 * to the same fb..
302 */
303struct drm_connector *omap_framebuffer_get_next_connector(
304 struct drm_framebuffer *fb, struct drm_connector *from)
305{
306 struct drm_device *dev = fb->dev;
307 struct list_head *connector_list = &dev->mode_config.connector_list;
308 struct drm_connector *connector = from;
309
YAMANE Toshiakiddcd49e2012-11-14 19:32:17 +0900310 if (!from)
Laurent Pinchart06fb2202013-12-24 12:58:01 +0100311 return list_first_entry_or_null(connector_list, typeof(*from),
312 head);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600313
314 list_for_each_entry_from(connector, connector_list, head) {
315 if (connector != from) {
316 struct drm_encoder *encoder = connector->encoder;
317 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
Matt Roperf4510a22014-04-01 15:22:40 -0700318 if (crtc && crtc->primary->fb == fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600319 return connector;
YAMANE Toshiakiddcd49e2012-11-14 19:32:17 +0900320
Rob Clarkcd5351f2011-11-12 12:09:40 -0600321 }
322 }
323
324 return NULL;
325}
326
Rob Clarkf6b60362012-03-05 10:48:36 -0600327#ifdef CONFIG_DEBUG_FS
328void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
329{
330 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
331 int i, n = drm_format_num_planes(fb->pixel_format);
332
333 seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
334 (char *)&fb->pixel_format);
335
336 for (i = 0; i < n; i++) {
337 struct plane *plane = &omap_fb->planes[i];
338 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
339 i, plane->offset, plane->pitch);
340 omap_gem_describe(plane->bo, m);
341 }
342}
343#endif
344
Rob Clarkcd5351f2011-11-12 12:09:40 -0600345struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
Rob Clarkae43d7c2012-01-16 12:51:15 -0600346 struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600347{
Rob Clarkae43d7c2012-01-16 12:51:15 -0600348 struct drm_gem_object *bos[4];
Rob Clarkcd5351f2011-11-12 12:09:40 -0600349 struct drm_framebuffer *fb;
Rob Clarkae43d7c2012-01-16 12:51:15 -0600350 int ret;
351
352 ret = objects_lookup(dev, file, mode_cmd->pixel_format,
353 bos, mode_cmd->handles);
354 if (ret)
355 return ERR_PTR(ret);
356
357 fb = omap_framebuffer_init(dev, mode_cmd, bos);
358 if (IS_ERR(fb)) {
359 int i, n = drm_format_num_planes(mode_cmd->pixel_format);
360 for (i = 0; i < n; i++)
361 drm_gem_object_unreference_unlocked(bos[i]);
362 return fb;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600363 }
364 return fb;
365}
366
367struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
Rob Clarkae43d7c2012-01-16 12:51:15 -0600368 struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600369{
370 struct omap_framebuffer *omap_fb;
371 struct drm_framebuffer *fb = NULL;
Rob Clarkae43d7c2012-01-16 12:51:15 -0600372 const struct format *format = NULL;
Rob Clark9a0774e2012-01-16 12:51:17 -0600373 int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600374
Rob Clarkae43d7c2012-01-16 12:51:15 -0600375 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
Rob Clarkcd5351f2011-11-12 12:09:40 -0600376 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
Rob Clarkae43d7c2012-01-16 12:51:15 -0600377 (char *)&mode_cmd->pixel_format);
378
379 for (i = 0; i < ARRAY_SIZE(formats); i++) {
380 if (formats[i].pixel_format == mode_cmd->pixel_format) {
381 format = &formats[i];
382 break;
383 }
384 }
385
386 if (!format) {
387 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
388 (char *)&mode_cmd->pixel_format);
389 ret = -EINVAL;
390 goto fail;
391 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600392
Rob Clarkcd5351f2011-11-12 12:09:40 -0600393 omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
394 if (!omap_fb) {
Rob Clarkae43d7c2012-01-16 12:51:15 -0600395 ret = -ENOMEM;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600396 goto fail;
397 }
398
399 fb = &omap_fb->base;
Rob Clark9a0774e2012-01-16 12:51:17 -0600400 omap_fb->format = format;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600401
Rob Clark9a0774e2012-01-16 12:51:17 -0600402 for (i = 0; i < n; i++) {
403 struct plane *plane = &omap_fb->planes[i];
404 int size, pitch = mode_cmd->pitches[i];
Rob Clarkcd5351f2011-11-12 12:09:40 -0600405
Rob Clark9a0774e2012-01-16 12:51:17 -0600406 if (pitch < (mode_cmd->width * format->planes[i].stride_bpp)) {
407 dev_err(dev->dev, "provided buffer pitch is too small! %d < %d\n",
408 pitch, mode_cmd->width * format->planes[i].stride_bpp);
409 ret = -EINVAL;
410 goto fail;
411 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600412
Rob Clark9a0774e2012-01-16 12:51:17 -0600413 size = pitch * mode_cmd->height / format->planes[i].sub_y;
414
Rob Clark3c810c62012-08-15 15:18:01 -0500415 if (size > (omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i])) {
Rob Clark9a0774e2012-01-16 12:51:17 -0600416 dev_err(dev->dev, "provided buffer object is too small! %d < %d\n",
417 bos[i]->size - mode_cmd->offsets[i], size);
418 ret = -EINVAL;
419 goto fail;
420 }
421
422 plane->bo = bos[i];
423 plane->offset = mode_cmd->offsets[i];
Rob Clark9f18c952012-03-05 10:48:34 -0600424 plane->pitch = pitch;
425 plane->paddr = 0;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600426 }
427
428 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
429
Daniel Vetterc7d73f62012-12-13 23:38:38 +0100430 ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
431 if (ret) {
432 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
433 goto fail;
434 }
435
436 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
437
Rob Clarkcd5351f2011-11-12 12:09:40 -0600438 return fb;
439
440fail:
YAMANE Toshiakiddcd49e2012-11-14 19:32:17 +0900441 if (fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600442 omap_framebuffer_destroy(fb);
YAMANE Toshiakiddcd49e2012-11-14 19:32:17 +0900443
Rob Clarkae43d7c2012-01-16 12:51:15 -0600444 return ERR_PTR(ret);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600445}