2 * Copyright (C) 2013-2017 Oracle Corporation
3 * This file is based on ast_fb.c
4 * Copyright 2012 Red Hat Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * Authors: Dave Airlie <airlied@redhat.com>
27 * Michael Thayer <michael.thayer@oracle.com,
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
34 #include <linux/tty.h>
35 #include <linux/sysrq.h>
36 #include <linux/delay.h>
38 #include <linux/init.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_fb_helper.h>
43 #include <drm/drm_crtc_helper.h>
46 #include "vboxvideo.h"
48 #define VBOX_DIRTY_DELAY (HZ / 30)
50 * Tell the host about dirty rectangles to update.
52 static void vbox_dirty_update(struct vbox_fbdev
*fbdev
,
53 int x
, int y
, int width
, int height
)
55 struct drm_gem_object
*obj
;
58 bool store_for_later
= false;
61 struct drm_clip_rect rect
;
64 bo
= gem_to_vbox_bo(obj
);
67 * try and reserve the BO, if we fail with busy
68 * then the BO is being moved and we should
69 * store up the damage until later.
72 ret
= vbox_bo_reserve(bo
, true);
77 store_for_later
= true;
82 spin_lock_irqsave(&fbdev
->dirty_lock
, flags
);
93 if (store_for_later
) {
98 spin_unlock_irqrestore(&fbdev
->dirty_lock
, flags
);
107 spin_unlock_irqrestore(&fbdev
->dirty_lock
, flags
);
110 * Not sure why the original code subtracted 1 here, but I will keep
111 * it that way to avoid unnecessary differences.
117 vbox_framebuffer_dirty_rectangles(&fbdev
->afb
.base
, &rect
, 1);
119 vbox_bo_unreserve(bo
);
122 #ifdef CONFIG_FB_DEFERRED_IO
123 static void vbox_deferred_io(struct fb_info
*info
, struct list_head
*pagelist
)
125 struct vbox_fbdev
*fbdev
= info
->par
;
126 unsigned long start
, end
, min
, max
;
132 list_for_each_entry(page
, pagelist
, lru
) {
133 start
= page
->index
<< PAGE_SHIFT
;
134 end
= start
+ PAGE_SIZE
- 1;
135 min
= min(min
, start
);
140 y1
= min
/ info
->fix
.line_length
;
141 y2
= (max
/ info
->fix
.line_length
) + 1;
142 DRM_INFO("%s: Calling dirty update: 0, %d, %d, %d\n",
143 __func__
, y1
, info
->var
.xres
, y2
- y1
- 1);
144 vbox_dirty_update(fbdev
, 0, y1
, info
->var
.xres
, y2
- y1
- 1);
148 static struct fb_deferred_io vbox_defio
= {
149 .delay
= VBOX_DIRTY_DELAY
,
150 .deferred_io
= vbox_deferred_io
,
154 static void vbox_fillrect(struct fb_info
*info
, const struct fb_fillrect
*rect
)
156 struct vbox_fbdev
*fbdev
= info
->par
;
158 sys_fillrect(info
, rect
);
159 vbox_dirty_update(fbdev
, rect
->dx
, rect
->dy
, rect
->width
, rect
->height
);
162 static void vbox_copyarea(struct fb_info
*info
, const struct fb_copyarea
*area
)
164 struct vbox_fbdev
*fbdev
= info
->par
;
166 sys_copyarea(info
, area
);
167 vbox_dirty_update(fbdev
, area
->dx
, area
->dy
, area
->width
, area
->height
);
170 static void vbox_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
172 struct vbox_fbdev
*fbdev
= info
->par
;
174 sys_imageblit(info
, image
);
175 vbox_dirty_update(fbdev
, image
->dx
, image
->dy
, image
->width
,
179 static struct fb_ops vboxfb_ops
= {
180 .owner
= THIS_MODULE
,
181 .fb_check_var
= drm_fb_helper_check_var
,
182 .fb_set_par
= drm_fb_helper_set_par
,
183 .fb_fillrect
= vbox_fillrect
,
184 .fb_copyarea
= vbox_copyarea
,
185 .fb_imageblit
= vbox_imageblit
,
186 .fb_pan_display
= drm_fb_helper_pan_display
,
187 .fb_blank
= drm_fb_helper_blank
,
188 .fb_setcmap
= drm_fb_helper_setcmap
,
189 .fb_debug_enter
= drm_fb_helper_debug_enter
,
190 .fb_debug_leave
= drm_fb_helper_debug_leave
,
193 static int vboxfb_create_object(struct vbox_fbdev
*fbdev
,
194 struct DRM_MODE_FB_CMD
*mode_cmd
,
195 struct drm_gem_object
**gobj_p
)
197 struct drm_device
*dev
= fbdev
->helper
.dev
;
199 struct drm_gem_object
*gobj
;
200 u32 pitch
= mode_cmd
->pitches
[0];
203 size
= pitch
* mode_cmd
->height
;
204 ret
= vbox_gem_create(dev
, size
, true, &gobj
);
213 static int vboxfb_create(struct drm_fb_helper
*helper
,
214 struct drm_fb_helper_surface_size
*sizes
)
216 struct vbox_fbdev
*fbdev
=
217 container_of(helper
, struct vbox_fbdev
, helper
);
218 struct drm_device
*dev
= fbdev
->helper
.dev
;
219 struct DRM_MODE_FB_CMD mode_cmd
;
220 struct drm_framebuffer
*fb
;
221 struct fb_info
*info
;
222 struct device
*device
= &dev
->pdev
->dev
;
223 struct drm_gem_object
*gobj
;
228 mode_cmd
.width
= sizes
->surface_width
;
229 mode_cmd
.height
= sizes
->surface_height
;
230 pitch
= mode_cmd
.width
* ((sizes
->surface_bpp
+ 7) / 8);
231 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
232 sizes
->surface_depth
);
233 mode_cmd
.pitches
[0] = pitch
;
235 size
= pitch
* mode_cmd
.height
;
237 ret
= vboxfb_create_object(fbdev
, &mode_cmd
, &gobj
);
239 DRM_ERROR("failed to create fbcon backing object %d\n", ret
);
243 ret
= vbox_framebuffer_init(dev
, &fbdev
->afb
, &mode_cmd
, gobj
);
247 bo
= gem_to_vbox_bo(gobj
);
249 ret
= vbox_bo_reserve(bo
, false);
253 ret
= vbox_bo_pin(bo
, TTM_PL_FLAG_VRAM
, NULL
);
255 vbox_bo_unreserve(bo
);
259 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
260 vbox_bo_unreserve(bo
);
262 DRM_ERROR("failed to kmap fbcon\n");
266 info
= framebuffer_alloc(0, device
);
273 fb
= &fbdev
->afb
.base
;
274 fbdev
->helper
.fb
= fb
;
275 fbdev
->helper
.fbdev
= info
;
277 strcpy(info
->fix
.id
, "vboxdrmfb");
280 * The last flag forces a mode set on VT switches even if the kernel
281 * does not think it is needed.
283 info
->flags
= FBINFO_DEFAULT
| FBINFO_CAN_FORCE_OUTPUT
|
284 FBINFO_MISC_ALWAYS_SETPAR
;
285 info
->fbops
= &vboxfb_ops
;
287 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
292 * This seems to be done for safety checking that the framebuffer
293 * is not registered twice by different drivers.
295 info
->apertures
= alloc_apertures(1);
296 if (!info
->apertures
)
298 info
->apertures
->ranges
[0].base
= pci_resource_start(dev
->pdev
, 0);
299 info
->apertures
->ranges
[0].size
= pci_resource_len(dev
->pdev
, 0);
301 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->format
->depth
);
302 drm_fb_helper_fill_var(info
, &fbdev
->helper
, sizes
->fb_width
,
305 info
->screen_base
= bo
->kmap
.virtual;
306 info
->screen_size
= size
;
308 #ifdef CONFIG_FB_DEFERRED_IO
309 info
->fbdefio
= &vbox_defio
;
310 fb_deferred_io_init(info
);
313 info
->pixmap
.flags
= FB_PIXMAP_SYSTEM
;
315 DRM_DEBUG_KMS("allocated %dx%d\n", fb
->width
, fb
->height
);
320 static void vbox_fb_gamma_set(struct drm_crtc
*crtc
, u16 red
, u16 green
,
325 static void vbox_fb_gamma_get(struct drm_crtc
*crtc
, u16
*red
, u16
*green
,
326 u16
*blue
, int regno
)
333 static struct drm_fb_helper_funcs vbox_fb_helper_funcs
= {
334 .gamma_set
= vbox_fb_gamma_set
,
335 .gamma_get
= vbox_fb_gamma_get
,
336 .fb_probe
= vboxfb_create
,
339 void vbox_fbdev_fini(struct drm_device
*dev
)
341 struct vbox_private
*vbox
= dev
->dev_private
;
342 struct vbox_fbdev
*fbdev
= vbox
->fbdev
;
343 struct vbox_framebuffer
*afb
= &fbdev
->afb
;
345 drm_fb_helper_unregister_fbi(&fbdev
->helper
);
348 struct vbox_bo
*bo
= gem_to_vbox_bo(afb
->obj
);
350 if (!vbox_bo_reserve(bo
, false)) {
351 if (bo
->kmap
.virtual)
352 ttm_bo_kunmap(&bo
->kmap
);
354 * QXL does this, but is it really needed before
359 vbox_bo_unreserve(bo
);
361 drm_gem_object_unreference_unlocked(afb
->obj
);
364 drm_fb_helper_fini(&fbdev
->helper
);
366 drm_framebuffer_unregister_private(&afb
->base
);
367 drm_framebuffer_cleanup(&afb
->base
);
370 int vbox_fbdev_init(struct drm_device
*dev
)
372 struct vbox_private
*vbox
= dev
->dev_private
;
373 struct vbox_fbdev
*fbdev
;
376 fbdev
= devm_kzalloc(dev
->dev
, sizeof(*fbdev
), GFP_KERNEL
);
381 spin_lock_init(&fbdev
->dirty_lock
);
383 drm_fb_helper_prepare(dev
, &fbdev
->helper
, &vbox_fb_helper_funcs
);
384 ret
= drm_fb_helper_init(dev
, &fbdev
->helper
, vbox
->num_crtcs
);
388 ret
= drm_fb_helper_single_add_all_connectors(&fbdev
->helper
);
392 /* disable all the possible outputs/crtcs before entering KMS mode */
393 drm_helper_disable_unused_functions(dev
);
395 ret
= drm_fb_helper_initial_config(&fbdev
->helper
, 32);
402 drm_fb_helper_fini(&fbdev
->helper
);
406 void vbox_fbdev_set_base(struct vbox_private
*vbox
, unsigned long gpu_addr
)
408 struct fb_info
*fbdev
= vbox
->fbdev
->helper
.fbdev
;
410 fbdev
->fix
.smem_start
= fbdev
->apertures
->ranges
[0].base
+ gpu_addr
;
411 fbdev
->fix
.smem_len
= vbox
->available_vram_size
- gpu_addr
;