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 #ifdef CONFIG_DRM_KMS_FB_HELPER
49 static struct fb_deferred_io vbox_defio = {
51 .deferred_io = drm_fb_helper_deferred_io,
55 static struct fb_ops vboxfb_ops = {
57 .fb_check_var = drm_fb_helper_check_var,
58 .fb_set_par = drm_fb_helper_set_par,
59 .fb_fillrect = drm_fb_helper_sys_fillrect,
60 .fb_copyarea = drm_fb_helper_sys_copyarea,
61 .fb_imageblit = drm_fb_helper_sys_imageblit,
62 .fb_pan_display = drm_fb_helper_pan_display,
63 .fb_blank = drm_fb_helper_blank,
64 .fb_setcmap = drm_fb_helper_setcmap,
65 .fb_debug_enter = drm_fb_helper_debug_enter,
66 .fb_debug_leave = drm_fb_helper_debug_leave,
69 static int vboxfb_create_object(struct vbox_fbdev *fbdev,
70 struct DRM_MODE_FB_CMD *mode_cmd,
71 struct drm_gem_object **gobj_p)
73 struct drm_device *dev = fbdev->helper.dev;
75 struct drm_gem_object *gobj;
76 u32 pitch = mode_cmd->pitches[0];
79 size = pitch * mode_cmd->height;
80 ret = vbox_gem_create(dev, size, true, &gobj);
89 static int vboxfb_create(struct drm_fb_helper *helper,
90 struct drm_fb_helper_surface_size *sizes)
92 struct vbox_fbdev *fbdev =
93 container_of(helper, struct vbox_fbdev, helper);
94 struct drm_device *dev = fbdev->helper.dev;
95 struct DRM_MODE_FB_CMD mode_cmd;
96 struct drm_framebuffer *fb;
98 struct drm_gem_object *gobj;
103 mode_cmd.width = sizes->surface_width;
104 mode_cmd.height = sizes->surface_height;
105 pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
106 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
107 sizes->surface_depth);
108 mode_cmd.pitches[0] = pitch;
110 size = pitch * mode_cmd.height;
112 ret = vboxfb_create_object(fbdev, &mode_cmd, &gobj);
114 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
118 ret = vbox_framebuffer_init(dev, &fbdev->afb, &mode_cmd, gobj);
122 bo = gem_to_vbox_bo(gobj);
124 ret = vbox_bo_reserve(bo, false);
128 ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
130 vbox_bo_unreserve(bo);
134 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
135 vbox_bo_unreserve(bo);
137 DRM_ERROR("failed to kmap fbcon\n");
141 info = drm_fb_helper_alloc_fbi(helper);
143 return -PTR_ERR(info);
149 fb = &fbdev->afb.base;
150 fbdev->helper.fb = fb;
152 strcpy(info->fix.id, "vboxdrmfb");
155 * The last flag forces a mode set on VT switches even if the kernel
156 * does not think it is needed.
158 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT |
159 FBINFO_MISC_ALWAYS_SETPAR;
160 info->fbops = &vboxfb_ops;
163 * This seems to be done for safety checking that the framebuffer
164 * is not registered twice by different drivers.
166 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
167 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
169 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
170 drm_fb_helper_fill_var(info, &fbdev->helper, sizes->fb_width,
173 info->screen_base = (char __iomem *)bo->kmap.virtual;
174 info->screen_size = size;
176 #ifdef CONFIG_DRM_KMS_FB_HELPER
177 info->fbdefio = &vbox_defio;
178 fb_deferred_io_init(info);
181 info->pixmap.flags = FB_PIXMAP_SYSTEM;
183 DRM_DEBUG_KMS("allocated %dx%d\n", fb->width, fb->height);
188 static struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
189 .fb_probe = vboxfb_create,
192 void vbox_fbdev_fini(struct drm_device *dev)
194 struct vbox_private *vbox = dev->dev_private;
195 struct vbox_fbdev *fbdev = vbox->fbdev;
196 struct vbox_framebuffer *afb = &fbdev->afb;
198 #ifdef CONFIG_DRM_KMS_FB_HELPER
199 if (fbdev->helper.fbdev && fbdev->helper.fbdev->fbdefio)
200 fb_deferred_io_cleanup(fbdev->helper.fbdev);
203 drm_fb_helper_unregister_fbi(&fbdev->helper);
206 struct vbox_bo *bo = gem_to_vbox_bo(afb->obj);
208 if (!vbox_bo_reserve(bo, false)) {
209 if (bo->kmap.virtual)
210 ttm_bo_kunmap(&bo->kmap);
212 * QXL does this, but is it really needed before
217 vbox_bo_unreserve(bo);
219 drm_gem_object_put_unlocked(afb->obj);
222 drm_fb_helper_fini(&fbdev->helper);
224 drm_framebuffer_unregister_private(&afb->base);
225 drm_framebuffer_cleanup(&afb->base);
228 int vbox_fbdev_init(struct drm_device *dev)
230 struct vbox_private *vbox = dev->dev_private;
231 struct vbox_fbdev *fbdev;
234 fbdev = devm_kzalloc(dev->dev, sizeof(*fbdev), GFP_KERNEL);
239 spin_lock_init(&fbdev->dirty_lock);
241 drm_fb_helper_prepare(dev, &fbdev->helper, &vbox_fb_helper_funcs);
242 ret = drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs);
246 ret = drm_fb_helper_single_add_all_connectors(&fbdev->helper);
250 /* disable all the possible outputs/crtcs before entering KMS mode */
251 drm_helper_disable_unused_functions(dev);
253 ret = drm_fb_helper_initial_config(&fbdev->helper, 32);
260 drm_fb_helper_fini(&fbdev->helper);
264 void vbox_fbdev_set_base(struct vbox_private *vbox, unsigned long gpu_addr)
266 struct fb_info *fbdev = vbox->fbdev->helper.fbdev;
268 fbdev->fix.smem_start = fbdev->apertures->ranges[0].base + gpu_addr;
269 fbdev->fix.smem_len = vbox->available_vram_size - gpu_addr;