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_fb_helper.h>
16 #include <drm/drm_crtc_helper.h>
18 #include "mgag200_drv.h"
20 static void mga_dirty_update(struct mga_fbdev
*mfbdev
,
21 int x
, int y
, int width
, int height
)
24 struct drm_gem_object
*obj
;
25 struct mgag200_bo
*bo
;
26 int src_offset
, dst_offset
;
27 int bpp
= (mfbdev
->mfb
.base
.bits_per_pixel
+ 7)/8;
30 bool store_for_later
= false;
34 obj
= mfbdev
->mfb
.obj
;
35 bo
= gem_to_mga_bo(obj
);
38 * try and reserve the BO, if we fail with busy
39 * then the BO is being moved and we should
40 * store up the damage until later.
43 ret
= mgag200_bo_reserve(bo
, true);
48 store_for_later
= true;
53 spin_lock_irqsave(&mfbdev
->dirty_lock
, flags
);
64 if (store_for_later
) {
69 spin_unlock_irqrestore(&mfbdev
->dirty_lock
, flags
);
73 mfbdev
->x1
= mfbdev
->y1
= INT_MAX
;
74 mfbdev
->x2
= mfbdev
->y2
= 0;
75 spin_unlock_irqrestore(&mfbdev
->dirty_lock
, flags
);
77 if (!bo
->kmap
.virtual) {
78 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
80 DRM_ERROR("failed to kmap fb updates\n");
81 mgag200_bo_unreserve(bo
);
86 for (i
= y
; i
<= y2
; i
++) {
87 /* assume equal stride for now */
88 src_offset
= dst_offset
= i
* mfbdev
->mfb
.base
.pitches
[0] + (x
* bpp
);
89 memcpy_toio(bo
->kmap
.virtual + src_offset
, mfbdev
->sysram
+ src_offset
, (x2
- x
+ 1) * bpp
);
93 ttm_bo_kunmap(&bo
->kmap
);
95 mgag200_bo_unreserve(bo
);
98 static void mga_fillrect(struct fb_info
*info
,
99 const struct fb_fillrect
*rect
)
101 struct mga_fbdev
*mfbdev
= info
->par
;
102 drm_fb_helper_sys_fillrect(info
, rect
);
103 mga_dirty_update(mfbdev
, rect
->dx
, rect
->dy
, rect
->width
,
107 static void mga_copyarea(struct fb_info
*info
,
108 const struct fb_copyarea
*area
)
110 struct mga_fbdev
*mfbdev
= info
->par
;
111 drm_fb_helper_sys_copyarea(info
, area
);
112 mga_dirty_update(mfbdev
, area
->dx
, area
->dy
, area
->width
,
116 static void mga_imageblit(struct fb_info
*info
,
117 const struct fb_image
*image
)
119 struct mga_fbdev
*mfbdev
= info
->par
;
120 drm_fb_helper_sys_imageblit(info
, image
);
121 mga_dirty_update(mfbdev
, image
->dx
, image
->dy
, image
->width
,
126 static struct fb_ops mgag200fb_ops
= {
127 .owner
= THIS_MODULE
,
128 .fb_check_var
= drm_fb_helper_check_var
,
129 .fb_set_par
= drm_fb_helper_set_par
,
130 .fb_fillrect
= mga_fillrect
,
131 .fb_copyarea
= mga_copyarea
,
132 .fb_imageblit
= mga_imageblit
,
133 .fb_pan_display
= drm_fb_helper_pan_display
,
134 .fb_blank
= drm_fb_helper_blank
,
135 .fb_setcmap
= drm_fb_helper_setcmap
,
138 static int mgag200fb_create_object(struct mga_fbdev
*afbdev
,
139 const struct drm_mode_fb_cmd2
*mode_cmd
,
140 struct drm_gem_object
**gobj_p
)
142 struct drm_device
*dev
= afbdev
->helper
.dev
;
144 struct drm_gem_object
*gobj
;
147 size
= mode_cmd
->pitches
[0] * mode_cmd
->height
;
148 ret
= mgag200_gem_create(dev
, size
, true, &gobj
);
156 static int mgag200fb_create(struct drm_fb_helper
*helper
,
157 struct drm_fb_helper_surface_size
*sizes
)
159 struct mga_fbdev
*mfbdev
=
160 container_of(helper
, struct mga_fbdev
, helper
);
161 struct drm_device
*dev
= mfbdev
->helper
.dev
;
162 struct drm_mode_fb_cmd2 mode_cmd
;
163 struct mga_device
*mdev
= dev
->dev_private
;
164 struct fb_info
*info
;
165 struct drm_framebuffer
*fb
;
166 struct drm_gem_object
*gobj
= NULL
;
171 mode_cmd
.width
= sizes
->surface_width
;
172 mode_cmd
.height
= sizes
->surface_height
;
173 mode_cmd
.pitches
[0] = mode_cmd
.width
* ((sizes
->surface_bpp
+ 7) / 8);
175 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
176 sizes
->surface_depth
);
177 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
179 ret
= mgag200fb_create_object(mfbdev
, &mode_cmd
, &gobj
);
181 DRM_ERROR("failed to create fbcon backing object %d\n", ret
);
185 sysram
= vmalloc(size
);
191 info
= drm_fb_helper_alloc_fbi(helper
);
199 ret
= mgag200_framebuffer_init(dev
, &mfbdev
->mfb
, &mode_cmd
, gobj
);
201 goto err_framebuffer_init
;
203 mfbdev
->sysram
= sysram
;
206 fb
= &mfbdev
->mfb
.base
;
209 mfbdev
->helper
.fb
= fb
;
211 strcpy(info
->fix
.id
, "mgadrmfb");
213 info
->flags
= FBINFO_DEFAULT
| FBINFO_CAN_FORCE_OUTPUT
;
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
->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
);
233 err_framebuffer_init
:
234 drm_fb_helper_release_fbi(helper
);
238 drm_gem_object_unreference_unlocked(gobj
);
243 static int mga_fbdev_destroy(struct drm_device
*dev
,
244 struct mga_fbdev
*mfbdev
)
246 struct mga_framebuffer
*mfb
= &mfbdev
->mfb
;
248 drm_fb_helper_unregister_fbi(&mfbdev
->helper
);
249 drm_fb_helper_release_fbi(&mfbdev
->helper
);
252 drm_gem_object_unreference_unlocked(mfb
->obj
);
255 drm_fb_helper_fini(&mfbdev
->helper
);
256 vfree(mfbdev
->sysram
);
257 drm_framebuffer_unregister_private(&mfb
->base
);
258 drm_framebuffer_cleanup(&mfb
->base
);
263 static const struct drm_fb_helper_funcs mga_fb_helper_funcs
= {
264 .gamma_set
= mga_crtc_fb_gamma_set
,
265 .gamma_get
= mga_crtc_fb_gamma_get
,
266 .fb_probe
= mgag200fb_create
,
269 int mgag200_fbdev_init(struct mga_device
*mdev
)
271 struct mga_fbdev
*mfbdev
;
275 /* prefer 16bpp on low end gpus with limited VRAM */
276 if (IS_G200_SE(mdev
) && mdev
->mc
.vram_size
< (2048*1024))
279 mfbdev
= devm_kzalloc(mdev
->dev
->dev
, sizeof(struct mga_fbdev
), GFP_KERNEL
);
283 mdev
->mfbdev
= mfbdev
;
284 spin_lock_init(&mfbdev
->dirty_lock
);
286 drm_fb_helper_prepare(mdev
->dev
, &mfbdev
->helper
, &mga_fb_helper_funcs
);
288 ret
= drm_fb_helper_init(mdev
->dev
, &mfbdev
->helper
,
289 mdev
->num_crtc
, MGAG200FB_CONN_LIMIT
);
293 ret
= drm_fb_helper_single_add_all_connectors(&mfbdev
->helper
);
297 /* disable all the possible outputs/crtcs before entering KMS mode */
298 drm_helper_disable_unused_functions(mdev
->dev
);
300 ret
= drm_fb_helper_initial_config(&mfbdev
->helper
, bpp_sel
);
307 drm_fb_helper_fini(&mfbdev
->helper
);
314 void mgag200_fbdev_fini(struct mga_device
*mdev
)
319 mga_fbdev_destroy(mdev
->dev
, mdev
->mfbdev
);