VirtualBox

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

最後變更 在這個檔案是 106061,由 vboxsync 提交於 3 月 前

Copyright year updates by scm.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 14.7 KB
 
1/* $Id: vbox_fb.c 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VirtualBox Additions Linux kernel video driver
4 */
5
6/*
7 * Copyright (C) 2013-2024 Oracle and/or its affiliates.
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 "vbox_drv.h"
46
47#include <drm/drm_crtc.h>
48#include <drm/drm_fb_helper.h>
49#include <drm/drm_crtc_helper.h>
50
51#include <VBoxVideo.h>
52
53#if RTLNX_VER_MIN(6,2,0) || RTLNX_RHEL_RANGE(8,9, 8,99) || RTLNX_RHEL_RANGE(9,3, 9,99)
54# define VBOX_FBDEV_INFO(_helper) _helper.info
55#else
56# define VBOX_FBDEV_INFO(_helper) _helper.fbdev
57#endif
58
59#if RTLNX_VER_MAX(4,7,0) && !RTLNX_RHEL_MAJ_PREREQ(7,4)
60/**
61 * Tell the host about dirty rectangles to update.
62 */
63static void vbox_dirty_update(struct vbox_fbdev *fbdev,
64 int x, int y, int width, int height)
65{
66 struct drm_gem_object *obj;
67 struct vbox_bo *bo;
68 int ret = -EBUSY;
69 bool store_for_later = false;
70 int x2, y2;
71 unsigned long flags;
72 struct drm_clip_rect rect;
73
74 obj = fbdev->afb.obj;
75 bo = gem_to_vbox_bo(obj);
76
77 /*
78 * try and reserve the BO, if we fail with busy
79 * then the BO is being moved and we should
80 * store up the damage until later.
81 */
82 if (drm_can_sleep())
83 ret = vbox_bo_reserve(bo, true);
84 if (ret) {
85 if (ret != -EBUSY)
86 return;
87
88 store_for_later = true;
89 }
90
91 x2 = x + width - 1;
92 y2 = y + height - 1;
93 spin_lock_irqsave(&fbdev->dirty_lock, flags);
94
95 if (fbdev->y1 < y)
96 y = fbdev->y1;
97 if (fbdev->y2 > y2)
98 y2 = fbdev->y2;
99 if (fbdev->x1 < x)
100 x = fbdev->x1;
101 if (fbdev->x2 > x2)
102 x2 = fbdev->x2;
103
104 if (store_for_later) {
105 fbdev->x1 = x;
106 fbdev->x2 = x2;
107 fbdev->y1 = y;
108 fbdev->y2 = y2;
109 spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
110 return;
111 }
112
113 fbdev->x1 = INT_MAX;
114 fbdev->y1 = INT_MAX;
115 fbdev->x2 = 0;
116 fbdev->y2 = 0;
117
118 spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
119
120 /*
121 * Not sure why the original code subtracted 1 here, but I will keep
122 * it that way to avoid unnecessary differences.
123 */
124 rect.x1 = x;
125 rect.x2 = x2 + 1;
126 rect.y1 = y;
127 rect.y2 = y2 + 1;
128 vbox_framebuffer_dirty_rectangles(&fbdev->afb.base, &rect, 1);
129
130 vbox_bo_unreserve(bo);
131}
132#endif /* RTLNX_VER_MAX(4,7,0) && !RTLNX_RHEL_MAJ_PREREQ(7,4) */
133
134#ifdef CONFIG_FB_DEFERRED_IO
135# if RTLNX_VER_MAX(4,7,0) && !RTLNX_RHEL_MAJ_PREREQ(7,4)
136static void drm_fb_helper_deferred_io(struct fb_info *info, struct list_head *pagelist)
137{
138 struct vbox_fbdev *fbdev = info->par;
139 unsigned long start, end, min, max;
140 struct page *page;
141 int y1, y2;
142
143 min = ULONG_MAX;
144 max = 0;
145 list_for_each_entry(page, pagelist, lru) {
146 start = page->index << PAGE_SHIFT;
147 end = start + PAGE_SIZE - 1;
148 min = min(min, start);
149 max = max(max, end);
150 }
151
152 if (min < max) {
153 y1 = min / info->fix.line_length;
154 y2 = (max / info->fix.line_length) + 1;
155 DRM_INFO("%s: Calling dirty update: 0, %d, %d, %d\n",
156 __func__, y1, info->var.xres, y2 - y1 - 1);
157 vbox_dirty_update(fbdev, 0, y1, info->var.xres, y2 - y1 - 1);
158 }
159}
160# endif
161
162static struct fb_deferred_io vbox_defio = {
163 .delay = HZ / 30,
164 .deferred_io = drm_fb_helper_deferred_io,
165};
166#endif /* CONFIG_FB_DEFERRED_IO */
167
168#if RTLNX_VER_MAX(4,3,0) && !RTLNX_RHEL_MAJ_PREREQ(7,3)
169static void drm_fb_helper_sys_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
170{
171 struct vbox_fbdev *fbdev = info->par;
172
173 sys_fillrect(info, rect);
174 vbox_dirty_update(fbdev, rect->dx, rect->dy, rect->width, rect->height);
175}
176
177static void drm_fb_helper_sys_copyarea(struct fb_info *info, const struct fb_copyarea *area)
178{
179 struct vbox_fbdev *fbdev = info->par;
180
181 sys_copyarea(info, area);
182 vbox_dirty_update(fbdev, area->dx, area->dy, area->width, area->height);
183}
184
185static void drm_fb_helper_sys_imageblit(struct fb_info *info, const struct fb_image *image)
186{
187 struct vbox_fbdev *fbdev = info->par;
188
189 sys_imageblit(info, image);
190 vbox_dirty_update(fbdev, image->dx, image->dy, image->width,
191 image->height);
192}
193#endif /* RTLNX_VER_MAX(4,3,0) && !RTLNX_RHEL_MAJ_PREREQ(7,3) */
194
195static struct fb_ops vboxfb_ops = {
196 .owner = THIS_MODULE,
197 .fb_check_var = drm_fb_helper_check_var,
198 .fb_set_par = drm_fb_helper_set_par,
199#if RTLNX_VER_MIN(6,5,0) || RTLNX_RHEL_RANGE(9,4, 9,99) || RTLNX_SUSE_MAJ_PREREQ(15, 6)
200 .fb_read = fb_sys_read,
201 .fb_write = fb_sys_write,
202 .fb_fillrect = sys_fillrect,
203 .fb_copyarea = sys_copyarea,
204 .fb_imageblit = sys_imageblit,
205 .fb_mmap = NULL,
206#else
207 .fb_fillrect = drm_fb_helper_sys_fillrect,
208 .fb_copyarea = drm_fb_helper_sys_copyarea,
209 .fb_imageblit = drm_fb_helper_sys_imageblit,
210#endif
211 .fb_pan_display = drm_fb_helper_pan_display,
212 .fb_blank = drm_fb_helper_blank,
213 .fb_setcmap = drm_fb_helper_setcmap,
214 .fb_debug_enter = drm_fb_helper_debug_enter,
215 .fb_debug_leave = drm_fb_helper_debug_leave,
216};
217
218static int vboxfb_create_object(struct vbox_fbdev *fbdev,
219 struct DRM_MODE_FB_CMD *mode_cmd,
220 struct drm_gem_object **gobj_p)
221{
222 struct drm_device *dev = fbdev->helper.dev;
223 u32 size;
224 struct drm_gem_object *gobj;
225#if RTLNX_VER_MAX(3,3,0)
226 u32 pitch = mode_cmd->pitch;
227#else
228 u32 pitch = mode_cmd->pitches[0];
229#endif
230
231 int ret;
232
233 size = pitch * mode_cmd->height;
234 ret = vbox_gem_create(dev, size, true, &gobj);
235 if (ret)
236 return ret;
237
238 *gobj_p = gobj;
239
240 return 0;
241}
242
243#if RTLNX_VER_MAX(4,3,0) && !RTLNX_RHEL_MAJ_PREREQ(7,3)
244static struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *helper)
245{
246 struct fb_info *info;
247 struct vbox_fbdev *fbdev =
248 container_of(helper, struct vbox_fbdev, helper);
249 struct drm_device *dev = fbdev->helper.dev;
250 struct device *device = &dev->pdev->dev;
251
252 info = framebuffer_alloc(0, device);
253 if (!info)
254 return ERR_PTR(-ENOMEM);
255 fbdev->helper.fbdev = info;
256
257 if (fb_alloc_cmap(&info->cmap, 256, 0))
258 return ERR_PTR(-ENOMEM);
259
260 info->apertures = alloc_apertures(1);
261 if (!info->apertures)
262 return ERR_PTR(-ENOMEM);
263
264 return info;
265}
266#endif
267
268static int vboxfb_create(struct drm_fb_helper *helper,
269 struct drm_fb_helper_surface_size *sizes)
270{
271 struct vbox_fbdev *fbdev =
272 container_of(helper, struct vbox_fbdev, helper);
273 struct drm_device *dev = fbdev->helper.dev;
274 struct DRM_MODE_FB_CMD mode_cmd;
275 struct drm_framebuffer *fb;
276 struct fb_info *info;
277 struct drm_gem_object *gobj;
278 struct vbox_bo *bo;
279 int size, ret;
280 u32 pitch;
281
282 mode_cmd.width = sizes->surface_width;
283 mode_cmd.height = sizes->surface_height;
284 pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
285#if RTLNX_VER_MAX(3,3,0)
286 mode_cmd.bpp = sizes->surface_bpp;
287 mode_cmd.depth = sizes->surface_depth;
288 mode_cmd.pitch = pitch;
289#else
290 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
291 sizes->surface_depth);
292 mode_cmd.pitches[0] = pitch;
293#endif
294
295 size = pitch * mode_cmd.height;
296
297 ret = vboxfb_create_object(fbdev, &mode_cmd, &gobj);
298 if (ret) {
299 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
300 return ret;
301 }
302
303 ret = vbox_framebuffer_init(dev, &fbdev->afb, &mode_cmd, gobj);
304 if (ret)
305 return ret;
306
307 bo = gem_to_vbox_bo(gobj);
308
309 ret = vbox_bo_reserve(bo, false);
310 if (ret)
311 return ret;
312
313 ret = vbox_bo_pin(bo, VBOX_MEM_TYPE_VRAM, NULL);
314 if (ret) {
315 vbox_bo_unreserve(bo);
316 return ret;
317 }
318
319#if RTLNX_VER_MIN(5,14,0) || RTLNX_RHEL_RANGE(8,6, 8,99)
320 ret = ttm_bo_kmap(&bo->bo, 0, VBOX_BO_RESOURCE_NUM_PAGES(bo->bo.resource), &bo->kmap);
321#elif RTLNX_VER_MIN(5,12,0) || RTLNX_RHEL_MAJ_PREREQ(8,5)
322 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.mem.num_pages, &bo->kmap);
323#else
324 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
325#endif
326
327 vbox_bo_unreserve(bo);
328 if (ret) {
329 DRM_ERROR("failed to kmap fbcon\n");
330 return ret;
331 }
332
333#if RTLNX_VER_MIN(6,2,0) || RTLNX_RHEL_RANGE(8,9, 8,99) || RTLNX_RHEL_RANGE(9,3, 9,99)
334 info = drm_fb_helper_alloc_info(helper);
335#else
336 info = drm_fb_helper_alloc_fbi(helper);
337#endif
338 if (IS_ERR(info))
339 return -PTR_ERR(info);
340
341 info->par = fbdev;
342
343 fbdev->size = size;
344
345 fb = &fbdev->afb.base;
346 fbdev->helper.fb = fb;
347
348 strcpy(info->fix.id, "vboxdrmfb");
349
350 /*
351 * The last flag forces a mode set on VT switches even if the kernel
352 * does not think it is needed.
353 */
354#if RTLNX_VER_MIN(6,6,0) || RTLNX_SUSE_MAJ_PREREQ(15, 6) || RTLNX_RHEL_RANGE(9,5, 9,99)
355 info->flags = FBINFO_MISC_ALWAYS_SETPAR;
356#else
357 info->flags = FBINFO_DEFAULT | FBINFO_MISC_ALWAYS_SETPAR;
358#endif
359 info->fbops = &vboxfb_ops;
360
361#if RTLNX_VER_MAX(6,3,0) && !RTLNX_RHEL_RANGE(8,9, 8,99) && !RTLNX_RHEL_RANGE(9,3, 9,99)
362 /*
363 * This seems to be done for safety checking that the framebuffer
364 * is not registered twice by different drivers.
365 */
366 info->apertures->ranges[0].base = pci_resource_start(VBOX_DRM_TO_PCI_DEV(dev), 0);
367 info->apertures->ranges[0].size = pci_resource_len(VBOX_DRM_TO_PCI_DEV(dev), 0);
368#endif
369
370#if RTLNX_VER_MIN(5,2,0) || RTLNX_RHEL_MAJ_PREREQ(8,2)
371 /*
372 * The corresponding 5.2-rc1 Linux DRM kernel changes have been
373 * also backported to older RedHat based 4.18.0 Linux kernels.
374 */
375 drm_fb_helper_fill_info(info, &fbdev->helper, sizes);
376#elif RTLNX_VER_MIN(4,11,0) || RTLNX_RHEL_MAJ_PREREQ(7, 5)
377 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
378#else
379 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
380#endif
381#if RTLNX_VER_MAX(5,2,0) && !RTLNX_RHEL_MAJ_PREREQ(8,2)
382 drm_fb_helper_fill_var(info, &fbdev->helper, sizes->fb_width,
383 sizes->fb_height);
384#endif
385
386#if RTLNX_VER_MIN(6,5,0)
387 info->screen_buffer = (char *)bo->kmap.virtual;
388 info->fix.smem_start = page_to_phys(vmalloc_to_page(bo->kmap.virtual));
389#endif
390 info->screen_base = (char __iomem *)bo->kmap.virtual;
391 info->screen_size = size;
392
393#ifdef CONFIG_FB_DEFERRED_IO
394# if RTLNX_VER_MIN(5,19,0) || RTLNX_RHEL_RANGE(8,8, 8,99) || RTLNX_RHEL_RANGE(9,3, 9,99) || RTLNX_SUSE_MAJ_PREREQ(15,5)
395 info->fix.smem_len = info->screen_size;
396# endif
397 info->fbdefio = &vbox_defio;
398# if RTLNX_VER_MIN(5,19,0)
399 ret = fb_deferred_io_init(info);
400 if (ret)
401 {
402 DRM_ERROR("failed to initialize deferred io: %d\n", ret);
403 return ret;
404 }
405# endif
406 fb_deferred_io_init(info);
407#endif
408
409 info->pixmap.flags = FB_PIXMAP_SYSTEM;
410
411 DRM_DEBUG_KMS("allocated %dx%d\n", fb->width, fb->height);
412
413 return 0;
414}
415
416static struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
417 .fb_probe = vboxfb_create,
418};
419
420#if RTLNX_VER_MAX(4,3,0) && !RTLNX_RHEL_MAJ_PREREQ(7,3)
421static void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
422{
423 if (fb_helper && fb_helper->fbdev)
424 unregister_framebuffer(fb_helper->fbdev);
425}
426#endif
427
428void vbox_fbdev_fini(struct drm_device *dev)
429{
430 struct vbox_private *vbox = dev->dev_private;
431 struct vbox_fbdev *fbdev = vbox->fbdev;
432 struct vbox_framebuffer *afb = &fbdev->afb;
433
434#ifdef CONFIG_FB_DEFERRED_IO
435 if (VBOX_FBDEV_INFO(fbdev->helper) && VBOX_FBDEV_INFO(fbdev->helper)->fbdefio)
436 fb_deferred_io_cleanup(VBOX_FBDEV_INFO(fbdev->helper));
437#endif
438
439#if RTLNX_VER_MIN(6,2,0) || RTLNX_RHEL_RANGE(8,9, 8,99) || RTLNX_RHEL_RANGE(9,3, 9,99)
440 drm_fb_helper_unregister_info(&fbdev->helper);
441#else
442 drm_fb_helper_unregister_fbi(&fbdev->helper);
443#endif
444
445 if (afb->obj) {
446 struct vbox_bo *bo = gem_to_vbox_bo(afb->obj);
447
448 if (!vbox_bo_reserve(bo, false)) {
449 if (bo->kmap.virtual)
450 ttm_bo_kunmap(&bo->kmap);
451 /*
452 * QXL does this, but is it really needed before
453 * freeing?
454 */
455 if (bo->pin_count)
456 vbox_bo_unpin(bo);
457 vbox_bo_unreserve(bo);
458 }
459#if RTLNX_VER_MIN(5,9,0) || RTLNX_RHEL_MIN(8,4) || RTLNX_SUSE_MAJ_PREREQ(15,3)
460 drm_gem_object_put(afb->obj);
461#else
462 drm_gem_object_put_unlocked(afb->obj);
463#endif
464 afb->obj = NULL;
465 }
466 drm_fb_helper_fini(&fbdev->helper);
467
468#if RTLNX_VER_MIN(3,9,0)
469 drm_framebuffer_unregister_private(&afb->base);
470#endif
471 drm_framebuffer_cleanup(&afb->base);
472}
473
474int vbox_fbdev_init(struct drm_device *dev)
475{
476 struct vbox_private *vbox = dev->dev_private;
477 struct vbox_fbdev *fbdev;
478 int ret = 0;
479
480 fbdev = devm_kzalloc(dev->dev, sizeof(*fbdev), GFP_KERNEL);
481 if (!fbdev)
482 return -ENOMEM;
483
484 vbox->fbdev = fbdev;
485 spin_lock_init(&fbdev->dirty_lock);
486
487#if RTLNX_VER_MIN(6,3,0) || RTLNX_RHEL_RANGE(8,9, 8,99) || RTLNX_RHEL_RANGE(9,3, 9,99)
488 drm_fb_helper_prepare(dev, &fbdev->helper, 32, &vbox_fb_helper_funcs);
489#elif RTLNX_VER_MIN(3,17,0) || RTLNX_RHEL_MIN(7,2)
490 drm_fb_helper_prepare(dev, &fbdev->helper, &vbox_fb_helper_funcs);
491#else
492 fbdev->helper.funcs = &vbox_fb_helper_funcs;
493#endif
494
495#if RTLNX_VER_MIN(5,7,0) || RTLNX_RHEL_MIN(8,4) || RTLNX_SUSE_MAJ_PREREQ(15,3)
496 ret = drm_fb_helper_init(dev, &fbdev->helper);
497#elif RTLNX_VER_MIN(4,11,0) || RTLNX_RHEL_MAJ_PREREQ(7,5)
498 ret = drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs);
499#else /* < 4.11.0 */
500 ret =
501 drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs,
502 vbox->num_crtcs);
503#endif
504 if (ret)
505 return ret;
506
507#if RTLNX_VER_MAX(5,7,0) && !RTLNX_RHEL_MAJ_PREREQ(8,4) && !RTLNX_SUSE_MAJ_PREREQ(15,3)
508 ret = drm_fb_helper_single_add_all_connectors(&fbdev->helper);
509 if (ret)
510 goto err_fini;
511#endif
512
513 /* disable all the possible outputs/crtcs before entering KMS mode */
514 drm_helper_disable_unused_functions(dev);
515
516#if RTLNX_VER_MIN(6,3,0) || RTLNX_RHEL_RANGE(8,9, 8,99) || RTLNX_RHEL_RANGE(9,3, 9,99)
517 ret = drm_fb_helper_initial_config(&fbdev->helper);
518#else
519 ret = drm_fb_helper_initial_config(&fbdev->helper, 32);
520#endif
521 if (ret)
522 goto err_fini;
523
524 return 0;
525
526err_fini:
527 drm_fb_helper_fini(&fbdev->helper);
528 return ret;
529}
530
531void vbox_fbdev_set_base(struct vbox_private *vbox, unsigned long gpu_addr)
532{
533 struct fb_info *fbdev = VBOX_FBDEV_INFO(vbox->fbdev->helper);
534
535#if RTLNX_VER_MIN(6,3,0) || RTLNX_RHEL_RANGE(8,9, 8,99) || RTLNX_RHEL_RANGE(9,3, 9,99)
536 fbdev->fix.smem_start = pci_resource_start(VBOX_DRM_TO_PCI_DEV(vbox->fbdev->helper.dev), 0) + gpu_addr;
537#else
538 fbdev->fix.smem_start = fbdev->apertures->ranges[0].base + gpu_addr;
539#endif
540 fbdev->fix.smem_len = vbox->available_vram_size - gpu_addr;
541}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette