VirtualBox

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

最後變更 在這個檔案從88357是 88274,由 vboxsync 提交於 4 年 前

GAs: Linux: make vboxvideo work w/ 5.12-rc4 kernel, bugref:9976.

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

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