]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/mgag200/mgag200_fb.c
drm/mga200g: Use drm_fb_helper_fill_info
[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_util.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_crtc_helper.h>
18
19 #include "mgag200_drv.h"
20
21 static void mga_dirty_update(struct mga_fbdev *mfbdev,
22                              int x, int y, int width, int height)
23 {
24         int i;
25         struct drm_gem_object *obj;
26         struct mgag200_bo *bo;
27         int src_offset, dst_offset;
28         int bpp = mfbdev->mfb.base.format->cpp[0];
29         int ret = -EBUSY;
30         bool unmap = false;
31         bool store_for_later = false;
32         int x2, y2;
33         unsigned long flags;
34
35         obj = mfbdev->mfb.obj;
36         bo = gem_to_mga_bo(obj);
37
38         /*
39          * try and reserve the BO, if we fail with busy
40          * then the BO is being moved and we should
41          * store up the damage until later.
42          */
43         if (drm_can_sleep())
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         drm_fb_helper_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         drm_fb_helper_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         drm_fb_helper_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                                    const 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 =
161                 container_of(helper, struct mga_fbdev, helper);
162         struct drm_device *dev = mfbdev->helper.dev;
163         struct drm_mode_fb_cmd2 mode_cmd;
164         struct mga_device *mdev = dev->dev_private;
165         struct fb_info *info;
166         struct drm_framebuffer *fb;
167         struct drm_gem_object *gobj = NULL;
168         int ret;
169         void *sysram;
170         int size;
171
172         mode_cmd.width = sizes->surface_width;
173         mode_cmd.height = sizes->surface_height;
174         mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
175
176         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
177                                                           sizes->surface_depth);
178         size = mode_cmd.pitches[0] * mode_cmd.height;
179
180         ret = mgag200fb_create_object(mfbdev, &mode_cmd, &gobj);
181         if (ret) {
182                 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
183                 return ret;
184         }
185
186         sysram = vmalloc(size);
187         if (!sysram) {
188                 ret = -ENOMEM;
189                 goto err_sysram;
190         }
191
192         info = drm_fb_helper_alloc_fbi(helper);
193         if (IS_ERR(info)) {
194                 ret = PTR_ERR(info);
195                 goto err_alloc_fbi;
196         }
197
198         ret = mgag200_framebuffer_init(dev, &mfbdev->mfb, &mode_cmd, gobj);
199         if (ret)
200                 goto err_alloc_fbi;
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
210         info->fbops = &mgag200fb_ops;
211
212         /* setup aperture base/size for vesafb takeover */
213         info->apertures->ranges[0].base = mdev->dev->mode_config.fb_base;
214         info->apertures->ranges[0].size = mdev->mc.vram_size;
215
216         drm_fb_helper_fill_info(info, &mfbdev->helper, sizes);
217
218         info->screen_base = sysram;
219         info->screen_size = size;
220         info->pixmap.flags = FB_PIXMAP_SYSTEM;
221
222         DRM_DEBUG_KMS("allocated %dx%d\n",
223                       fb->width, fb->height);
224
225         return 0;
226
227 err_alloc_fbi:
228         vfree(sysram);
229 err_sysram:
230         drm_gem_object_put_unlocked(gobj);
231
232         return ret;
233 }
234
235 static int mga_fbdev_destroy(struct drm_device *dev,
236                                 struct mga_fbdev *mfbdev)
237 {
238         struct mga_framebuffer *mfb = &mfbdev->mfb;
239
240         drm_fb_helper_unregister_fbi(&mfbdev->helper);
241
242         if (mfb->obj) {
243                 drm_gem_object_put_unlocked(mfb->obj);
244                 mfb->obj = NULL;
245         }
246         drm_fb_helper_fini(&mfbdev->helper);
247         vfree(mfbdev->sysram);
248         drm_framebuffer_unregister_private(&mfb->base);
249         drm_framebuffer_cleanup(&mfb->base);
250
251         return 0;
252 }
253
254 static const struct drm_fb_helper_funcs mga_fb_helper_funcs = {
255         .fb_probe = mgag200fb_create,
256 };
257
258 int mgag200_fbdev_init(struct mga_device *mdev)
259 {
260         struct mga_fbdev *mfbdev;
261         int ret;
262         int bpp_sel = 32;
263
264         /* prefer 16bpp on low end gpus with limited VRAM */
265         if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024))
266                 bpp_sel = 16;
267
268         mfbdev = devm_kzalloc(mdev->dev->dev, sizeof(struct mga_fbdev), GFP_KERNEL);
269         if (!mfbdev)
270                 return -ENOMEM;
271
272         mdev->mfbdev = mfbdev;
273         spin_lock_init(&mfbdev->dirty_lock);
274
275         drm_fb_helper_prepare(mdev->dev, &mfbdev->helper, &mga_fb_helper_funcs);
276
277         ret = drm_fb_helper_init(mdev->dev, &mfbdev->helper,
278                                  MGAG200FB_CONN_LIMIT);
279         if (ret)
280                 goto err_fb_helper;
281
282         ret = drm_fb_helper_single_add_all_connectors(&mfbdev->helper);
283         if (ret)
284                 goto err_fb_setup;
285
286         /* disable all the possible outputs/crtcs before entering KMS mode */
287         drm_helper_disable_unused_functions(mdev->dev);
288
289         ret = drm_fb_helper_initial_config(&mfbdev->helper, bpp_sel);
290         if (ret)
291                 goto err_fb_setup;
292
293         return 0;
294
295 err_fb_setup:
296         drm_fb_helper_fini(&mfbdev->helper);
297 err_fb_helper:
298         mdev->mfbdev = NULL;
299
300         return ret;
301 }
302
303 void mgag200_fbdev_fini(struct mga_device *mdev)
304 {
305         if (!mdev->mfbdev)
306                 return;
307
308         mga_fbdev_destroy(mdev->dev, mdev->mfbdev);
309 }