2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
26 * Authors: Dave Airlie <airlied@redhat.com>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/string.h>
33 #include <linux/tty.h>
34 #include <linux/sysrq.h>
35 #include <linux/delay.h>
37 #include <linux/init.h>
43 #include "drm_fb_helper.h"
46 static void ast_dirty_update(struct ast_fbdev
*afbdev
,
47 int x
, int y
, int width
, int height
)
50 struct drm_gem_object
*obj
;
52 int src_offset
, dst_offset
;
53 int bpp
= (afbdev
->afb
.base
.bits_per_pixel
+ 7)/8;
57 obj
= afbdev
->afb
.obj
;
58 bo
= gem_to_ast_bo(obj
);
60 ret
= ast_bo_reserve(bo
, true);
62 DRM_ERROR("failed to reserve fb bo\n");
66 if (!bo
->kmap
.virtual) {
67 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
69 DRM_ERROR("failed to kmap fb updates\n");
75 for (i
= y
; i
< y
+ height
; i
++) {
76 /* assume equal stride for now */
77 src_offset
= dst_offset
= i
* afbdev
->afb
.base
.pitches
[0] + (x
* bpp
);
78 memcpy_toio(bo
->kmap
.virtual + src_offset
, afbdev
->sysram
+ src_offset
, width
* bpp
);
82 ttm_bo_kunmap(&bo
->kmap
);
87 static void ast_fillrect(struct fb_info
*info
,
88 const struct fb_fillrect
*rect
)
90 struct ast_fbdev
*afbdev
= info
->par
;
91 sys_fillrect(info
, rect
);
92 ast_dirty_update(afbdev
, rect
->dx
, rect
->dy
, rect
->width
,
96 static void ast_copyarea(struct fb_info
*info
,
97 const struct fb_copyarea
*area
)
99 struct ast_fbdev
*afbdev
= info
->par
;
100 sys_copyarea(info
, area
);
101 ast_dirty_update(afbdev
, area
->dx
, area
->dy
, area
->width
,
105 static void ast_imageblit(struct fb_info
*info
,
106 const struct fb_image
*image
)
108 struct ast_fbdev
*afbdev
= info
->par
;
109 sys_imageblit(info
, image
);
110 ast_dirty_update(afbdev
, image
->dx
, image
->dy
, image
->width
,
114 static struct fb_ops astfb_ops
= {
115 .owner
= THIS_MODULE
,
116 .fb_check_var
= drm_fb_helper_check_var
,
117 .fb_set_par
= drm_fb_helper_set_par
,
118 .fb_fillrect
= ast_fillrect
,
119 .fb_copyarea
= ast_copyarea
,
120 .fb_imageblit
= ast_imageblit
,
121 .fb_pan_display
= drm_fb_helper_pan_display
,
122 .fb_blank
= drm_fb_helper_blank
,
123 .fb_setcmap
= drm_fb_helper_setcmap
,
124 .fb_debug_enter
= drm_fb_helper_debug_enter
,
125 .fb_debug_leave
= drm_fb_helper_debug_leave
,
128 static int astfb_create_object(struct ast_fbdev
*afbdev
,
129 struct drm_mode_fb_cmd2
*mode_cmd
,
130 struct drm_gem_object
**gobj_p
)
132 struct drm_device
*dev
= afbdev
->helper
.dev
;
135 struct drm_gem_object
*gobj
;
138 drm_fb_get_bpp_depth(mode_cmd
->pixel_format
, &depth
, &bpp
);
140 size
= mode_cmd
->pitches
[0] * mode_cmd
->height
;
141 ret
= ast_gem_create(dev
, size
, true, &gobj
);
149 static int astfb_create(struct ast_fbdev
*afbdev
,
150 struct drm_fb_helper_surface_size
*sizes
)
152 struct drm_device
*dev
= afbdev
->helper
.dev
;
153 struct drm_mode_fb_cmd2 mode_cmd
;
154 struct drm_framebuffer
*fb
;
155 struct fb_info
*info
;
157 struct device
*device
= &dev
->pdev
->dev
;
159 struct drm_gem_object
*gobj
= NULL
;
160 struct ast_bo
*bo
= NULL
;
161 mode_cmd
.width
= sizes
->surface_width
;
162 mode_cmd
.height
= sizes
->surface_height
;
163 mode_cmd
.pitches
[0] = mode_cmd
.width
* ((sizes
->surface_bpp
+ 7)/8);
165 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
166 sizes
->surface_depth
);
168 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
170 ret
= astfb_create_object(afbdev
, &mode_cmd
, &gobj
);
172 DRM_ERROR("failed to create fbcon backing object %d\n", ret
);
175 bo
= gem_to_ast_bo(gobj
);
177 sysram
= vmalloc(size
);
181 info
= framebuffer_alloc(0, device
);
188 ret
= ast_framebuffer_init(dev
, &afbdev
->afb
, &mode_cmd
, gobj
);
192 afbdev
->sysram
= sysram
;
195 fb
= &afbdev
->afb
.base
;
196 afbdev
->helper
.fb
= fb
;
197 afbdev
->helper
.fbdev
= info
;
199 strcpy(info
->fix
.id
, "astdrmfb");
201 info
->flags
= FBINFO_DEFAULT
| FBINFO_CAN_FORCE_OUTPUT
;
202 info
->fbops
= &astfb_ops
;
204 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
210 info
->apertures
= alloc_apertures(1);
211 if (!info
->apertures
) {
215 info
->apertures
->ranges
[0].base
= pci_resource_start(dev
->pdev
, 0);
216 info
->apertures
->ranges
[0].size
= pci_resource_len(dev
->pdev
, 0);
218 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->depth
);
219 drm_fb_helper_fill_var(info
, &afbdev
->helper
, sizes
->fb_width
, sizes
->fb_height
);
221 info
->screen_base
= sysram
;
222 info
->screen_size
= size
;
224 info
->pixmap
.flags
= FB_PIXMAP_SYSTEM
;
226 DRM_DEBUG_KMS("allocated %dx%d\n",
227 fb
->width
, fb
->height
);
234 static void ast_fb_gamma_set(struct drm_crtc
*crtc
, u16 red
, u16 green
,
237 struct ast_crtc
*ast_crtc
= to_ast_crtc(crtc
);
238 ast_crtc
->lut_r
[regno
] = red
>> 8;
239 ast_crtc
->lut_g
[regno
] = green
>> 8;
240 ast_crtc
->lut_b
[regno
] = blue
>> 8;
243 static void ast_fb_gamma_get(struct drm_crtc
*crtc
, u16
*red
, u16
*green
,
244 u16
*blue
, int regno
)
246 struct ast_crtc
*ast_crtc
= to_ast_crtc(crtc
);
247 *red
= ast_crtc
->lut_r
[regno
] << 8;
248 *green
= ast_crtc
->lut_g
[regno
] << 8;
249 *blue
= ast_crtc
->lut_b
[regno
] << 8;
252 static int ast_find_or_create_single(struct drm_fb_helper
*helper
,
253 struct drm_fb_helper_surface_size
*sizes
)
255 struct ast_fbdev
*afbdev
= (struct ast_fbdev
*)helper
;
260 ret
= astfb_create(afbdev
, sizes
);
268 static struct drm_fb_helper_funcs ast_fb_helper_funcs
= {
269 .gamma_set
= ast_fb_gamma_set
,
270 .gamma_get
= ast_fb_gamma_get
,
271 .fb_probe
= ast_find_or_create_single
,
274 static void ast_fbdev_destroy(struct drm_device
*dev
,
275 struct ast_fbdev
*afbdev
)
277 struct fb_info
*info
;
278 struct ast_framebuffer
*afb
= &afbdev
->afb
;
279 if (afbdev
->helper
.fbdev
) {
280 info
= afbdev
->helper
.fbdev
;
281 unregister_framebuffer(info
);
283 fb_dealloc_cmap(&info
->cmap
);
284 framebuffer_release(info
);
288 drm_gem_object_unreference_unlocked(afb
->obj
);
291 drm_fb_helper_fini(&afbdev
->helper
);
293 vfree(afbdev
->sysram
);
294 drm_framebuffer_cleanup(&afb
->base
);
297 int ast_fbdev_init(struct drm_device
*dev
)
299 struct ast_private
*ast
= dev
->dev_private
;
300 struct ast_fbdev
*afbdev
;
303 afbdev
= kzalloc(sizeof(struct ast_fbdev
), GFP_KERNEL
);
308 afbdev
->helper
.funcs
= &ast_fb_helper_funcs
;
309 ret
= drm_fb_helper_init(dev
, &afbdev
->helper
,
316 drm_fb_helper_single_add_all_connectors(&afbdev
->helper
);
317 drm_fb_helper_initial_config(&afbdev
->helper
, 32);
321 void ast_fbdev_fini(struct drm_device
*dev
)
323 struct ast_private
*ast
= dev
->dev_private
;
328 ast_fbdev_destroy(dev
, ast
->fbdev
);
333 void ast_fbdev_set_suspend(struct drm_device
*dev
, int state
)
335 struct ast_private
*ast
= dev
->dev_private
;
340 fb_set_suspend(ast
->fbdev
->helper
.fbdev
, state
);