116a8ff315aca8d4ace903979f9a627de4c38a5c
[linux-2.6-microblaze.git] / 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(omap_fb->format->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 /* pins buffer in preparation for scanout */
141 int omap_framebuffer_pin(struct drm_framebuffer *fb)
142 {
143         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
144         int ret, i, n = drm_format_num_planes(omap_fb->format->pixel_format);
145
146         for (i = 0; i < n; i++) {
147                 struct plane *plane = &omap_fb->planes[i];
148                 ret = omap_gem_get_paddr(plane->bo, &plane->paddr, true);
149                 if (ret)
150                         goto fail;
151         }
152
153         return 0;
154
155 fail:
156         while (--i > 0) {
157                 struct plane *plane = &omap_fb->planes[i];
158                 omap_gem_put_paddr(plane->bo);
159         }
160         return ret;
161 }
162
163 /* releases buffer when done with scanout */
164 void omap_framebuffer_unpin(struct drm_framebuffer *fb)
165 {
166         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
167         int i, n = drm_format_num_planes(omap_fb->format->pixel_format);
168
169         for (i = 0; i < n; i++) {
170                 struct plane *plane = &omap_fb->planes[i];
171                 omap_gem_put_paddr(plane->bo);
172         }
173 }
174
175 /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
176  */
177 void omap_framebuffer_update_scanout(struct drm_framebuffer *fb, int x, int y,
178                 struct omap_overlay_info *info)
179 {
180         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
181         const struct format *format = omap_fb->format;
182         struct plane *plane = &omap_fb->planes[0];
183         unsigned int offset;
184
185         offset = plane->offset +
186                         (x * format->planes[0].stride_bpp) +
187                         (y * plane->pitch / format->planes[0].sub_y);
188
189         info->color_mode   = format->dss_format;
190         info->paddr        = plane->paddr + offset;
191         info->screen_width = plane->pitch / format->planes[0].stride_bpp;
192
193         if (format->dss_format == OMAP_DSS_COLOR_NV12) {
194                 plane = &omap_fb->planes[1];
195                 offset = plane->offset +
196                                 (x * format->planes[1].stride_bpp) +
197                                 (y * plane->pitch / format->planes[1].sub_y);
198                 info->p_uv_addr = plane->paddr + offset;
199         } else {
200                 info->p_uv_addr = 0;
201         }
202 }
203
204 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb, int p)
205 {
206         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
207         if (p >= drm_format_num_planes(omap_fb->format->pixel_format))
208                 return NULL;
209         return omap_fb->planes[p].bo;
210 }
211
212 /* iterate thru all the connectors, returning ones that are attached
213  * to the same fb..
214  */
215 struct drm_connector *omap_framebuffer_get_next_connector(
216                 struct drm_framebuffer *fb, struct drm_connector *from)
217 {
218         struct drm_device *dev = fb->dev;
219         struct list_head *connector_list = &dev->mode_config.connector_list;
220         struct drm_connector *connector = from;
221
222         if (!from) {
223                 return list_first_entry(connector_list, typeof(*from), head);
224         }
225
226         list_for_each_entry_from(connector, connector_list, head) {
227                 if (connector != from) {
228                         struct drm_encoder *encoder = connector->encoder;
229                         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
230                         if (crtc && crtc->fb == fb) {
231                                 return connector;
232                         }
233                 }
234         }
235
236         return NULL;
237 }
238
239 /* flush an area of the framebuffer (in case of manual update display that
240  * is not automatically flushed)
241  */
242 void omap_framebuffer_flush(struct drm_framebuffer *fb,
243                 int x, int y, int w, int h)
244 {
245         struct drm_connector *connector = NULL;
246
247         VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
248
249         while ((connector = omap_framebuffer_get_next_connector(fb, connector))) {
250                 /* only consider connectors that are part of a chain */
251                 if (connector->encoder && connector->encoder->crtc) {
252                         /* TODO: maybe this should propagate thru the crtc who
253                          * could do the coordinate translation..
254                          */
255                         struct drm_crtc *crtc = connector->encoder->crtc;
256                         int cx = max(0, x - crtc->x);
257                         int cy = max(0, y - crtc->y);
258                         int cw = w + (x - crtc->x) - cx;
259                         int ch = h + (y - crtc->y) - cy;
260
261                         omap_connector_flush(connector, cx, cy, cw, ch);
262                 }
263         }
264 }
265
266 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
267                 struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
268 {
269         struct drm_gem_object *bos[4];
270         struct drm_framebuffer *fb;
271         int ret;
272
273         ret = objects_lookup(dev, file, mode_cmd->pixel_format,
274                         bos, mode_cmd->handles);
275         if (ret)
276                 return ERR_PTR(ret);
277
278         fb = omap_framebuffer_init(dev, mode_cmd, bos);
279         if (IS_ERR(fb)) {
280                 int i, n = drm_format_num_planes(mode_cmd->pixel_format);
281                 for (i = 0; i < n; i++)
282                         drm_gem_object_unreference_unlocked(bos[i]);
283                 return fb;
284         }
285         return fb;
286 }
287
288 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
289                 struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
290 {
291         struct omap_framebuffer *omap_fb;
292         struct drm_framebuffer *fb = NULL;
293         const struct format *format = NULL;
294         int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
295
296         DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
297                         dev, mode_cmd, mode_cmd->width, mode_cmd->height,
298                         (char *)&mode_cmd->pixel_format);
299
300         for (i = 0; i < ARRAY_SIZE(formats); i++) {
301                 if (formats[i].pixel_format == mode_cmd->pixel_format) {
302                         format = &formats[i];
303                         break;
304                 }
305         }
306
307         if (!format) {
308                 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
309                                 (char *)&mode_cmd->pixel_format);
310                 ret = -EINVAL;
311                 goto fail;
312         }
313
314         omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
315         if (!omap_fb) {
316                 dev_err(dev->dev, "could not allocate fb\n");
317                 ret = -ENOMEM;
318                 goto fail;
319         }
320
321         fb = &omap_fb->base;
322         ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
323         if (ret) {
324                 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
325                 goto fail;
326         }
327
328         DBG("create: FB ID: %d (%p)", fb->base.id, fb);
329
330         omap_fb->format = format;
331
332         for (i = 0; i < n; i++) {
333                 struct plane *plane = &omap_fb->planes[i];
334                 int size, pitch = mode_cmd->pitches[i];
335
336                 if (pitch < (mode_cmd->width * format->planes[i].stride_bpp)) {
337                         dev_err(dev->dev, "provided buffer pitch is too small! %d < %d\n",
338                                         pitch, mode_cmd->width * format->planes[i].stride_bpp);
339                         ret = -EINVAL;
340                         goto fail;
341                 }
342
343                 size = pitch * mode_cmd->height / format->planes[i].sub_y;
344
345                 if (size > (bos[i]->size - mode_cmd->offsets[i])) {
346                         dev_err(dev->dev, "provided buffer object is too small! %d < %d\n",
347                                         bos[i]->size - mode_cmd->offsets[i], size);
348                         ret = -EINVAL;
349                         goto fail;
350                 }
351
352                 plane->bo     = bos[i];
353                 plane->offset = mode_cmd->offsets[i];
354                 plane->pitch  = mode_cmd->pitches[i];
355                 plane->paddr  = pitch;
356         }
357
358         drm_helper_mode_fill_fb_struct(fb, mode_cmd);
359
360         return fb;
361
362 fail:
363         if (fb) {
364                 omap_framebuffer_destroy(fb);
365         }
366         return ERR_PTR(ret);
367 }