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 ret = pci_enable_device(pdev);
68 pci_set_drvdata(pdev, dev);
70 ret = vbox_driver_load(dev);
72 goto err_vbox_driver_load;
74 ret = drm_dev_register(dev, 0);
76 goto err_drv_dev_register;
81 vbox_driver_unload(dev);
83 pci_disable_device(pdev);
90 static void vbox_pci_remove(struct pci_dev *pdev)
92 struct drm_device *dev = pci_get_drvdata(pdev);
94 drm_dev_unregister(dev);
95 vbox_driver_unload(dev);
99 static int vbox_drm_freeze(struct drm_device *dev)
101 struct vbox_private *vbox = dev->dev_private;
103 drm_kms_helper_poll_disable(dev);
105 pci_save_state(dev->pdev);
107 drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, true);
112 static int vbox_drm_thaw(struct drm_device *dev)
114 struct vbox_private *vbox = dev->dev_private;
116 drm_mode_config_reset(dev);
117 drm_helper_resume_force_mode(dev);
118 drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, false);
123 static int vbox_drm_resume(struct drm_device *dev)
127 if (pci_enable_device(dev->pdev))
130 ret = vbox_drm_thaw(dev);
134 drm_kms_helper_poll_enable(dev);
139 static int vbox_pm_suspend(struct device *dev)
141 struct pci_dev *pdev = to_pci_dev(dev);
142 struct drm_device *ddev = pci_get_drvdata(pdev);
145 error = vbox_drm_freeze(ddev);
149 pci_disable_device(pdev);
150 pci_set_power_state(pdev, PCI_D3hot);
155 static int vbox_pm_resume(struct device *dev)
157 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
159 return vbox_drm_resume(ddev);
162 static int vbox_pm_freeze(struct device *dev)
164 struct pci_dev *pdev = to_pci_dev(dev);
165 struct drm_device *ddev = pci_get_drvdata(pdev);
167 if (!ddev || !ddev->dev_private)
170 return vbox_drm_freeze(ddev);
173 static int vbox_pm_thaw(struct device *dev)
175 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
177 return vbox_drm_thaw(ddev);
180 static int vbox_pm_poweroff(struct device *dev)
182 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
184 return vbox_drm_freeze(ddev);
187 static const struct dev_pm_ops vbox_pm_ops = {
188 .suspend = vbox_pm_suspend,
189 .resume = vbox_pm_resume,
190 .freeze = vbox_pm_freeze,
191 .thaw = vbox_pm_thaw,
192 .poweroff = vbox_pm_poweroff,
193 .restore = vbox_pm_resume,
196 static struct pci_driver vbox_pci_driver = {
198 .id_table = pciidlist,
199 .probe = vbox_pci_probe,
200 .remove = vbox_pci_remove,
201 .driver.pm = &vbox_pm_ops,
204 static const struct file_operations vbox_fops = {
205 .owner = THIS_MODULE,
207 .release = drm_release,
208 .unlocked_ioctl = drm_ioctl,
212 .compat_ioctl = drm_compat_ioctl,
217 static int vbox_master_set(struct drm_device *dev,
218 struct drm_file *file_priv, bool from_open)
220 struct vbox_private *vbox = dev->dev_private;
223 * We do not yet know whether the new owner can handle hotplug, so we
224 * do not advertise dynamic modes on the first query and send a
225 * tentative hotplug notification after that to see if they query again.
227 vbox->initial_mode_queried = false;
229 mutex_lock(&vbox->hw_mutex);
231 * Disable VBVA when someone releases master in case the next person
232 * tries tries to do VESA.
234 /** @todo work out if anyone is likely to and whether it will work. */
236 * Update: we also disable it because if the new master does not do
237 * dirty rectangle reporting (e.g. old versions of Plymouth) then at
238 * least the first screen will still be updated. We enable it as soon
239 * as we receive a dirty rectangle report.
241 vbox_disable_accel(vbox);
242 mutex_unlock(&vbox->hw_mutex);
247 static void vbox_master_drop(struct drm_device *dev, struct drm_file *file_priv)
249 struct vbox_private *vbox = dev->dev_private;
251 /* See vbox_master_set() */
252 vbox->initial_mode_queried = false;
254 mutex_lock(&vbox->hw_mutex);
255 vbox_disable_accel(vbox);
256 mutex_unlock(&vbox->hw_mutex);
259 static struct drm_driver driver = {
261 DRIVER_MODESET | DRIVER_GEM | DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED |
265 .lastclose = vbox_driver_lastclose,
266 .master_set = vbox_master_set,
267 .master_drop = vbox_master_drop,
270 .irq_handler = vbox_irq_handler,
274 .major = DRIVER_MAJOR,
275 .minor = DRIVER_MINOR,
276 .patchlevel = DRIVER_PATCHLEVEL,
278 .gem_free_object_unlocked = vbox_gem_free_object,
279 .dumb_create = vbox_dumb_create,
280 .dumb_map_offset = vbox_dumb_mmap_offset,
281 .dumb_destroy = drm_gem_dumb_destroy,
282 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
283 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
284 .gem_prime_export = drm_gem_prime_export,
285 .gem_prime_import = drm_gem_prime_import,
286 .gem_prime_pin = vbox_gem_prime_pin,
287 .gem_prime_unpin = vbox_gem_prime_unpin,
288 .gem_prime_get_sg_table = vbox_gem_prime_get_sg_table,
289 .gem_prime_import_sg_table = vbox_gem_prime_import_sg_table,
290 .gem_prime_vmap = vbox_gem_prime_vmap,
291 .gem_prime_vunmap = vbox_gem_prime_vunmap,
292 .gem_prime_mmap = vbox_gem_prime_mmap,
295 static int __init vbox_init(void)
297 #ifdef CONFIG_VGA_CONSOLE
298 if (vgacon_text_force() && vbox_modeset == -1)
302 if (vbox_modeset == 0)
305 return pci_register_driver(&vbox_pci_driver);
308 static void __exit vbox_exit(void)
310 pci_unregister_driver(&vbox_pci_driver);
313 module_init(vbox_init);
314 module_exit(vbox_exit);
316 MODULE_AUTHOR("Oracle Corporation");
317 MODULE_DESCRIPTION(DRIVER_DESC);
318 MODULE_LICENSE("GPL and additional rights");