VirtualBox

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

最後變更 在這個檔案從85196是 83109,由 vboxsync 提交於 5 年 前

GA/Linux: ticketref:19145 kernel 5.5-rc1/rc2 - we need changes part 2 fix includes

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

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