1 // SPDX-License-Identifier: MIT 2 /* 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> 9 */ 10 #include <linux/console.h> 11 #include <linux/module.h> 12 #include <linux/pci.h> 13 #include <linux/vt_kern.h> 14 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> 20 #include <drm/drm_managed.h> 21 22 #include "vbox_drv.h" 23 24 static int vbox_modeset = -1; 25 26 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting"); 27 module_param_named(modeset, vbox_modeset, int, 0400); 28 29 static const struct drm_driver driver; 30 31 static const struct pci_device_id pciidlist[] = { 32 { PCI_DEVICE(0x80ee, 0xbeef) }, 33 { } 34 }; 35 MODULE_DEVICE_TABLE(pci, pciidlist); 36 37 static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 38 { 39 struct vbox_private *vbox; 40 int ret = 0; 41 42 if (!vbox_check_supported(VBE_DISPI_ID_HGSMI)) 43 return -ENODEV; 44 45 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "vboxvideodrmfb"); 46 if (ret) 47 return ret; 48 49 vbox = devm_drm_dev_alloc(&pdev->dev, &driver, 50 struct vbox_private, ddev); 51 if (IS_ERR(vbox)) 52 return PTR_ERR(vbox); 53 54 pci_set_drvdata(pdev, vbox); 55 mutex_init(&vbox->hw_mutex); 56 57 ret = pcim_enable_device(pdev); 58 if (ret) 59 return ret; 60 61 ret = vbox_hw_init(vbox); 62 if (ret) 63 return ret; 64 65 ret = vbox_mm_init(vbox); 66 if (ret) 67 goto err_hw_fini; 68 69 ret = vbox_mode_init(vbox); 70 if (ret) 71 goto err_mm_fini; 72 73 ret = vbox_irq_init(vbox); 74 if (ret) 75 goto err_mode_fini; 76 77 ret = drm_dev_register(&vbox->ddev, 0); 78 if (ret) 79 goto err_irq_fini; 80 81 drm_fbdev_generic_setup(&vbox->ddev, 32); 82 83 return 0; 84 85 err_irq_fini: 86 vbox_irq_fini(vbox); 87 err_mode_fini: 88 vbox_mode_fini(vbox); 89 err_mm_fini: 90 vbox_mm_fini(vbox); 91 err_hw_fini: 92 vbox_hw_fini(vbox); 93 return ret; 94 } 95 96 static void vbox_pci_remove(struct pci_dev *pdev) 97 { 98 struct vbox_private *vbox = pci_get_drvdata(pdev); 99 100 drm_dev_unregister(&vbox->ddev); 101 vbox_irq_fini(vbox); 102 vbox_mode_fini(vbox); 103 vbox_mm_fini(vbox); 104 vbox_hw_fini(vbox); 105 } 106 107 #ifdef CONFIG_PM_SLEEP 108 static int vbox_pm_suspend(struct device *dev) 109 { 110 struct vbox_private *vbox = dev_get_drvdata(dev); 111 struct pci_dev *pdev = to_pci_dev(dev); 112 int error; 113 114 error = drm_mode_config_helper_suspend(&vbox->ddev); 115 if (error) 116 return error; 117 118 pci_save_state(pdev); 119 pci_disable_device(pdev); 120 pci_set_power_state(pdev, PCI_D3hot); 121 122 return 0; 123 } 124 125 static int vbox_pm_resume(struct device *dev) 126 { 127 struct vbox_private *vbox = dev_get_drvdata(dev); 128 struct pci_dev *pdev = to_pci_dev(dev); 129 130 if (pci_enable_device(pdev)) 131 return -EIO; 132 133 return drm_mode_config_helper_resume(&vbox->ddev); 134 } 135 136 static int vbox_pm_freeze(struct device *dev) 137 { 138 struct vbox_private *vbox = dev_get_drvdata(dev); 139 140 return drm_mode_config_helper_suspend(&vbox->ddev); 141 } 142 143 static int vbox_pm_thaw(struct device *dev) 144 { 145 struct vbox_private *vbox = dev_get_drvdata(dev); 146 147 return drm_mode_config_helper_resume(&vbox->ddev); 148 } 149 150 static int vbox_pm_poweroff(struct device *dev) 151 { 152 struct vbox_private *vbox = dev_get_drvdata(dev); 153 154 return drm_mode_config_helper_suspend(&vbox->ddev); 155 } 156 157 static const struct dev_pm_ops vbox_pm_ops = { 158 .suspend = vbox_pm_suspend, 159 .resume = vbox_pm_resume, 160 .freeze = vbox_pm_freeze, 161 .thaw = vbox_pm_thaw, 162 .poweroff = vbox_pm_poweroff, 163 .restore = vbox_pm_resume, 164 }; 165 #endif 166 167 static struct pci_driver vbox_pci_driver = { 168 .name = DRIVER_NAME, 169 .id_table = pciidlist, 170 .probe = vbox_pci_probe, 171 .remove = vbox_pci_remove, 172 #ifdef CONFIG_PM_SLEEP 173 .driver.pm = &vbox_pm_ops, 174 #endif 175 }; 176 177 DEFINE_DRM_GEM_FOPS(vbox_fops); 178 179 static const struct drm_driver driver = { 180 .driver_features = 181 DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, 182 183 .lastclose = drm_fb_helper_lastclose, 184 185 .fops = &vbox_fops, 186 .irq_handler = vbox_irq_handler, 187 .name = DRIVER_NAME, 188 .desc = DRIVER_DESC, 189 .date = DRIVER_DATE, 190 .major = DRIVER_MAJOR, 191 .minor = DRIVER_MINOR, 192 .patchlevel = DRIVER_PATCHLEVEL, 193 194 DRM_GEM_VRAM_DRIVER, 195 }; 196 197 static int __init vbox_init(void) 198 { 199 #ifdef CONFIG_VGA_CONSOLE 200 if (vgacon_text_force() && vbox_modeset == -1) 201 return -EINVAL; 202 #endif 203 204 if (vbox_modeset == 0) 205 return -EINVAL; 206 207 return pci_register_driver(&vbox_pci_driver); 208 } 209 210 static void __exit vbox_exit(void) 211 { 212 pci_unregister_driver(&vbox_pci_driver); 213 } 214 215 module_init(vbox_init); 216 module_exit(vbox_exit); 217 218 MODULE_AUTHOR("Oracle Corporation"); 219 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); 220 MODULE_DESCRIPTION(DRIVER_DESC); 221 MODULE_LICENSE("GPL and additional rights"); 222