]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/vboxvideo/vbox_fb.c
staging: vboxvideo: Use more drm_fb_helper functions
[linux.git] / drivers / staging / vboxvideo / vbox_fb.c
1 /*
2  * Copyright (C) 2013-2017 Oracle Corporation
3  * This file is based on ast_fb.c
4  * Copyright 2012 Red Hat Inc.
5  *
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:
13  *
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.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * Authors: Dave Airlie <airlied@redhat.com>
27  *          Michael Thayer <michael.thayer@oracle.com,
28  */
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/mm.h>
34 #include <linux/tty.h>
35 #include <linux/sysrq.h>
36 #include <linux/delay.h>
37 #include <linux/fb.h>
38 #include <linux/init.h>
39
40 #include <drm/drmP.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_fb_helper.h>
43 #include <drm/drm_crtc_helper.h>
44
45 #include "vbox_drv.h"
46 #include "vboxvideo.h"
47
48 #ifdef CONFIG_DRM_KMS_FB_HELPER
49 static struct fb_deferred_io vbox_defio = {
50         .delay = HZ / 30,
51         .deferred_io = drm_fb_helper_deferred_io,
52 };
53 #endif
54
55 static struct fb_ops vboxfb_ops = {
56         .owner = THIS_MODULE,
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,
67 };
68
69 int vboxfb_create(struct drm_fb_helper *helper,
70                   struct drm_fb_helper_surface_size *sizes)
71 {
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;
77         struct fb_info *info;
78         struct drm_gem_object *gobj;
79         struct vbox_bo *bo;
80         int size, ret;
81         u64 gpu_addr;
82         u32 pitch;
83
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;
90
91         size = pitch * mode_cmd.height;
92
93         ret = vbox_gem_create(vbox, size, true, &gobj);
94         if (ret) {
95                 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
96                 return ret;
97         }
98
99         ret = vbox_framebuffer_init(vbox, &vbox->afb, &mode_cmd, gobj);
100         if (ret)
101                 return ret;
102
103         bo = gem_to_vbox_bo(gobj);
104
105         ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM);
106         if (ret)
107                 return ret;
108
109         info = drm_fb_helper_alloc_fbi(helper);
110         if (IS_ERR(info))
111                 return PTR_ERR(info);
112
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);
117
118         info->par = helper;
119
120         fb = &vbox->afb.base;
121         helper->fb = fb;
122
123         strcpy(info->fix.id, "vboxdrmfb");
124
125         /*
126          * The last flag forces a mode set on VT switches even if the kernel
127          * does not think it is needed.
128          */
129         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT |
130                       FBINFO_MISC_ALWAYS_SETPAR;
131         info->fbops = &vboxfb_ops;
132
133         /*
134          * This seems to be done for safety checking that the framebuffer
135          * is not registered twice by different drivers.
136          */
137         info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
138         info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
139
140         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
141         drm_fb_helper_fill_var(info, helper, sizes->fb_width,
142                                sizes->fb_height);
143
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;
147
148 #ifdef CONFIG_DRM_KMS_FB_HELPER
149         info->fbdefio = &vbox_defio;
150         fb_deferred_io_init(info);
151 #endif
152
153         info->pixmap.flags = FB_PIXMAP_SYSTEM;
154
155         DRM_DEBUG_KMS("allocated %dx%d\n", fb->width, fb->height);
156
157         return 0;
158 }
159
160 void vbox_fbdev_fini(struct vbox_private *vbox)
161 {
162         struct vbox_framebuffer *afb = &vbox->afb;
163
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);
167 #endif
168
169         drm_fb_helper_unregister_fbi(&vbox->fb_helper);
170
171         if (afb->obj) {
172                 struct vbox_bo *bo = gem_to_vbox_bo(afb->obj);
173
174                 vbox_bo_kunmap(bo);
175
176                 if (bo->pin_count)
177                         vbox_bo_unpin(bo);
178
179                 drm_gem_object_put_unlocked(afb->obj);
180                 afb->obj = NULL;
181         }
182         drm_fb_helper_fini(&vbox->fb_helper);
183
184         drm_framebuffer_unregister_private(&afb->base);
185         drm_framebuffer_cleanup(&afb->base);
186 }