Merge tag 'irqchip-fixes-5.12-1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / gpu / drm / bochs / bochs_drv.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  */
4
5 #include <linux/module.h>
6 #include <linux/pci.h>
7
8 #include <drm/drm_drv.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_managed.h>
11
12 #include "bochs.h"
13
14 static int bochs_modeset = -1;
15 module_param_named(modeset, bochs_modeset, int, 0444);
16 MODULE_PARM_DESC(modeset, "enable/disable kernel modesetting");
17
18 /* ---------------------------------------------------------------------- */
19 /* drm interface                                                          */
20
21 static void bochs_unload(struct drm_device *dev)
22 {
23         struct bochs_device *bochs = dev->dev_private;
24
25         bochs_mm_fini(bochs);
26 }
27
28 static int bochs_load(struct drm_device *dev)
29 {
30         struct bochs_device *bochs;
31         int ret;
32
33         bochs = drmm_kzalloc(dev, sizeof(*bochs), GFP_KERNEL);
34         if (bochs == NULL)
35                 return -ENOMEM;
36         dev->dev_private = bochs;
37         bochs->dev = dev;
38
39         ret = bochs_hw_init(dev);
40         if (ret)
41                 goto err;
42
43         ret = bochs_mm_init(bochs);
44         if (ret)
45                 goto err;
46
47         ret = bochs_kms_init(bochs);
48         if (ret)
49                 goto err;
50
51         return 0;
52
53 err:
54         bochs_unload(dev);
55         return ret;
56 }
57
58 DEFINE_DRM_GEM_FOPS(bochs_fops);
59
60 static const struct drm_driver bochs_driver = {
61         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
62         .fops                   = &bochs_fops,
63         .name                   = "bochs-drm",
64         .desc                   = "bochs dispi vga interface (qemu stdvga)",
65         .date                   = "20130925",
66         .major                  = 1,
67         .minor                  = 0,
68         DRM_GEM_VRAM_DRIVER,
69         .release                = bochs_unload,
70 };
71
72 /* ---------------------------------------------------------------------- */
73 /* pm interface                                                           */
74
75 #ifdef CONFIG_PM_SLEEP
76 static int bochs_pm_suspend(struct device *dev)
77 {
78         struct drm_device *drm_dev = dev_get_drvdata(dev);
79
80         return drm_mode_config_helper_suspend(drm_dev);
81 }
82
83 static int bochs_pm_resume(struct device *dev)
84 {
85         struct drm_device *drm_dev = dev_get_drvdata(dev);
86
87         return drm_mode_config_helper_resume(drm_dev);
88 }
89 #endif
90
91 static const struct dev_pm_ops bochs_pm_ops = {
92         SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
93                                 bochs_pm_resume)
94 };
95
96 /* ---------------------------------------------------------------------- */
97 /* pci interface                                                          */
98
99 static int bochs_pci_probe(struct pci_dev *pdev,
100                            const struct pci_device_id *ent)
101 {
102         struct drm_device *dev;
103         unsigned long fbsize;
104         int ret;
105
106         fbsize = pci_resource_len(pdev, 0);
107         if (fbsize < 4 * 1024 * 1024) {
108                 DRM_ERROR("less than 4 MB video memory, ignoring device\n");
109                 return -ENOMEM;
110         }
111
112         ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "bochsdrmfb");
113         if (ret)
114                 return ret;
115
116         dev = drm_dev_alloc(&bochs_driver, &pdev->dev);
117         if (IS_ERR(dev))
118                 return PTR_ERR(dev);
119
120         ret = pci_enable_device(pdev);
121         if (ret)
122                 goto err_free_dev;
123
124         pci_set_drvdata(pdev, dev);
125
126         ret = bochs_load(dev);
127         if (ret)
128                 goto err_free_dev;
129
130         ret = drm_dev_register(dev, 0);
131         if (ret)
132                 goto err_unload;
133
134         drm_fbdev_generic_setup(dev, 32);
135         return ret;
136
137 err_unload:
138         bochs_unload(dev);
139 err_free_dev:
140         drm_dev_put(dev);
141         return ret;
142 }
143
144 static void bochs_pci_remove(struct pci_dev *pdev)
145 {
146         struct drm_device *dev = pci_get_drvdata(pdev);
147
148         drm_dev_unplug(dev);
149         drm_atomic_helper_shutdown(dev);
150         bochs_hw_fini(dev);
151         drm_dev_put(dev);
152 }
153
154 static const struct pci_device_id bochs_pci_tbl[] = {
155         {
156                 .vendor      = 0x1234,
157                 .device      = 0x1111,
158                 .subvendor   = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
159                 .subdevice   = PCI_SUBDEVICE_ID_QEMU,
160                 .driver_data = BOCHS_QEMU_STDVGA,
161         },
162         {
163                 .vendor      = 0x1234,
164                 .device      = 0x1111,
165                 .subvendor   = PCI_ANY_ID,
166                 .subdevice   = PCI_ANY_ID,
167                 .driver_data = BOCHS_UNKNOWN,
168         },
169         { /* end of list */ }
170 };
171
172 static struct pci_driver bochs_pci_driver = {
173         .name =         "bochs-drm",
174         .id_table =     bochs_pci_tbl,
175         .probe =        bochs_pci_probe,
176         .remove =       bochs_pci_remove,
177         .driver.pm =    &bochs_pm_ops,
178 };
179
180 /* ---------------------------------------------------------------------- */
181 /* module init/exit                                                       */
182
183 static int __init bochs_init(void)
184 {
185         if (vgacon_text_force() && bochs_modeset == -1)
186                 return -EINVAL;
187
188         if (bochs_modeset == 0)
189                 return -EINVAL;
190
191         return pci_register_driver(&bochs_pci_driver);
192 }
193
194 static void __exit bochs_exit(void)
195 {
196         pci_unregister_driver(&bochs_pci_driver);
197 }
198
199 module_init(bochs_init);
200 module_exit(bochs_exit);
201
202 MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
203 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
204 MODULE_LICENSE("GPL");