1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2013-2017 Oracle Corporation
4 * This file is based on ast_drv.c
5 * Copyright 2012 Red Hat Inc.
6 * Authors: Dave Airlie <airlied@redhat.com>
7 * Michael Thayer <michael.thayer@oracle.com,
8 * Hans de Goede <hdegoede@redhat.com>
10 #include <linux/console.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/vt_kern.h>
15 #include <drm/drm_crtc_helper.h>
16 #include <drm/drm_drv.h>
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_file.h>
19 #include <drm/drm_ioctl.h>
23 static int vbox_modeset = -1;
25 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
26 module_param_named(modeset, vbox_modeset, int, 0400);
28 static struct drm_driver driver;
30 static const struct pci_device_id pciidlist[] = {
31 { PCI_DEVICE(0x80ee, 0xbeef) },
34 MODULE_DEVICE_TABLE(pci, pciidlist);
36 static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
38 struct vbox_private *vbox;
41 if (!vbox_check_supported(VBE_DISPI_ID_HGSMI))
44 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "vboxvideodrmfb");
48 vbox = kzalloc(sizeof(*vbox), GFP_KERNEL);
52 ret = drm_dev_init(&vbox->ddev, &driver, &pdev->dev);
58 vbox->ddev.pdev = pdev;
59 vbox->ddev.dev_private = vbox;
60 pci_set_drvdata(pdev, vbox);
61 mutex_init(&vbox->hw_mutex);
63 ret = pci_enable_device(pdev);
67 ret = vbox_hw_init(vbox);
71 ret = vbox_mm_init(vbox);
75 ret = vbox_mode_init(vbox);
79 ret = vbox_irq_init(vbox);
83 ret = drm_fbdev_generic_setup(&vbox->ddev, 32);
87 ret = drm_dev_register(&vbox->ddev, 0);
102 pci_disable_device(pdev);
104 drm_dev_put(&vbox->ddev);
108 static void vbox_pci_remove(struct pci_dev *pdev)
110 struct vbox_private *vbox = pci_get_drvdata(pdev);
112 drm_dev_unregister(&vbox->ddev);
114 vbox_mode_fini(vbox);
117 drm_dev_put(&vbox->ddev);
120 #ifdef CONFIG_PM_SLEEP
121 static int vbox_pm_suspend(struct device *dev)
123 struct vbox_private *vbox = dev_get_drvdata(dev);
126 error = drm_mode_config_helper_suspend(&vbox->ddev);
130 pci_save_state(vbox->ddev.pdev);
131 pci_disable_device(vbox->ddev.pdev);
132 pci_set_power_state(vbox->ddev.pdev, PCI_D3hot);
137 static int vbox_pm_resume(struct device *dev)
139 struct vbox_private *vbox = dev_get_drvdata(dev);
141 if (pci_enable_device(vbox->ddev.pdev))
144 return drm_mode_config_helper_resume(&vbox->ddev);
147 static int vbox_pm_freeze(struct device *dev)
149 struct vbox_private *vbox = dev_get_drvdata(dev);
151 return drm_mode_config_helper_suspend(&vbox->ddev);
154 static int vbox_pm_thaw(struct device *dev)
156 struct vbox_private *vbox = dev_get_drvdata(dev);
158 return drm_mode_config_helper_resume(&vbox->ddev);
161 static int vbox_pm_poweroff(struct device *dev)
163 struct vbox_private *vbox = dev_get_drvdata(dev);
165 return drm_mode_config_helper_suspend(&vbox->ddev);
168 static const struct dev_pm_ops vbox_pm_ops = {
169 .suspend = vbox_pm_suspend,
170 .resume = vbox_pm_resume,
171 .freeze = vbox_pm_freeze,
172 .thaw = vbox_pm_thaw,
173 .poweroff = vbox_pm_poweroff,
174 .restore = vbox_pm_resume,
178 static struct pci_driver vbox_pci_driver = {
180 .id_table = pciidlist,
181 .probe = vbox_pci_probe,
182 .remove = vbox_pci_remove,
183 #ifdef CONFIG_PM_SLEEP
184 .driver.pm = &vbox_pm_ops,
188 DEFINE_DRM_GEM_FOPS(vbox_fops);
190 static struct drm_driver driver = {
192 DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
194 .lastclose = drm_fb_helper_lastclose,
197 .irq_handler = vbox_irq_handler,
201 .major = DRIVER_MAJOR,
202 .minor = DRIVER_MINOR,
203 .patchlevel = DRIVER_PATCHLEVEL,
208 static int __init vbox_init(void)
210 #ifdef CONFIG_VGA_CONSOLE
211 if (vgacon_text_force() && vbox_modeset == -1)
215 if (vbox_modeset == 0)
218 return pci_register_driver(&vbox_pci_driver);
221 static void __exit vbox_exit(void)
223 pci_unregister_driver(&vbox_pci_driver);
226 module_init(vbox_init);
227 module_exit(vbox_exit);
229 MODULE_AUTHOR("Oracle Corporation");
230 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
231 MODULE_DESCRIPTION(DRIVER_DESC);
232 MODULE_LICENSE("GPL and additional rights");