VirtualBox

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

Last change on this file since 53793 was 53793, checked in by vboxsync, 10 years ago

Additions/linux/drm: forgotten to export some files.

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