Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[cascardo/linux.git] / drivers / gpu / drm / mgag200 / mgag200_fb.c
1 /*
2  * Copyright 2010 Matt Turner.
3  * Copyright 2012 Red Hat
4  *
5  * This file is subject to the terms and conditions of the GNU General
6  * Public License version 2. See the file COPYING in the main
7  * directory of this archive for more details.
8  *
9  * Authors: Matthew Garrett
10  *          Matt Turner
11  *          Dave Airlie
12  */
13 #include <linux/module.h>
14 #include <drm/drmP.h>
15 #include <drm/drm_fb_helper.h>
16 #include <drm/drm_crtc_helper.h>
17
18 #include <linux/fb.h>
19
20 #include "mgag200_drv.h"
21
22 static void mga_dirty_update(struct mga_fbdev *mfbdev,
23                              int x, int y, int width, int height)
24 {
25         int i;
26         struct drm_gem_object *obj;
27         struct mgag200_bo *bo;
28         int src_offset, dst_offset;
29         int bpp = (mfbdev->mfb.base.bits_per_pixel + 7)/8;
30         int ret;
31         bool unmap = false;
32         bool store_for_later = false;
33         int x2, y2;
34         unsigned long flags;
35
36         obj = mfbdev->mfb.obj;
37         bo = gem_to_mga_bo(obj);
38
39         /*
40          * try and reserve the BO, if we fail with busy
41          * then the BO is being moved and we should
42          * store up the damage until later.
43          */
44         ret = mgag200_bo_reserve(bo, true);
45         if (ret) {
46                 if (ret != -EBUSY)
47                         return;
48
49                 store_for_later = true;
50         }
51
52         x2 = x + width - 1;
53         y2 = y + height - 1;
54         spin_lock_irqsave(&mfbdev->dirty_lock, flags);
55
56         if (mfbdev->y1 < y)
57                 y = mfbdev->y1;
58         if (mfbdev->y2 > y2)
59                 y2 = mfbdev->y2;
60         if (mfbdev->x1 < x)
61                 x = mfbdev->x1;
62         if (mfbdev->x2 > x2)
63                 x2 = mfbdev->x2;
64
65         if (store_for_later) {
66                 mfbdev->x1 = x;
67                 mfbdev->x2 = x2;
68                 mfbdev->y1 = y;
69                 mfbdev->y2 = y2;
70                 spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
71                 return;
72         }
73
74         mfbdev->x1 = mfbdev->y1 = INT_MAX;
75         mfbdev->x2 = mfbdev->y2 = 0;
76         spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
77
78         if (!bo->kmap.virtual) {
79                 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
80                 if (ret) {
81                         DRM_ERROR("failed to kmap fb updates\n");
82                         mgag200_bo_unreserve(bo);
83                         return;
84                 }
85                 unmap = true;
86         }
87         for (i = y; i <= y2; i++) {
88                 /* assume equal stride for now */
89                 src_offset = dst_offset = i * mfbdev->mfb.base.pitches[0] + (x * bpp);
90                 memcpy_toio(bo->kmap.virtual + src_offset, mfbdev->sysram + src_offset, (x2 - x + 1) * bpp);
91
92         }
93         if (unmap)
94                 ttm_bo_kunmap(&bo->kmap);
95
96         mgag200_bo_unreserve(bo);
97 }
98
99 static void mga_fillrect(struct fb_info *info,
100                          const struct fb_fillrect *rect)
101 {
102         struct mga_fbdev *mfbdev = info->par;
103         sys_fillrect(info, rect);
104         mga_dirty_update(mfbdev, rect->dx, rect->dy, rect->width,
105                          rect->height);
106 }
107
108 static void mga_copyarea(struct fb_info *info,
109                          const struct fb_copyarea *area)
110 {
111         struct mga_fbdev *mfbdev = info->par;
112         sys_copyarea(info, area);
113         mga_dirty_update(mfbdev, area->dx, area->dy, area->width,
114                          area->height);
115 }
116
117 static void mga_imageblit(struct fb_info *info,
118                           const struct fb_image *image)
119 {
120         struct mga_fbdev *mfbdev = info->par;
121         sys_imageblit(info, image);
122         mga_dirty_update(mfbdev, image->dx, image->dy, image->width,
123                          image->height);
124 }
125
126
127 static struct fb_ops mgag200fb_ops = {
128         .owner = THIS_MODULE,
129         .fb_check_var = drm_fb_helper_check_var,
130         .fb_set_par = drm_fb_helper_set_par,
131         .fb_fillrect = mga_fillrect,
132         .fb_copyarea = mga_copyarea,
133         .fb_imageblit = mga_imageblit,
134         .fb_pan_display = drm_fb_helper_pan_display,
135         .fb_blank = drm_fb_helper_blank,
136         .fb_setcmap = drm_fb_helper_setcmap,
137 };
138
139 static int mgag200fb_create_object(struct mga_fbdev *afbdev,
140                                    struct drm_mode_fb_cmd2 *mode_cmd,
141                                    struct drm_gem_object **gobj_p)
142 {
143         struct drm_device *dev = afbdev->helper.dev;
144         u32 size;
145         struct drm_gem_object *gobj;
146         int ret = 0;
147
148         size = mode_cmd->pitches[0] * mode_cmd->height;
149         ret = mgag200_gem_create(dev, size, true, &gobj);
150         if (ret)
151                 return ret;
152
153         *gobj_p = gobj;
154         return ret;
155 }
156
157 static int mgag200fb_create(struct drm_fb_helper *helper,
158                            struct drm_fb_helper_surface_size *sizes)
159 {
160         struct mga_fbdev *mfbdev = (struct mga_fbdev *)helper;
161         struct drm_device *dev = mfbdev->helper.dev;
162         struct drm_mode_fb_cmd2 mode_cmd;
163         struct mga_device *mdev = dev->dev_private;
164         struct fb_info *info;
165         struct drm_framebuffer *fb;
166         struct drm_gem_object *gobj = NULL;
167         struct device *device = &dev->pdev->dev;
168         struct mgag200_bo *bo;
169         int ret;
170         void *sysram;
171         int size;
172
173         mode_cmd.width = sizes->surface_width;
174         mode_cmd.height = sizes->surface_height;
175         mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
176
177         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
178                                                           sizes->surface_depth);
179         size = mode_cmd.pitches[0] * mode_cmd.height;
180
181         ret = mgag200fb_create_object(mfbdev, &mode_cmd, &gobj);
182         if (ret) {
183                 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
184                 return ret;
185         }
186         bo = gem_to_mga_bo(gobj);
187
188         sysram = vmalloc(size);
189         if (!sysram)
190                 return -ENOMEM;
191
192         info = framebuffer_alloc(0, device);
193         if (info == NULL)
194                 return -ENOMEM;
195
196         info->par = mfbdev;
197
198         ret = mgag200_framebuffer_init(dev, &mfbdev->mfb, &mode_cmd, gobj);
199         if (ret)
200                 return ret;
201
202         mfbdev->sysram = sysram;
203         mfbdev->size = size;
204
205         fb = &mfbdev->mfb.base;
206
207         /* setup helper */
208         mfbdev->helper.fb = fb;
209         mfbdev->helper.fbdev = info;
210
211         ret = fb_alloc_cmap(&info->cmap, 256, 0);
212         if (ret) {
213                 DRM_ERROR("%s: can't allocate color map\n", info->fix.id);
214                 ret = -ENOMEM;
215                 goto out;
216         }
217
218         strcpy(info->fix.id, "mgadrmfb");
219
220         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
221         info->fbops = &mgag200fb_ops;
222
223         /* setup aperture base/size for vesafb takeover */
224         info->apertures = alloc_apertures(1);
225         if (!info->apertures) {
226                 ret = -ENOMEM;
227                 goto out;
228         }
229         info->apertures->ranges[0].base = mdev->dev->mode_config.fb_base;
230         info->apertures->ranges[0].size = mdev->mc.vram_size;
231
232         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
233         drm_fb_helper_fill_var(info, &mfbdev->helper, sizes->fb_width,
234                                sizes->fb_height);
235
236         info->screen_base = sysram;
237         info->screen_size = size;
238         info->pixmap.flags = FB_PIXMAP_SYSTEM;
239
240         DRM_DEBUG_KMS("allocated %dx%d\n",
241                       fb->width, fb->height);
242         return 0;
243 out:
244         return ret;
245 }
246
247 static int mga_fbdev_destroy(struct drm_device *dev,
248                                 struct mga_fbdev *mfbdev)
249 {
250         struct fb_info *info;
251         struct mga_framebuffer *mfb = &mfbdev->mfb;
252
253         if (mfbdev->helper.fbdev) {
254                 info = mfbdev->helper.fbdev;
255
256                 unregister_framebuffer(info);
257                 if (info->cmap.len)
258                         fb_dealloc_cmap(&info->cmap);
259                 framebuffer_release(info);
260         }
261
262         if (mfb->obj) {
263                 drm_gem_object_unreference_unlocked(mfb->obj);
264                 mfb->obj = NULL;
265         }
266         drm_fb_helper_fini(&mfbdev->helper);
267         vfree(mfbdev->sysram);
268         drm_framebuffer_unregister_private(&mfb->base);
269         drm_framebuffer_cleanup(&mfb->base);
270
271         return 0;
272 }
273
274 static struct drm_fb_helper_funcs mga_fb_helper_funcs = {
275         .gamma_set = mga_crtc_fb_gamma_set,
276         .gamma_get = mga_crtc_fb_gamma_get,
277         .fb_probe = mgag200fb_create,
278 };
279
280 int mgag200_fbdev_init(struct mga_device *mdev)
281 {
282         struct mga_fbdev *mfbdev;
283         int ret;
284
285         mfbdev = devm_kzalloc(mdev->dev->dev, sizeof(struct mga_fbdev), GFP_KERNEL);
286         if (!mfbdev)
287                 return -ENOMEM;
288
289         mdev->mfbdev = mfbdev;
290         mfbdev->helper.funcs = &mga_fb_helper_funcs;
291         spin_lock_init(&mfbdev->dirty_lock);
292
293         ret = drm_fb_helper_init(mdev->dev, &mfbdev->helper,
294                                  mdev->num_crtc, MGAG200FB_CONN_LIMIT);
295         if (ret)
296                 return ret;
297
298         drm_fb_helper_single_add_all_connectors(&mfbdev->helper);
299
300         /* disable all the possible outputs/crtcs before entering KMS mode */
301         drm_helper_disable_unused_functions(mdev->dev);
302
303         drm_fb_helper_initial_config(&mfbdev->helper, 32);
304
305         return 0;
306 }
307
308 void mgag200_fbdev_fini(struct mga_device *mdev)
309 {
310         if (!mdev->mfbdev)
311                 return;
312
313         mga_fbdev_destroy(mdev->dev, mdev->mfbdev);
314 }