VirtualBox

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

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

Additions/linux/drm: Linux 4.7 fixes

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