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 mgag200_bo
*bo
;
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 bo
= gem_to_mga_bo(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
= mgag200_bo_reserve(bo
, 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 (!bo
->kmap
.virtual) {
79 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
81 DRM_ERROR("failed to kmap fb updates\n");
82 mgag200_bo_unreserve(bo
);
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
);
94 ttm_bo_kunmap(&bo
->kmap
);
96 mgag200_bo_unreserve(bo
);
99 static void mga_fillrect(struct fb_info
*info
,
100 const struct fb_fillrect
*rect
)
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
,
108 static void mga_copyarea(struct fb_info
*info
,
109 const struct fb_copyarea
*area
)
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
,
117 static void mga_imageblit(struct fb_info
*info
,
118 const struct fb_image
*image
)
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
,
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
,
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
)
143 struct drm_device
*dev
= afbdev
->helper
.dev
;
145 struct drm_gem_object
*gobj
;
148 size
= mode_cmd
->pitches
[0] * mode_cmd
->height
;
149 ret
= mgag200_gem_create(dev
, size
, true, &gobj
);
157 static int mgag200fb_create(struct drm_fb_helper
*helper
,
158 struct drm_fb_helper_surface_size
*sizes
)
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
;
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);
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
;
180 ret
= mgag200fb_create_object(mfbdev
, &mode_cmd
, &gobj
);
182 DRM_ERROR("failed to create fbcon backing object %d\n", ret
);
186 sysram
= vmalloc(size
);
192 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 strcpy(info
->fix
.id
, "mgadrmfb");
214 info
->fbops
= &mgag200fb_ops
;
216 /* setup aperture base/size for vesafb takeover */
217 info
->apertures
->ranges
[0].base
= mdev
->dev
->mode_config
.fb_base
;
218 info
->apertures
->ranges
[0].size
= mdev
->mc
.vram_size
;
220 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->format
->depth
);
221 drm_fb_helper_fill_var(info
, &mfbdev
->helper
, sizes
->fb_width
,
224 info
->screen_base
= sysram
;
225 info
->screen_size
= size
;
226 info
->pixmap
.flags
= FB_PIXMAP_SYSTEM
;
228 DRM_DEBUG_KMS("allocated %dx%d\n",
229 fb
->width
, fb
->height
);
236 drm_gem_object_put_unlocked(gobj
);
241 static int mga_fbdev_destroy(struct drm_device
*dev
,
242 struct mga_fbdev
*mfbdev
)
244 struct mga_framebuffer
*mfb
= &mfbdev
->mfb
;
246 drm_fb_helper_unregister_fbi(&mfbdev
->helper
);
249 drm_gem_object_put_unlocked(mfb
->obj
);
252 drm_fb_helper_fini(&mfbdev
->helper
);
253 vfree(mfbdev
->sysram
);
254 drm_framebuffer_unregister_private(&mfb
->base
);
255 drm_framebuffer_cleanup(&mfb
->base
);
260 static const struct drm_fb_helper_funcs mga_fb_helper_funcs
= {
261 .fb_probe
= mgag200fb_create
,
264 int mgag200_fbdev_init(struct mga_device
*mdev
)
266 struct mga_fbdev
*mfbdev
;
270 /* prefer 16bpp on low end gpus with limited VRAM */
271 if (IS_G200_SE(mdev
) && mdev
->mc
.vram_size
< (2048*1024))
274 mfbdev
= devm_kzalloc(mdev
->dev
->dev
, sizeof(struct mga_fbdev
), GFP_KERNEL
);
278 mdev
->mfbdev
= mfbdev
;
279 spin_lock_init(&mfbdev
->dirty_lock
);
281 drm_fb_helper_prepare(mdev
->dev
, &mfbdev
->helper
, &mga_fb_helper_funcs
);
283 ret
= drm_fb_helper_init(mdev
->dev
, &mfbdev
->helper
,
284 MGAG200FB_CONN_LIMIT
);
288 ret
= drm_fb_helper_single_add_all_connectors(&mfbdev
->helper
);
292 /* disable all the possible outputs/crtcs before entering KMS mode */
293 drm_helper_disable_unused_functions(mdev
->dev
);
295 ret
= drm_fb_helper_initial_config(&mfbdev
->helper
, bpp_sel
);
302 drm_fb_helper_fini(&mfbdev
->helper
);
309 void mgag200_fbdev_fini(struct mga_device
*mdev
)
314 mga_fbdev_destroy(mdev
->dev
, mdev
->mfbdev
);