Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mgag200_fb.c
Go to the documentation of this file.
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 
17 #include <linux/fb.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.bits_per_pixel + 7)/8;
29  int ret;
30  bool unmap = false;
31 
32  obj = mfbdev->mfb.obj;
33  bo = gem_to_mga_bo(obj);
34 
35  ret = mgag200_bo_reserve(bo, true);
36  if (ret) {
37  DRM_ERROR("failed to reserve fb bo\n");
38  return;
39  }
40 
41  if (!bo->kmap.virtual) {
42  ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
43  if (ret) {
44  DRM_ERROR("failed to kmap fb updates\n");
46  return;
47  }
48  unmap = true;
49  }
50  for (i = y; i < y + height; i++) {
51  /* assume equal stride for now */
52  src_offset = dst_offset = i * mfbdev->mfb.base.pitches[0] + (x * bpp);
53  memcpy_toio(bo->kmap.virtual + src_offset, mfbdev->sysram + src_offset, width * bpp);
54 
55  }
56  if (unmap)
57  ttm_bo_kunmap(&bo->kmap);
58 
60 }
61 
62 static void mga_fillrect(struct fb_info *info,
63  const struct fb_fillrect *rect)
64 {
65  struct mga_fbdev *mfbdev = info->par;
66  sys_fillrect(info, rect);
67  mga_dirty_update(mfbdev, rect->dx, rect->dy, rect->width,
68  rect->height);
69 }
70 
71 static void mga_copyarea(struct fb_info *info,
72  const struct fb_copyarea *area)
73 {
74  struct mga_fbdev *mfbdev = info->par;
75  sys_copyarea(info, area);
76  mga_dirty_update(mfbdev, area->dx, area->dy, area->width,
77  area->height);
78 }
79 
80 static void mga_imageblit(struct fb_info *info,
81  const struct fb_image *image)
82 {
83  struct mga_fbdev *mfbdev = info->par;
84  sys_imageblit(info, image);
85  mga_dirty_update(mfbdev, image->dx, image->dy, image->width,
86  image->height);
87 }
88 
89 
90 static struct fb_ops mgag200fb_ops = {
91  .owner = THIS_MODULE,
92  .fb_check_var = drm_fb_helper_check_var,
93  .fb_set_par = drm_fb_helper_set_par,
94  .fb_fillrect = mga_fillrect,
95  .fb_copyarea = mga_copyarea,
96  .fb_imageblit = mga_imageblit,
97  .fb_pan_display = drm_fb_helper_pan_display,
98  .fb_blank = drm_fb_helper_blank,
99  .fb_setcmap = drm_fb_helper_setcmap,
100 };
101 
102 static int mgag200fb_create_object(struct mga_fbdev *afbdev,
103  struct drm_mode_fb_cmd2 *mode_cmd,
104  struct drm_gem_object **gobj_p)
105 {
106  struct drm_device *dev = afbdev->helper.dev;
107  u32 bpp, depth;
108  u32 size;
109  struct drm_gem_object *gobj;
110 
111  int ret = 0;
112  drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
113 
114  size = mode_cmd->pitches[0] * mode_cmd->height;
115  ret = mgag200_gem_create(dev, size, true, &gobj);
116  if (ret)
117  return ret;
118 
119  *gobj_p = gobj;
120  return ret;
121 }
122 
123 static int mgag200fb_create(struct mga_fbdev *mfbdev,
124  struct drm_fb_helper_surface_size *sizes)
125 {
126  struct drm_device *dev = mfbdev->helper.dev;
127  struct drm_mode_fb_cmd2 mode_cmd;
128  struct mga_device *mdev = dev->dev_private;
129  struct fb_info *info;
130  struct drm_framebuffer *fb;
131  struct drm_gem_object *gobj = NULL;
132  struct device *device = &dev->pdev->dev;
133  struct mgag200_bo *bo;
134  int ret;
135  void *sysram;
136  int size;
137 
138  mode_cmd.width = sizes->surface_width;
139  mode_cmd.height = sizes->surface_height;
140  mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
141 
143  sizes->surface_depth);
144  size = mode_cmd.pitches[0] * mode_cmd.height;
145 
146  ret = mgag200fb_create_object(mfbdev, &mode_cmd, &gobj);
147  if (ret) {
148  DRM_ERROR("failed to create fbcon backing object %d\n", ret);
149  return ret;
150  }
151  bo = gem_to_mga_bo(gobj);
152 
153  sysram = vmalloc(size);
154  if (!sysram)
155  return -ENOMEM;
156 
157  info = framebuffer_alloc(0, device);
158  if (info == NULL)
159  return -ENOMEM;
160 
161  info->par = mfbdev;
162 
163  ret = mgag200_framebuffer_init(dev, &mfbdev->mfb, &mode_cmd, gobj);
164  if (ret)
165  return ret;
166 
167  mfbdev->sysram = sysram;
168  mfbdev->size = size;
169 
170  fb = &mfbdev->mfb.base;
171 
172  /* setup helper */
173  mfbdev->helper.fb = fb;
174  mfbdev->helper.fbdev = info;
175 
176  ret = fb_alloc_cmap(&info->cmap, 256, 0);
177  if (ret) {
178  DRM_ERROR("%s: can't allocate color map\n", info->fix.id);
179  ret = -ENOMEM;
180  goto out;
181  }
182 
183  strcpy(info->fix.id, "mgadrmfb");
184 
186  info->fbops = &mgag200fb_ops;
187 
188  /* setup aperture base/size for vesafb takeover */
189  info->apertures = alloc_apertures(1);
190  if (!info->apertures) {
191  ret = -ENOMEM;
192  goto out;
193  }
194  info->apertures->ranges[0].base = mdev->dev->mode_config.fb_base;
195  info->apertures->ranges[0].size = mdev->mc.vram_size;
196 
197  drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
198  drm_fb_helper_fill_var(info, &mfbdev->helper, sizes->fb_width,
199  sizes->fb_height);
200 
201  info->screen_base = sysram;
202  info->screen_size = size;
203  info->pixmap.flags = FB_PIXMAP_SYSTEM;
204 
205  DRM_DEBUG_KMS("allocated %dx%d\n",
206  fb->width, fb->height);
207  return 0;
208 out:
209  return ret;
210 }
211 
212 static int mga_fb_find_or_create_single(struct drm_fb_helper *helper,
214  *sizes)
215 {
216  struct mga_fbdev *mfbdev = (struct mga_fbdev *)helper;
217  int new_fb = 0;
218  int ret;
219 
220  if (!helper->fb) {
221  ret = mgag200fb_create(mfbdev, sizes);
222  if (ret)
223  return ret;
224  new_fb = 1;
225  }
226  return new_fb;
227 }
228 
229 static int mga_fbdev_destroy(struct drm_device *dev,
230  struct mga_fbdev *mfbdev)
231 {
232  struct fb_info *info;
233  struct mga_framebuffer *mfb = &mfbdev->mfb;
234 
235  if (mfbdev->helper.fbdev) {
236  info = mfbdev->helper.fbdev;
237 
239  if (info->cmap.len)
240  fb_dealloc_cmap(&info->cmap);
241  framebuffer_release(info);
242  }
243 
244  if (mfb->obj) {
245  drm_gem_object_unreference_unlocked(mfb->obj);
246  mfb->obj = NULL;
247  }
248  drm_fb_helper_fini(&mfbdev->helper);
249  vfree(mfbdev->sysram);
251 
252  return 0;
253 }
254 
255 static struct drm_fb_helper_funcs mga_fb_helper_funcs = {
256  .gamma_set = mga_crtc_fb_gamma_set,
257  .gamma_get = mga_crtc_fb_gamma_get,
258  .fb_probe = mga_fb_find_or_create_single,
259 };
260 
262 {
263  struct mga_fbdev *mfbdev;
264  int ret;
265 
266  mfbdev = kzalloc(sizeof(struct mga_fbdev), GFP_KERNEL);
267  if (!mfbdev)
268  return -ENOMEM;
269 
270  mdev->mfbdev = mfbdev;
271  mfbdev->helper.funcs = &mga_fb_helper_funcs;
272 
273  ret = drm_fb_helper_init(mdev->dev, &mfbdev->helper,
275  if (ret) {
276  kfree(mfbdev);
277  return ret;
278  }
280  drm_fb_helper_initial_config(&mfbdev->helper, 32);
281 
282  return 0;
283 }
284 
285 void mgag200_fbdev_fini(struct mga_device *mdev)
286 {
287  if (!mdev->mfbdev)
288  return;
289 
290  mga_fbdev_destroy(mdev->dev, mdev->mfbdev);
291  kfree(mdev->mfbdev);
292  mdev->mfbdev = NULL;
293 }