VirtualBox

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

Last change on this file since 76939 was 76939, checked in by vboxsync, 6 years ago

Reverted previous commit: merged too much.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
1/* $Id: vbox_fb.c 76939 2019-01-22 16:51:28Z vboxsync $ */
2/** @file
3 * VirtualBox Additions Linux kernel video driver
4 */
5
6/*
7 * Copyright (C) 2013-2019 Oracle Corporation
8 * This file is based on ast_fb.c
9 * Copyright 2012 Red Hat Inc.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the
13 * "Software"), to deal in the Software without restriction, including
14 * without limitation the rights to use, copy, modify, merge, publish,
15 * distribute, sub license, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * The above copyright notice and this permission notice (including the
28 * next paragraph) shall be included in all copies or substantial portions
29 * of the Software.
30 *
31 * Authors: Dave Airlie <[email protected]>
32 * Michael Thayer <[email protected],
33 */
34#include <linux/module.h>
35#include <linux/kernel.h>
36#include <linux/errno.h>
37#include <linux/string.h>
38#include <linux/mm.h>
39#include <linux/tty.h>
40#include <linux/sysrq.h>
41#include <linux/delay.h>
42#include <linux/fb.h>
43#include <linux/init.h>
44
45#include <drm/drmP.h>
46#include <drm/drm_crtc.h>
47#include <drm/drm_fb_helper.h>
48#include <drm/drm_crtc_helper.h>
49
50#include "vbox_drv.h"
51#include <VBoxVideo.h>
52
53#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0) && !defined(RHEL_74)
54/**
55 * Tell the host about dirty rectangles to update.
56 */
57static void vbox_dirty_update(struct vbox_fbdev *fbdev,
58 int x, int y, int width, int height)
59{
60 struct drm_gem_object *obj;
61 struct vbox_bo *bo;
62 int ret = -EBUSY;
63 bool store_for_later = false;
64 int x2, y2;
65 unsigned long flags;
66 struct drm_clip_rect rect;
67
68 obj = fbdev->afb.obj;
69 bo = gem_to_vbox_bo(obj);
70
71 /*
72 * try and reserve the BO, if we fail with busy
73 * then the BO is being moved and we should
74 * store up the damage until later.
75 */
76 if (drm_can_sleep())
77 ret = vbox_bo_reserve(bo, true);
78 if (ret) {
79 if (ret != -EBUSY)
80 return;
81
82 store_for_later = true;
83 }
84
85 x2 = x + width - 1;
86 y2 = y + height - 1;
87 spin_lock_irqsave(&fbdev->dirty_lock, flags);
88
89 if (fbdev->y1 < y)
90 y = fbdev->y1;
91 if (fbdev->y2 > y2)
92 y2 = fbdev->y2;
93 if (fbdev->x1 < x)
94 x = fbdev->x1;
95 if (fbdev->x2 > x2)
96 x2 = fbdev->x2;
97
98 if (store_for_later) {
99 fbdev->x1 = x;
100 fbdev->x2 = x2;
101 fbdev->y1 = y;
102 fbdev->y2 = y2;
103 spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
104 return;
105 }
106
107 fbdev->x1 = INT_MAX;
108 fbdev->y1 = INT_MAX;
109 fbdev->x2 = 0;
110 fbdev->y2 = 0;
111
112 spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
113
114 /*
115 * Not sure why the original code subtracted 1 here, but I will keep
116 * it that way to avoid unnecessary differences.
117 */
118 rect.x1 = x;
119 rect.x2 = x2 + 1;
120 rect.y1 = y;
121 rect.y2 = y2 + 1;
122 vbox_framebuffer_dirty_rectangles(&fbdev->afb.base, &rect, 1);
123
124 vbox_bo_unreserve(bo);
125}
126#endif
127
128#ifdef CONFIG_FB_DEFERRED_IO
129# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0) && !defined(RHEL_74)
130static void drm_fb_helper_deferred_io(struct fb_info *info, struct list_head *pagelist)
131{
132 struct vbox_fbdev *fbdev = info->par;
133 unsigned long start, end, min, max;
134 struct page *page;
135 int y1, y2;
136
137 min = ULONG_MAX;
138 max = 0;
139 list_for_each_entry(page, pagelist, lru) {
140 start = page->index << PAGE_SHIFT;
141 end = start + PAGE_SIZE - 1;
142 min = min(min, start);
143 max = max(max, end);
144 }
145
146 if (min < max) {
147 y1 = min / info->fix.line_length;
148 y2 = (max / info->fix.line_length) + 1;
149 DRM_INFO("%s: Calling dirty update: 0, %d, %d, %d\n",
150 __func__, y1, info->var.xres, y2 - y1 - 1);
151 vbox_dirty_update(fbdev, 0, y1, info->var.xres, y2 - y1 - 1);
152 }
153}
154# endif
155
156static struct fb_deferred_io vbox_defio = {
157 .delay = HZ / 30,
158 .deferred_io = drm_fb_helper_deferred_io,
159};
160#endif
161
162#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) && !defined(RHEL_73)
163static void drm_fb_helper_sys_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
164{
165 struct vbox_fbdev *fbdev = info->par;
166
167 sys_fillrect(info, rect);
168 vbox_dirty_update(fbdev, rect->dx, rect->dy, rect->width, rect->height);
169}
170
171static void drm_fb_helper_sys_copyarea(struct fb_info *info, const struct fb_copyarea *area)
172{
173 struct vbox_fbdev *fbdev = info->par;
174
175 sys_copyarea(info, area);
176 vbox_dirty_update(fbdev, area->dx, area->dy, area->width, area->height);
177}
178
179static void drm_fb_helper_sys_imageblit(struct fb_info *info, const struct fb_image *image)
180{
181 struct vbox_fbdev *fbdev = info->par;
182
183 sys_imageblit(info, image);
184 vbox_dirty_update(fbdev, image->dx, image->dy, image->width,
185 image->height);
186}
187#endif
188
189static struct fb_ops vboxfb_ops = {
190 .owner = THIS_MODULE,
191 .fb_check_var = drm_fb_helper_check_var,
192 .fb_set_par = drm_fb_helper_set_par,
193 .fb_fillrect = drm_fb_helper_sys_fillrect,
194 .fb_copyarea = drm_fb_helper_sys_copyarea,
195 .fb_imageblit = drm_fb_helper_sys_imageblit,
196 .fb_pan_display = drm_fb_helper_pan_display,
197 .fb_blank = drm_fb_helper_blank,
198 .fb_setcmap = drm_fb_helper_setcmap,
199 .fb_debug_enter = drm_fb_helper_debug_enter,
200 .fb_debug_leave = drm_fb_helper_debug_leave,
201};
202
203static int vboxfb_create_object(struct vbox_fbdev *fbdev,
204 struct DRM_MODE_FB_CMD *mode_cmd,
205 struct drm_gem_object **gobj_p)
206{
207 struct drm_device *dev = fbdev->helper.dev;
208 u32 size;
209 struct drm_gem_object *gobj;
210#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
211 u32 pitch = mode_cmd->pitch;
212#else
213 u32 pitch = mode_cmd->pitches[0];
214#endif
215
216 int ret;
217
218 size = pitch * mode_cmd->height;
219 ret = vbox_gem_create(dev, size, true, &gobj);
220 if (ret)
221 return ret;
222
223 *gobj_p = gobj;
224
225 return 0;
226}
227
228#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) && !defined(RHEL_73)
229static struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *helper)
230{
231 struct fb_info *info;
232 struct vbox_fbdev *fbdev =
233 container_of(helper, struct vbox_fbdev, helper);
234 struct drm_device *dev = fbdev->helper.dev;
235 struct device *device = &dev->pdev->dev;
236
237 info = framebuffer_alloc(0, device);
238 if (!info)
239 return ERR_PTR(-ENOMEM);
240 fbdev->helper.fbdev = info;
241
242 if (fb_alloc_cmap(&info->cmap, 256, 0))
243 return ERR_PTR(-ENOMEM);
244
245 info->apertures = alloc_apertures(1);
246 if (!info->apertures)
247 return ERR_PTR(-ENOMEM);
248
249 return info;
250}
251#endif
252
253static int vboxfb_create(struct drm_fb_helper *helper,
254 struct drm_fb_helper_surface_size *sizes)
255{
256 struct vbox_fbdev *fbdev =
257 container_of(helper, struct vbox_fbdev, helper);
258 struct drm_device *dev = fbdev->helper.dev;
259 struct DRM_MODE_FB_CMD mode_cmd;
260 struct drm_framebuffer *fb;
261 struct fb_info *info;
262 struct drm_gem_object *gobj;
263 struct vbox_bo *bo;
264 int size, ret;
265 u32 pitch;
266
267 mode_cmd.width = sizes->surface_width;
268 mode_cmd.height = sizes->surface_height;
269 pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
270#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
271 mode_cmd.bpp = sizes->surface_bpp;
272 mode_cmd.depth = sizes->surface_depth;
273 mode_cmd.pitch = pitch;
274#else
275 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
276 sizes->surface_depth);
277 mode_cmd.pitches[0] = pitch;
278#endif
279
280 size = pitch * mode_cmd.height;
281
282 ret = vboxfb_create_object(fbdev, &mode_cmd, &gobj);
283 if (ret) {
284 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
285 return ret;
286 }
287
288 ret = vbox_framebuffer_init(dev, &fbdev->afb, &mode_cmd, gobj);
289 if (ret)
290 return ret;
291
292 bo = gem_to_vbox_bo(gobj);
293
294 ret = vbox_bo_reserve(bo, false);
295 if (ret)
296 return ret;
297
298 ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
299 if (ret) {
300 vbox_bo_unreserve(bo);
301 return ret;
302 }
303
304 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
305 vbox_bo_unreserve(bo);
306 if (ret) {
307 DRM_ERROR("failed to kmap fbcon\n");
308 return ret;
309 }
310
311 info = drm_fb_helper_alloc_fbi(helper);
312 if (IS_ERR(info))
313 return -PTR_ERR(info);
314
315 info->par = fbdev;
316
317 fbdev->size = size;
318
319 fb = &fbdev->afb.base;
320 fbdev->helper.fb = fb;
321
322 strcpy(info->fix.id, "vboxdrmfb");
323
324 /*
325 * The last flag forces a mode set on VT switches even if the kernel
326 * does not think it is needed.
327 */
328 info->flags = FBINFO_DEFAULT | FBINFO_MISC_ALWAYS_SETPAR;
329 info->fbops = &vboxfb_ops;
330
331 /*
332 * This seems to be done for safety checking that the framebuffer
333 * is not registered twice by different drivers.
334 */
335 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
336 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
337
338#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) || defined(RHEL_75)
339 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
340#else
341 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
342#endif
343 drm_fb_helper_fill_var(info, &fbdev->helper, sizes->fb_width,
344 sizes->fb_height);
345
346 info->screen_base = bo->kmap.virtual;
347 info->screen_size = size;
348
349#ifdef CONFIG_FB_DEFERRED_IO
350 info->fbdefio = &vbox_defio;
351 fb_deferred_io_init(info);
352#endif
353
354 info->pixmap.flags = FB_PIXMAP_SYSTEM;
355
356 DRM_DEBUG_KMS("allocated %dx%d\n", fb->width, fb->height);
357
358 return 0;
359}
360
361static struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
362 .fb_probe = vboxfb_create,
363};
364
365#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) && !defined(RHEL_73)
366static void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
367{
368 if (fb_helper && fb_helper->fbdev)
369 unregister_framebuffer(fb_helper->fbdev);
370}
371#endif
372
373void vbox_fbdev_fini(struct drm_device *dev)
374{
375 struct vbox_private *vbox = dev->dev_private;
376 struct vbox_fbdev *fbdev = vbox->fbdev;
377 struct vbox_framebuffer *afb = &fbdev->afb;
378
379#ifdef CONFIG_FB_DEFERRED_IO
380 if (fbdev->helper.fbdev && fbdev->helper.fbdev->fbdefio)
381 fb_deferred_io_cleanup(fbdev->helper.fbdev);
382#endif
383
384 drm_fb_helper_unregister_fbi(&fbdev->helper);
385
386 if (afb->obj) {
387 struct vbox_bo *bo = gem_to_vbox_bo(afb->obj);
388
389 if (!vbox_bo_reserve(bo, false)) {
390 if (bo->kmap.virtual)
391 ttm_bo_kunmap(&bo->kmap);
392 /*
393 * QXL does this, but is it really needed before
394 * freeing?
395 */
396 if (bo->pin_count)
397 vbox_bo_unpin(bo);
398 vbox_bo_unreserve(bo);
399 }
400 drm_gem_object_put_unlocked(afb->obj);
401 afb->obj = NULL;
402 }
403 drm_fb_helper_fini(&fbdev->helper);
404
405#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
406 drm_framebuffer_unregister_private(&afb->base);
407#endif
408 drm_framebuffer_cleanup(&afb->base);
409}
410
411int vbox_fbdev_init(struct drm_device *dev)
412{
413 struct vbox_private *vbox = dev->dev_private;
414 struct vbox_fbdev *fbdev;
415 int ret;
416
417 fbdev = devm_kzalloc(dev->dev, sizeof(*fbdev), GFP_KERNEL);
418 if (!fbdev)
419 return -ENOMEM;
420
421 vbox->fbdev = fbdev;
422 spin_lock_init(&fbdev->dirty_lock);
423
424#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0) && !defined(RHEL_73)
425 fbdev->helper.funcs = &vbox_fb_helper_funcs;
426#else
427 drm_fb_helper_prepare(dev, &fbdev->helper, &vbox_fb_helper_funcs);
428#endif
429#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) || defined(RHEL_75)
430 ret = drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs);
431#else
432 ret =
433 drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs,
434 vbox->num_crtcs);
435#endif
436 if (ret)
437 return ret;
438
439 ret = drm_fb_helper_single_add_all_connectors(&fbdev->helper);
440 if (ret)
441 goto err_fini;
442
443 /* disable all the possible outputs/crtcs before entering KMS mode */
444 drm_helper_disable_unused_functions(dev);
445
446 ret = drm_fb_helper_initial_config(&fbdev->helper, 32);
447 if (ret)
448 goto err_fini;
449
450 return 0;
451
452err_fini:
453 drm_fb_helper_fini(&fbdev->helper);
454 return ret;
455}
456
457void vbox_fbdev_set_base(struct vbox_private *vbox, unsigned long gpu_addr)
458{
459 struct fb_info *fbdev = vbox->fbdev->helper.fbdev;
460
461 fbdev->fix.smem_start = fbdev->apertures->ranges[0].base + gpu_addr;
462 fbdev->fix.smem_len = vbox->available_vram_size - gpu_addr;
463}
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