VirtualBox

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

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

bugref:8087: Additions/x11: support non-root X server: adjust DRM/KMS driver code to bring it back closer to the original AST driver on which it is based, including enabling ejecting unused frame-buffers from VRAM to save memory.

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