]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/vboxvideo/vbox_fb.c
Merge branch 'drm-next-4.20' of git://people.freedesktop.org/~agd5f/linux into drm-next
[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 static int vboxfb_create_object(struct vbox_fbdev *fbdev,
70                                 struct DRM_MODE_FB_CMD *mode_cmd,
71                                 struct drm_gem_object **gobj_p)
72 {
73         struct drm_device *dev = fbdev->helper.dev;
74         u32 size;
75         struct drm_gem_object *gobj;
76         u32 pitch = mode_cmd->pitches[0];
77         int ret;
78
79         size = pitch * mode_cmd->height;
80         ret = vbox_gem_create(dev, size, true, &gobj);
81         if (ret)
82                 return ret;
83
84         *gobj_p = gobj;
85
86         return 0;
87 }
88
89 static int vboxfb_create(struct drm_fb_helper *helper,
90                          struct drm_fb_helper_surface_size *sizes)
91 {
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;
97         struct fb_info *info;
98         struct drm_gem_object *gobj;
99         struct vbox_bo *bo;
100         int size, ret;
101         u32 pitch;
102
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;
109
110         size = pitch * mode_cmd.height;
111
112         ret = vboxfb_create_object(fbdev, &mode_cmd, &gobj);
113         if (ret) {
114                 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
115                 return ret;
116         }
117
118         ret = vbox_framebuffer_init(dev, &fbdev->afb, &mode_cmd, gobj);
119         if (ret)
120                 return ret;
121
122         bo = gem_to_vbox_bo(gobj);
123
124         ret = vbox_bo_reserve(bo, false);
125         if (ret)
126                 return ret;
127
128         ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
129         if (ret) {
130                 vbox_bo_unreserve(bo);
131                 return ret;
132         }
133
134         ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
135         vbox_bo_unreserve(bo);
136         if (ret) {
137                 DRM_ERROR("failed to kmap fbcon\n");
138                 return ret;
139         }
140
141         info = drm_fb_helper_alloc_fbi(helper);
142         if (IS_ERR(info))
143                 return -PTR_ERR(info);
144
145         info->par = fbdev;
146
147         fbdev->size = size;
148
149         fb = &fbdev->afb.base;
150         fbdev->helper.fb = fb;
151
152         strcpy(info->fix.id, "vboxdrmfb");
153
154         /*
155          * The last flag forces a mode set on VT switches even if the kernel
156          * does not think it is needed.
157          */
158         info->flags = FBINFO_DEFAULT | FBINFO_MISC_ALWAYS_SETPAR;
159         info->fbops = &vboxfb_ops;
160
161         /*
162          * This seems to be done for safety checking that the framebuffer
163          * is not registered twice by different drivers.
164          */
165         info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
166         info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
167
168         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
169         drm_fb_helper_fill_var(info, &fbdev->helper, sizes->fb_width,
170                                sizes->fb_height);
171
172         info->screen_base = (char __iomem *)bo->kmap.virtual;
173         info->screen_size = size;
174
175 #ifdef CONFIG_DRM_KMS_FB_HELPER
176         info->fbdefio = &vbox_defio;
177         fb_deferred_io_init(info);
178 #endif
179
180         info->pixmap.flags = FB_PIXMAP_SYSTEM;
181
182         DRM_DEBUG_KMS("allocated %dx%d\n", fb->width, fb->height);
183
184         return 0;
185 }
186
187 static struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
188         .fb_probe = vboxfb_create,
189 };
190
191 void vbox_fbdev_fini(struct drm_device *dev)
192 {
193         struct vbox_private *vbox = dev->dev_private;
194         struct vbox_fbdev *fbdev = vbox->fbdev;
195         struct vbox_framebuffer *afb = &fbdev->afb;
196
197 #ifdef CONFIG_DRM_KMS_FB_HELPER
198         if (fbdev->helper.fbdev && fbdev->helper.fbdev->fbdefio)
199                 fb_deferred_io_cleanup(fbdev->helper.fbdev);
200 #endif
201
202         drm_fb_helper_unregister_fbi(&fbdev->helper);
203
204         if (afb->obj) {
205                 struct vbox_bo *bo = gem_to_vbox_bo(afb->obj);
206
207                 if (!vbox_bo_reserve(bo, false)) {
208                         if (bo->kmap.virtual)
209                                 ttm_bo_kunmap(&bo->kmap);
210                         /*
211                          * QXL does this, but is it really needed before
212                          * freeing?
213                          */
214                         if (bo->pin_count)
215                                 vbox_bo_unpin(bo);
216                         vbox_bo_unreserve(bo);
217                 }
218                 drm_gem_object_put_unlocked(afb->obj);
219                 afb->obj = NULL;
220         }
221         drm_fb_helper_fini(&fbdev->helper);
222
223         drm_framebuffer_unregister_private(&afb->base);
224         drm_framebuffer_cleanup(&afb->base);
225 }
226
227 int vbox_fbdev_init(struct drm_device *dev)
228 {
229         struct vbox_private *vbox = dev->dev_private;
230         struct vbox_fbdev *fbdev;
231         int ret;
232
233         fbdev = devm_kzalloc(dev->dev, sizeof(*fbdev), GFP_KERNEL);
234         if (!fbdev)
235                 return -ENOMEM;
236
237         vbox->fbdev = fbdev;
238         spin_lock_init(&fbdev->dirty_lock);
239
240         drm_fb_helper_prepare(dev, &fbdev->helper, &vbox_fb_helper_funcs);
241         ret = drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs);
242         if (ret)
243                 return ret;
244
245         ret = drm_fb_helper_single_add_all_connectors(&fbdev->helper);
246         if (ret)
247                 goto err_fini;
248
249         /* disable all the possible outputs/crtcs before entering KMS mode */
250         drm_helper_disable_unused_functions(dev);
251
252         ret = drm_fb_helper_initial_config(&fbdev->helper, 32);
253         if (ret)
254                 goto err_fini;
255
256         return 0;
257
258 err_fini:
259         drm_fb_helper_fini(&fbdev->helper);
260         return ret;
261 }
262
263 void vbox_fbdev_set_base(struct vbox_private *vbox, unsigned long gpu_addr)
264 {
265         struct fb_info *fbdev = vbox->fbdev->helper.fbdev;
266
267         fbdev->fix.smem_start = fbdev->apertures->ranges[0].base + gpu_addr;
268         fbdev->fix.smem_len = vbox->available_vram_size - gpu_addr;
269 }