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>
16 #include "cirrus_drv.h"
18 static void cirrus_dirty_update(struct cirrus_fbdev
*afbdev
,
19 int x
, int y
, int width
, int height
)
22 struct drm_gem_object
*obj
;
24 int src_offset
, dst_offset
;
25 int bpp
= afbdev
->gfb
.base
.format
->cpp
[0];
28 bool store_for_later
= false;
32 obj
= afbdev
->gfb
.obj
;
33 bo
= gem_to_cirrus_bo(obj
);
36 * try and reserve the BO, if we fail with busy
37 * then the BO is being moved and we should
38 * store up the damage until later.
41 ret
= cirrus_bo_reserve(bo
, true);
45 store_for_later
= true;
50 spin_lock_irqsave(&afbdev
->dirty_lock
, flags
);
61 if (store_for_later
) {
66 spin_unlock_irqrestore(&afbdev
->dirty_lock
, flags
);
70 afbdev
->x1
= afbdev
->y1
= INT_MAX
;
71 afbdev
->x2
= afbdev
->y2
= 0;
72 spin_unlock_irqrestore(&afbdev
->dirty_lock
, flags
);
74 if (!bo
->kmap
.virtual) {
75 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
77 DRM_ERROR("failed to kmap fb updates\n");
78 cirrus_bo_unreserve(bo
);
83 for (i
= y
; i
< y
+ height
; i
++) {
84 /* assume equal stride for now */
85 src_offset
= dst_offset
= i
* afbdev
->gfb
.base
.pitches
[0] + (x
* bpp
);
86 memcpy_toio(bo
->kmap
.virtual + src_offset
, afbdev
->sysram
+ src_offset
, width
* bpp
);
90 ttm_bo_kunmap(&bo
->kmap
);
92 cirrus_bo_unreserve(bo
);
95 static void cirrus_fillrect(struct fb_info
*info
,
96 const struct fb_fillrect
*rect
)
98 struct cirrus_fbdev
*afbdev
= info
->par
;
99 drm_fb_helper_sys_fillrect(info
, rect
);
100 cirrus_dirty_update(afbdev
, rect
->dx
, rect
->dy
, rect
->width
,
104 static void cirrus_copyarea(struct fb_info
*info
,
105 const struct fb_copyarea
*area
)
107 struct cirrus_fbdev
*afbdev
= info
->par
;
108 drm_fb_helper_sys_copyarea(info
, area
);
109 cirrus_dirty_update(afbdev
, area
->dx
, area
->dy
, area
->width
,
113 static void cirrus_imageblit(struct fb_info
*info
,
114 const struct fb_image
*image
)
116 struct cirrus_fbdev
*afbdev
= info
->par
;
117 drm_fb_helper_sys_imageblit(info
, image
);
118 cirrus_dirty_update(afbdev
, image
->dx
, image
->dy
, image
->width
,
123 static struct fb_ops cirrusfb_ops
= {
124 .owner
= THIS_MODULE
,
125 .fb_check_var
= drm_fb_helper_check_var
,
126 .fb_set_par
= drm_fb_helper_set_par
,
127 .fb_fillrect
= cirrus_fillrect
,
128 .fb_copyarea
= cirrus_copyarea
,
129 .fb_imageblit
= cirrus_imageblit
,
130 .fb_pan_display
= drm_fb_helper_pan_display
,
131 .fb_blank
= drm_fb_helper_blank
,
132 .fb_setcmap
= drm_fb_helper_setcmap
,
135 static int cirrusfb_create_object(struct cirrus_fbdev
*afbdev
,
136 const struct drm_mode_fb_cmd2
*mode_cmd
,
137 struct drm_gem_object
**gobj_p
)
139 struct drm_device
*dev
= afbdev
->helper
.dev
;
140 struct cirrus_device
*cdev
= dev
->dev_private
;
143 struct drm_gem_object
*gobj
;
146 bpp
= drm_format_plane_cpp(mode_cmd
->pixel_format
, 0) * 8;
148 if (!cirrus_check_framebuffer(cdev
, mode_cmd
->width
, mode_cmd
->height
,
149 bpp
, mode_cmd
->pitches
[0]))
152 size
= mode_cmd
->pitches
[0] * mode_cmd
->height
;
153 ret
= cirrus_gem_create(dev
, size
, true, &gobj
);
161 static int cirrusfb_create(struct drm_fb_helper
*helper
,
162 struct drm_fb_helper_surface_size
*sizes
)
164 struct cirrus_fbdev
*gfbdev
=
165 container_of(helper
, struct cirrus_fbdev
, helper
);
166 struct cirrus_device
*cdev
= gfbdev
->helper
.dev
->dev_private
;
167 struct fb_info
*info
;
168 struct drm_framebuffer
*fb
;
169 struct drm_mode_fb_cmd2 mode_cmd
;
171 struct drm_gem_object
*gobj
= NULL
;
172 struct cirrus_bo
*bo
= NULL
;
175 mode_cmd
.width
= sizes
->surface_width
;
176 mode_cmd
.height
= sizes
->surface_height
;
177 mode_cmd
.pitches
[0] = mode_cmd
.width
* ((sizes
->surface_bpp
+ 7) / 8);
178 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
179 sizes
->surface_depth
);
180 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
182 ret
= cirrusfb_create_object(gfbdev
, &mode_cmd
, &gobj
);
184 DRM_ERROR("failed to create fbcon backing object %d\n", ret
);
188 bo
= gem_to_cirrus_bo(gobj
);
190 sysram
= vmalloc(size
);
194 info
= drm_fb_helper_alloc_fbi(helper
);
196 return PTR_ERR(info
);
200 ret
= cirrus_framebuffer_init(cdev
->dev
, &gfbdev
->gfb
, &mode_cmd
, gobj
);
204 gfbdev
->sysram
= sysram
;
207 fb
= &gfbdev
->gfb
.base
;
209 DRM_INFO("fb is NULL\n");
214 gfbdev
->helper
.fb
= fb
;
216 strcpy(info
->fix
.id
, "cirrusdrmfb");
218 info
->flags
= FBINFO_DEFAULT
;
219 info
->fbops
= &cirrusfb_ops
;
221 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->format
->depth
);
222 drm_fb_helper_fill_var(info
, &gfbdev
->helper
, sizes
->fb_width
,
225 /* setup aperture base/size for vesafb takeover */
226 info
->apertures
->ranges
[0].base
= cdev
->dev
->mode_config
.fb_base
;
227 info
->apertures
->ranges
[0].size
= cdev
->mc
.vram_size
;
229 info
->fix
.smem_start
= cdev
->dev
->mode_config
.fb_base
;
230 info
->fix
.smem_len
= cdev
->mc
.vram_size
;
232 info
->screen_base
= sysram
;
233 info
->screen_size
= size
;
235 info
->fix
.mmio_start
= 0;
236 info
->fix
.mmio_len
= 0;
238 DRM_INFO("fb mappable at 0x%lX\n", info
->fix
.smem_start
);
239 DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info
->fix
.smem_start
);
240 DRM_INFO("size %lu\n", (unsigned long)info
->fix
.smem_len
);
241 DRM_INFO("fb depth is %d\n", fb
->format
->depth
);
242 DRM_INFO(" pitch is %d\n", fb
->pitches
[0]);
247 static int cirrus_fbdev_destroy(struct drm_device
*dev
,
248 struct cirrus_fbdev
*gfbdev
)
250 struct cirrus_framebuffer
*gfb
= &gfbdev
->gfb
;
252 drm_fb_helper_unregister_fbi(&gfbdev
->helper
);
253 drm_fb_helper_release_fbi(&gfbdev
->helper
);
256 drm_gem_object_unreference_unlocked(gfb
->obj
);
260 vfree(gfbdev
->sysram
);
261 drm_fb_helper_fini(&gfbdev
->helper
);
262 drm_framebuffer_unregister_private(&gfb
->base
);
263 drm_framebuffer_cleanup(&gfb
->base
);
268 static const struct drm_fb_helper_funcs cirrus_fb_helper_funcs
= {
269 .gamma_set
= cirrus_crtc_fb_gamma_set
,
270 .gamma_get
= cirrus_crtc_fb_gamma_get
,
271 .fb_probe
= cirrusfb_create
,
274 int cirrus_fbdev_init(struct cirrus_device
*cdev
)
276 struct cirrus_fbdev
*gfbdev
;
281 gfbdev
= kzalloc(sizeof(struct cirrus_fbdev
), GFP_KERNEL
);
285 cdev
->mode_info
.gfbdev
= gfbdev
;
286 spin_lock_init(&gfbdev
->dirty_lock
);
288 drm_fb_helper_prepare(cdev
->dev
, &gfbdev
->helper
,
289 &cirrus_fb_helper_funcs
);
291 ret
= drm_fb_helper_init(cdev
->dev
, &gfbdev
->helper
,
292 cdev
->num_crtc
, CIRRUSFB_CONN_LIMIT
);
296 ret
= drm_fb_helper_single_add_all_connectors(&gfbdev
->helper
);
300 /* disable all the possible outputs/crtcs before entering KMS mode */
301 drm_helper_disable_unused_functions(cdev
->dev
);
303 return drm_fb_helper_initial_config(&gfbdev
->helper
, bpp_sel
);
306 void cirrus_fbdev_fini(struct cirrus_device
*cdev
)
308 if (!cdev
->mode_info
.gfbdev
)
311 cirrus_fbdev_destroy(cdev
->dev
, cdev
->mode_info
.gfbdev
);
312 kfree(cdev
->mode_info
.gfbdev
);
313 cdev
->mode_info
.gfbdev
= NULL
;