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