2 * Copyright 2010 Matt Turner.
3 * Copyright 2012 Red Hat
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.
9 * Authors: Matthew Garrett
13 #include <linux/module.h>
15 #include <drm/drm_util.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_crtc_helper.h>
19 #include "mgag200_drv.h"
21 static void mga_dirty_update(struct mga_fbdev *mfbdev,
22 int x, int y, int width, int height)
25 struct drm_gem_object *obj;
26 struct drm_gem_vram_object *gbo;
27 int src_offset, dst_offset;
28 int bpp = mfbdev->mfb.base.format->cpp[0];
31 bool store_for_later = false;
35 obj = mfbdev->mfb.obj;
36 gbo = drm_gem_vram_of_gem(obj);
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.
44 ret = drm_gem_vram_reserve(gbo, true);
49 store_for_later = true;
54 spin_lock_irqsave(&mfbdev->dirty_lock, flags);
65 if (store_for_later) {
70 spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
74 mfbdev->x1 = mfbdev->y1 = INT_MAX;
75 mfbdev->x2 = mfbdev->y2 = 0;
76 spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
78 if (!gbo->kmap.virtual) {
79 ret = ttm_bo_kmap(&gbo->bo, 0, gbo->bo.num_pages, &gbo->kmap);
81 DRM_ERROR("failed to kmap fb updates\n");
82 drm_gem_vram_unreserve(gbo);
87 for (i = y; i <= y2; i++) {
88 /* assume equal stride for now */
89 src_offset = dst_offset =
90 i * mfbdev->mfb.base.pitches[0] + (x * bpp);
91 memcpy_toio(gbo->kmap.virtual + src_offset,
92 mfbdev->sysram + dst_offset, (x2 - x + 1) * bpp);
96 ttm_bo_kunmap(&gbo->kmap);
98 drm_gem_vram_unreserve(gbo);
101 static void mga_fillrect(struct fb_info *info,
102 const struct fb_fillrect *rect)
104 struct mga_fbdev *mfbdev = info->par;
105 drm_fb_helper_sys_fillrect(info, rect);
106 mga_dirty_update(mfbdev, rect->dx, rect->dy, rect->width,
110 static void mga_copyarea(struct fb_info *info,
111 const struct fb_copyarea *area)
113 struct mga_fbdev *mfbdev = info->par;
114 drm_fb_helper_sys_copyarea(info, area);
115 mga_dirty_update(mfbdev, area->dx, area->dy, area->width,
119 static void mga_imageblit(struct fb_info *info,
120 const struct fb_image *image)
122 struct mga_fbdev *mfbdev = info->par;
123 drm_fb_helper_sys_imageblit(info, image);
124 mga_dirty_update(mfbdev, image->dx, image->dy, image->width,
129 static struct fb_ops mgag200fb_ops = {
130 .owner = THIS_MODULE,
131 .fb_check_var = drm_fb_helper_check_var,
132 .fb_set_par = drm_fb_helper_set_par,
133 .fb_fillrect = mga_fillrect,
134 .fb_copyarea = mga_copyarea,
135 .fb_imageblit = mga_imageblit,
136 .fb_pan_display = drm_fb_helper_pan_display,
137 .fb_blank = drm_fb_helper_blank,
138 .fb_setcmap = drm_fb_helper_setcmap,
141 static int mgag200fb_create_object(struct mga_fbdev *afbdev,
142 const struct drm_mode_fb_cmd2 *mode_cmd,
143 struct drm_gem_object **gobj_p)
145 struct drm_device *dev = afbdev->helper.dev;
147 struct drm_gem_object *gobj;
150 size = mode_cmd->pitches[0] * mode_cmd->height;
151 ret = mgag200_gem_create(dev, size, true, &gobj);
159 static int mgag200fb_create(struct drm_fb_helper *helper,
160 struct drm_fb_helper_surface_size *sizes)
162 struct mga_fbdev *mfbdev =
163 container_of(helper, struct mga_fbdev, helper);
164 struct drm_device *dev = mfbdev->helper.dev;
165 struct drm_mode_fb_cmd2 mode_cmd;
166 struct mga_device *mdev = dev->dev_private;
167 struct fb_info *info;
168 struct drm_framebuffer *fb;
169 struct drm_gem_object *gobj = NULL;
174 mode_cmd.width = sizes->surface_width;
175 mode_cmd.height = sizes->surface_height;
176 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;
182 ret = mgag200fb_create_object(mfbdev, &mode_cmd, &gobj);
184 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
188 sysram = vmalloc(size);
194 info = drm_fb_helper_alloc_fbi(helper);
200 ret = mgag200_framebuffer_init(dev, &mfbdev->mfb, &mode_cmd, gobj);
204 mfbdev->sysram = sysram;
207 fb = &mfbdev->mfb.base;
210 mfbdev->helper.fb = fb;
212 info->fbops = &mgag200fb_ops;
214 /* setup aperture base/size for vesafb takeover */
215 info->apertures->ranges[0].base = mdev->dev->mode_config.fb_base;
216 info->apertures->ranges[0].size = mdev->mc.vram_size;
218 drm_fb_helper_fill_info(info, &mfbdev->helper, sizes);
220 info->screen_base = sysram;
221 info->screen_size = size;
222 info->pixmap.flags = FB_PIXMAP_SYSTEM;
224 DRM_DEBUG_KMS("allocated %dx%d\n",
225 fb->width, fb->height);
232 drm_gem_object_put_unlocked(gobj);
237 static int mga_fbdev_destroy(struct drm_device *dev,
238 struct mga_fbdev *mfbdev)
240 struct mga_framebuffer *mfb = &mfbdev->mfb;
242 drm_fb_helper_unregister_fbi(&mfbdev->helper);
245 drm_gem_object_put_unlocked(mfb->obj);
248 drm_fb_helper_fini(&mfbdev->helper);
249 vfree(mfbdev->sysram);
250 drm_framebuffer_unregister_private(&mfb->base);
251 drm_framebuffer_cleanup(&mfb->base);
256 static const struct drm_fb_helper_funcs mga_fb_helper_funcs = {
257 .fb_probe = mgag200fb_create,
260 int mgag200_fbdev_init(struct mga_device *mdev)
262 struct mga_fbdev *mfbdev;
266 /* prefer 16bpp on low end gpus with limited VRAM */
267 if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024))
270 mfbdev = devm_kzalloc(mdev->dev->dev, sizeof(struct mga_fbdev), GFP_KERNEL);
274 mdev->mfbdev = mfbdev;
275 spin_lock_init(&mfbdev->dirty_lock);
277 drm_fb_helper_prepare(mdev->dev, &mfbdev->helper, &mga_fb_helper_funcs);
279 ret = drm_fb_helper_init(mdev->dev, &mfbdev->helper,
280 MGAG200FB_CONN_LIMIT);
284 ret = drm_fb_helper_single_add_all_connectors(&mfbdev->helper);
288 /* disable all the possible outputs/crtcs before entering KMS mode */
289 drm_helper_disable_unused_functions(mdev->dev);
291 ret = drm_fb_helper_initial_config(&mfbdev->helper, bpp_sel);
298 drm_fb_helper_fini(&mfbdev->helper);
305 void mgag200_fbdev_fini(struct mga_device *mdev)
310 mga_fbdev_destroy(mdev->dev, mdev->mfbdev);