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