VirtualBox

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

Last change on this file since 93228 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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