1 | /* $Id: vbox_fb.c 60352 2016-04-06 11:07:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Additions Linux kernel video driver
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | * --------------------------------------------------------------------
|
---|
17 | *
|
---|
18 | * This code is based on
|
---|
19 | * ast_fb.c
|
---|
20 | * with the following copyright and permission notice:
|
---|
21 | *
|
---|
22 | * Copyright 2012 Red Hat Inc.
|
---|
23 | *
|
---|
24 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
25 | * copy of this software and associated documentation files (the
|
---|
26 | * "Software"), to deal in the Software without restriction, including
|
---|
27 | * without limitation the rights to use, copy, modify, merge, publish,
|
---|
28 | * distribute, sub license, and/or sell copies of the Software, and to
|
---|
29 | * permit persons to whom the Software is furnished to do so, subject to
|
---|
30 | * the following conditions:
|
---|
31 | *
|
---|
32 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
33 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
34 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
---|
35 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
---|
36 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
---|
37 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
---|
38 | * USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
39 | *
|
---|
40 | * The above copyright notice and this permission notice (including the
|
---|
41 | * next paragraph) shall be included in all copies or substantial portions
|
---|
42 | * of the Software.
|
---|
43 | *
|
---|
44 | */
|
---|
45 | /*
|
---|
46 | * Authors: Dave Airlie <airlied@redhat.com>
|
---|
47 | */
|
---|
48 | /* Include from most specific to most general to be able to override things. */
|
---|
49 | #include "vbox_drv.h"
|
---|
50 | #include <VBox/VBoxVideo.h>
|
---|
51 | #include <VBox/VMMDev.h>
|
---|
52 |
|
---|
53 | #include <linux/module.h>
|
---|
54 | #include <linux/kernel.h>
|
---|
55 | #include <linux/errno.h>
|
---|
56 | #include <linux/string.h>
|
---|
57 | #include <linux/mm.h>
|
---|
58 | #include <linux/tty.h>
|
---|
59 | #include <linux/sysrq.h>
|
---|
60 | #include <linux/delay.h>
|
---|
61 | #include <linux/fb.h>
|
---|
62 | #include <linux/init.h>
|
---|
63 |
|
---|
64 |
|
---|
65 | #include <drm/drmP.h>
|
---|
66 | #include <drm/drm_crtc.h>
|
---|
67 | #include <drm/drm_fb_helper.h>
|
---|
68 | #include <drm/drm_crtc_helper.h>
|
---|
69 | #include "vbox_drv.h"
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Tell the host about dirty rectangles to update.
|
---|
73 | */
|
---|
74 | static void vbox_dirty_update(struct vbox_fbdev *fbdev,
|
---|
75 | int x, int y, int width, int height)
|
---|
76 | {
|
---|
77 | struct drm_device *dev = fbdev->helper.dev;
|
---|
78 | int i;
|
---|
79 |
|
---|
80 | struct drm_gem_object *obj;
|
---|
81 | struct vbox_bo *bo;
|
---|
82 | int src_offset, dst_offset;
|
---|
83 | int bpp = (fbdev->afb.base.bits_per_pixel + 7)/8;
|
---|
84 | int ret = -EBUSY;
|
---|
85 | bool unmap = false;
|
---|
86 | bool store_for_later = false;
|
---|
87 | int x2, y2;
|
---|
88 | unsigned long flags;
|
---|
89 | struct drm_clip_rect rect;
|
---|
90 |
|
---|
91 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
92 | obj = fbdev->afb.obj;
|
---|
93 | bo = gem_to_vbox_bo(obj);
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * try and reserve the BO, if we fail with busy
|
---|
97 | * then the BO is being moved and we should
|
---|
98 | * store up the damage until later.
|
---|
99 | */
|
---|
100 | if (drm_can_sleep())
|
---|
101 | ret = vbox_bo_reserve(bo, true);
|
---|
102 | if (ret) {
|
---|
103 | if (ret != -EBUSY)
|
---|
104 | return;
|
---|
105 |
|
---|
106 | store_for_later = true;
|
---|
107 | }
|
---|
108 |
|
---|
109 | x2 = x + width - 1;
|
---|
110 | y2 = y + height - 1;
|
---|
111 | spin_lock_irqsave(&fbdev->dirty_lock, flags);
|
---|
112 |
|
---|
113 | if (fbdev->y1 < y)
|
---|
114 | y = fbdev->y1;
|
---|
115 | if (fbdev->y2 > y2)
|
---|
116 | y2 = fbdev->y2;
|
---|
117 | if (fbdev->x1 < x)
|
---|
118 | x = fbdev->x1;
|
---|
119 | if (fbdev->x2 > x2)
|
---|
120 | x2 = fbdev->x2;
|
---|
121 |
|
---|
122 | if (store_for_later) {
|
---|
123 | fbdev->x1 = x;
|
---|
124 | fbdev->x2 = x2;
|
---|
125 | fbdev->y1 = y;
|
---|
126 | fbdev->y2 = y2;
|
---|
127 | spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
|
---|
128 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | fbdev->x1 = fbdev->y1 = INT_MAX;
|
---|
133 | fbdev->x2 = fbdev->y2 = 0;
|
---|
134 | spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
|
---|
135 |
|
---|
136 | if (!bo->kmap.virtual) {
|
---|
137 | ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
|
---|
138 | if (ret) {
|
---|
139 | DRM_ERROR("failed to kmap fb updates\n");
|
---|
140 | vbox_bo_unreserve(bo);
|
---|
141 | return;
|
---|
142 | }
|
---|
143 | unmap = true;
|
---|
144 | }
|
---|
145 | for (i = y; i <= y2; i++) {
|
---|
146 | /* assume equal stride for now */
|
---|
147 | src_offset = dst_offset = i * fbdev->afb.base.pitches[0] + (x * bpp);
|
---|
148 | memcpy_toio(bo->kmap.virtual + src_offset, (char *)fbdev->sysram + src_offset, (x2 - x + 1) * bpp);
|
---|
149 | }
|
---|
150 | /* Not sure why the original code subtracted 1 here, but I will keep it that
|
---|
151 | * way to avoid unnecessary differences. */
|
---|
152 | rect.x1 = x;
|
---|
153 | rect.x2 = x2 + 1;
|
---|
154 | rect.y1 = y;
|
---|
155 | rect.y2 = y2 + 1;
|
---|
156 | vbox_framebuffer_dirty_rectangles(&fbdev->afb.base, &rect, 1);
|
---|
157 | LogFunc(("vboxvideo: %d, bo->kmap.virtual=%p, fbdev->sysram=%p, x=%d, y=%d, x2=%d, y2=%d, unmap=%RTbool\n",
|
---|
158 | __LINE__, bo->kmap.virtual, fbdev->sysram, (int)x, (int)y, (int)x2, (int)y2, unmap));
|
---|
159 | if (unmap)
|
---|
160 | ttm_bo_kunmap(&bo->kmap);
|
---|
161 |
|
---|
162 | vbox_bo_unreserve(bo);
|
---|
163 | }
|
---|
164 |
|
---|
165 | static void vbox_fillrect(struct fb_info *info,
|
---|
166 | const struct fb_fillrect *rect)
|
---|
167 | {
|
---|
168 | struct vbox_fbdev *fbdev = info->par;
|
---|
169 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
170 | sys_fillrect(info, rect);
|
---|
171 | vbox_dirty_update(fbdev, rect->dx, rect->dy, rect->width,
|
---|
172 | rect->height);
|
---|
173 | }
|
---|
174 |
|
---|
175 | static void vbox_copyarea(struct fb_info *info,
|
---|
176 | const struct fb_copyarea *area)
|
---|
177 | {
|
---|
178 | struct vbox_fbdev *fbdev = info->par;
|
---|
179 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
180 | sys_copyarea(info, area);
|
---|
181 | vbox_dirty_update(fbdev, area->dx, area->dy, area->width,
|
---|
182 | area->height);
|
---|
183 | }
|
---|
184 |
|
---|
185 | static void vbox_imageblit(struct fb_info *info,
|
---|
186 | const struct fb_image *image)
|
---|
187 | {
|
---|
188 | struct vbox_fbdev *fbdev = info->par;
|
---|
189 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
190 | sys_imageblit(info, image);
|
---|
191 | vbox_dirty_update(fbdev, image->dx, image->dy, image->width,
|
---|
192 | image->height);
|
---|
193 | }
|
---|
194 |
|
---|
195 | static struct fb_ops vboxfb_ops = {
|
---|
196 | .owner = THIS_MODULE,
|
---|
197 | .fb_check_var = drm_fb_helper_check_var,
|
---|
198 | .fb_set_par = drm_fb_helper_set_par,
|
---|
199 | .fb_fillrect = vbox_fillrect,
|
---|
200 | .fb_copyarea = vbox_copyarea,
|
---|
201 | .fb_imageblit = vbox_imageblit,
|
---|
202 | .fb_pan_display = drm_fb_helper_pan_display,
|
---|
203 | .fb_blank = drm_fb_helper_blank,
|
---|
204 | .fb_setcmap = drm_fb_helper_setcmap,
|
---|
205 | .fb_debug_enter = drm_fb_helper_debug_enter,
|
---|
206 | .fb_debug_leave = drm_fb_helper_debug_leave,
|
---|
207 | };
|
---|
208 |
|
---|
209 | static int vboxfb_create_object(struct vbox_fbdev *fbdev,
|
---|
210 | struct DRM_MODE_FB_CMD *mode_cmd,
|
---|
211 | struct drm_gem_object **gobj_p)
|
---|
212 | {
|
---|
213 | struct drm_device *dev = fbdev->helper.dev;
|
---|
214 | u32 bpp, depth;
|
---|
215 | u32 size;
|
---|
216 | struct drm_gem_object *gobj;
|
---|
217 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
|
---|
218 | __u32 pitch = mode_cmd->pitch;
|
---|
219 | #else
|
---|
220 | __u32 pitch = mode_cmd->pitches[0];
|
---|
221 | #endif
|
---|
222 |
|
---|
223 | int ret = 0;
|
---|
224 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
225 | drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
|
---|
226 |
|
---|
227 | size = pitch * mode_cmd->height;
|
---|
228 | ret = vbox_gem_create(dev, size, true, &gobj);
|
---|
229 | if (ret)
|
---|
230 | return ret;
|
---|
231 |
|
---|
232 | *gobj_p = gobj;
|
---|
233 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
234 | return ret;
|
---|
235 | }
|
---|
236 |
|
---|
237 | static int vboxfb_create(struct drm_fb_helper *helper,
|
---|
238 | struct drm_fb_helper_surface_size *sizes)
|
---|
239 | {
|
---|
240 | struct vbox_fbdev *fbdev =
|
---|
241 | container_of(helper, struct vbox_fbdev, helper);
|
---|
242 | struct drm_device *dev = fbdev->helper.dev;
|
---|
243 | struct DRM_MODE_FB_CMD mode_cmd;
|
---|
244 | struct drm_framebuffer *fb;
|
---|
245 | struct fb_info *info;
|
---|
246 | __u32 pitch;
|
---|
247 | int size, ret;
|
---|
248 | struct device *device = &dev->pdev->dev;
|
---|
249 | void *sysram;
|
---|
250 | struct drm_gem_object *gobj = NULL;
|
---|
251 | struct vbox_bo *bo = NULL;
|
---|
252 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
253 | mode_cmd.width = sizes->surface_width;
|
---|
254 | mode_cmd.height = sizes->surface_height;
|
---|
255 | pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
|
---|
256 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
|
---|
257 | mode_cmd.bpp = sizes->surface_bpp;
|
---|
258 | mode_cmd.depth = sizes->surface_depth;
|
---|
259 | mode_cmd.pitch = pitch;
|
---|
260 | #else
|
---|
261 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
|
---|
262 | sizes->surface_depth);
|
---|
263 | mode_cmd.pitches[0] = pitch;
|
---|
264 | #endif
|
---|
265 |
|
---|
266 | size = pitch * mode_cmd.height;
|
---|
267 |
|
---|
268 | ret = vboxfb_create_object(fbdev, &mode_cmd, &gobj);
|
---|
269 | if (ret) {
|
---|
270 | DRM_ERROR("failed to create fbcon backing object %d\n", ret);
|
---|
271 | return ret;
|
---|
272 | }
|
---|
273 | bo = gem_to_vbox_bo(gobj);
|
---|
274 |
|
---|
275 | sysram = vmalloc(size);
|
---|
276 | if (!sysram)
|
---|
277 | return -ENOMEM;
|
---|
278 |
|
---|
279 | info = framebuffer_alloc(0, device);
|
---|
280 | if (!info) {
|
---|
281 | ret = -ENOMEM;
|
---|
282 | goto out;
|
---|
283 | }
|
---|
284 | info->par = fbdev;
|
---|
285 |
|
---|
286 | ret = vbox_framebuffer_init(dev, &fbdev->afb, &mode_cmd, gobj);
|
---|
287 | if (ret)
|
---|
288 | goto out;
|
---|
289 |
|
---|
290 | fbdev->sysram = sysram;
|
---|
291 | fbdev->size = size;
|
---|
292 |
|
---|
293 | fb = &fbdev->afb.base;
|
---|
294 | fbdev->helper.fb = fb;
|
---|
295 | fbdev->helper.fbdev = info;
|
---|
296 |
|
---|
297 | strcpy(info->fix.id, "vboxdrmfb");
|
---|
298 |
|
---|
299 | /* The last flag forces a mode set on VT switches even if the kernel does
|
---|
300 | * not think it is needed. */
|
---|
301 | info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT
|
---|
302 | | FBINFO_MISC_ALWAYS_SETPAR;
|
---|
303 | info->fbops = &vboxfb_ops;
|
---|
304 |
|
---|
305 | ret = fb_alloc_cmap(&info->cmap, 256, 0);
|
---|
306 | if (ret) {
|
---|
307 | ret = -ENOMEM;
|
---|
308 | goto out;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /* This seems to be done for safety checking that the framebuffer is not
|
---|
312 | * registered twice by different drivers. */
|
---|
313 | info->apertures = alloc_apertures(1);
|
---|
314 | if (!info->apertures) {
|
---|
315 | ret = -ENOMEM;
|
---|
316 | goto out;
|
---|
317 | }
|
---|
318 | info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
|
---|
319 | info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
|
---|
320 |
|
---|
321 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
|
---|
322 | drm_fb_helper_fill_var(info, &fbdev->helper, sizes->fb_width, sizes->fb_height);
|
---|
323 |
|
---|
324 | info->screen_base = sysram;
|
---|
325 | info->screen_size = size;
|
---|
326 |
|
---|
327 | info->pixmap.flags = FB_PIXMAP_SYSTEM;
|
---|
328 |
|
---|
329 | DRM_DEBUG_KMS("allocated %dx%d\n",
|
---|
330 | fb->width, fb->height);
|
---|
331 |
|
---|
332 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
333 | return 0;
|
---|
334 | out:
|
---|
335 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
336 | return ret;
|
---|
337 | }
|
---|
338 |
|
---|
339 | static void vbox_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
|
---|
340 | u16 blue, int regno)
|
---|
341 | {
|
---|
342 |
|
---|
343 | }
|
---|
344 |
|
---|
345 | static void vbox_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
|
---|
346 | u16 *blue, int regno)
|
---|
347 | {
|
---|
348 | *red = regno;
|
---|
349 | *green = regno;
|
---|
350 | *blue = regno;
|
---|
351 | }
|
---|
352 |
|
---|
353 | static struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
|
---|
354 | .gamma_set = vbox_fb_gamma_set,
|
---|
355 | .gamma_get = vbox_fb_gamma_get,
|
---|
356 | .fb_probe = vboxfb_create,
|
---|
357 | };
|
---|
358 |
|
---|
359 | static void vbox_fbdev_destroy(struct drm_device *dev,
|
---|
360 | struct vbox_fbdev *fbdev)
|
---|
361 | {
|
---|
362 | struct fb_info *info;
|
---|
363 | struct vbox_framebuffer *afb = &fbdev->afb;
|
---|
364 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
365 | if (fbdev->helper.fbdev) {
|
---|
366 | info = fbdev->helper.fbdev;
|
---|
367 | unregister_framebuffer(info);
|
---|
368 | if (info->cmap.len)
|
---|
369 | fb_dealloc_cmap(&info->cmap);
|
---|
370 | framebuffer_release(info);
|
---|
371 | }
|
---|
372 |
|
---|
373 | if (afb->obj) {
|
---|
374 | drm_gem_object_unreference_unlocked(afb->obj);
|
---|
375 | afb->obj = NULL;
|
---|
376 | }
|
---|
377 | drm_fb_helper_fini(&fbdev->helper);
|
---|
378 |
|
---|
379 | vfree(fbdev->sysram);
|
---|
380 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
|
---|
381 | drm_framebuffer_unregister_private(&afb->base);
|
---|
382 | #endif
|
---|
383 | drm_framebuffer_cleanup(&afb->base);
|
---|
384 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
385 | }
|
---|
386 |
|
---|
387 | int vbox_fbdev_init(struct drm_device *dev)
|
---|
388 | {
|
---|
389 | struct vbox_private *vbox = dev->dev_private;
|
---|
390 | struct vbox_fbdev *fbdev;
|
---|
391 | int ret;
|
---|
392 |
|
---|
393 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
394 | fbdev = kzalloc(sizeof(struct vbox_fbdev), GFP_KERNEL);
|
---|
395 | if (!fbdev)
|
---|
396 | return -ENOMEM;
|
---|
397 |
|
---|
398 | vbox->fbdev = fbdev;
|
---|
399 | spin_lock_init(&fbdev->dirty_lock);
|
---|
400 |
|
---|
401 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
|
---|
402 | fbdev->helper.funcs = &vbox_fb_helper_funcs;
|
---|
403 | #else
|
---|
404 | drm_fb_helper_prepare(dev, &fbdev->helper, &vbox_fb_helper_funcs);
|
---|
405 | #endif
|
---|
406 | ret = drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs, vbox->num_crtcs);
|
---|
407 | if (ret)
|
---|
408 | goto free;
|
---|
409 |
|
---|
410 | ret = drm_fb_helper_single_add_all_connectors(&fbdev->helper);
|
---|
411 | if (ret)
|
---|
412 | goto fini;
|
---|
413 |
|
---|
414 | /* disable all the possible outputs/crtcs before entering KMS mode */
|
---|
415 | drm_helper_disable_unused_functions(dev);
|
---|
416 |
|
---|
417 | ret = drm_fb_helper_initial_config(&fbdev->helper, 32);
|
---|
418 | if (ret)
|
---|
419 | goto fini;
|
---|
420 |
|
---|
421 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
422 | return 0;
|
---|
423 | fini:
|
---|
424 | drm_fb_helper_fini(&fbdev->helper);
|
---|
425 | free:
|
---|
426 | kfree(fbdev);
|
---|
427 | vbox->fbdev = NULL;
|
---|
428 | LogFunc(("vboxvideo: %d, ret=%d\n", __LINE__, ret));
|
---|
429 | return ret;
|
---|
430 | }
|
---|
431 |
|
---|
432 | void vbox_fbdev_fini(struct drm_device *dev)
|
---|
433 | {
|
---|
434 | struct vbox_private *vbox = dev->dev_private;
|
---|
435 |
|
---|
436 | if (!vbox->fbdev)
|
---|
437 | return;
|
---|
438 |
|
---|
439 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
440 | vbox_fbdev_destroy(dev, vbox->fbdev);
|
---|
441 | kfree(vbox->fbdev);
|
---|
442 | vbox->fbdev = NULL;
|
---|
443 | }
|
---|
444 |
|
---|
445 | void vbox_fbdev_set_suspend(struct drm_device *dev, int state)
|
---|
446 | {
|
---|
447 | struct vbox_private *vbox = dev->dev_private;
|
---|
448 |
|
---|
449 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
450 | if (!vbox->fbdev)
|
---|
451 | return;
|
---|
452 |
|
---|
453 | fb_set_suspend(vbox->fbdev->helper.fbdev, state);
|
---|
454 | }
|
---|