2 * Copyright (C) 2013-2017 Oracle Corporation
3 * This file is based on ast_drv.c
4 * Copyright 2012 Red Hat Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * Authors: Dave Airlie <airlied@redhat.com>
27 * Michael Thayer <michael.thayer@oracle.com,
28 * Hans de Goede <hdegoede@redhat.com>
30 #include <linux/module.h>
31 #include <linux/console.h>
32 #include <linux/vt_kern.h>
35 #include <drm/drm_crtc_helper.h>
39 static int vbox_modeset = -1;
41 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
42 module_param_named(modeset, vbox_modeset, int, 0400);
44 static struct drm_driver driver;
46 static const struct pci_device_id pciidlist[] = {
47 { 0x80ee, 0xbeef, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
50 MODULE_DEVICE_TABLE(pci, pciidlist);
52 static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
54 struct drm_device *dev = NULL;
57 dev = drm_dev_alloc(&driver, &pdev->dev);
63 pci_set_drvdata(pdev, dev);
65 ret = vbox_driver_load(dev);
67 goto err_vbox_driver_load;
69 ret = drm_dev_register(dev, 0);
71 goto err_drv_dev_register;
76 vbox_driver_unload(dev);
83 static void vbox_pci_remove(struct pci_dev *pdev)
85 struct drm_device *dev = pci_get_drvdata(pdev);
87 drm_dev_unregister(dev);
88 vbox_driver_unload(dev);
92 static int vbox_drm_freeze(struct drm_device *dev)
94 struct vbox_private *vbox = dev->dev_private;
96 drm_kms_helper_poll_disable(dev);
98 pci_save_state(dev->pdev);
100 drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, true);
105 static int vbox_drm_thaw(struct drm_device *dev)
107 struct vbox_private *vbox = dev->dev_private;
109 drm_mode_config_reset(dev);
110 drm_helper_resume_force_mode(dev);
111 drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, false);
116 static int vbox_drm_resume(struct drm_device *dev)
120 if (pci_enable_device(dev->pdev))
123 ret = vbox_drm_thaw(dev);
127 drm_kms_helper_poll_enable(dev);
132 static int vbox_pm_suspend(struct device *dev)
134 struct pci_dev *pdev = to_pci_dev(dev);
135 struct drm_device *ddev = pci_get_drvdata(pdev);
138 error = vbox_drm_freeze(ddev);
142 pci_disable_device(pdev);
143 pci_set_power_state(pdev, PCI_D3hot);
148 static int vbox_pm_resume(struct device *dev)
150 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
152 return vbox_drm_resume(ddev);
155 static int vbox_pm_freeze(struct device *dev)
157 struct pci_dev *pdev = to_pci_dev(dev);
158 struct drm_device *ddev = pci_get_drvdata(pdev);
160 if (!ddev || !ddev->dev_private)
163 return vbox_drm_freeze(ddev);
166 static int vbox_pm_thaw(struct device *dev)
168 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
170 return vbox_drm_thaw(ddev);
173 static int vbox_pm_poweroff(struct device *dev)
175 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
177 return vbox_drm_freeze(ddev);
180 static const struct dev_pm_ops vbox_pm_ops = {
181 .suspend = vbox_pm_suspend,
182 .resume = vbox_pm_resume,
183 .freeze = vbox_pm_freeze,
184 .thaw = vbox_pm_thaw,
185 .poweroff = vbox_pm_poweroff,
186 .restore = vbox_pm_resume,
189 static struct pci_driver vbox_pci_driver = {
191 .id_table = pciidlist,
192 .probe = vbox_pci_probe,
193 .remove = vbox_pci_remove,
194 .driver.pm = &vbox_pm_ops,
197 static const struct file_operations vbox_fops = {
198 .owner = THIS_MODULE,
200 .release = drm_release,
201 .unlocked_ioctl = drm_ioctl,
205 .compat_ioctl = drm_compat_ioctl,
210 static int vbox_master_set(struct drm_device *dev,
211 struct drm_file *file_priv, bool from_open)
213 struct vbox_private *vbox = dev->dev_private;
216 * We do not yet know whether the new owner can handle hotplug, so we
217 * do not advertise dynamic modes on the first query and send a
218 * tentative hotplug notification after that to see if they query again.
220 vbox->initial_mode_queried = false;
222 mutex_lock(&vbox->hw_mutex);
224 * Disable VBVA when someone releases master in case the next person
225 * tries tries to do VESA.
227 /** @todo work out if anyone is likely to and whether it will work. */
229 * Update: we also disable it because if the new master does not do
230 * dirty rectangle reporting (e.g. old versions of Plymouth) then at
231 * least the first screen will still be updated. We enable it as soon
232 * as we receive a dirty rectangle report.
234 vbox_disable_accel(vbox);
235 mutex_unlock(&vbox->hw_mutex);
240 static void vbox_master_drop(struct drm_device *dev, struct drm_file *file_priv)
242 struct vbox_private *vbox = dev->dev_private;
244 /* See vbox_master_set() */
245 vbox->initial_mode_queried = false;
247 mutex_lock(&vbox->hw_mutex);
248 vbox_disable_accel(vbox);
249 mutex_unlock(&vbox->hw_mutex);
252 static struct drm_driver driver = {
254 DRIVER_MODESET | DRIVER_GEM | DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED |
258 .lastclose = vbox_driver_lastclose,
259 .master_set = vbox_master_set,
260 .master_drop = vbox_master_drop,
263 .irq_handler = vbox_irq_handler,
267 .major = DRIVER_MAJOR,
268 .minor = DRIVER_MINOR,
269 .patchlevel = DRIVER_PATCHLEVEL,
271 .gem_free_object_unlocked = vbox_gem_free_object,
272 .dumb_create = vbox_dumb_create,
273 .dumb_map_offset = vbox_dumb_mmap_offset,
274 .dumb_destroy = drm_gem_dumb_destroy,
275 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
276 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
277 .gem_prime_export = drm_gem_prime_export,
278 .gem_prime_import = drm_gem_prime_import,
279 .gem_prime_pin = vbox_gem_prime_pin,
280 .gem_prime_unpin = vbox_gem_prime_unpin,
281 .gem_prime_get_sg_table = vbox_gem_prime_get_sg_table,
282 .gem_prime_import_sg_table = vbox_gem_prime_import_sg_table,
283 .gem_prime_vmap = vbox_gem_prime_vmap,
284 .gem_prime_vunmap = vbox_gem_prime_vunmap,
285 .gem_prime_mmap = vbox_gem_prime_mmap,
288 static int __init vbox_init(void)
290 #ifdef CONFIG_VGA_CONSOLE
291 if (vgacon_text_force() && vbox_modeset == -1)
295 if (vbox_modeset == 0)
298 return pci_register_driver(&vbox_pci_driver);
301 static void __exit vbox_exit(void)
303 pci_unregister_driver(&vbox_pci_driver);
306 module_init(vbox_init);
307 module_exit(vbox_exit);
309 MODULE_AUTHOR("Oracle Corporation");
310 MODULE_DESCRIPTION(DRIVER_DESC);
311 MODULE_LICENSE("GPL and additional rights");