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_aperture.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_fb_helper.h>
19 #include <drm/drm_file.h>
20 #include <drm/drm_ioctl.h>
21 #include <drm/drm_managed.h>
25 static int vbox_modeset = -1;
27 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
28 module_param_named(modeset, vbox_modeset, int, 0400);
30 static const struct drm_driver driver;
32 static const struct pci_device_id pciidlist[] = {
33 { PCI_DEVICE(0x80ee, 0xbeef) },
36 MODULE_DEVICE_TABLE(pci, pciidlist);
38 static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
40 struct vbox_private *vbox;
43 if (!vbox_check_supported(VBE_DISPI_ID_HGSMI))
46 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
50 vbox = devm_drm_dev_alloc(&pdev->dev, &driver,
51 struct vbox_private, ddev);
55 pci_set_drvdata(pdev, vbox);
56 mutex_init(&vbox->hw_mutex);
58 ret = pcim_enable_device(pdev);
62 ret = vbox_hw_init(vbox);
66 ret = vbox_mm_init(vbox);
70 ret = vbox_mode_init(vbox);
74 ret = vbox_irq_init(vbox);
78 ret = drm_dev_register(&vbox->ddev, 0);
82 drm_fbdev_generic_setup(&vbox->ddev, 32);
95 static void vbox_pci_remove(struct pci_dev *pdev)
97 struct vbox_private *vbox = pci_get_drvdata(pdev);
99 drm_dev_unregister(&vbox->ddev);
101 vbox_mode_fini(vbox);
105 #ifdef CONFIG_PM_SLEEP
106 static int vbox_pm_suspend(struct device *dev)
108 struct vbox_private *vbox = dev_get_drvdata(dev);
109 struct pci_dev *pdev = to_pci_dev(dev);
112 error = drm_mode_config_helper_suspend(&vbox->ddev);
116 pci_save_state(pdev);
117 pci_disable_device(pdev);
118 pci_set_power_state(pdev, PCI_D3hot);
123 static int vbox_pm_resume(struct device *dev)
125 struct vbox_private *vbox = dev_get_drvdata(dev);
126 struct pci_dev *pdev = to_pci_dev(dev);
128 if (pci_enable_device(pdev))
131 return drm_mode_config_helper_resume(&vbox->ddev);
134 static int vbox_pm_freeze(struct device *dev)
136 struct vbox_private *vbox = dev_get_drvdata(dev);
138 return drm_mode_config_helper_suspend(&vbox->ddev);
141 static int vbox_pm_thaw(struct device *dev)
143 struct vbox_private *vbox = dev_get_drvdata(dev);
145 return drm_mode_config_helper_resume(&vbox->ddev);
148 static int vbox_pm_poweroff(struct device *dev)
150 struct vbox_private *vbox = dev_get_drvdata(dev);
152 return drm_mode_config_helper_suspend(&vbox->ddev);
155 static const struct dev_pm_ops vbox_pm_ops = {
156 .suspend = vbox_pm_suspend,
157 .resume = vbox_pm_resume,
158 .freeze = vbox_pm_freeze,
159 .thaw = vbox_pm_thaw,
160 .poweroff = vbox_pm_poweroff,
161 .restore = vbox_pm_resume,
165 static struct pci_driver vbox_pci_driver = {
167 .id_table = pciidlist,
168 .probe = vbox_pci_probe,
169 .remove = vbox_pci_remove,
170 #ifdef CONFIG_PM_SLEEP
171 .driver.pm = &vbox_pm_ops,
175 DEFINE_DRM_GEM_FOPS(vbox_fops);
177 static const struct drm_driver driver = {
179 DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
181 .lastclose = drm_fb_helper_lastclose,
187 .major = DRIVER_MAJOR,
188 .minor = DRIVER_MINOR,
189 .patchlevel = DRIVER_PATCHLEVEL,
194 static int __init vbox_init(void)
196 #ifdef CONFIG_VGA_CONSOLE
197 if (vgacon_text_force() && vbox_modeset == -1)
201 if (vbox_modeset == 0)
204 return pci_register_driver(&vbox_pci_driver);
207 static void __exit vbox_exit(void)
209 pci_unregister_driver(&vbox_pci_driver);
212 module_init(vbox_init);
213 module_exit(vbox_exit);
215 MODULE_AUTHOR("Oracle Corporation");
216 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
217 MODULE_DESCRIPTION(DRIVER_DESC);
218 MODULE_LICENSE("GPL and additional rights");