VirtualBox

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

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

linux/drm: Fixed incorrect file headers. (copy + past is difficult, apparently)

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