2 * Copyright 2012 Red Hat
4 * This file is subject to the terms and conditions of the GNU General
5 * Public License version 2. See the file COPYING in the main
6 * directory of this archive for more details.
8 * Authors: Matthew Garrett
11 #include <linux/module.h>
13 #include <drm/drm_fb_helper.h>
14 #include <drm/drm_crtc_helper.h>
18 #include "cirrus_drv.h"
20 static void cirrus_dirty_update(struct cirrus_fbdev
*afbdev
,
21 int x
, int y
, int width
, int height
)
24 struct drm_gem_object
*obj
;
26 int src_offset
, dst_offset
;
27 int bpp
= (afbdev
->gfb
.base
.bits_per_pixel
+ 7)/8;
31 obj
= afbdev
->gfb
.obj
;
32 bo
= gem_to_cirrus_bo(obj
);
34 ret
= cirrus_bo_reserve(bo
, true);
36 DRM_ERROR("failed to reserve fb bo\n");
40 if (!bo
->kmap
.virtual) {
41 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
43 DRM_ERROR("failed to kmap fb updates\n");
44 cirrus_bo_unreserve(bo
);
49 for (i
= y
; i
< y
+ height
; i
++) {
50 /* assume equal stride for now */
51 src_offset
= dst_offset
= i
* afbdev
->gfb
.base
.pitches
[0] + (x
* bpp
);
52 memcpy_toio(bo
->kmap
.virtual + src_offset
, afbdev
->sysram
+ src_offset
, width
* bpp
);
56 ttm_bo_kunmap(&bo
->kmap
);
58 cirrus_bo_unreserve(bo
);
61 static void cirrus_fillrect(struct fb_info
*info
,
62 const struct fb_fillrect
*rect
)
64 struct cirrus_fbdev
*afbdev
= info
->par
;
65 sys_fillrect(info
, rect
);
66 cirrus_dirty_update(afbdev
, rect
->dx
, rect
->dy
, rect
->width
,
70 static void cirrus_copyarea(struct fb_info
*info
,
71 const struct fb_copyarea
*area
)
73 struct cirrus_fbdev
*afbdev
= info
->par
;
74 sys_copyarea(info
, area
);
75 cirrus_dirty_update(afbdev
, area
->dx
, area
->dy
, area
->width
,
79 static void cirrus_imageblit(struct fb_info
*info
,
80 const struct fb_image
*image
)
82 struct cirrus_fbdev
*afbdev
= info
->par
;
83 sys_imageblit(info
, image
);
84 cirrus_dirty_update(afbdev
, image
->dx
, image
->dy
, image
->width
,
89 static struct fb_ops cirrusfb_ops
= {
91 .fb_check_var
= drm_fb_helper_check_var
,
92 .fb_set_par
= drm_fb_helper_set_par
,
93 .fb_fillrect
= cirrus_fillrect
,
94 .fb_copyarea
= cirrus_copyarea
,
95 .fb_imageblit
= cirrus_imageblit
,
96 .fb_pan_display
= drm_fb_helper_pan_display
,
97 .fb_blank
= drm_fb_helper_blank
,
98 .fb_setcmap
= drm_fb_helper_setcmap
,
101 static int cirrusfb_create_object(struct cirrus_fbdev
*afbdev
,
102 struct drm_mode_fb_cmd2
*mode_cmd
,
103 struct drm_gem_object
**gobj_p
)
105 struct drm_device
*dev
= afbdev
->helper
.dev
;
108 struct drm_gem_object
*gobj
;
111 drm_fb_get_bpp_depth(mode_cmd
->pixel_format
, &depth
, &bpp
);
115 size
= mode_cmd
->pitches
[0] * mode_cmd
->height
;
116 ret
= cirrus_gem_create(dev
, size
, true, &gobj
);
124 static int cirrusfb_create(struct drm_fb_helper
*helper
,
125 struct drm_fb_helper_surface_size
*sizes
)
127 struct cirrus_fbdev
*gfbdev
= (struct cirrus_fbdev
*)helper
;
128 struct drm_device
*dev
= gfbdev
->helper
.dev
;
129 struct cirrus_device
*cdev
= gfbdev
->helper
.dev
->dev_private
;
130 struct fb_info
*info
;
131 struct drm_framebuffer
*fb
;
132 struct drm_mode_fb_cmd2 mode_cmd
;
133 struct device
*device
= &dev
->pdev
->dev
;
135 struct drm_gem_object
*gobj
= NULL
;
136 struct cirrus_bo
*bo
= NULL
;
139 mode_cmd
.width
= sizes
->surface_width
;
140 mode_cmd
.height
= sizes
->surface_height
;
141 mode_cmd
.pitches
[0] = mode_cmd
.width
* ((sizes
->surface_bpp
+ 7) / 8);
142 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
143 sizes
->surface_depth
);
144 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
146 ret
= cirrusfb_create_object(gfbdev
, &mode_cmd
, &gobj
);
148 DRM_ERROR("failed to create fbcon backing object %d\n", ret
);
152 bo
= gem_to_cirrus_bo(gobj
);
154 sysram
= vmalloc(size
);
158 info
= framebuffer_alloc(0, device
);
164 ret
= cirrus_framebuffer_init(cdev
->dev
, &gfbdev
->gfb
, &mode_cmd
, gobj
);
168 gfbdev
->sysram
= sysram
;
171 fb
= &gfbdev
->gfb
.base
;
173 DRM_INFO("fb is NULL\n");
178 gfbdev
->helper
.fb
= fb
;
179 gfbdev
->helper
.fbdev
= info
;
181 strcpy(info
->fix
.id
, "cirrusdrmfb");
184 info
->flags
= FBINFO_DEFAULT
;
185 info
->fbops
= &cirrusfb_ops
;
187 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->depth
);
188 drm_fb_helper_fill_var(info
, &gfbdev
->helper
, sizes
->fb_width
,
191 /* setup aperture base/size for vesafb takeover */
192 info
->apertures
= alloc_apertures(1);
193 if (!info
->apertures
) {
197 info
->apertures
->ranges
[0].base
= cdev
->dev
->mode_config
.fb_base
;
198 info
->apertures
->ranges
[0].size
= cdev
->mc
.vram_size
;
200 info
->screen_base
= sysram
;
201 info
->screen_size
= size
;
203 info
->fix
.mmio_start
= 0;
204 info
->fix
.mmio_len
= 0;
206 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
208 DRM_ERROR("%s: can't allocate color map\n", info
->fix
.id
);
213 DRM_INFO("fb mappable at 0x%lX\n", info
->fix
.smem_start
);
214 DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info
->fix
.smem_start
);
215 DRM_INFO("size %lu\n", (unsigned long)info
->fix
.smem_len
);
216 DRM_INFO("fb depth is %d\n", fb
->depth
);
217 DRM_INFO(" pitch is %d\n", fb
->pitches
[0]);
224 static int cirrus_fbdev_destroy(struct drm_device
*dev
,
225 struct cirrus_fbdev
*gfbdev
)
227 struct fb_info
*info
;
228 struct cirrus_framebuffer
*gfb
= &gfbdev
->gfb
;
230 if (gfbdev
->helper
.fbdev
) {
231 info
= gfbdev
->helper
.fbdev
;
233 unregister_framebuffer(info
);
235 fb_dealloc_cmap(&info
->cmap
);
236 framebuffer_release(info
);
240 drm_gem_object_unreference_unlocked(gfb
->obj
);
244 vfree(gfbdev
->sysram
);
245 drm_fb_helper_fini(&gfbdev
->helper
);
246 drm_framebuffer_unregister_private(&gfb
->base
);
247 drm_framebuffer_cleanup(&gfb
->base
);
252 static struct drm_fb_helper_funcs cirrus_fb_helper_funcs
= {
253 .gamma_set
= cirrus_crtc_fb_gamma_set
,
254 .gamma_get
= cirrus_crtc_fb_gamma_get
,
255 .fb_probe
= cirrusfb_create
,
258 int cirrus_fbdev_init(struct cirrus_device
*cdev
)
260 struct cirrus_fbdev
*gfbdev
;
265 gfbdev
= kzalloc(sizeof(struct cirrus_fbdev
), GFP_KERNEL
);
269 cdev
->mode_info
.gfbdev
= gfbdev
;
270 gfbdev
->helper
.funcs
= &cirrus_fb_helper_funcs
;
272 ret
= drm_fb_helper_init(cdev
->dev
, &gfbdev
->helper
,
273 cdev
->num_crtc
, CIRRUSFB_CONN_LIMIT
);
278 drm_fb_helper_single_add_all_connectors(&gfbdev
->helper
);
280 /* disable all the possible outputs/crtcs before entering KMS mode */
281 drm_helper_disable_unused_functions(cdev
->dev
);
282 drm_fb_helper_initial_config(&gfbdev
->helper
, bpp_sel
);
287 void cirrus_fbdev_fini(struct cirrus_device
*cdev
)
289 if (!cdev
->mode_info
.gfbdev
)
292 cirrus_fbdev_destroy(cdev
->dev
, cdev
->mode_info
.gfbdev
);
293 kfree(cdev
->mode_info
.gfbdev
);
294 cdev
->mode_info
.gfbdev
= NULL
;