VirtualBox

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

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

Additions: Linux: vboxvideo: Print message to dmesg advising to switch to VMSVGA graphics for newer kernels (move to .probe), bugref:10279.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 13.0 KB
 
1/* $Id: vbox_drv.c 108379 2025-02-25 15:44:10Z 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_drv.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 * Hans de Goede <[email protected]>
34 */
35#include <linux/module.h>
36#include <linux/console.h>
37#include <linux/vt_kern.h>
38
39#include "vbox_drv.h"
40
41#include <drm/drm_crtc_helper.h>
42#if RTLNX_VER_MIN(5,1,0) || RTLNX_RHEL_MAJ_PREREQ(8,1)
43# include <drm/drm_probe_helper.h>
44#endif
45
46#if RTLNX_VER_RANGE(5,14,0, 6,13,0) || RTLNX_RHEL_RANGE(8,6, 8,99)
47# include <drm/drm_aperture.h>
48#endif
49
50#include "version-generated.h"
51#include "revision-generated.h"
52
53/** Detect whether kernel mode setting is OFF. */
54#if defined(CONFIG_VGA_CONSOLE)
55# if RTLNX_VER_MIN(5,17,0) || RTLNX_RHEL_RANGE(8,7, 8,99) || RTLNX_RHEL_MIN(9,1) || RTLNX_SUSE_MAJ_PREREQ(15,5)
56# define VBOX_VIDEO_NOMODESET() drm_firmware_drivers_only() && vbox_modeset == -1
57# elif RTLNX_VER_MIN(4,7,0)
58# define VBOX_VIDEO_NOMODESET() vgacon_text_force() && vbox_modeset == -1
59# else /* < 4.7.0 */
60# define VBOX_VIDEO_NOMODESET() 0
61# endif /* < 4.7.0 */
62#else /* !CONFIG_VGA_CONSOLE */
63# define VBOX_VIDEO_NOMODESET() 0
64#endif /* !CONFIG_VGA_CONSOLE */
65
66#include <VBox/VBoxLnxModInline.h>
67
68static int vbox_modeset = -1;
69
70MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
71module_param_named(modeset, vbox_modeset, int, 0400);
72
73static struct drm_driver driver;
74
75static const struct pci_device_id pciidlist[] = {
76 { 0x80ee, 0xbeef, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
77 { 0, 0, 0},
78};
79MODULE_DEVICE_TABLE(pci, pciidlist);
80
81static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
82{
83#if RTLNX_VER_MIN(4,19,0) || RTLNX_RHEL_MIN(8,3)
84 struct drm_device *dev = NULL;
85 int ret = 0;
86
87#if RTLNX_VER_MIN(6,0,0)
88 static bool fWarned = false;
89 if (!fWarned)
90 {
91 printk(KERN_ERR "vboxvideo: VM is using legacy graphics controller, "
92 "please consider to configure this guest to use VMSVGA instead\n");
93 fWarned = true;
94 }
95#endif
96
97# if RTLNX_VER_RANGE(5,14,0, 6,13,0) || RTLNX_RHEL_RANGE(8,6, 8,99)
98# if RTLNX_VER_MIN(5,15,0) || RTLNX_RHEL_RANGE(8,7, 8,99) || RTLNX_RHEL_MIN(9,1) || RTLNX_SUSE_MAJ_PREREQ(15,4)
99 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
100# else
101 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, "vboxvideofb");
102# endif
103 if (ret)
104 {
105 printk("unable to remove conflicting framebuffer devices\n");
106 return ret;
107 }
108# endif /* >= 5.14. */
109
110 dev = drm_dev_alloc(&driver, &pdev->dev);
111 if (IS_ERR(dev)) {
112 ret = PTR_ERR(dev);
113 goto err_drv_alloc;
114 }
115# if RTLNX_VER_MAX(5,14,0) && !RTLNX_RHEL_RANGE(8,6, 8,99)
116 dev->pdev = pdev;
117# endif
118 pci_set_drvdata(pdev, dev);
119
120 ret = vbox_driver_load(dev);
121 if (ret)
122 goto err_vbox_driver_load;
123
124 ret = drm_dev_register(dev, 0);
125 if (ret)
126 goto err_drv_dev_register;
127 return ret;
128
129err_drv_dev_register:
130 vbox_driver_unload(dev);
131err_vbox_driver_load:
132 drm_dev_put(dev);
133err_drv_alloc:
134 return ret;
135#else /* < 4.19.0 || RHEL < 8.3 */
136 return drm_get_pci_dev(pdev, ent, &driver);
137#endif
138}
139
140static void vbox_pci_remove(struct pci_dev *pdev)
141{
142 struct drm_device *dev = pci_get_drvdata(pdev);
143
144#if RTLNX_VER_MAX(4,19,0)
145 drm_put_dev(dev);
146#else
147 drm_dev_unregister(dev);
148 vbox_driver_unload(dev);
149 drm_dev_put(dev);
150#endif
151}
152
153#if RTLNX_VER_MAX(4,9,0) && !RTLNX_RHEL_MAJ_PREREQ(7,4)
154static void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
155 bool suspend)
156{
157 if (!fb_helper || !fb_helper->fbdev)
158 return;
159
160 console_lock();
161 fb_set_suspend(fb_helper->fbdev, suspend);
162 console_unlock();
163}
164#endif
165
166static int vbox_drm_freeze(struct drm_device *dev)
167{
168 struct vbox_private *vbox = dev->dev_private;
169
170 drm_kms_helper_poll_disable(dev);
171
172 pci_save_state(VBOX_DRM_TO_PCI_DEV(dev));
173
174 drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, true);
175
176 return 0;
177}
178
179static int vbox_drm_thaw(struct drm_device *dev)
180{
181 struct vbox_private *vbox = dev->dev_private;
182
183 drm_mode_config_reset(dev);
184 drm_helper_resume_force_mode(dev);
185 drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, false);
186
187 return 0;
188}
189
190static int vbox_drm_resume(struct drm_device *dev)
191{
192 int ret;
193
194 if (pci_enable_device(VBOX_DRM_TO_PCI_DEV(dev)))
195 return -EIO;
196
197 ret = vbox_drm_thaw(dev);
198 if (ret)
199 return ret;
200
201 drm_kms_helper_poll_enable(dev);
202
203 return 0;
204}
205
206static int vbox_pm_suspend(struct device *dev)
207{
208 struct pci_dev *pdev = to_pci_dev(dev);
209 struct drm_device *ddev = pci_get_drvdata(pdev);
210 int error;
211
212 error = vbox_drm_freeze(ddev);
213 if (error)
214 return error;
215
216 pci_disable_device(pdev);
217 pci_set_power_state(pdev, PCI_D3hot);
218
219 return 0;
220}
221
222static int vbox_pm_resume(struct device *dev)
223{
224 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
225
226 return vbox_drm_resume(ddev);
227}
228
229static int vbox_pm_freeze(struct device *dev)
230{
231 struct pci_dev *pdev = to_pci_dev(dev);
232 struct drm_device *ddev = pci_get_drvdata(pdev);
233
234 if (!ddev || !ddev->dev_private)
235 return -ENODEV;
236
237 return vbox_drm_freeze(ddev);
238}
239
240static int vbox_pm_thaw(struct device *dev)
241{
242 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
243
244 return vbox_drm_thaw(ddev);
245}
246
247static int vbox_pm_poweroff(struct device *dev)
248{
249 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
250
251 return vbox_drm_freeze(ddev);
252}
253
254static const struct dev_pm_ops vbox_pm_ops = {
255 .suspend = vbox_pm_suspend,
256 .resume = vbox_pm_resume,
257 .freeze = vbox_pm_freeze,
258 .thaw = vbox_pm_thaw,
259 .poweroff = vbox_pm_poweroff,
260 .restore = vbox_pm_resume,
261};
262
263static struct pci_driver vbox_pci_driver = {
264 .name = DRIVER_NAME,
265 .id_table = pciidlist,
266 .probe = vbox_pci_probe,
267 .remove = vbox_pci_remove,
268 .driver.pm = &vbox_pm_ops,
269};
270
271#if RTLNX_VER_MAX(4,7,0) && !RTLNX_RHEL_MAJ_PREREQ(7,4)
272/* This works around a bug in X servers prior to 1.18.4, which sometimes
273 * submit more dirty rectangles than the kernel is willing to handle and
274 * then disable dirty rectangle handling altogether when they see the
275 * EINVAL error. I do not want the code to hang around forever, which is
276 * why I am limiting it to certain kernel versions. We can increase the
277 * limit if some distributions uses old X servers with new kernels. */
278long vbox_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
279{
280 long rc = drm_ioctl(filp, cmd, arg);
281
282 if (cmd == DRM_IOCTL_MODE_DIRTYFB && rc == -EINVAL)
283 return -EOVERFLOW;
284
285 return rc;
286}
287#endif /* RTLNX_VER_MAX(4,7,0) && !RTLNX_RHEL_MAJ_PREREQ(7,4) */
288
289static const struct file_operations vbox_fops = {
290 .owner = THIS_MODULE,
291 .open = drm_open,
292 .release = drm_release,
293#if RTLNX_VER_MAX(4,7,0) && !RTLNX_RHEL_MAJ_PREREQ(7,4)
294 .unlocked_ioctl = vbox_ioctl,
295#else
296 .unlocked_ioctl = drm_ioctl,
297#endif
298 .mmap = vbox_mmap,
299 .poll = drm_poll,
300#if RTLNX_VER_MAX(3,12,0) && !RTLNX_RHEL_MAJ_PREREQ(7,0)
301 .fasync = drm_fasync,
302#endif
303#ifdef CONFIG_COMPAT
304 .compat_ioctl = drm_compat_ioctl,
305#endif
306 .read = drm_read,
307#if RTLNX_VER_MIN(6,12,0)
308 .fop_flags = FOP_UNSIGNED_OFFSET,
309#endif
310};
311
312#if RTLNX_VER_MIN(5,9,0) || RTLNX_RHEL_MIN(8,4) || RTLNX_SUSE_MAJ_PREREQ(15,3)
313static void
314#else
315static int
316#endif
317vbox_master_set(struct drm_device *dev,
318 struct drm_file *file_priv, bool from_open)
319{
320 struct vbox_private *vbox = dev->dev_private;
321
322 /*
323 * We do not yet know whether the new owner can handle hotplug, so we
324 * do not advertise dynamic modes on the first query and send a
325 * tentative hotplug notification after that to see if they query again.
326 */
327 vbox->initial_mode_queried = false;
328
329 mutex_lock(&vbox->hw_mutex);
330 /* Start the refresh timer in case the user does not provide dirty
331 * rectangles. */
332 vbox->need_refresh_timer = true;
333 schedule_delayed_work(&vbox->refresh_work, VBOX_REFRESH_PERIOD);
334 mutex_unlock(&vbox->hw_mutex);
335
336#if RTLNX_VER_MAX(5,9,0) && !RTLNX_RHEL_MAJ_PREREQ(8,4) && !RTLNX_SUSE_MAJ_PREREQ(15,3)
337 return 0;
338#endif
339}
340
341#if RTLNX_VER_MAX(4,8,0) && !RTLNX_RHEL_MAJ_PREREQ(7,4)
342static void vbox_master_drop(struct drm_device *dev,
343 struct drm_file *file_priv, bool from_release)
344#else
345static void vbox_master_drop(struct drm_device *dev, struct drm_file *file_priv)
346#endif
347{
348 struct vbox_private *vbox = dev->dev_private;
349
350 /* See vbox_master_set() */
351 vbox->initial_mode_queried = false;
352 vbox_report_caps(vbox);
353
354 mutex_lock(&vbox->hw_mutex);
355 vbox->need_refresh_timer = false;
356 mutex_unlock(&vbox->hw_mutex);
357}
358
359static struct drm_driver driver = {
360#if RTLNX_VER_MAX(5,4,0) && !RTLNX_RHEL_MAJ_PREREQ(8,3) && !RTLNX_SUSE_MAJ_PREREQ(15,3)
361 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_HAVE_IRQ |
362# if RTLNX_VER_MAX(5,1,0) && !RTLNX_RHEL_MAJ_PREREQ(8,1)
363 DRIVER_IRQ_SHARED |
364# endif
365 DRIVER_PRIME,
366#else /* >= 5.4.0 && RHEL >= 8.3 && SLES >= 15-SP3 */
367 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_HAVE_IRQ,
368#endif /* < 5.4.0 */
369
370#if RTLNX_VER_MAX(4,19,0) && !RTLNX_RHEL_MAJ_PREREQ(8,3)
371 /* Legacy hooks, but still supported. */
372 .load = vbox_driver_load,
373 .unload = vbox_driver_unload,
374#endif
375#if RTLNX_VER_MAX(6,12,0)
376 .lastclose = vbox_driver_lastclose,
377#endif
378 .master_set = vbox_master_set,
379 .master_drop = vbox_master_drop,
380#if RTLNX_VER_MIN(3,18,0) || RTLNX_RHEL_MAJ_PREREQ(7,2)
381# if RTLNX_VER_MAX(4,14,0) && !RTLNX_RHEL_MAJ_PREREQ(7,5) && !RTLNX_SUSE_MAJ_PREREQ(15,1) && !RTLNX_SUSE_MAJ_PREREQ(12,5)
382 .set_busid = drm_pci_set_busid,
383# endif
384#endif
385
386 .fops = &vbox_fops,
387#if RTLNX_VER_MAX(5,15,0) && !RTLNX_RHEL_RANGE(8,7, 8,99) && !RTLNX_RHEL_MAJ_PREREQ(9,1) && !RTLNX_SUSE_MAJ_PREREQ(15,5)
388 .irq_handler = vbox_irq_handler,
389#endif
390 .name = DRIVER_NAME,
391 .desc = DRIVER_DESC,
392#if RTLNX_VER_MAX(6,14,0)
393 .date = DRIVER_DATE,
394#endif
395 .major = DRIVER_MAJOR,
396 .minor = DRIVER_MINOR,
397 .patchlevel = DRIVER_PATCHLEVEL,
398
399#if RTLNX_VER_MAX(4,7,0)
400 .gem_free_object = vbox_gem_free_object,
401#endif
402 .dumb_create = vbox_dumb_create,
403 .dumb_map_offset = vbox_dumb_mmap_offset,
404#if RTLNX_VER_MAX(3,12,0) && !RTLNX_RHEL_MAJ_PREREQ(7,3)
405 .dumb_destroy = vbox_dumb_destroy,
406#elif RTLNX_VER_MAX(5,12,0) && !RTLNX_RHEL_MAJ_PREREQ(8,5)
407 .dumb_destroy = drm_gem_dumb_destroy,
408#endif
409#if RTLNX_VER_MAX(6,6,0) && !RTLNX_RHEL_RANGE(9,4, 9,99)
410 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
411 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
412#endif
413 .gem_prime_import = drm_gem_prime_import,
414 .gem_prime_import_sg_table = vbox_gem_prime_import_sg_table,
415#if RTLNX_VER_MAX(6,6,0) && !RTLNX_RHEL_RANGE(9,4, 9,99) && !RTLNX_SUSE_MAJ_PREREQ(15, 6)
416 .gem_prime_mmap = vbox_gem_prime_mmap,
417#endif
418
419#if RTLNX_VER_MAX(5,11,0) && !RTLNX_RHEL_MAJ_PREREQ(8,5)
420 .dev_priv_size = 0,
421# if RTLNX_VER_MIN(4,7,0)
422 .gem_free_object_unlocked = vbox_gem_free_object,
423# endif
424 .gem_prime_export = drm_gem_prime_export,
425 .gem_prime_pin = vbox_gem_prime_pin,
426 .gem_prime_unpin = vbox_gem_prime_unpin,
427 .gem_prime_get_sg_table = vbox_gem_prime_get_sg_table,
428 .gem_prime_vmap = vbox_gem_prime_vmap,
429 .gem_prime_vunmap = vbox_gem_prime_vunmap,
430#endif
431};
432
433static int __init vbox_init(void)
434{
435 /* Check if modue loading was disabled. */
436 if (!vbox_mod_should_load())
437 return -EINVAL;
438
439 printk("vboxvideo: loading version " VBOX_VERSION_STRING " r" __stringify(VBOX_SVN_REV) "\n");
440 if (VBOX_VIDEO_NOMODESET())
441 {
442 printk("vboxvideo: kernel is running with *nomodeset* parameter,\n");
443 printk("vboxvideo: please consider either to remove it or load driver\n");
444 printk("vboxvideo: with parameter modeset=1, unloading\n");
445 return -EINVAL;
446 }
447
448 if (vbox_modeset == 0)
449 {
450 printk("vboxvideo: driver loaded with modeset=0 parameter, unloading\n");
451 return -EINVAL;
452 }
453
454#if RTLNX_VER_MIN(3,18,0) || RTLNX_RHEL_MAJ_PREREQ(7,3)
455 return pci_register_driver(&vbox_pci_driver);
456#else
457 return drm_pci_init(&driver, &vbox_pci_driver);
458#endif
459}
460
461static void __exit vbox_exit(void)
462{
463#if RTLNX_VER_MIN(3,18,0) || RTLNX_RHEL_MAJ_PREREQ(7,3)
464 pci_unregister_driver(&vbox_pci_driver);
465#else
466 drm_pci_exit(&driver, &vbox_pci_driver);
467#endif
468}
469
470module_init(vbox_init);
471module_exit(vbox_exit);
472
473MODULE_AUTHOR(DRIVER_AUTHOR);
474MODULE_DESCRIPTION(DRIVER_DESC);
475MODULE_LICENSE("GPL and additional rights");
476#ifdef MODULE_VERSION
477MODULE_VERSION(VBOX_VERSION_STRING " r" __stringify(VBOX_SVN_REV));
478#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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