VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/drm/vbox_fb.c@ 60092

Last change on this file since 60092 was 59964, checked in by vboxsync, 9 years ago

bugref:8087: Additions/x11: support non-root X server: additional kernel module build fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/* $Id: vbox_fb.c 59964 2016-03-09 11:20:48Z 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.virtualbox.org. 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 <[email protected]>
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/** This is called whenever there is a virtual console switch. We do two things
72 * here. First we re-set all video modes in case the last console owner
73 * programmed the card directly. Second we disable VBVA in case the new console
74 * owner is about to programme the card directly and doesn't know about VBVA.
75 * We re-enable VBVA if necessary when we get dirty rectangle information, as
76 * the owner should not be sending that if they plan to programme the card
77 * themselves. Update: we also do the same for reporting hot-plug support. I
78 * wonder whether we should allow it at all on the console. */
79static int vbox_set_par(struct fb_info *info)
80{
81 struct vbox_fbdev *fbdev = info->par;
82 struct drm_device *dev;
83 struct vbox_private *vbox;
84
85 LogFunc(("vboxvideo: %d\n", __LINE__));
86 dev = fbdev->helper.dev;
87 vbox = dev->dev_private;
88 vbox_refresh_modes(dev);
89 return drm_fb_helper_set_par(info);
90}
91
92/**
93 * Tell the host about dirty rectangles to update.
94 */
95static void vbox_dirty_update(struct vbox_fbdev *fbdev,
96 int x, int y, int width, int height)
97{
98 struct drm_device *dev = fbdev->helper.dev;
99 int i;
100
101 struct drm_gem_object *obj;
102 struct vbox_bo *bo;
103 int src_offset, dst_offset;
104 int bpp = (fbdev->afb.base.bits_per_pixel + 7)/8;
105 int ret = -EBUSY;
106 bool unmap = false;
107 bool store_for_later = false;
108 int x2, y2;
109 unsigned long flags;
110 struct drm_clip_rect rect;
111
112 LogFunc(("vboxvideo: %d\n", __LINE__));
113 obj = fbdev->afb.obj;
114 bo = gem_to_vbox_bo(obj);
115
116 /*
117 * try and reserve the BO, if we fail with busy
118 * then the BO is being moved and we should
119 * store up the damage until later.
120 */
121 if (drm_can_sleep())
122 ret = vbox_bo_reserve(bo, true);
123 if (ret) {
124 if (ret != -EBUSY)
125 return;
126
127 store_for_later = true;
128 }
129
130 x2 = x + width - 1;
131 y2 = y + height - 1;
132 spin_lock_irqsave(&fbdev->dirty_lock, flags);
133
134 if (fbdev->y1 < y)
135 y = fbdev->y1;
136 if (fbdev->y2 > y2)
137 y2 = fbdev->y2;
138 if (fbdev->x1 < x)
139 x = fbdev->x1;
140 if (fbdev->x2 > x2)
141 x2 = fbdev->x2;
142
143 if (store_for_later) {
144 fbdev->x1 = x;
145 fbdev->x2 = x2;
146 fbdev->y1 = y;
147 fbdev->y2 = y2;
148 spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
149 LogFunc(("vboxvideo: %d\n", __LINE__));
150 return;
151 }
152
153 fbdev->x1 = fbdev->y1 = INT_MAX;
154 fbdev->x2 = fbdev->y2 = 0;
155 spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
156
157 if (!bo->kmap.virtual) {
158 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
159 if (ret) {
160 DRM_ERROR("failed to kmap fb updates\n");
161 vbox_bo_unreserve(bo);
162 return;
163 }
164 unmap = true;
165 }
166 for (i = y; i <= y2; i++) {
167 /* assume equal stride for now */
168 src_offset = dst_offset = i * fbdev->afb.base.pitches[0] + (x * bpp);
169 memcpy_toio(bo->kmap.virtual + src_offset, (char *)fbdev->sysram + src_offset, (x2 - x + 1) * bpp);
170 }
171 /* Not sure why the original code subtracted 1 here, but I will keep it that
172 * way to avoid unnecessary differences. */
173 rect.x1 = x;
174 rect.x2 = x2 + 1;
175 rect.y1 = y;
176 rect.y2 = y2 + 1;
177 vbox_framebuffer_dirty_rectangles(&fbdev->afb.base, &rect, 1);
178 LogFunc(("vboxvideo: %d, bo->kmap.virtual=%p, fbdev->sysram=%p, x=%d, y=%d, x2=%d, y2=%d, unmap=%RTbool\n",
179 __LINE__, bo->kmap.virtual, fbdev->sysram, (int)x, (int)y, (int)x2, (int)y2, unmap));
180 if (unmap)
181 ttm_bo_kunmap(&bo->kmap);
182
183 vbox_bo_unreserve(bo);
184}
185
186static void vbox_fillrect(struct fb_info *info,
187 const struct fb_fillrect *rect)
188{
189 struct vbox_fbdev *fbdev = info->par;
190 LogFunc(("vboxvideo: %d\n", __LINE__));
191 sys_fillrect(info, rect);
192 vbox_dirty_update(fbdev, rect->dx, rect->dy, rect->width,
193 rect->height);
194}
195
196static void vbox_copyarea(struct fb_info *info,
197 const struct fb_copyarea *area)
198{
199 struct vbox_fbdev *fbdev = info->par;
200 LogFunc(("vboxvideo: %d\n", __LINE__));
201 sys_copyarea(info, area);
202 vbox_dirty_update(fbdev, area->dx, area->dy, area->width,
203 area->height);
204}
205
206static void vbox_imageblit(struct fb_info *info,
207 const struct fb_image *image)
208{
209 struct vbox_fbdev *fbdev = info->par;
210 LogFunc(("vboxvideo: %d\n", __LINE__));
211 sys_imageblit(info, image);
212 vbox_dirty_update(fbdev, image->dx, image->dy, image->width,
213 image->height);
214}
215
216static struct fb_ops vboxfb_ops = {
217 .owner = THIS_MODULE,
218 .fb_check_var = drm_fb_helper_check_var,
219 .fb_set_par = vbox_set_par,
220 .fb_fillrect = vbox_fillrect,
221 .fb_copyarea = vbox_copyarea,
222 .fb_imageblit = vbox_imageblit,
223 .fb_pan_display = drm_fb_helper_pan_display,
224 .fb_blank = drm_fb_helper_blank,
225 .fb_setcmap = drm_fb_helper_setcmap,
226 .fb_debug_enter = drm_fb_helper_debug_enter,
227 .fb_debug_leave = drm_fb_helper_debug_leave,
228};
229
230static int vboxfb_create_object(struct vbox_fbdev *fbdev,
231 struct DRM_MODE_FB_CMD *mode_cmd,
232 struct drm_gem_object **gobj_p)
233{
234 struct drm_device *dev = fbdev->helper.dev;
235 u32 bpp, depth;
236 u32 size;
237 struct drm_gem_object *gobj;
238#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
239 __u32 pitch = mode_cmd->pitch;
240#else
241 __u32 pitch = mode_cmd->pitches[0];
242#endif
243
244 int ret = 0;
245 LogFunc(("vboxvideo: %d\n", __LINE__));
246 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
247
248 size = pitch * mode_cmd->height;
249 ret = vbox_gem_create(dev, size, true, &gobj);
250 if (ret)
251 return ret;
252
253 *gobj_p = gobj;
254 LogFunc(("vboxvideo: %d\n", __LINE__));
255 return ret;
256}
257
258static int vboxfb_create(struct drm_fb_helper *helper,
259 struct drm_fb_helper_surface_size *sizes)
260{
261 struct vbox_fbdev *fbdev =
262 container_of(helper, struct vbox_fbdev, helper);
263 struct drm_device *dev = fbdev->helper.dev;
264 struct DRM_MODE_FB_CMD mode_cmd;
265 struct drm_framebuffer *fb;
266 struct fb_info *info;
267 __u32 pitch;
268 int size, ret;
269 struct device *device = &dev->pdev->dev;
270 void *sysram;
271 struct drm_gem_object *gobj = NULL;
272 struct vbox_bo *bo = NULL;
273 LogFunc(("vboxvideo: %d\n", __LINE__));
274 mode_cmd.width = sizes->surface_width;
275 mode_cmd.height = sizes->surface_height;
276 pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
277#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
278 mode_cmd.bpp = sizes->surface_bpp;
279 mode_cmd.depth = sizes->surface_depth;
280 mode_cmd.pitch = pitch;
281#else
282 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
283 sizes->surface_depth);
284 mode_cmd.pitches[0] = pitch;
285#endif
286
287 size = pitch * mode_cmd.height;
288
289 ret = vboxfb_create_object(fbdev, &mode_cmd, &gobj);
290 if (ret) {
291 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
292 return ret;
293 }
294 bo = gem_to_vbox_bo(gobj);
295
296 sysram = vmalloc(size);
297 if (!sysram)
298 return -ENOMEM;
299
300 info = framebuffer_alloc(0, device);
301 if (!info) {
302 ret = -ENOMEM;
303 goto out;
304 }
305 info->par = fbdev;
306
307 ret = vbox_framebuffer_init(dev, &fbdev->afb, &mode_cmd, gobj);
308 if (ret)
309 goto out;
310
311 fbdev->sysram = sysram;
312 fbdev->size = size;
313
314 fb = &fbdev->afb.base;
315 fbdev->helper.fb = fb;
316 fbdev->helper.fbdev = info;
317
318 strcpy(info->fix.id, "vboxdrmfb");
319
320 /* The last flag forces a mode set on VT switches even if the kernel does
321 * not think it is needed. */
322 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT
323 | FBINFO_MISC_ALWAYS_SETPAR;
324 info->fbops = &vboxfb_ops;
325
326 ret = fb_alloc_cmap(&info->cmap, 256, 0);
327 if (ret) {
328 ret = -ENOMEM;
329 goto out;
330 }
331
332 /* This seems to be done for safety checking that the framebuffer is not
333 * registered twice by different drivers. */
334 info->apertures = alloc_apertures(1);
335 if (!info->apertures) {
336 ret = -ENOMEM;
337 goto out;
338 }
339 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
340 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
341
342 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
343 drm_fb_helper_fill_var(info, &fbdev->helper, sizes->fb_width, sizes->fb_height);
344
345 info->screen_base = sysram;
346 info->screen_size = size;
347
348 info->pixmap.flags = FB_PIXMAP_SYSTEM;
349
350 DRM_DEBUG_KMS("allocated %dx%d\n",
351 fb->width, fb->height);
352
353 LogFunc(("vboxvideo: %d\n", __LINE__));
354 return 0;
355out:
356 LogFunc(("vboxvideo: %d\n", __LINE__));
357 return ret;
358}
359
360static void vbox_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
361 u16 blue, int regno)
362{
363
364}
365
366static void vbox_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
367 u16 *blue, int regno)
368{
369 *red = regno;
370 *green = regno;
371 *blue = regno;
372}
373
374static struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
375 .gamma_set = vbox_fb_gamma_set,
376 .gamma_get = vbox_fb_gamma_get,
377 .fb_probe = vboxfb_create,
378};
379
380static void vbox_fbdev_destroy(struct drm_device *dev,
381 struct vbox_fbdev *fbdev)
382{
383 struct fb_info *info;
384 struct vbox_framebuffer *afb = &fbdev->afb;
385 LogFunc(("vboxvideo: %d\n", __LINE__));
386 if (fbdev->helper.fbdev) {
387 info = fbdev->helper.fbdev;
388 unregister_framebuffer(info);
389 if (info->cmap.len)
390 fb_dealloc_cmap(&info->cmap);
391 framebuffer_release(info);
392 }
393
394 if (afb->obj) {
395 drm_gem_object_unreference_unlocked(afb->obj);
396 afb->obj = NULL;
397 }
398 drm_fb_helper_fini(&fbdev->helper);
399
400 vfree(fbdev->sysram);
401#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
402 drm_framebuffer_unregister_private(&afb->base);
403#endif
404 drm_framebuffer_cleanup(&afb->base);
405 LogFunc(("vboxvideo: %d\n", __LINE__));
406}
407
408int vbox_fbdev_init(struct drm_device *dev)
409{
410 struct vbox_private *vbox = dev->dev_private;
411 struct vbox_fbdev *fbdev;
412 int ret;
413
414 LogFunc(("vboxvideo: %d\n", __LINE__));
415 fbdev = kzalloc(sizeof(struct vbox_fbdev), GFP_KERNEL);
416 if (!fbdev)
417 return -ENOMEM;
418
419 vbox->fbdev = fbdev;
420 spin_lock_init(&fbdev->dirty_lock);
421
422#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
423 fbdev->helper.funcs = &vbox_fb_helper_funcs;
424#else
425 drm_fb_helper_prepare(dev, &fbdev->helper, &vbox_fb_helper_funcs);
426#endif
427 ret = drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs, vbox->num_crtcs);
428 if (ret)
429 goto free;
430
431 ret = drm_fb_helper_single_add_all_connectors(&fbdev->helper);
432 if (ret)
433 goto fini;
434
435 /* disable all the possible outputs/crtcs before entering KMS mode */
436 drm_helper_disable_unused_functions(dev);
437
438 ret = drm_fb_helper_initial_config(&fbdev->helper, 32);
439 if (ret)
440 goto fini;
441
442 LogFunc(("vboxvideo: %d\n", __LINE__));
443 return 0;
444fini:
445 drm_fb_helper_fini(&fbdev->helper);
446free:
447 kfree(fbdev);
448 vbox->fbdev = NULL;
449 LogFunc(("vboxvideo: %d, ret=%d\n", __LINE__, ret));
450 return ret;
451}
452
453void vbox_fbdev_fini(struct drm_device *dev)
454{
455 struct vbox_private *vbox = dev->dev_private;
456
457 if (!vbox->fbdev)
458 return;
459
460 LogFunc(("vboxvideo: %d\n", __LINE__));
461 vbox_fbdev_destroy(dev, vbox->fbdev);
462 kfree(vbox->fbdev);
463 vbox->fbdev = NULL;
464}
465
466void vbox_fbdev_set_suspend(struct drm_device *dev, int state)
467{
468 struct vbox_private *vbox = dev->dev_private;
469
470 LogFunc(("vboxvideo: %d\n", __LINE__));
471 if (!vbox->fbdev)
472 return;
473
474 fb_set_suspend(vbox->fbdev->helper.fbdev, state);
475}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette