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