1 | /* $Id: vbox_drv.c 63807 2016-09-13 08:19:12Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Additions Linux kernel video driver
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | * --------------------------------------------------------------------
|
---|
17 | *
|
---|
18 | * This code is based on
|
---|
19 | * ast_drv.c
|
---|
20 | * with the following copyright and permission notice:
|
---|
21 | *
|
---|
22 | * Copyright 2012 Red Hat Inc.
|
---|
23 | *
|
---|
24 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
25 | * copy of this software and associated documentation files (the
|
---|
26 | * "Software"), to deal in the Software without restriction, including
|
---|
27 | * without limitation the rights to use, copy, modify, merge, publish,
|
---|
28 | * distribute, sub license, and/or sell copies of the Software, and to
|
---|
29 | * permit persons to whom the Software is furnished to do so, subject to
|
---|
30 | * the following conditions:
|
---|
31 | *
|
---|
32 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
33 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
34 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
---|
35 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
---|
36 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
---|
37 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
---|
38 | * USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
39 | *
|
---|
40 | * The above copyright notice and this permission notice (including the
|
---|
41 | * next paragraph) shall be included in all copies or substantial portions
|
---|
42 | * of the Software.
|
---|
43 | *
|
---|
44 | */
|
---|
45 | /*
|
---|
46 | * Authors: Dave Airlie <[email protected]>
|
---|
47 | */
|
---|
48 | #include "vbox_drv.h"
|
---|
49 |
|
---|
50 | #include <VBox/VBoxGuest.h>
|
---|
51 | #include "version-generated.h"
|
---|
52 | #include "revision-generated.h"
|
---|
53 |
|
---|
54 | #include <linux/module.h>
|
---|
55 | #include <linux/console.h>
|
---|
56 | #include <linux/vt_kern.h>
|
---|
57 |
|
---|
58 | #include <drm/drmP.h>
|
---|
59 | #include <drm/drm_crtc_helper.h>
|
---|
60 |
|
---|
61 | int vbox_modeset = -1;
|
---|
62 |
|
---|
63 | MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
|
---|
64 | module_param_named(modeset, vbox_modeset, int, 0400);
|
---|
65 |
|
---|
66 | static struct drm_driver driver;
|
---|
67 |
|
---|
68 | static const struct pci_device_id pciidlist[] =
|
---|
69 | {
|
---|
70 | {0x80ee, 0xbeef, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
|
---|
71 | {0, 0, 0},
|
---|
72 | };
|
---|
73 |
|
---|
74 | MODULE_DEVICE_TABLE(pci, pciidlist);
|
---|
75 |
|
---|
76 | static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
|
---|
77 | {
|
---|
78 | return drm_get_pci_dev(pdev, ent, &driver);
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | static void vbox_pci_remove(struct pci_dev *pdev)
|
---|
83 | {
|
---|
84 | struct drm_device *dev = pci_get_drvdata(pdev);
|
---|
85 |
|
---|
86 | drm_put_dev(dev);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | static int vbox_drm_freeze(struct drm_device *dev)
|
---|
92 | {
|
---|
93 | drm_kms_helper_poll_disable(dev);
|
---|
94 |
|
---|
95 | pci_save_state(dev->pdev);
|
---|
96 |
|
---|
97 | console_lock();
|
---|
98 | vbox_fbdev_set_suspend(dev, 1);
|
---|
99 | console_unlock();
|
---|
100 | return 0;
|
---|
101 | }
|
---|
102 |
|
---|
103 | static int vbox_drm_thaw(struct drm_device *dev)
|
---|
104 | {
|
---|
105 | int error = 0;
|
---|
106 |
|
---|
107 | drm_mode_config_reset(dev);
|
---|
108 | drm_helper_resume_force_mode(dev);
|
---|
109 |
|
---|
110 | console_lock();
|
---|
111 | vbox_fbdev_set_suspend(dev, 0);
|
---|
112 | console_unlock();
|
---|
113 | return error;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static int vbox_drm_resume(struct drm_device *dev)
|
---|
117 | {
|
---|
118 | int ret;
|
---|
119 |
|
---|
120 | if (pci_enable_device(dev->pdev))
|
---|
121 | return -EIO;
|
---|
122 |
|
---|
123 | ret = vbox_drm_thaw(dev);
|
---|
124 | if (ret)
|
---|
125 | return ret;
|
---|
126 |
|
---|
127 | drm_kms_helper_poll_enable(dev);
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static int vbox_pm_suspend(struct device *dev)
|
---|
132 | {
|
---|
133 | struct pci_dev *pdev = to_pci_dev(dev);
|
---|
134 | struct drm_device *ddev = pci_get_drvdata(pdev);
|
---|
135 | int error;
|
---|
136 |
|
---|
137 | error = vbox_drm_freeze(ddev);
|
---|
138 | if (error)
|
---|
139 | return error;
|
---|
140 |
|
---|
141 | pci_disable_device(pdev);
|
---|
142 | pci_set_power_state(pdev, PCI_D3hot);
|
---|
143 | return 0;
|
---|
144 | }
|
---|
145 |
|
---|
146 | static int vbox_pm_resume(struct device *dev)
|
---|
147 | {
|
---|
148 | struct pci_dev *pdev = to_pci_dev(dev);
|
---|
149 | struct drm_device *ddev = pci_get_drvdata(pdev);
|
---|
150 | return vbox_drm_resume(ddev);
|
---|
151 | }
|
---|
152 |
|
---|
153 | static int vbox_pm_freeze(struct device *dev)
|
---|
154 | {
|
---|
155 | struct pci_dev *pdev = to_pci_dev(dev);
|
---|
156 | struct drm_device *ddev = pci_get_drvdata(pdev);
|
---|
157 |
|
---|
158 | if (!ddev || !ddev->dev_private)
|
---|
159 | return -ENODEV;
|
---|
160 | return vbox_drm_freeze(ddev);
|
---|
161 |
|
---|
162 | }
|
---|
163 |
|
---|
164 | static int vbox_pm_thaw(struct device *dev)
|
---|
165 | {
|
---|
166 | struct pci_dev *pdev = to_pci_dev(dev);
|
---|
167 | struct drm_device *ddev = pci_get_drvdata(pdev);
|
---|
168 | return vbox_drm_thaw(ddev);
|
---|
169 | }
|
---|
170 |
|
---|
171 | static int vbox_pm_poweroff(struct device *dev)
|
---|
172 | {
|
---|
173 | struct pci_dev *pdev = to_pci_dev(dev);
|
---|
174 | struct drm_device *ddev = pci_get_drvdata(pdev);
|
---|
175 |
|
---|
176 | return vbox_drm_freeze(ddev);
|
---|
177 | }
|
---|
178 |
|
---|
179 | static const struct dev_pm_ops vbox_pm_ops = {
|
---|
180 | .suspend = vbox_pm_suspend,
|
---|
181 | .resume = vbox_pm_resume,
|
---|
182 | .freeze = vbox_pm_freeze,
|
---|
183 | .thaw = vbox_pm_thaw,
|
---|
184 | .poweroff = vbox_pm_poweroff,
|
---|
185 | .restore = vbox_pm_resume,
|
---|
186 | };
|
---|
187 |
|
---|
188 | static struct pci_driver vbox_pci_driver =
|
---|
189 | {
|
---|
190 | .name = DRIVER_NAME,
|
---|
191 | .id_table = pciidlist,
|
---|
192 | .probe = vbox_pci_probe,
|
---|
193 | .remove = vbox_pci_remove,
|
---|
194 | .driver.pm = &vbox_pm_ops,
|
---|
195 | };
|
---|
196 |
|
---|
197 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
|
---|
198 | /* This works around a bug in X servers prior to 1.18.4, which sometimes
|
---|
199 | * submit more dirty rectangles than the kernel is willing to handle and
|
---|
200 | * then disable dirty rectangle handling altogether when they see the
|
---|
201 | * EINVAL error. I do not want the code to hang around forever, which is
|
---|
202 | * why I am limiting it to certain kernel versions. We can increase the
|
---|
203 | * limit if some distributions uses old X servers with new kernels. */
|
---|
204 | long vbox_ioctl(struct file *filp,
|
---|
205 | unsigned int cmd, unsigned long arg)
|
---|
206 | {
|
---|
207 | long rc = drm_ioctl(filp, cmd, arg);
|
---|
208 | if (cmd == DRM_IOCTL_MODE_DIRTYFB && rc == -EINVAL)
|
---|
209 | return -EOVERFLOW;
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 | #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0) */
|
---|
213 |
|
---|
214 | static const struct file_operations vbox_fops =
|
---|
215 | {
|
---|
216 | .owner = THIS_MODULE,
|
---|
217 | .open = drm_open,
|
---|
218 | .release = drm_release,
|
---|
219 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
|
---|
220 | .unlocked_ioctl = vbox_ioctl,
|
---|
221 | #else
|
---|
222 | .unlocked_ioctl = drm_ioctl,
|
---|
223 | #endif
|
---|
224 | .mmap = vbox_mmap,
|
---|
225 | .poll = drm_poll,
|
---|
226 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
|
---|
227 | .fasync = drm_fasync,
|
---|
228 | #endif
|
---|
229 | #ifdef CONFIG_COMPAT
|
---|
230 | .compat_ioctl = drm_compat_ioctl,
|
---|
231 | #endif
|
---|
232 | .read = drm_read,
|
---|
233 | };
|
---|
234 |
|
---|
235 | static int vbox_master_set(struct drm_device *dev,
|
---|
236 | struct drm_file *file_priv,
|
---|
237 | bool from_open)
|
---|
238 | {
|
---|
239 | struct vbox_private *vbox = dev->dev_private;
|
---|
240 | /* We do not yet know whether the new owner can handle hotplug, so we
|
---|
241 | * do not advertise dynamic modes on the first query and send a
|
---|
242 | * tentative hotplug notification after that to see if they query again. */
|
---|
243 | vbox->initial_mode_queried = false;
|
---|
244 | mutex_lock(&vbox->hw_mutex);
|
---|
245 | /* Disable VBVA when someone releases master in case the next person tries
|
---|
246 | * to do VESA. */
|
---|
247 | /** @todo work out if anyone is likely to and whether it will even work. */
|
---|
248 | /* Update: we also disable it because if the new master does not do dirty
|
---|
249 | * rectangle reporting (e.g. old versions of Plymouth) then at least the
|
---|
250 | * first screen will still be updated. We enable it as soon as we
|
---|
251 | * receive a dirty rectangle report. */
|
---|
252 | vbox_disable_accel(vbox);
|
---|
253 | mutex_unlock(&vbox->hw_mutex);
|
---|
254 | return 0;
|
---|
255 | }
|
---|
256 |
|
---|
257 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
|
---|
258 | static void vbox_master_drop(struct drm_device *dev,
|
---|
259 | struct drm_file *file_priv,
|
---|
260 | bool from_release)
|
---|
261 | #else
|
---|
262 | static void vbox_master_drop(struct drm_device *dev,
|
---|
263 | struct drm_file *file_priv)
|
---|
264 | #endif
|
---|
265 | {
|
---|
266 | struct vbox_private *vbox = dev->dev_private;
|
---|
267 | /* See vbox_master_set() */
|
---|
268 | vbox->initial_mode_queried = false;
|
---|
269 | mutex_lock(&vbox->hw_mutex);
|
---|
270 | vbox_disable_accel(vbox);
|
---|
271 | mutex_unlock(&vbox->hw_mutex);
|
---|
272 | }
|
---|
273 |
|
---|
274 | static struct drm_driver driver =
|
---|
275 | {
|
---|
276 | .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED,
|
---|
277 | .dev_priv_size = 0,
|
---|
278 |
|
---|
279 | .load = vbox_driver_load,
|
---|
280 | .unload = vbox_driver_unload,
|
---|
281 | .lastclose = vbox_driver_lastclose,
|
---|
282 | .master_set = vbox_master_set,
|
---|
283 | .master_drop = vbox_master_drop,
|
---|
284 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
|
---|
285 | .set_busid = drm_pci_set_busid,
|
---|
286 | #endif
|
---|
287 |
|
---|
288 | .fops = &vbox_fops,
|
---|
289 | .irq_handler = vbox_irq_handler,
|
---|
290 | .name = DRIVER_NAME,
|
---|
291 | .desc = DRIVER_DESC,
|
---|
292 | .date = DRIVER_DATE,
|
---|
293 | .major = DRIVER_MAJOR,
|
---|
294 | .minor = DRIVER_MINOR,
|
---|
295 | .patchlevel = DRIVER_PATCHLEVEL,
|
---|
296 |
|
---|
297 | .gem_free_object = vbox_gem_free_object,
|
---|
298 | .dumb_create = vbox_dumb_create,
|
---|
299 | .dumb_map_offset = vbox_dumb_mmap_offset,
|
---|
300 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
|
---|
301 | .dumb_destroy = vbox_dumb_destroy,
|
---|
302 | #else
|
---|
303 | .dumb_destroy = drm_gem_dumb_destroy,
|
---|
304 | #endif
|
---|
305 |
|
---|
306 | };
|
---|
307 |
|
---|
308 | static int __init vbox_init(void)
|
---|
309 | {
|
---|
310 | unsigned i;
|
---|
311 |
|
---|
312 | #ifdef CONFIG_VGA_CONSOLE
|
---|
313 | if (vgacon_text_force() && vbox_modeset == -1)
|
---|
314 | return -EINVAL;
|
---|
315 | #endif
|
---|
316 |
|
---|
317 | if (vbox_modeset == 0)
|
---|
318 | return -EINVAL;
|
---|
319 |
|
---|
320 | /* Do not load if any of the virtual consoles is in graphics mode to be
|
---|
321 | * sure that we do not pick a fight with a user-mode driver or VESA. */
|
---|
322 | for (i = 0; i < MAX_NR_CONSOLES - 1; ++i)
|
---|
323 | if (vc_cons[i].d && vc_cons[i].d->vc_mode == KD_GRAPHICS)
|
---|
324 | return -EINVAL;
|
---|
325 |
|
---|
326 | return drm_pci_init(&driver, &vbox_pci_driver);
|
---|
327 | }
|
---|
328 | static void __exit vbox_exit(void)
|
---|
329 | {
|
---|
330 | drm_pci_exit(&driver, &vbox_pci_driver);
|
---|
331 | }
|
---|
332 |
|
---|
333 | module_init(vbox_init);
|
---|
334 | module_exit(vbox_exit);
|
---|
335 |
|
---|
336 | MODULE_AUTHOR(DRIVER_AUTHOR);
|
---|
337 | MODULE_DESCRIPTION(DRIVER_DESC);
|
---|
338 | MODULE_LICENSE("GPL and additional rights");
|
---|
339 | #ifdef MODULE_VERSION
|
---|
340 | MODULE_VERSION(VBOX_VERSION_STRING " r" RT_XSTR(VBOX_SVN_REV));
|
---|
341 | #endif
|
---|