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 int vboxfb_create(struct drm_fb_helper *helper,
70 struct drm_fb_helper_surface_size *sizes)
72 struct vbox_private *vbox =
73 container_of(helper, struct vbox_private, fb_helper);
74 struct pci_dev *pdev = vbox->ddev.pdev;
75 struct DRM_MODE_FB_CMD mode_cmd;
76 struct drm_framebuffer *fb;
78 struct drm_gem_object *gobj;
84 mode_cmd.width = sizes->surface_width;
85 mode_cmd.height = sizes->surface_height;
86 pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
87 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
88 sizes->surface_depth);
89 mode_cmd.pitches[0] = pitch;
91 size = pitch * mode_cmd.height;
93 ret = vbox_gem_create(vbox, size, true, &gobj);
95 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
99 ret = vbox_framebuffer_init(vbox, &vbox->afb, &mode_cmd, gobj);
103 bo = gem_to_vbox_bo(gobj);
105 ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM);
109 info = drm_fb_helper_alloc_fbi(helper);
111 return PTR_ERR(info);
113 info->screen_size = size;
114 info->screen_base = (char __iomem *)vbox_bo_kmap(bo);
115 if (IS_ERR(info->screen_base))
116 return PTR_ERR(info->screen_base);
120 fb = &vbox->afb.base;
123 strcpy(info->fix.id, "vboxdrmfb");
126 * The last flag forces a mode set on VT switches even if the kernel
127 * does not think it is needed.
129 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT |
130 FBINFO_MISC_ALWAYS_SETPAR;
131 info->fbops = &vboxfb_ops;
134 * This seems to be done for safety checking that the framebuffer
135 * is not registered twice by different drivers.
137 info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
138 info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
140 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
141 drm_fb_helper_fill_var(info, helper, sizes->fb_width,
144 gpu_addr = vbox_bo_gpu_offset(bo);
145 info->fix.smem_start = info->apertures->ranges[0].base + gpu_addr;
146 info->fix.smem_len = vbox->available_vram_size - gpu_addr;
148 #ifdef CONFIG_DRM_KMS_FB_HELPER
149 info->fbdefio = &vbox_defio;
150 fb_deferred_io_init(info);
153 info->pixmap.flags = FB_PIXMAP_SYSTEM;
155 DRM_DEBUG_KMS("allocated %dx%d\n", fb->width, fb->height);
160 void vbox_fbdev_fini(struct vbox_private *vbox)
162 struct vbox_framebuffer *afb = &vbox->afb;
164 #ifdef CONFIG_DRM_KMS_FB_HELPER
165 if (vbox->fb_helper.fbdev && vbox->fb_helper.fbdev->fbdefio)
166 fb_deferred_io_cleanup(vbox->fb_helper.fbdev);
169 drm_fb_helper_unregister_fbi(&vbox->fb_helper);
172 struct vbox_bo *bo = gem_to_vbox_bo(afb->obj);
179 drm_gem_object_put_unlocked(afb->obj);
182 drm_fb_helper_fini(&vbox->fb_helper);
184 drm_framebuffer_unregister_private(&afb->base);
185 drm_framebuffer_cleanup(&afb->base);