Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: MIT
0002 /*
0003  * Copyright (C) 2013-2017 Oracle Corporation
0004  * This file is based on ast_drv.c
0005  * Copyright 2012 Red Hat Inc.
0006  * Authors: Dave Airlie <airlied@redhat.com>
0007  *          Michael Thayer <michael.thayer@oracle.com,
0008  *          Hans de Goede <hdegoede@redhat.com>
0009  */
0010 #include <linux/module.h>
0011 #include <linux/pci.h>
0012 #include <linux/vt_kern.h>
0013 
0014 #include <drm/drm_aperture.h>
0015 #include <drm/drm_crtc_helper.h>
0016 #include <drm/drm_drv.h>
0017 #include <drm/drm_fb_helper.h>
0018 #include <drm/drm_file.h>
0019 #include <drm/drm_ioctl.h>
0020 #include <drm/drm_managed.h>
0021 #include <drm/drm_module.h>
0022 
0023 #include "vbox_drv.h"
0024 
0025 static int vbox_modeset = -1;
0026 
0027 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
0028 module_param_named(modeset, vbox_modeset, int, 0400);
0029 
0030 static const struct drm_driver driver;
0031 
0032 static const struct pci_device_id pciidlist[] = {
0033     { PCI_DEVICE(0x80ee, 0xbeef) },
0034     { }
0035 };
0036 MODULE_DEVICE_TABLE(pci, pciidlist);
0037 
0038 static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
0039 {
0040     struct vbox_private *vbox;
0041     int ret = 0;
0042 
0043     if (!vbox_check_supported(VBE_DISPI_ID_HGSMI))
0044         return -ENODEV;
0045 
0046     ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
0047     if (ret)
0048         return ret;
0049 
0050     vbox = devm_drm_dev_alloc(&pdev->dev, &driver,
0051                   struct vbox_private, ddev);
0052     if (IS_ERR(vbox))
0053         return PTR_ERR(vbox);
0054 
0055     pci_set_drvdata(pdev, vbox);
0056     mutex_init(&vbox->hw_mutex);
0057 
0058     ret = pcim_enable_device(pdev);
0059     if (ret)
0060         return ret;
0061 
0062     ret = vbox_hw_init(vbox);
0063     if (ret)
0064         return ret;
0065 
0066     ret = vbox_mm_init(vbox);
0067     if (ret)
0068         goto err_hw_fini;
0069 
0070     ret = vbox_mode_init(vbox);
0071     if (ret)
0072         goto err_hw_fini;
0073 
0074     ret = vbox_irq_init(vbox);
0075     if (ret)
0076         goto err_mode_fini;
0077 
0078     ret = drm_dev_register(&vbox->ddev, 0);
0079     if (ret)
0080         goto err_irq_fini;
0081 
0082     drm_fbdev_generic_setup(&vbox->ddev, 32);
0083 
0084     return 0;
0085 
0086 err_irq_fini:
0087     vbox_irq_fini(vbox);
0088 err_mode_fini:
0089     vbox_mode_fini(vbox);
0090 err_hw_fini:
0091     vbox_hw_fini(vbox);
0092     return ret;
0093 }
0094 
0095 static void vbox_pci_remove(struct pci_dev *pdev)
0096 {
0097     struct vbox_private *vbox = pci_get_drvdata(pdev);
0098 
0099     drm_dev_unregister(&vbox->ddev);
0100     vbox_irq_fini(vbox);
0101     vbox_mode_fini(vbox);
0102     vbox_hw_fini(vbox);
0103 }
0104 
0105 #ifdef CONFIG_PM_SLEEP
0106 static int vbox_pm_suspend(struct device *dev)
0107 {
0108     struct vbox_private *vbox = dev_get_drvdata(dev);
0109     struct pci_dev *pdev = to_pci_dev(dev);
0110     int error;
0111 
0112     error = drm_mode_config_helper_suspend(&vbox->ddev);
0113     if (error)
0114         return error;
0115 
0116     pci_save_state(pdev);
0117     pci_disable_device(pdev);
0118     pci_set_power_state(pdev, PCI_D3hot);
0119 
0120     return 0;
0121 }
0122 
0123 static int vbox_pm_resume(struct device *dev)
0124 {
0125     struct vbox_private *vbox = dev_get_drvdata(dev);
0126     struct pci_dev *pdev = to_pci_dev(dev);
0127 
0128     if (pci_enable_device(pdev))
0129         return -EIO;
0130 
0131     return drm_mode_config_helper_resume(&vbox->ddev);
0132 }
0133 
0134 static int vbox_pm_freeze(struct device *dev)
0135 {
0136     struct vbox_private *vbox = dev_get_drvdata(dev);
0137 
0138     return drm_mode_config_helper_suspend(&vbox->ddev);
0139 }
0140 
0141 static int vbox_pm_thaw(struct device *dev)
0142 {
0143     struct vbox_private *vbox = dev_get_drvdata(dev);
0144 
0145     return drm_mode_config_helper_resume(&vbox->ddev);
0146 }
0147 
0148 static int vbox_pm_poweroff(struct device *dev)
0149 {
0150     struct vbox_private *vbox = dev_get_drvdata(dev);
0151 
0152     return drm_mode_config_helper_suspend(&vbox->ddev);
0153 }
0154 
0155 static const struct dev_pm_ops vbox_pm_ops = {
0156     .suspend = vbox_pm_suspend,
0157     .resume = vbox_pm_resume,
0158     .freeze = vbox_pm_freeze,
0159     .thaw = vbox_pm_thaw,
0160     .poweroff = vbox_pm_poweroff,
0161     .restore = vbox_pm_resume,
0162 };
0163 #endif
0164 
0165 static struct pci_driver vbox_pci_driver = {
0166     .name = DRIVER_NAME,
0167     .id_table = pciidlist,
0168     .probe = vbox_pci_probe,
0169     .remove = vbox_pci_remove,
0170 #ifdef CONFIG_PM_SLEEP
0171     .driver.pm = &vbox_pm_ops,
0172 #endif
0173 };
0174 
0175 DEFINE_DRM_GEM_FOPS(vbox_fops);
0176 
0177 static const struct drm_driver driver = {
0178     .driver_features =
0179         DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
0180 
0181     .lastclose = drm_fb_helper_lastclose,
0182 
0183     .fops = &vbox_fops,
0184     .name = DRIVER_NAME,
0185     .desc = DRIVER_DESC,
0186     .date = DRIVER_DATE,
0187     .major = DRIVER_MAJOR,
0188     .minor = DRIVER_MINOR,
0189     .patchlevel = DRIVER_PATCHLEVEL,
0190 
0191     DRM_GEM_VRAM_DRIVER,
0192 };
0193 
0194 drm_module_pci_driver_if_modeset(vbox_pci_driver, vbox_modeset);
0195 
0196 MODULE_AUTHOR("Oracle Corporation");
0197 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0198 MODULE_DESCRIPTION(DRIVER_DESC);
0199 MODULE_LICENSE("GPL and additional rights");