]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/bochs/bochs_drv.c
f48b212a2535188bf30fbd8d91efa4ad4b5ee652
[linux.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         .fops                   = &bochs_fops,
70         .name                   = "bochs-drm",
71         .desc                   = "bochs dispi vga interface (qemu stdvga)",
72         .date                   = "20130925",
73         .major                  = 1,
74         .minor                  = 0,
75         DRM_GEM_VRAM_DRIVER,
76 };
77
78 /* ---------------------------------------------------------------------- */
79 /* pm interface                                                           */
80
81 #ifdef CONFIG_PM_SLEEP
82 static int bochs_pm_suspend(struct device *dev)
83 {
84         struct pci_dev *pdev = to_pci_dev(dev);
85         struct drm_device *drm_dev = pci_get_drvdata(pdev);
86
87         return drm_mode_config_helper_suspend(drm_dev);
88 }
89
90 static int bochs_pm_resume(struct device *dev)
91 {
92         struct pci_dev *pdev = to_pci_dev(dev);
93         struct drm_device *drm_dev = pci_get_drvdata(pdev);
94
95         return drm_mode_config_helper_resume(drm_dev);
96 }
97 #endif
98
99 static const struct dev_pm_ops bochs_pm_ops = {
100         SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
101                                 bochs_pm_resume)
102 };
103
104 /* ---------------------------------------------------------------------- */
105 /* pci interface                                                          */
106
107 static int bochs_pci_probe(struct pci_dev *pdev,
108                            const struct pci_device_id *ent)
109 {
110         struct drm_device *dev;
111         unsigned long fbsize;
112         int ret;
113
114         fbsize = pci_resource_len(pdev, 0);
115         if (fbsize < 4 * 1024 * 1024) {
116                 DRM_ERROR("less than 4 MB video memory, ignoring device\n");
117                 return -ENOMEM;
118         }
119
120         ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, 0, "bochsdrmfb");
121         if (ret)
122                 return ret;
123
124         dev = drm_dev_alloc(&bochs_driver, &pdev->dev);
125         if (IS_ERR(dev))
126                 return PTR_ERR(dev);
127
128         ret = pci_enable_device(pdev);
129         if (ret)
130                 goto err_free_dev;
131
132         dev->pdev = pdev;
133         pci_set_drvdata(pdev, dev);
134
135         ret = bochs_load(dev);
136         if (ret)
137                 goto err_free_dev;
138
139         ret = drm_dev_register(dev, 0);
140         if (ret)
141                 goto err_unload;
142
143         drm_fbdev_generic_setup(dev, 32);
144         return ret;
145
146 err_unload:
147         bochs_unload(dev);
148 err_free_dev:
149         drm_dev_put(dev);
150         return ret;
151 }
152
153 static void bochs_pci_remove(struct pci_dev *pdev)
154 {
155         struct drm_device *dev = pci_get_drvdata(pdev);
156
157         drm_atomic_helper_shutdown(dev);
158         drm_dev_unregister(dev);
159         bochs_unload(dev);
160         drm_dev_put(dev);
161 }
162
163 static const struct pci_device_id bochs_pci_tbl[] = {
164         {
165                 .vendor      = 0x1234,
166                 .device      = 0x1111,
167                 .subvendor   = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
168                 .subdevice   = PCI_SUBDEVICE_ID_QEMU,
169                 .driver_data = BOCHS_QEMU_STDVGA,
170         },
171         {
172                 .vendor      = 0x1234,
173                 .device      = 0x1111,
174                 .subvendor   = PCI_ANY_ID,
175                 .subdevice   = PCI_ANY_ID,
176                 .driver_data = BOCHS_UNKNOWN,
177         },
178         { /* end of list */ }
179 };
180
181 static struct pci_driver bochs_pci_driver = {
182         .name =         "bochs-drm",
183         .id_table =     bochs_pci_tbl,
184         .probe =        bochs_pci_probe,
185         .remove =       bochs_pci_remove,
186         .driver.pm =    &bochs_pm_ops,
187 };
188
189 /* ---------------------------------------------------------------------- */
190 /* module init/exit                                                       */
191
192 static int __init bochs_init(void)
193 {
194         if (vgacon_text_force() && bochs_modeset == -1)
195                 return -EINVAL;
196
197         if (bochs_modeset == 0)
198                 return -EINVAL;
199
200         return pci_register_driver(&bochs_pci_driver);
201 }
202
203 static void __exit bochs_exit(void)
204 {
205         pci_unregister_driver(&bochs_pci_driver);
206 }
207
208 module_init(bochs_init);
209 module_exit(bochs_exit);
210
211 MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
212 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
213 MODULE_LICENSE("GPL");