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>
14 #include "drm_fb_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 cirrus_fbdev
*gfbdev
,
125 struct drm_fb_helper_surface_size
*sizes
)
127 struct drm_device
*dev
= gfbdev
->helper
.dev
;
128 struct cirrus_device
*cdev
= gfbdev
->helper
.dev
->dev_private
;
129 struct fb_info
*info
;
130 struct drm_framebuffer
*fb
;
131 struct drm_mode_fb_cmd2 mode_cmd
;
132 struct device
*device
= &dev
->pdev
->dev
;
134 struct drm_gem_object
*gobj
= NULL
;
135 struct cirrus_bo
*bo
= NULL
;
138 mode_cmd
.width
= sizes
->surface_width
;
139 mode_cmd
.height
= sizes
->surface_height
;
140 mode_cmd
.pitches
[0] = mode_cmd
.width
* ((sizes
->surface_bpp
+ 7) / 8);
141 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
142 sizes
->surface_depth
);
143 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
145 ret
= cirrusfb_create_object(gfbdev
, &mode_cmd
, &gobj
);
147 DRM_ERROR("failed to create fbcon backing object %d\n", ret
);
151 bo
= gem_to_cirrus_bo(gobj
);
153 sysram
= vmalloc(size
);
157 info
= framebuffer_alloc(0, device
);
163 ret
= cirrus_framebuffer_init(cdev
->dev
, &gfbdev
->gfb
, &mode_cmd
, gobj
);
167 gfbdev
->sysram
= sysram
;
170 fb
= &gfbdev
->gfb
.base
;
172 DRM_INFO("fb is NULL\n");
177 gfbdev
->helper
.fb
= fb
;
178 gfbdev
->helper
.fbdev
= info
;
180 strcpy(info
->fix
.id
, "cirrusdrmfb");
183 info
->flags
= FBINFO_DEFAULT
;
184 info
->fbops
= &cirrusfb_ops
;
186 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->depth
);
187 drm_fb_helper_fill_var(info
, &gfbdev
->helper
, sizes
->fb_width
,
190 /* setup aperture base/size for vesafb takeover */
191 info
->apertures
= alloc_apertures(1);
192 if (!info
->apertures
) {
196 info
->apertures
->ranges
[0].base
= cdev
->dev
->mode_config
.fb_base
;
197 info
->apertures
->ranges
[0].size
= cdev
->mc
.vram_size
;
199 info
->screen_base
= sysram
;
200 info
->screen_size
= size
;
202 info
->fix
.mmio_start
= 0;
203 info
->fix
.mmio_len
= 0;
205 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
207 DRM_ERROR("%s: can't allocate color map\n", info
->fix
.id
);
212 DRM_INFO("fb mappable at 0x%lX\n", info
->fix
.smem_start
);
213 DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info
->fix
.smem_start
);
214 DRM_INFO("size %lu\n", (unsigned long)info
->fix
.smem_len
);
215 DRM_INFO("fb depth is %d\n", fb
->depth
);
216 DRM_INFO(" pitch is %d\n", fb
->pitches
[0]);
223 static int cirrus_fb_find_or_create_single(struct drm_fb_helper
*helper
,
224 struct drm_fb_helper_surface_size
227 struct cirrus_fbdev
*gfbdev
= (struct cirrus_fbdev
*)helper
;
232 ret
= cirrusfb_create(gfbdev
, sizes
);
240 static int cirrus_fbdev_destroy(struct drm_device
*dev
,
241 struct cirrus_fbdev
*gfbdev
)
243 struct fb_info
*info
;
244 struct cirrus_framebuffer
*gfb
= &gfbdev
->gfb
;
246 if (gfbdev
->helper
.fbdev
) {
247 info
= gfbdev
->helper
.fbdev
;
249 unregister_framebuffer(info
);
251 fb_dealloc_cmap(&info
->cmap
);
252 framebuffer_release(info
);
256 drm_gem_object_unreference_unlocked(gfb
->obj
);
260 vfree(gfbdev
->sysram
);
261 drm_fb_helper_fini(&gfbdev
->helper
);
262 drm_framebuffer_cleanup(&gfb
->base
);
267 static struct drm_fb_helper_funcs cirrus_fb_helper_funcs
= {
268 .gamma_set
= cirrus_crtc_fb_gamma_set
,
269 .gamma_get
= cirrus_crtc_fb_gamma_get
,
270 .fb_probe
= cirrus_fb_find_or_create_single
,
273 int cirrus_fbdev_init(struct cirrus_device
*cdev
)
275 struct cirrus_fbdev
*gfbdev
;
280 gfbdev
= kzalloc(sizeof(struct cirrus_fbdev
), GFP_KERNEL
);
284 cdev
->mode_info
.gfbdev
= gfbdev
;
285 gfbdev
->helper
.funcs
= &cirrus_fb_helper_funcs
;
287 ret
= drm_fb_helper_init(cdev
->dev
, &gfbdev
->helper
,
288 cdev
->num_crtc
, CIRRUSFB_CONN_LIMIT
);
293 drm_fb_helper_single_add_all_connectors(&gfbdev
->helper
);
294 drm_fb_helper_initial_config(&gfbdev
->helper
, bpp_sel
);
299 void cirrus_fbdev_fini(struct cirrus_device
*cdev
)
301 if (!cdev
->mode_info
.gfbdev
)
304 cirrus_fbdev_destroy(cdev
->dev
, cdev
->mode_info
.gfbdev
);
305 kfree(cdev
->mode_info
.gfbdev
);
306 cdev
->mode_info
.gfbdev
= NULL
;