VirtualBox

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

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

bugref:4567: build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 KB
Line 
1/* $Id: vbox_fb.c 61602 2016-06-09 07:13:44Z 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#define VBOX_DIRTY_DELAY (HZ / 30)
72/**
73 * Tell the host about dirty rectangles to update.
74 */
75static void vbox_dirty_update(struct vbox_fbdev *fbdev,
76 int x, int y, int width, int height)
77{
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#ifdef CONFIG_FB_DEFERRED_IO
166static void vbox_deferred_io(struct fb_info *info,
167 struct list_head *pagelist)
168{
169 struct vbox_fbdev *fbdev = info->par;
170 unsigned long start, end, min, max;
171 struct page *page;
172 int y1, y2;
173
174 min = ULONG_MAX;
175 max = 0;
176 list_for_each_entry(page, pagelist, lru) {
177 start = page->index << PAGE_SHIFT;
178 end = start + PAGE_SIZE - 1;
179 min = min(min, start);
180 max = max(max, end);
181 }
182
183 if (min < max) {
184 y1 = min / info->fix.line_length;
185 y2 = (max / info->fix.line_length) + 1;
186 printk(KERN_INFO "%s: Calling dirty update: 0, %d, %d, %d\n",
187 __func__, y1, info->var.xres, y2 - y1 - 1);
188 vbox_dirty_update(fbdev, 0, y1, info->var.xres, y2 - y1 - 1);
189 }
190}
191
192static struct fb_deferred_io vbox_defio =
193{
194 .delay = VBOX_DIRTY_DELAY,
195 .deferred_io = vbox_deferred_io,
196};
197#endif
198
199static void vbox_fillrect(struct fb_info *info,
200 const struct fb_fillrect *rect)
201{
202 struct vbox_fbdev *fbdev = info->par;
203 LogFunc(("vboxvideo: %d\n", __LINE__));
204 sys_fillrect(info, rect);
205 vbox_dirty_update(fbdev, rect->dx, rect->dy, rect->width,
206 rect->height);
207}
208
209static void vbox_copyarea(struct fb_info *info,
210 const struct fb_copyarea *area)
211{
212 struct vbox_fbdev *fbdev = info->par;
213 LogFunc(("vboxvideo: %d\n", __LINE__));
214 sys_copyarea(info, area);
215 vbox_dirty_update(fbdev, area->dx, area->dy, area->width,
216 area->height);
217}
218
219static void vbox_imageblit(struct fb_info *info,
220 const struct fb_image *image)
221{
222 struct vbox_fbdev *fbdev = info->par;
223 LogFunc(("vboxvideo: %d\n", __LINE__));
224 sys_imageblit(info, image);
225 vbox_dirty_update(fbdev, image->dx, image->dy, image->width,
226 image->height);
227}
228
229static struct fb_ops vboxfb_ops = {
230 .owner = THIS_MODULE,
231 .fb_check_var = drm_fb_helper_check_var,
232 .fb_set_par = drm_fb_helper_set_par,
233 .fb_fillrect = vbox_fillrect,
234 .fb_copyarea = vbox_copyarea,
235 .fb_imageblit = vbox_imageblit,
236 .fb_pan_display = drm_fb_helper_pan_display,
237 .fb_blank = drm_fb_helper_blank,
238 .fb_setcmap = drm_fb_helper_setcmap,
239 .fb_debug_enter = drm_fb_helper_debug_enter,
240 .fb_debug_leave = drm_fb_helper_debug_leave,
241};
242
243static int vboxfb_create_object(struct vbox_fbdev *fbdev,
244 struct DRM_MODE_FB_CMD *mode_cmd,
245 struct drm_gem_object **gobj_p)
246{
247 struct drm_device *dev = fbdev->helper.dev;
248 u32 bpp, depth;
249 u32 size;
250 struct drm_gem_object *gobj;
251#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
252 __u32 pitch = mode_cmd->pitch;
253#else
254 __u32 pitch = mode_cmd->pitches[0];
255#endif
256
257 int ret = 0;
258 LogFunc(("vboxvideo: %d\n", __LINE__));
259 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
260
261 size = pitch * mode_cmd->height;
262 ret = vbox_gem_create(dev, size, true, &gobj);
263 if (ret)
264 return ret;
265
266 *gobj_p = gobj;
267 LogFunc(("vboxvideo: %d\n", __LINE__));
268 return ret;
269}
270
271static int vboxfb_create(struct drm_fb_helper *helper,
272 struct drm_fb_helper_surface_size *sizes)
273{
274 struct vbox_fbdev *fbdev =
275 container_of(helper, struct vbox_fbdev, helper);
276 struct drm_device *dev = fbdev->helper.dev;
277 struct DRM_MODE_FB_CMD mode_cmd;
278 struct drm_framebuffer *fb;
279 struct fb_info *info;
280 __u32 pitch;
281 int size, ret;
282 struct device *device = &dev->pdev->dev;
283 void *sysram;
284 struct drm_gem_object *gobj = NULL;
285 struct vbox_bo *bo = NULL;
286 LogFunc(("vboxvideo: %d\n", __LINE__));
287 mode_cmd.width = sizes->surface_width;
288 mode_cmd.height = sizes->surface_height;
289 pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
290#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
291 mode_cmd.bpp = sizes->surface_bpp;
292 mode_cmd.depth = sizes->surface_depth;
293 mode_cmd.pitch = pitch;
294#else
295 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
296 sizes->surface_depth);
297 mode_cmd.pitches[0] = pitch;
298#endif
299
300 size = pitch * mode_cmd.height;
301
302 ret = vboxfb_create_object(fbdev, &mode_cmd, &gobj);
303 if (ret) {
304 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
305 return ret;
306 }
307 bo = gem_to_vbox_bo(gobj);
308
309 sysram = vmalloc(size);
310 if (!sysram)
311 return -ENOMEM;
312
313 info = framebuffer_alloc(0, device);
314 if (!info) {
315 ret = -ENOMEM;
316 goto out;
317 }
318 info->par = fbdev;
319
320 ret = vbox_framebuffer_init(dev, &fbdev->afb, &mode_cmd, gobj);
321 if (ret)
322 goto out;
323
324 fbdev->sysram = sysram;
325 fbdev->size = size;
326
327 fb = &fbdev->afb.base;
328 fbdev->helper.fb = fb;
329 fbdev->helper.fbdev = info;
330
331 strcpy(info->fix.id, "vboxdrmfb");
332
333 /* The last flag forces a mode set on VT switches even if the kernel does
334 * not think it is needed. */
335 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT
336 | FBINFO_MISC_ALWAYS_SETPAR;
337 info->fbops = &vboxfb_ops;
338
339 ret = fb_alloc_cmap(&info->cmap, 256, 0);
340 if (ret) {
341 ret = -ENOMEM;
342 goto out;
343 }
344
345 /* This seems to be done for safety checking that the framebuffer is not
346 * registered twice by different drivers. */
347 info->apertures = alloc_apertures(1);
348 if (!info->apertures) {
349 ret = -ENOMEM;
350 goto out;
351 }
352 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
353 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
354
355 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
356 drm_fb_helper_fill_var(info, &fbdev->helper, sizes->fb_width, sizes->fb_height);
357
358 info->screen_base = sysram;
359 info->screen_size = size;
360
361#ifdef CONFIG_FB_DEFERRED_IO
362 info->fbdefio = &vbox_defio;
363 fb_deferred_io_init(info);
364#endif
365
366 info->pixmap.flags = FB_PIXMAP_SYSTEM;
367
368 DRM_DEBUG_KMS("allocated %dx%d\n",
369 fb->width, fb->height);
370
371 LogFunc(("vboxvideo: %d\n", __LINE__));
372 return 0;
373out:
374 LogFunc(("vboxvideo: %d\n", __LINE__));
375 return ret;
376}
377
378static void vbox_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
379 u16 blue, int regno)
380{
381
382}
383
384static void vbox_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
385 u16 *blue, int regno)
386{
387 *red = regno;
388 *green = regno;
389 *blue = regno;
390}
391
392static struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
393 .gamma_set = vbox_fb_gamma_set,
394 .gamma_get = vbox_fb_gamma_get,
395 .fb_probe = vboxfb_create,
396};
397
398static void vbox_fbdev_destroy(struct drm_device *dev,
399 struct vbox_fbdev *fbdev)
400{
401 struct fb_info *info;
402 struct vbox_framebuffer *afb = &fbdev->afb;
403 LogFunc(("vboxvideo: %d\n", __LINE__));
404 if (fbdev->helper.fbdev) {
405 info = fbdev->helper.fbdev;
406 unregister_framebuffer(info);
407 if (info->cmap.len)
408 fb_dealloc_cmap(&info->cmap);
409 framebuffer_release(info);
410 }
411
412 if (afb->obj) {
413 drm_gem_object_unreference_unlocked(afb->obj);
414 afb->obj = NULL;
415 }
416 drm_fb_helper_fini(&fbdev->helper);
417
418 vfree(fbdev->sysram);
419#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
420 drm_framebuffer_unregister_private(&afb->base);
421#endif
422 drm_framebuffer_cleanup(&afb->base);
423 LogFunc(("vboxvideo: %d\n", __LINE__));
424}
425
426int vbox_fbdev_init(struct drm_device *dev)
427{
428 struct vbox_private *vbox = dev->dev_private;
429 struct vbox_fbdev *fbdev;
430 int ret;
431
432 LogFunc(("vboxvideo: %d\n", __LINE__));
433 fbdev = kzalloc(sizeof(struct vbox_fbdev), GFP_KERNEL);
434 if (!fbdev)
435 return -ENOMEM;
436
437 vbox->fbdev = fbdev;
438 spin_lock_init(&fbdev->dirty_lock);
439
440#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
441 fbdev->helper.funcs = &vbox_fb_helper_funcs;
442#else
443 drm_fb_helper_prepare(dev, &fbdev->helper, &vbox_fb_helper_funcs);
444#endif
445 ret = drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs, vbox->num_crtcs);
446 if (ret)
447 goto free;
448
449 ret = drm_fb_helper_single_add_all_connectors(&fbdev->helper);
450 if (ret)
451 goto fini;
452
453 /* disable all the possible outputs/crtcs before entering KMS mode */
454 drm_helper_disable_unused_functions(dev);
455
456 ret = drm_fb_helper_initial_config(&fbdev->helper, 32);
457 if (ret)
458 goto fini;
459
460 LogFunc(("vboxvideo: %d\n", __LINE__));
461 return 0;
462fini:
463 drm_fb_helper_fini(&fbdev->helper);
464free:
465 kfree(fbdev);
466 vbox->fbdev = NULL;
467 LogFunc(("vboxvideo: %d, ret=%d\n", __LINE__, ret));
468 return ret;
469}
470
471void vbox_fbdev_fini(struct drm_device *dev)
472{
473 struct vbox_private *vbox = dev->dev_private;
474
475 if (!vbox->fbdev)
476 return;
477
478 LogFunc(("vboxvideo: %d\n", __LINE__));
479 vbox_fbdev_destroy(dev, vbox->fbdev);
480 kfree(vbox->fbdev);
481 vbox->fbdev = NULL;
482}
483
484void vbox_fbdev_set_suspend(struct drm_device *dev, int state)
485{
486 struct vbox_private *vbox = dev->dev_private;
487
488 LogFunc(("vboxvideo: %d\n", __LINE__));
489 if (!vbox->fbdev)
490 return;
491
492 fb_set_suspend(vbox->fbdev->helper.fbdev, state);
493}
494
495void vbox_fbdev_set_base(struct vbox_private *vbox, unsigned long gpu_addr)
496{
497 vbox->fbdev->helper.fbdev->fix.smem_start =
498 vbox->fbdev->helper.fbdev->apertures->ranges[0].base +
499 gpu_addr;
500 vbox->fbdev->helper.fbdev->fix.smem_len = vbox->vram_size - gpu_addr;
501}
Note: See TracBrowser for help on using the repository browser.

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