]> Pileus Git - ~andy/linux/blob - drivers/staging/omapdrm/omap_fb.c
Merge tag 'tag-for-linus-3.5' of git://git.linaro.org/people/sumitsemwal/linux-dma-buf
[~andy/linux] / drivers / staging / omapdrm / omap_fb.c
1 /*
2  * drivers/staging/omapdrm/omap_fb.c
3  *
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"
21
22 #include "drm_crtc.h"
23 #include "drm_crtc_helper.h"
24
25 /*
26  * framebuffer funcs
27  */
28
29 /* per-format info: */
30 struct format {
31         enum omap_color_mode dss_format;
32         uint32_t pixel_format;
33         struct {
34                 int stride_bpp;           /* this times width is stride */
35                 int sub_y;                /* sub-sample in y dimension */
36         } planes[4];
37         bool yuv;
38 };
39
40 static const struct format formats[] = {
41         /* 16bpp [A]RGB: */
42         { OMAP_DSS_COLOR_RGB16,       DRM_FORMAT_RGB565,   {{2, 1}}, false }, /* RGB16-565 */
43         { OMAP_DSS_COLOR_RGB12U,      DRM_FORMAT_RGBX4444, {{2, 1}}, false }, /* RGB12x-4444 */
44         { OMAP_DSS_COLOR_RGBX16,      DRM_FORMAT_XRGB4444, {{2, 1}}, false }, /* xRGB12-4444 */
45         { OMAP_DSS_COLOR_RGBA16,      DRM_FORMAT_RGBA4444, {{2, 1}}, false }, /* RGBA12-4444 */
46         { OMAP_DSS_COLOR_ARGB16,      DRM_FORMAT_ARGB4444, {{2, 1}}, false }, /* ARGB16-4444 */
47         { OMAP_DSS_COLOR_XRGB16_1555, DRM_FORMAT_XRGB1555, {{2, 1}}, false }, /* xRGB15-1555 */
48         { OMAP_DSS_COLOR_ARGB16_1555, DRM_FORMAT_ARGB1555, {{2, 1}}, false }, /* ARGB16-1555 */
49         /* 24bpp RGB: */
50         { OMAP_DSS_COLOR_RGB24P,      DRM_FORMAT_RGB888,   {{3, 1}}, false }, /* RGB24-888 */
51         /* 32bpp [A]RGB: */
52         { OMAP_DSS_COLOR_RGBX32,      DRM_FORMAT_RGBX8888, {{4, 1}}, false }, /* RGBx24-8888 */
53         { OMAP_DSS_COLOR_RGB24U,      DRM_FORMAT_XRGB8888, {{4, 1}}, false }, /* xRGB24-8888 */
54         { OMAP_DSS_COLOR_RGBA32,      DRM_FORMAT_RGBA8888, {{4, 1}}, false }, /* RGBA32-8888 */
55         { OMAP_DSS_COLOR_ARGB32,      DRM_FORMAT_ARGB8888, {{4, 1}}, false }, /* ARGB32-8888 */
56         /* YUV: */
57         { OMAP_DSS_COLOR_NV12,        DRM_FORMAT_NV12,     {{1, 1}, {1, 2}}, true },
58         { OMAP_DSS_COLOR_YUV2,        DRM_FORMAT_YUYV,     {{2, 1}}, true },
59         { OMAP_DSS_COLOR_UYVY,        DRM_FORMAT_UYVY,     {{2, 1}}, true },
60 };
61
62 /* convert from overlay's pixel formats bitmask to an array of fourcc's */
63 uint32_t omap_framebuffer_get_formats(uint32_t *pixel_formats,
64                 uint32_t max_formats, enum omap_color_mode supported_modes)
65 {
66         uint32_t nformats = 0;
67         int i = 0;
68
69         for (i = 0; i < ARRAY_SIZE(formats) && nformats < max_formats; i++)
70                 if (formats[i].dss_format & supported_modes)
71                         pixel_formats[nformats++] = formats[i].pixel_format;
72
73         return nformats;
74 }
75
76 /* per-plane info for the fb: */
77 struct plane {
78         struct drm_gem_object *bo;
79         uint32_t pitch;
80         uint32_t offset;
81         dma_addr_t paddr;
82 };
83
84 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
85
86 struct omap_framebuffer {
87         struct drm_framebuffer base;
88         const struct format *format;
89         struct plane planes[4];
90 };
91
92 static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
93                 struct drm_file *file_priv,
94                 unsigned int *handle)
95 {
96         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
97         return drm_gem_handle_create(file_priv,
98                         omap_fb->planes[0].bo, handle);
99 }
100
101 static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
102 {
103         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
104         int i, n = drm_format_num_planes(fb->pixel_format);
105
106         DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
107
108         drm_framebuffer_cleanup(fb);
109
110         for (i = 0; i < n; i++) {
111                 struct plane *plane = &omap_fb->planes[i];
112                 if (plane->bo)
113                         drm_gem_object_unreference_unlocked(plane->bo);
114         }
115
116         kfree(omap_fb);
117 }
118
119 static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
120                 struct drm_file *file_priv, unsigned flags, unsigned color,
121                 struct drm_clip_rect *clips, unsigned num_clips)
122 {
123         int i;
124
125         for (i = 0; i < num_clips; i++) {
126                 omap_framebuffer_flush(fb, clips[i].x1, clips[i].y1,
127                                         clips[i].x2 - clips[i].x1,
128                                         clips[i].y2 - clips[i].y1);
129         }
130
131         return 0;
132 }
133
134 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
135         .create_handle = omap_framebuffer_create_handle,
136         .destroy = omap_framebuffer_destroy,
137         .dirty = omap_framebuffer_dirty,
138 };
139
140 /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
141  */
142 void omap_framebuffer_update_scanout(struct drm_framebuffer *fb, int x, int y,
143                 struct omap_overlay_info *info)
144 {
145         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
146         const struct format *format = omap_fb->format;
147         struct plane *plane = &omap_fb->planes[0];
148         unsigned int offset;
149
150         offset = plane->offset +
151                         (x * format->planes[0].stride_bpp) +
152                         (y * plane->pitch / format->planes[0].sub_y);
153
154         info->color_mode   = format->dss_format;
155         info->paddr        = plane->paddr + offset;
156         info->screen_width = plane->pitch / format->planes[0].stride_bpp;
157
158         if (format->dss_format == OMAP_DSS_COLOR_NV12) {
159                 plane = &omap_fb->planes[1];
160                 offset = plane->offset +
161                                 (x * format->planes[1].stride_bpp) +
162                                 (y * plane->pitch / format->planes[1].sub_y);
163                 info->p_uv_addr = plane->paddr + offset;
164         } else {
165                 info->p_uv_addr = 0;
166         }
167 }
168
169 /* Call for unpin 'a' (if not NULL), and pin 'b' (if not NULL).  Although
170  * buffers to unpin are just pushed to the unpin fifo so that the
171  * caller can defer unpin until vblank.
172  *
173  * Note if this fails (ie. something went very wrong!), all buffers are
174  * unpinned, and the caller disables the overlay.  We could have tried
175  * to revert back to the previous set of pinned buffers but if things are
176  * hosed there is no guarantee that would succeed.
177  */
178 int omap_framebuffer_replace(struct drm_framebuffer *a,
179                 struct drm_framebuffer *b, void *arg,
180                 void (*unpin)(void *arg, struct drm_gem_object *bo))
181 {
182         int ret = 0, i, na, nb;
183         struct omap_framebuffer *ofba = to_omap_framebuffer(a);
184         struct omap_framebuffer *ofbb = to_omap_framebuffer(b);
185
186         na = a ? drm_format_num_planes(a->pixel_format) : 0;
187         nb = b ? drm_format_num_planes(b->pixel_format) : 0;
188
189         for (i = 0; i < max(na, nb); i++) {
190                 struct plane *pa, *pb;
191
192                 pa = (i < na) ? &ofba->planes[i] : NULL;
193                 pb = (i < nb) ? &ofbb->planes[i] : NULL;
194
195                 if (pa) {
196                         unpin(arg, pa->bo);
197                         pa->paddr = 0;
198                 }
199
200                 if (pb && !ret) {
201                         ret = omap_gem_get_paddr(pb->bo, &pb->paddr, true);
202                         if (!ret)
203                                 omap_gem_dma_sync(pb->bo, DMA_TO_DEVICE);
204                 }
205         }
206
207         if (ret) {
208                 /* something went wrong.. unpin what has been pinned */
209                 for (i = 0; i < nb; i++) {
210                         struct plane *pb = &ofba->planes[i];
211                         if (pb->paddr) {
212                                 unpin(arg, pb->bo);
213                                 pb->paddr = 0;
214                         }
215                 }
216         }
217
218         return ret;
219 }
220
221 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb, int p)
222 {
223         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
224         if (p >= drm_format_num_planes(fb->pixel_format))
225                 return NULL;
226         return omap_fb->planes[p].bo;
227 }
228
229 /* iterate thru all the connectors, returning ones that are attached
230  * to the same fb..
231  */
232 struct drm_connector *omap_framebuffer_get_next_connector(
233                 struct drm_framebuffer *fb, struct drm_connector *from)
234 {
235         struct drm_device *dev = fb->dev;
236         struct list_head *connector_list = &dev->mode_config.connector_list;
237         struct drm_connector *connector = from;
238
239         if (!from) {
240                 return list_first_entry(connector_list, typeof(*from), head);
241         }
242
243         list_for_each_entry_from(connector, connector_list, head) {
244                 if (connector != from) {
245                         struct drm_encoder *encoder = connector->encoder;
246                         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
247                         if (crtc && crtc->fb == fb) {
248                                 return connector;
249                         }
250                 }
251         }
252
253         return NULL;
254 }
255
256 /* flush an area of the framebuffer (in case of manual update display that
257  * is not automatically flushed)
258  */
259 void omap_framebuffer_flush(struct drm_framebuffer *fb,
260                 int x, int y, int w, int h)
261 {
262         struct drm_connector *connector = NULL;
263
264         VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
265
266         while ((connector = omap_framebuffer_get_next_connector(fb, connector))) {
267                 /* only consider connectors that are part of a chain */
268                 if (connector->encoder && connector->encoder->crtc) {
269                         /* TODO: maybe this should propagate thru the crtc who
270                          * could do the coordinate translation..
271                          */
272                         struct drm_crtc *crtc = connector->encoder->crtc;
273                         int cx = max(0, x - crtc->x);
274                         int cy = max(0, y - crtc->y);
275                         int cw = w + (x - crtc->x) - cx;
276                         int ch = h + (y - crtc->y) - cy;
277
278                         omap_connector_flush(connector, cx, cy, cw, ch);
279                 }
280         }
281 }
282
283 #ifdef CONFIG_DEBUG_FS
284 void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
285 {
286         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
287         int i, n = drm_format_num_planes(fb->pixel_format);
288
289         seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
290                         (char *)&fb->pixel_format);
291
292         for (i = 0; i < n; i++) {
293                 struct plane *plane = &omap_fb->planes[i];
294                 seq_printf(m, "   %d: offset=%d pitch=%d, obj: ",
295                                 i, plane->offset, plane->pitch);
296                 omap_gem_describe(plane->bo, m);
297         }
298 }
299 #endif
300
301 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
302                 struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
303 {
304         struct drm_gem_object *bos[4];
305         struct drm_framebuffer *fb;
306         int ret;
307
308         ret = objects_lookup(dev, file, mode_cmd->pixel_format,
309                         bos, mode_cmd->handles);
310         if (ret)
311                 return ERR_PTR(ret);
312
313         fb = omap_framebuffer_init(dev, mode_cmd, bos);
314         if (IS_ERR(fb)) {
315                 int i, n = drm_format_num_planes(mode_cmd->pixel_format);
316                 for (i = 0; i < n; i++)
317                         drm_gem_object_unreference_unlocked(bos[i]);
318                 return fb;
319         }
320         return fb;
321 }
322
323 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
324                 struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
325 {
326         struct omap_framebuffer *omap_fb;
327         struct drm_framebuffer *fb = NULL;
328         const struct format *format = NULL;
329         int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
330
331         DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
332                         dev, mode_cmd, mode_cmd->width, mode_cmd->height,
333                         (char *)&mode_cmd->pixel_format);
334
335         for (i = 0; i < ARRAY_SIZE(formats); i++) {
336                 if (formats[i].pixel_format == mode_cmd->pixel_format) {
337                         format = &formats[i];
338                         break;
339                 }
340         }
341
342         if (!format) {
343                 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
344                                 (char *)&mode_cmd->pixel_format);
345                 ret = -EINVAL;
346                 goto fail;
347         }
348
349         omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
350         if (!omap_fb) {
351                 dev_err(dev->dev, "could not allocate fb\n");
352                 ret = -ENOMEM;
353                 goto fail;
354         }
355
356         fb = &omap_fb->base;
357         ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
358         if (ret) {
359                 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
360                 goto fail;
361         }
362
363         DBG("create: FB ID: %d (%p)", fb->base.id, fb);
364
365         omap_fb->format = format;
366
367         for (i = 0; i < n; i++) {
368                 struct plane *plane = &omap_fb->planes[i];
369                 int size, pitch = mode_cmd->pitches[i];
370
371                 if (pitch < (mode_cmd->width * format->planes[i].stride_bpp)) {
372                         dev_err(dev->dev, "provided buffer pitch is too small! %d < %d\n",
373                                         pitch, mode_cmd->width * format->planes[i].stride_bpp);
374                         ret = -EINVAL;
375                         goto fail;
376                 }
377
378                 size = pitch * mode_cmd->height / format->planes[i].sub_y;
379
380                 if (size > (bos[i]->size - mode_cmd->offsets[i])) {
381                         dev_err(dev->dev, "provided buffer object is too small! %d < %d\n",
382                                         bos[i]->size - mode_cmd->offsets[i], size);
383                         ret = -EINVAL;
384                         goto fail;
385                 }
386
387                 plane->bo     = bos[i];
388                 plane->offset = mode_cmd->offsets[i];
389                 plane->pitch  = pitch;
390                 plane->paddr  = 0;
391         }
392
393         drm_helper_mode_fill_fb_struct(fb, mode_cmd);
394
395         return fb;
396
397 fail:
398         if (fb) {
399                 omap_framebuffer_destroy(fb);
400         }
401         return ERR_PTR(ret);
402 }