]> Pileus Git - ~andy/linux/blob - drivers/gpu/drm/cirrus/cirrus_fbdev.c
Merge branch 'fixes' of git://git.linaro.org/people/rmk/linux-arm
[~andy/linux] / 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_fb_helper.h>
14 #include <drm/drm_crtc_helper.h>
15
16 #include <linux/fb.h>
17
18 #include "cirrus_drv.h"
19
20 static void cirrus_dirty_update(struct cirrus_fbdev *afbdev,
21                              int x, int y, int width, int height)
22 {
23         int i;
24         struct drm_gem_object *obj;
25         struct cirrus_bo *bo;
26         int src_offset, dst_offset;
27         int bpp = (afbdev->gfb.base.bits_per_pixel + 7)/8;
28         int ret;
29         bool unmap = false;
30
31         obj = afbdev->gfb.obj;
32         bo = gem_to_cirrus_bo(obj);
33
34         ret = cirrus_bo_reserve(bo, true);
35         if (ret) {
36                 DRM_ERROR("failed to reserve fb bo\n");
37                 return;
38         }
39
40         if (!bo->kmap.virtual) {
41                 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
42                 if (ret) {
43                         DRM_ERROR("failed to kmap fb updates\n");
44                         cirrus_bo_unreserve(bo);
45                         return;
46                 }
47                 unmap = true;
48         }
49         for (i = y; i < y + height; i++) {
50                 /* assume equal stride for now */
51                 src_offset = dst_offset = i * afbdev->gfb.base.pitches[0] + (x * bpp);
52                 memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
53
54         }
55         if (unmap)
56                 ttm_bo_kunmap(&bo->kmap);
57
58         cirrus_bo_unreserve(bo);
59 }
60
61 static void cirrus_fillrect(struct fb_info *info,
62                          const struct fb_fillrect *rect)
63 {
64         struct cirrus_fbdev *afbdev = info->par;
65         sys_fillrect(info, rect);
66         cirrus_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
67                          rect->height);
68 }
69
70 static void cirrus_copyarea(struct fb_info *info,
71                          const struct fb_copyarea *area)
72 {
73         struct cirrus_fbdev *afbdev = info->par;
74         sys_copyarea(info, area);
75         cirrus_dirty_update(afbdev, area->dx, area->dy, area->width,
76                          area->height);
77 }
78
79 static void cirrus_imageblit(struct fb_info *info,
80                           const struct fb_image *image)
81 {
82         struct cirrus_fbdev *afbdev = info->par;
83         sys_imageblit(info, image);
84         cirrus_dirty_update(afbdev, image->dx, image->dy, image->width,
85                          image->height);
86 }
87
88
89 static struct fb_ops cirrusfb_ops = {
90         .owner = THIS_MODULE,
91         .fb_check_var = drm_fb_helper_check_var,
92         .fb_set_par = drm_fb_helper_set_par,
93         .fb_fillrect = cirrus_fillrect,
94         .fb_copyarea = cirrus_copyarea,
95         .fb_imageblit = cirrus_imageblit,
96         .fb_pan_display = drm_fb_helper_pan_display,
97         .fb_blank = drm_fb_helper_blank,
98         .fb_setcmap = drm_fb_helper_setcmap,
99 };
100
101 static int cirrusfb_create_object(struct cirrus_fbdev *afbdev,
102                                struct drm_mode_fb_cmd2 *mode_cmd,
103                                struct drm_gem_object **gobj_p)
104 {
105         struct drm_device *dev = afbdev->helper.dev;
106         u32 bpp, depth;
107         u32 size;
108         struct drm_gem_object *gobj;
109
110         int ret = 0;
111         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
112
113         if (bpp > 24)
114                 return -EINVAL;
115         size = mode_cmd->pitches[0] * mode_cmd->height;
116         ret = cirrus_gem_create(dev, size, true, &gobj);
117         if (ret)
118                 return ret;
119
120         *gobj_p = gobj;
121         return ret;
122 }
123
124 static int cirrusfb_create(struct drm_fb_helper *helper,
125                            struct drm_fb_helper_surface_size *sizes)
126 {
127         struct cirrus_fbdev *gfbdev = (struct cirrus_fbdev *)helper;
128         struct drm_device *dev = gfbdev->helper.dev;
129         struct cirrus_device *cdev = gfbdev->helper.dev->dev_private;
130         struct fb_info *info;
131         struct drm_framebuffer *fb;
132         struct drm_mode_fb_cmd2 mode_cmd;
133         struct device *device = &dev->pdev->dev;
134         void *sysram;
135         struct drm_gem_object *gobj = NULL;
136         struct cirrus_bo *bo = NULL;
137         int size, ret;
138
139         mode_cmd.width = sizes->surface_width;
140         mode_cmd.height = sizes->surface_height;
141         mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
142         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
143                                                           sizes->surface_depth);
144         size = mode_cmd.pitches[0] * mode_cmd.height;
145
146         ret = cirrusfb_create_object(gfbdev, &mode_cmd, &gobj);
147         if (ret) {
148                 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
149                 return ret;
150         }
151
152         bo = gem_to_cirrus_bo(gobj);
153
154         sysram = vmalloc(size);
155         if (!sysram)
156                 return -ENOMEM;
157
158         info = framebuffer_alloc(0, device);
159         if (info == NULL)
160                 return -ENOMEM;
161
162         info->par = gfbdev;
163
164         ret = cirrus_framebuffer_init(cdev->dev, &gfbdev->gfb, &mode_cmd, gobj);
165         if (ret)
166                 return ret;
167
168         gfbdev->sysram = sysram;
169         gfbdev->size = size;
170
171         fb = &gfbdev->gfb.base;
172         if (!fb) {
173                 DRM_INFO("fb is NULL\n");
174                 return -EINVAL;
175         }
176
177         /* setup helper */
178         gfbdev->helper.fb = fb;
179         gfbdev->helper.fbdev = info;
180
181         strcpy(info->fix.id, "cirrusdrmfb");
182
183
184         info->flags = FBINFO_DEFAULT;
185         info->fbops = &cirrusfb_ops;
186
187         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
188         drm_fb_helper_fill_var(info, &gfbdev->helper, sizes->fb_width,
189                                sizes->fb_height);
190
191         /* setup aperture base/size for vesafb takeover */
192         info->apertures = alloc_apertures(1);
193         if (!info->apertures) {
194                 ret = -ENOMEM;
195                 goto out_iounmap;
196         }
197         info->apertures->ranges[0].base = cdev->dev->mode_config.fb_base;
198         info->apertures->ranges[0].size = cdev->mc.vram_size;
199
200         info->screen_base = sysram;
201         info->screen_size = size;
202
203         info->fix.mmio_start = 0;
204         info->fix.mmio_len = 0;
205
206         ret = fb_alloc_cmap(&info->cmap, 256, 0);
207         if (ret) {
208                 DRM_ERROR("%s: can't allocate color map\n", info->fix.id);
209                 ret = -ENOMEM;
210                 goto out_iounmap;
211         }
212
213         DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
214         DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info->fix.smem_start);
215         DRM_INFO("size %lu\n", (unsigned long)info->fix.smem_len);
216         DRM_INFO("fb depth is %d\n", fb->depth);
217         DRM_INFO("   pitch is %d\n", fb->pitches[0]);
218
219         return 0;
220 out_iounmap:
221         return ret;
222 }
223
224 static int cirrus_fbdev_destroy(struct drm_device *dev,
225                                 struct cirrus_fbdev *gfbdev)
226 {
227         struct fb_info *info;
228         struct cirrus_framebuffer *gfb = &gfbdev->gfb;
229
230         if (gfbdev->helper.fbdev) {
231                 info = gfbdev->helper.fbdev;
232
233                 unregister_framebuffer(info);
234                 if (info->cmap.len)
235                         fb_dealloc_cmap(&info->cmap);
236                 framebuffer_release(info);
237         }
238
239         if (gfb->obj) {
240                 drm_gem_object_unreference_unlocked(gfb->obj);
241                 gfb->obj = NULL;
242         }
243
244         vfree(gfbdev->sysram);
245         drm_fb_helper_fini(&gfbdev->helper);
246         drm_framebuffer_unregister_private(&gfb->base);
247         drm_framebuffer_cleanup(&gfb->base);
248
249         return 0;
250 }
251
252 static struct drm_fb_helper_funcs cirrus_fb_helper_funcs = {
253         .gamma_set = cirrus_crtc_fb_gamma_set,
254         .gamma_get = cirrus_crtc_fb_gamma_get,
255         .fb_probe = cirrusfb_create,
256 };
257
258 int cirrus_fbdev_init(struct cirrus_device *cdev)
259 {
260         struct cirrus_fbdev *gfbdev;
261         int ret;
262         int bpp_sel = 24;
263
264         /*bpp_sel = 8;*/
265         gfbdev = kzalloc(sizeof(struct cirrus_fbdev), GFP_KERNEL);
266         if (!gfbdev)
267                 return -ENOMEM;
268
269         cdev->mode_info.gfbdev = gfbdev;
270         gfbdev->helper.funcs = &cirrus_fb_helper_funcs;
271
272         ret = drm_fb_helper_init(cdev->dev, &gfbdev->helper,
273                                  cdev->num_crtc, CIRRUSFB_CONN_LIMIT);
274         if (ret) {
275                 kfree(gfbdev);
276                 return ret;
277         }
278         drm_fb_helper_single_add_all_connectors(&gfbdev->helper);
279
280         /* disable all the possible outputs/crtcs before entering KMS mode */
281         drm_helper_disable_unused_functions(cdev->dev);
282         drm_fb_helper_initial_config(&gfbdev->helper, bpp_sel);
283
284         return 0;
285 }
286
287 void cirrus_fbdev_fini(struct cirrus_device *cdev)
288 {
289         if (!cdev->mode_info.gfbdev)
290                 return;
291
292         cirrus_fbdev_destroy(cdev->dev, cdev->mode_info.gfbdev);
293         kfree(cdev->mode_info.gfbdev);
294         cdev->mode_info.gfbdev = NULL;
295 }