Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes/pci
[cascardo/linux.git] / drivers / staging / omapdrm / omap_fbdev.c
1 /*
2  * drivers/staging/omapdrm/omap_fbdev.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_fb_helper.h"
24
25 MODULE_PARM_DESC(ywrap, "Enable ywrap scrolling (omap44xx and later, default 'y')");
26 static bool ywrap_enabled = true;
27 module_param_named(ywrap, ywrap_enabled, bool, 0644);
28
29 /*
30  * fbdev funcs, to implement legacy fbdev interface on top of drm driver
31  */
32
33 #define to_omap_fbdev(x) container_of(x, struct omap_fbdev, base)
34
35 struct omap_fbdev {
36         struct drm_fb_helper base;
37         struct drm_framebuffer *fb;
38         struct drm_gem_object *bo;
39         bool ywrap_enabled;
40 };
41
42 static void omap_fbdev_flush(struct fb_info *fbi, int x, int y, int w, int h);
43 static struct drm_fb_helper *get_fb(struct fb_info *fbi);
44
45 static ssize_t omap_fbdev_write(struct fb_info *fbi, const char __user *buf,
46                 size_t count, loff_t *ppos)
47 {
48         ssize_t res;
49
50         res = fb_sys_write(fbi, buf, count, ppos);
51         omap_fbdev_flush(fbi, 0, 0, fbi->var.xres, fbi->var.yres);
52
53         return res;
54 }
55
56 static void omap_fbdev_fillrect(struct fb_info *fbi,
57                 const struct fb_fillrect *rect)
58 {
59         sys_fillrect(fbi, rect);
60         omap_fbdev_flush(fbi, rect->dx, rect->dy, rect->width, rect->height);
61 }
62
63 static void omap_fbdev_copyarea(struct fb_info *fbi,
64                 const struct fb_copyarea *area)
65 {
66         sys_copyarea(fbi, area);
67         omap_fbdev_flush(fbi, area->dx, area->dy, area->width, area->height);
68 }
69
70 static void omap_fbdev_imageblit(struct fb_info *fbi,
71                 const struct fb_image *image)
72 {
73         sys_imageblit(fbi, image);
74         omap_fbdev_flush(fbi, image->dx, image->dy,
75                                 image->width, image->height);
76 }
77
78 static int omap_fbdev_pan_display(struct fb_var_screeninfo *var,
79                 struct fb_info *fbi)
80 {
81         struct drm_fb_helper *helper = get_fb(fbi);
82         struct omap_fbdev *fbdev = to_omap_fbdev(helper);
83         int npages;
84
85         if (!helper)
86                 goto fallback;
87
88         if (!fbdev->ywrap_enabled)
89                 goto fallback;
90
91         /* DMM roll shifts in 4K pages: */
92         npages = fbi->fix.line_length >> PAGE_SHIFT;
93         omap_gem_roll(fbdev->bo, var->yoffset * npages);
94
95         return 0;
96
97 fallback:
98         return drm_fb_helper_pan_display(var, fbi);
99 }
100
101 static struct fb_ops omap_fb_ops = {
102         .owner = THIS_MODULE,
103
104         /* Note: to properly handle manual update displays, we wrap the
105          * basic fbdev ops which write to the framebuffer
106          */
107         .fb_read = fb_sys_read,
108         .fb_write = omap_fbdev_write,
109         .fb_fillrect = omap_fbdev_fillrect,
110         .fb_copyarea = omap_fbdev_copyarea,
111         .fb_imageblit = omap_fbdev_imageblit,
112
113         .fb_check_var = drm_fb_helper_check_var,
114         .fb_set_par = drm_fb_helper_set_par,
115         .fb_pan_display = omap_fbdev_pan_display,
116         .fb_blank = drm_fb_helper_blank,
117         .fb_setcmap = drm_fb_helper_setcmap,
118
119         .fb_debug_enter = drm_fb_helper_debug_enter,
120         .fb_debug_leave = drm_fb_helper_debug_leave,
121 };
122
123 static int omap_fbdev_create(struct drm_fb_helper *helper,
124                 struct drm_fb_helper_surface_size *sizes)
125 {
126         struct omap_fbdev *fbdev = to_omap_fbdev(helper);
127         struct drm_device *dev = helper->dev;
128         struct omap_drm_private *priv = dev->dev_private;
129         struct drm_framebuffer *fb = NULL;
130         union omap_gem_size gsize;
131         struct fb_info *fbi = NULL;
132         struct drm_mode_fb_cmd2 mode_cmd = {0};
133         dma_addr_t paddr;
134         int ret;
135
136         /* only doing ARGB32 since this is what is needed to alpha-blend
137          * with video overlays:
138          */
139         sizes->surface_bpp = 32;
140         sizes->surface_depth = 32;
141
142         DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
143                         sizes->surface_height, sizes->surface_bpp,
144                         sizes->fb_width, sizes->fb_height);
145
146         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
147                         sizes->surface_depth);
148
149         mode_cmd.width = sizes->surface_width;
150         mode_cmd.height = sizes->surface_height;
151
152         mode_cmd.pitches[0] = align_pitch(
153                         mode_cmd.width * ((sizes->surface_bpp + 7) / 8),
154                         mode_cmd.width, sizes->surface_bpp);
155
156         fbdev->ywrap_enabled = priv->has_dmm && ywrap_enabled;
157         if (fbdev->ywrap_enabled) {
158                 /* need to align pitch to page size if using DMM scrolling */
159                 mode_cmd.pitches[0] = ALIGN(mode_cmd.pitches[0], PAGE_SIZE);
160         }
161
162         /* allocate backing bo */
163         gsize = (union omap_gem_size){
164                 .bytes = PAGE_ALIGN(mode_cmd.pitches[0] * mode_cmd.height),
165         };
166         DBG("allocating %d bytes for fb %d", gsize.bytes, dev->primary->index);
167         fbdev->bo = omap_gem_new(dev, gsize, OMAP_BO_SCANOUT | OMAP_BO_WC);
168         if (!fbdev->bo) {
169                 dev_err(dev->dev, "failed to allocate buffer object\n");
170                 ret = -ENOMEM;
171                 goto fail;
172         }
173
174         fb = omap_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
175         if (IS_ERR(fb)) {
176                 dev_err(dev->dev, "failed to allocate fb\n");
177                 /* note: if fb creation failed, we can't rely on fb destroy
178                  * to unref the bo:
179                  */
180                 drm_gem_object_unreference(fbdev->bo);
181                 ret = PTR_ERR(fb);
182                 goto fail;
183         }
184
185         /* note: this keeps the bo pinned.. which is perhaps not ideal,
186          * but is needed as long as we use fb_mmap() to mmap to userspace
187          * (since this happens using fix.smem_start).  Possibly we could
188          * implement our own mmap using GEM mmap support to avoid this
189          * (non-tiled buffer doesn't need to be pinned for fbcon to write
190          * to it).  Then we just need to be sure that we are able to re-
191          * pin it in case of an opps.
192          */
193         ret = omap_gem_get_paddr(fbdev->bo, &paddr, true);
194         if (ret) {
195                 dev_err(dev->dev, "could not map (paddr)!\n");
196                 ret = -ENOMEM;
197                 goto fail;
198         }
199
200         mutex_lock(&dev->struct_mutex);
201
202         fbi = framebuffer_alloc(0, dev->dev);
203         if (!fbi) {
204                 dev_err(dev->dev, "failed to allocate fb info\n");
205                 ret = -ENOMEM;
206                 goto fail_unlock;
207         }
208
209         DBG("fbi=%p, dev=%p", fbi, dev);
210
211         fbdev->fb = fb;
212         helper->fb = fb;
213         helper->fbdev = fbi;
214
215         fbi->par = helper;
216         fbi->flags = FBINFO_DEFAULT;
217         fbi->fbops = &omap_fb_ops;
218
219         strcpy(fbi->fix.id, MODULE_NAME);
220
221         ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
222         if (ret) {
223                 ret = -ENOMEM;
224                 goto fail_unlock;
225         }
226
227         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
228         drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
229
230         dev->mode_config.fb_base = paddr;
231
232         fbi->screen_base = omap_gem_vaddr(fbdev->bo);
233         fbi->screen_size = fbdev->bo->size;
234         fbi->fix.smem_start = paddr;
235         fbi->fix.smem_len = fbdev->bo->size;
236
237         /* if we have DMM, then we can use it for scrolling by just
238          * shuffling pages around in DMM rather than doing sw blit.
239          */
240         if (fbdev->ywrap_enabled) {
241                 DRM_INFO("Enabling DMM ywrap scrolling\n");
242                 fbi->flags |= FBINFO_HWACCEL_YWRAP | FBINFO_READS_FAST;
243                 fbi->fix.ywrapstep = 1;
244         }
245
246
247         DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
248         DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
249
250         mutex_unlock(&dev->struct_mutex);
251
252         return 0;
253
254 fail_unlock:
255         mutex_unlock(&dev->struct_mutex);
256 fail:
257
258         if (ret) {
259                 if (fbi)
260                         framebuffer_release(fbi);
261                 if (fb)
262                         fb->funcs->destroy(fb);
263         }
264
265         return ret;
266 }
267
268 static void omap_crtc_fb_gamma_set(struct drm_crtc *crtc,
269                 u16 red, u16 green, u16 blue, int regno)
270 {
271         DBG("fbdev: set gamma");
272 }
273
274 static void omap_crtc_fb_gamma_get(struct drm_crtc *crtc,
275                 u16 *red, u16 *green, u16 *blue, int regno)
276 {
277         DBG("fbdev: get gamma");
278 }
279
280 static int omap_fbdev_probe(struct drm_fb_helper *helper,
281                 struct drm_fb_helper_surface_size *sizes)
282 {
283         int new_fb = 0;
284         int ret;
285
286         if (!helper->fb) {
287                 ret = omap_fbdev_create(helper, sizes);
288                 if (ret)
289                         return ret;
290                 new_fb = 1;
291         }
292         return new_fb;
293 }
294
295 static struct drm_fb_helper_funcs omap_fb_helper_funcs = {
296         .gamma_set = omap_crtc_fb_gamma_set,
297         .gamma_get = omap_crtc_fb_gamma_get,
298         .fb_probe = omap_fbdev_probe,
299 };
300
301 static struct drm_fb_helper *get_fb(struct fb_info *fbi)
302 {
303         if (!fbi || strcmp(fbi->fix.id, MODULE_NAME)) {
304                 /* these are not the fb's you're looking for */
305                 return NULL;
306         }
307         return fbi->par;
308 }
309
310 /* flush an area of the framebuffer (in case of manual update display that
311  * is not automatically flushed)
312  */
313 static void omap_fbdev_flush(struct fb_info *fbi, int x, int y, int w, int h)
314 {
315         struct drm_fb_helper *helper = get_fb(fbi);
316
317         if (!helper)
318                 return;
319
320         VERB("flush fbdev: %d,%d %dx%d, fbi=%p", x, y, w, h, fbi);
321
322         omap_framebuffer_flush(helper->fb, x, y, w, h);
323 }
324
325 /* initialize fbdev helper */
326 struct drm_fb_helper *omap_fbdev_init(struct drm_device *dev)
327 {
328         struct omap_drm_private *priv = dev->dev_private;
329         struct omap_fbdev *fbdev = NULL;
330         struct drm_fb_helper *helper;
331         int ret = 0;
332
333         fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
334         if (!fbdev) {
335                 dev_err(dev->dev, "could not allocate fbdev\n");
336                 goto fail;
337         }
338
339         helper = &fbdev->base;
340
341         helper->funcs = &omap_fb_helper_funcs;
342
343         ret = drm_fb_helper_init(dev, helper,
344                         priv->num_crtcs, priv->num_connectors);
345         if (ret) {
346                 dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
347                 goto fail;
348         }
349
350         drm_fb_helper_single_add_all_connectors(helper);
351         drm_fb_helper_initial_config(helper, 32);
352
353         priv->fbdev = helper;
354
355         return helper;
356
357 fail:
358         kfree(fbdev);
359         return NULL;
360 }
361
362 void omap_fbdev_free(struct drm_device *dev)
363 {
364         struct omap_drm_private *priv = dev->dev_private;
365         struct drm_fb_helper *helper = priv->fbdev;
366         struct omap_fbdev *fbdev;
367         struct fb_info *fbi;
368
369         DBG();
370
371         fbi = helper->fbdev;
372
373         unregister_framebuffer(fbi);
374         framebuffer_release(fbi);
375
376         drm_fb_helper_fini(helper);
377
378         fbdev = to_omap_fbdev(priv->fbdev);
379
380         /* this will free the backing object */
381         if (fbdev->fb)
382                 fbdev->fb->funcs->destroy(fbdev->fb);
383
384         kfree(fbdev);
385
386         priv->fbdev = NULL;
387 }