VirtualBox

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

Last change on this file since 100838 was 100800, checked in by vboxsync, 21 months ago

Additions: Linux: vboxvideo: Introduce initial support for kernel 6.5, bugref:10482.

  • 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 100800 2023-08-04 18:12:46Z vboxsync $ */
2/** @file
3 * VirtualBox Additions Linux kernel video driver
4 */
5
6/*
7 * Copyright (C) 2013-2023 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_MAJ_PREREQ(9,3)
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)
200 FB_DEFAULT_SYS_OPS,
201#else
202 .fb_fillrect = drm_fb_helper_sys_fillrect,
203 .fb_copyarea = drm_fb_helper_sys_copyarea,
204 .fb_imageblit = drm_fb_helper_sys_imageblit,
205#endif
206 .fb_pan_display = drm_fb_helper_pan_display,
207 .fb_blank = drm_fb_helper_blank,
208 .fb_setcmap = drm_fb_helper_setcmap,
209 .fb_debug_enter = drm_fb_helper_debug_enter,
210 .fb_debug_leave = drm_fb_helper_debug_leave,
211};
212
213static int vboxfb_create_object(struct vbox_fbdev *fbdev,
214 struct DRM_MODE_FB_CMD *mode_cmd,
215 struct drm_gem_object **gobj_p)
216{
217 struct drm_device *dev = fbdev->helper.dev;
218 u32 size;
219 struct drm_gem_object *gobj;
220#if RTLNX_VER_MAX(3,3,0)
221 u32 pitch = mode_cmd->pitch;
222#else
223 u32 pitch = mode_cmd->pitches[0];
224#endif
225
226 int ret;
227
228 size = pitch * mode_cmd->height;
229 ret = vbox_gem_create(dev, size, true, &gobj);
230 if (ret)
231 return ret;
232
233 *gobj_p = gobj;
234
235 return 0;
236}
237
238#if RTLNX_VER_MAX(4,3,0) && !RTLNX_RHEL_MAJ_PREREQ(7,3)
239static struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *helper)
240{
241 struct fb_info *info;
242 struct vbox_fbdev *fbdev =
243 container_of(helper, struct vbox_fbdev, helper);
244 struct drm_device *dev = fbdev->helper.dev;
245 struct device *device = &dev->pdev->dev;
246
247 info = framebuffer_alloc(0, device);
248 if (!info)
249 return ERR_PTR(-ENOMEM);
250 fbdev->helper.fbdev = info;
251
252 if (fb_alloc_cmap(&info->cmap, 256, 0))
253 return ERR_PTR(-ENOMEM);
254
255 info->apertures = alloc_apertures(1);
256 if (!info->apertures)
257 return ERR_PTR(-ENOMEM);
258
259 return info;
260}
261#endif
262
263static int vboxfb_create(struct drm_fb_helper *helper,
264 struct drm_fb_helper_surface_size *sizes)
265{
266 struct vbox_fbdev *fbdev =
267 container_of(helper, struct vbox_fbdev, helper);
268 struct drm_device *dev = fbdev->helper.dev;
269 struct DRM_MODE_FB_CMD mode_cmd;
270 struct drm_framebuffer *fb;
271 struct fb_info *info;
272 struct drm_gem_object *gobj;
273 struct vbox_bo *bo;
274 int size, ret;
275 u32 pitch;
276
277 mode_cmd.width = sizes->surface_width;
278 mode_cmd.height = sizes->surface_height;
279 pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
280#if RTLNX_VER_MAX(3,3,0)
281 mode_cmd.bpp = sizes->surface_bpp;
282 mode_cmd.depth = sizes->surface_depth;
283 mode_cmd.pitch = pitch;
284#else
285 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
286 sizes->surface_depth);
287 mode_cmd.pitches[0] = pitch;
288#endif
289
290 size = pitch * mode_cmd.height;
291
292 ret = vboxfb_create_object(fbdev, &mode_cmd, &gobj);
293 if (ret) {
294 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
295 return ret;
296 }
297
298 ret = vbox_framebuffer_init(dev, &fbdev->afb, &mode_cmd, gobj);
299 if (ret)
300 return ret;
301
302 bo = gem_to_vbox_bo(gobj);
303
304 ret = vbox_bo_reserve(bo, false);
305 if (ret)
306 return ret;
307
308 ret = vbox_bo_pin(bo, VBOX_MEM_TYPE_VRAM, NULL);
309 if (ret) {
310 vbox_bo_unreserve(bo);
311 return ret;
312 }
313
314#if RTLNX_VER_MIN(5,14,0) || RTLNX_RHEL_RANGE(8,6, 8,99)
315 ret = ttm_bo_kmap(&bo->bo, 0, VBOX_BO_RESOURCE_NUM_PAGES(bo->bo.resource), &bo->kmap);
316#elif RTLNX_VER_MIN(5,12,0) || RTLNX_RHEL_MAJ_PREREQ(8,5)
317 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.mem.num_pages, &bo->kmap);
318#else
319 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
320#endif
321
322 vbox_bo_unreserve(bo);
323 if (ret) {
324 DRM_ERROR("failed to kmap fbcon\n");
325 return ret;
326 }
327
328#if RTLNX_VER_MIN(6,2,0) || RTLNX_RHEL_MAJ_PREREQ(9,3)
329 info = drm_fb_helper_alloc_info(helper);
330#else
331 info = drm_fb_helper_alloc_fbi(helper);
332#endif
333 if (IS_ERR(info))
334 return -PTR_ERR(info);
335
336 info->par = fbdev;
337
338 fbdev->size = size;
339
340 fb = &fbdev->afb.base;
341 fbdev->helper.fb = fb;
342
343 strcpy(info->fix.id, "vboxdrmfb");
344
345 /*
346 * The last flag forces a mode set on VT switches even if the kernel
347 * does not think it is needed.
348 */
349 info->flags = FBINFO_DEFAULT | FBINFO_MISC_ALWAYS_SETPAR;
350 info->fbops = &vboxfb_ops;
351
352#if RTLNX_VER_MAX(6,3,0) && !RTLNX_RHEL_MAJ_PREREQ(9,3)
353 /*
354 * This seems to be done for safety checking that the framebuffer
355 * is not registered twice by different drivers.
356 */
357 info->apertures->ranges[0].base = pci_resource_start(VBOX_DRM_TO_PCI_DEV(dev), 0);
358 info->apertures->ranges[0].size = pci_resource_len(VBOX_DRM_TO_PCI_DEV(dev), 0);
359#endif
360
361#if RTLNX_VER_MIN(5,2,0) || RTLNX_RHEL_MAJ_PREREQ(8,2)
362 /*
363 * The corresponding 5.2-rc1 Linux DRM kernel changes have been
364 * also backported to older RedHat based 4.18.0 Linux kernels.
365 */
366 drm_fb_helper_fill_info(info, &fbdev->helper, sizes);
367#elif RTLNX_VER_MIN(4,11,0) || RTLNX_RHEL_MAJ_PREREQ(7, 5)
368 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
369#else
370 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
371#endif
372#if RTLNX_VER_MAX(5,2,0) && !RTLNX_RHEL_MAJ_PREREQ(8,2)
373 drm_fb_helper_fill_var(info, &fbdev->helper, sizes->fb_width,
374 sizes->fb_height);
375#endif
376
377#if RTLNX_VER_MIN(6,5,0)
378 info->screen_buffer = (char *)bo->kmap.virtual;
379 info->fix.smem_start = page_to_phys(vmalloc_to_page(bo->kmap.virtual));
380#endif
381 info->screen_base = (char __iomem *)bo->kmap.virtual;
382 info->screen_size = size;
383
384#ifdef CONFIG_FB_DEFERRED_IO
385# if RTLNX_VER_MIN(5,19,0) || RTLNX_RHEL_RANGE(8,8, 8,99) || RTLNX_RHEL_MAJ_PREREQ(9,3) || RTLNX_SUSE_MAJ_PREREQ(15,5)
386 info->fix.smem_len = info->screen_size;
387# endif
388 info->fbdefio = &vbox_defio;
389# if RTLNX_VER_MIN(5,19,0)
390 ret = fb_deferred_io_init(info);
391 if (ret)
392 {
393 DRM_ERROR("failed to initialize deferred io: %d\n", ret);
394 return ret;
395 }
396# endif
397 fb_deferred_io_init(info);
398#endif
399
400 info->pixmap.flags = FB_PIXMAP_SYSTEM;
401
402 DRM_DEBUG_KMS("allocated %dx%d\n", fb->width, fb->height);
403
404 return 0;
405}
406
407static struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
408 .fb_probe = vboxfb_create,
409};
410
411#if RTLNX_VER_MAX(4,3,0) && !RTLNX_RHEL_MAJ_PREREQ(7,3)
412static void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
413{
414 if (fb_helper && fb_helper->fbdev)
415 unregister_framebuffer(fb_helper->fbdev);
416}
417#endif
418
419void vbox_fbdev_fini(struct drm_device *dev)
420{
421 struct vbox_private *vbox = dev->dev_private;
422 struct vbox_fbdev *fbdev = vbox->fbdev;
423 struct vbox_framebuffer *afb = &fbdev->afb;
424
425#ifdef CONFIG_FB_DEFERRED_IO
426 if (VBOX_FBDEV_INFO(fbdev->helper) && VBOX_FBDEV_INFO(fbdev->helper)->fbdefio)
427 fb_deferred_io_cleanup(VBOX_FBDEV_INFO(fbdev->helper));
428#endif
429
430#if RTLNX_VER_MIN(6,2,0) || RTLNX_RHEL_MAJ_PREREQ(9,3)
431 drm_fb_helper_unregister_info(&fbdev->helper);
432#else
433 drm_fb_helper_unregister_fbi(&fbdev->helper);
434#endif
435
436 if (afb->obj) {
437 struct vbox_bo *bo = gem_to_vbox_bo(afb->obj);
438
439 if (!vbox_bo_reserve(bo, false)) {
440 if (bo->kmap.virtual)
441 ttm_bo_kunmap(&bo->kmap);
442 /*
443 * QXL does this, but is it really needed before
444 * freeing?
445 */
446 if (bo->pin_count)
447 vbox_bo_unpin(bo);
448 vbox_bo_unreserve(bo);
449 }
450#if RTLNX_VER_MIN(5,9,0) || RTLNX_RHEL_MIN(8,4) || RTLNX_SUSE_MAJ_PREREQ(15,3)
451 drm_gem_object_put(afb->obj);
452#else
453 drm_gem_object_put_unlocked(afb->obj);
454#endif
455 afb->obj = NULL;
456 }
457 drm_fb_helper_fini(&fbdev->helper);
458
459#if RTLNX_VER_MIN(3,9,0)
460 drm_framebuffer_unregister_private(&afb->base);
461#endif
462 drm_framebuffer_cleanup(&afb->base);
463}
464
465int vbox_fbdev_init(struct drm_device *dev)
466{
467 struct vbox_private *vbox = dev->dev_private;
468 struct vbox_fbdev *fbdev;
469 int ret = 0;
470
471 fbdev = devm_kzalloc(dev->dev, sizeof(*fbdev), GFP_KERNEL);
472 if (!fbdev)
473 return -ENOMEM;
474
475 vbox->fbdev = fbdev;
476 spin_lock_init(&fbdev->dirty_lock);
477
478#if RTLNX_VER_MIN(6,3,0) || RTLNX_RHEL_MAJ_PREREQ(9,3)
479 drm_fb_helper_prepare(dev, &fbdev->helper, 32, &vbox_fb_helper_funcs);
480#elif RTLNX_VER_MIN(3,17,0) || RTLNX_RHEL_MIN(7,2)
481 drm_fb_helper_prepare(dev, &fbdev->helper, &vbox_fb_helper_funcs);
482#else
483 fbdev->helper.funcs = &vbox_fb_helper_funcs;
484#endif
485
486#if RTLNX_VER_MIN(5,7,0) || RTLNX_RHEL_MIN(8,4) || RTLNX_SUSE_MAJ_PREREQ(15,3)
487 ret = drm_fb_helper_init(dev, &fbdev->helper);
488#elif RTLNX_VER_MIN(4,11,0) || RTLNX_RHEL_MAJ_PREREQ(7,5)
489 ret = drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs);
490#else /* < 4.11.0 */
491 ret =
492 drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs,
493 vbox->num_crtcs);
494#endif
495 if (ret)
496 return ret;
497
498#if RTLNX_VER_MAX(5,7,0) && !RTLNX_RHEL_MAJ_PREREQ(8,4) && !RTLNX_SUSE_MAJ_PREREQ(15,3)
499 ret = drm_fb_helper_single_add_all_connectors(&fbdev->helper);
500 if (ret)
501 goto err_fini;
502#endif
503
504 /* disable all the possible outputs/crtcs before entering KMS mode */
505 drm_helper_disable_unused_functions(dev);
506
507#if RTLNX_VER_MIN(6,3,0) || RTLNX_RHEL_MAJ_PREREQ(9,3)
508 ret = drm_fb_helper_initial_config(&fbdev->helper);
509#else
510 ret = drm_fb_helper_initial_config(&fbdev->helper, 32);
511#endif
512 if (ret)
513 goto err_fini;
514
515 return 0;
516
517err_fini:
518 drm_fb_helper_fini(&fbdev->helper);
519 return ret;
520}
521
522void vbox_fbdev_set_base(struct vbox_private *vbox, unsigned long gpu_addr)
523{
524 struct fb_info *fbdev = VBOX_FBDEV_INFO(vbox->fbdev->helper);
525
526#if RTLNX_VER_MIN(6,3,0) || RTLNX_RHEL_MAJ_PREREQ(9,3)
527 fbdev->fix.smem_start = pci_resource_start(VBOX_DRM_TO_PCI_DEV(vbox->fbdev->helper.dev), 0) + gpu_addr;
528#else
529 fbdev->fix.smem_start = fbdev->apertures->ranges[0].base + gpu_addr;
530#endif
531 fbdev->fix.smem_len = vbox->available_vram_size - gpu_addr;
532}
Note: See TracBrowser for help on using the repository browser.

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