VirtualBox

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

Last change on this file since 98870 was 98870, checked in by vboxsync, 2 years ago

Additions: Linux: vboxvideo: Introduce initial support for kernel 6.3.x series (scm fix), bugref:10381.

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