]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/cirrus/cirrus_fbdev.c
drm/cirrus: Use drm_fb_helper_fill_info
[linux.git] / drivers / gpu / drm / cirrus / cirrus_fbdev.c
1 /*
2  * Copyright 2012 Red Hat
3  *
4  * This file is subject to the terms and conditions of the GNU General
5  * Public License version 2. See the file COPYING in the main
6  * directory of this archive for more details.
7  *
8  * Authors: Matthew Garrett
9  *          Dave Airlie
10  */
11 #include <linux/module.h>
12 #include <drm/drmP.h>
13 #include <drm/drm_util.h>
14 #include <drm/drm_fb_helper.h>
15 #include <drm/drm_crtc_helper.h>
16
17 #include "cirrus_drv.h"
18
19 static void cirrus_dirty_update(struct cirrus_fbdev *afbdev,
20                              int x, int y, int width, int height)
21 {
22         int i;
23         struct drm_gem_object *obj;
24         struct cirrus_bo *bo;
25         int src_offset, dst_offset;
26         int bpp = afbdev->gfb->format->cpp[0];
27         int ret = -EBUSY;
28         bool unmap = false;
29         bool store_for_later = false;
30         int x2, y2;
31         unsigned long flags;
32
33         obj = afbdev->gfb->obj[0];
34         bo = gem_to_cirrus_bo(obj);
35
36         /*
37          * try and reserve the BO, if we fail with busy
38          * then the BO is being moved and we should
39          * store up the damage until later.
40          */
41         if (drm_can_sleep())
42                 ret = cirrus_bo_reserve(bo, true);
43         if (ret) {
44                 if (ret != -EBUSY)
45                         return;
46                 store_for_later = true;
47         }
48
49         x2 = x + width - 1;
50         y2 = y + height - 1;
51         spin_lock_irqsave(&afbdev->dirty_lock, flags);
52
53         if (afbdev->y1 < y)
54                 y = afbdev->y1;
55         if (afbdev->y2 > y2)
56                 y2 = afbdev->y2;
57         if (afbdev->x1 < x)
58                 x = afbdev->x1;
59         if (afbdev->x2 > x2)
60                 x2 = afbdev->x2;
61
62         if (store_for_later) {
63                 afbdev->x1 = x;
64                 afbdev->x2 = x2;
65                 afbdev->y1 = y;
66                 afbdev->y2 = y2;
67                 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
68                 return;
69         }
70
71         afbdev->x1 = afbdev->y1 = INT_MAX;
72         afbdev->x2 = afbdev->y2 = 0;
73         spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
74
75         if (!bo->kmap.virtual) {
76                 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
77                 if (ret) {
78                         DRM_ERROR("failed to kmap fb updates\n");
79                         cirrus_bo_unreserve(bo);
80                         return;
81                 }
82                 unmap = true;
83         }
84         for (i = y; i < y + height; i++) {
85                 /* assume equal stride for now */
86                 src_offset = dst_offset = i * afbdev->gfb->pitches[0] + (x * bpp);
87                 memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
88
89         }
90         if (unmap)
91                 ttm_bo_kunmap(&bo->kmap);
92
93         cirrus_bo_unreserve(bo);
94 }
95
96 static void cirrus_fillrect(struct fb_info *info,
97                          const struct fb_fillrect *rect)
98 {
99         struct cirrus_fbdev *afbdev = info->par;
100         drm_fb_helper_sys_fillrect(info, rect);
101         cirrus_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
102                          rect->height);
103 }
104
105 static void cirrus_copyarea(struct fb_info *info,
106                          const struct fb_copyarea *area)
107 {
108         struct cirrus_fbdev *afbdev = info->par;
109         drm_fb_helper_sys_copyarea(info, area);
110         cirrus_dirty_update(afbdev, area->dx, area->dy, area->width,
111                          area->height);
112 }
113
114 static void cirrus_imageblit(struct fb_info *info,
115                           const struct fb_image *image)
116 {
117         struct cirrus_fbdev *afbdev = info->par;
118         drm_fb_helper_sys_imageblit(info, image);
119         cirrus_dirty_update(afbdev, image->dx, image->dy, image->width,
120                          image->height);
121 }
122
123
124 static struct fb_ops cirrusfb_ops = {
125         .owner = THIS_MODULE,
126         .fb_check_var = drm_fb_helper_check_var,
127         .fb_set_par = drm_fb_helper_set_par,
128         .fb_fillrect = cirrus_fillrect,
129         .fb_copyarea = cirrus_copyarea,
130         .fb_imageblit = cirrus_imageblit,
131         .fb_pan_display = drm_fb_helper_pan_display,
132         .fb_blank = drm_fb_helper_blank,
133         .fb_setcmap = drm_fb_helper_setcmap,
134 };
135
136 static int cirrusfb_create_object(struct cirrus_fbdev *afbdev,
137                                const struct drm_mode_fb_cmd2 *mode_cmd,
138                                struct drm_gem_object **gobj_p)
139 {
140         struct drm_device *dev = afbdev->helper.dev;
141         struct cirrus_device *cdev = dev->dev_private;
142         u32 bpp;
143         u32 size;
144         struct drm_gem_object *gobj;
145         int ret = 0;
146
147         bpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0) * 8;
148
149         if (!cirrus_check_framebuffer(cdev, mode_cmd->width, mode_cmd->height,
150                                       bpp, mode_cmd->pitches[0]))
151                 return -EINVAL;
152
153         size = mode_cmd->pitches[0] * mode_cmd->height;
154         ret = cirrus_gem_create(dev, size, true, &gobj);
155         if (ret)
156                 return ret;
157
158         *gobj_p = gobj;
159         return ret;
160 }
161
162 static int cirrusfb_create(struct drm_fb_helper *helper,
163                            struct drm_fb_helper_surface_size *sizes)
164 {
165         struct cirrus_fbdev *gfbdev =
166                 container_of(helper, struct cirrus_fbdev, helper);
167         struct cirrus_device *cdev = gfbdev->helper.dev->dev_private;
168         struct fb_info *info;
169         struct drm_framebuffer *fb;
170         struct drm_mode_fb_cmd2 mode_cmd;
171         void *sysram;
172         struct drm_gem_object *gobj = NULL;
173         int size, ret;
174
175         mode_cmd.width = sizes->surface_width;
176         mode_cmd.height = sizes->surface_height;
177         mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
178         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
179                                                           sizes->surface_depth);
180         size = mode_cmd.pitches[0] * mode_cmd.height;
181
182         ret = cirrusfb_create_object(gfbdev, &mode_cmd, &gobj);
183         if (ret) {
184                 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
185                 return ret;
186         }
187
188         sysram = vmalloc(size);
189         if (!sysram)
190                 return -ENOMEM;
191
192         info = drm_fb_helper_alloc_fbi(helper);
193         if (IS_ERR(info)) {
194                 ret = PTR_ERR(info);
195                 goto err_vfree;
196         }
197
198         fb = kzalloc(sizeof(*fb), GFP_KERNEL);
199         if (!fb) {
200                 ret = -ENOMEM;
201                 goto err_drm_gem_object_put_unlocked;
202         }
203
204         ret = cirrus_framebuffer_init(cdev->dev, fb, &mode_cmd, gobj);
205         if (ret)
206                 goto err_kfree;
207
208         gfbdev->sysram = sysram;
209         gfbdev->size = size;
210         gfbdev->gfb = fb;
211
212         /* setup helper */
213         gfbdev->helper.fb = fb;
214
215         info->fbops = &cirrusfb_ops;
216
217         drm_fb_helper_fill_info(info, &gfbdev->helper, sizes);
218
219         /* setup aperture base/size for vesafb takeover */
220         info->apertures->ranges[0].base = cdev->dev->mode_config.fb_base;
221         info->apertures->ranges[0].size = cdev->mc.vram_size;
222
223         info->fix.smem_start = cdev->dev->mode_config.fb_base;
224         info->fix.smem_len = cdev->mc.vram_size;
225
226         info->screen_base = sysram;
227         info->screen_size = size;
228
229         info->fix.mmio_start = 0;
230         info->fix.mmio_len = 0;
231
232         DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
233         DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info->fix.smem_start);
234         DRM_INFO("size %lu\n", (unsigned long)info->fix.smem_len);
235         DRM_INFO("fb depth is %d\n", fb->format->depth);
236         DRM_INFO("   pitch is %d\n", fb->pitches[0]);
237
238         return 0;
239
240 err_kfree:
241         kfree(fb);
242 err_drm_gem_object_put_unlocked:
243         drm_gem_object_put_unlocked(gobj);
244 err_vfree:
245         vfree(sysram);
246         return ret;
247 }
248
249 static int cirrus_fbdev_destroy(struct drm_device *dev,
250                                 struct cirrus_fbdev *gfbdev)
251 {
252         struct drm_framebuffer *gfb = gfbdev->gfb;
253
254         drm_helper_force_disable_all(dev);
255
256         drm_fb_helper_unregister_fbi(&gfbdev->helper);
257
258         vfree(gfbdev->sysram);
259         drm_fb_helper_fini(&gfbdev->helper);
260         if (gfb)
261                 drm_framebuffer_put(gfb);
262
263         return 0;
264 }
265
266 static const struct drm_fb_helper_funcs cirrus_fb_helper_funcs = {
267         .fb_probe = cirrusfb_create,
268 };
269
270 int cirrus_fbdev_init(struct cirrus_device *cdev)
271 {
272         struct cirrus_fbdev *gfbdev;
273         int ret;
274
275         /*bpp_sel = 8;*/
276         gfbdev = kzalloc(sizeof(struct cirrus_fbdev), GFP_KERNEL);
277         if (!gfbdev)
278                 return -ENOMEM;
279
280         cdev->mode_info.gfbdev = gfbdev;
281         spin_lock_init(&gfbdev->dirty_lock);
282
283         drm_fb_helper_prepare(cdev->dev, &gfbdev->helper,
284                               &cirrus_fb_helper_funcs);
285
286         ret = drm_fb_helper_init(cdev->dev, &gfbdev->helper,
287                                  CIRRUSFB_CONN_LIMIT);
288         if (ret)
289                 return ret;
290
291         ret = drm_fb_helper_single_add_all_connectors(&gfbdev->helper);
292         if (ret)
293                 return ret;
294
295         /* disable all the possible outputs/crtcs before entering KMS mode */
296         drm_helper_disable_unused_functions(cdev->dev);
297
298         return drm_fb_helper_initial_config(&gfbdev->helper, cirrus_bpp);
299 }
300
301 void cirrus_fbdev_fini(struct cirrus_device *cdev)
302 {
303         if (!cdev->mode_info.gfbdev)
304                 return;
305
306         cirrus_fbdev_destroy(cdev->dev, cdev->mode_info.gfbdev);
307         kfree(cdev->mode_info.gfbdev);
308         cdev->mode_info.gfbdev = NULL;
309 }