1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 */
7
8 #include <linux/mm.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <drm/drm_fb_helper.h>
12
13 #include "bochs.h"
14
15 static int bochs_modeset = -1;
16 module_param_named(modeset, bochs_modeset, int, 0444);
17 MODULE_PARM_DESC(modeset, "enable/disable kernel modesetting");
18
19 static bool enable_fbdev = true;
20 module_param_named(fbdev, enable_fbdev, bool, 0444);
21 MODULE_PARM_DESC(fbdev, "register fbdev device");
22
23 /* ---------------------------------------------------------------------- */
24 /* drm interface */
25
bochs_unload(struct drm_device * dev)26 static void bochs_unload(struct drm_device *dev)
27 {
28 struct bochs_device *bochs = dev->dev_private;
29
30 bochs_fbdev_fini(bochs);
31 bochs_kms_fini(bochs);
32 bochs_mm_fini(bochs);
33 bochs_hw_fini(dev);
34 kfree(bochs);
35 dev->dev_private = NULL;
36 }
37
bochs_load(struct drm_device * dev,unsigned long flags)38 static int bochs_load(struct drm_device *dev, unsigned long flags)
39 {
40 struct bochs_device *bochs;
41 int ret;
42
43 bochs = kzalloc(sizeof(*bochs), GFP_KERNEL);
44 if (bochs == NULL)
45 return -ENOMEM;
46 dev->dev_private = bochs;
47 bochs->dev = dev;
48
49 ret = bochs_hw_init(dev, flags);
50 if (ret)
51 goto err;
52
53 ret = bochs_mm_init(bochs);
54 if (ret)
55 goto err;
56
57 ret = bochs_kms_init(bochs);
58 if (ret)
59 goto err;
60
61 if (enable_fbdev)
62 bochs_fbdev_init(bochs);
63
64 return 0;
65
66 err:
67 bochs_unload(dev);
68 return ret;
69 }
70
71 static const struct file_operations bochs_fops = {
72 .owner = THIS_MODULE,
73 .open = drm_open,
74 .release = drm_release,
75 .unlocked_ioctl = drm_ioctl,
76 .compat_ioctl = drm_compat_ioctl,
77 .poll = drm_poll,
78 .read = drm_read,
79 .llseek = no_llseek,
80 .mmap = bochs_mmap,
81 };
82
83 static struct drm_driver bochs_driver = {
84 .driver_features = DRIVER_GEM | DRIVER_MODESET,
85 .load = bochs_load,
86 .unload = bochs_unload,
87 .fops = &bochs_fops,
88 .name = "bochs-drm",
89 .desc = "bochs dispi vga interface (qemu stdvga)",
90 .date = "20130925",
91 .major = 1,
92 .minor = 0,
93 .gem_free_object_unlocked = bochs_gem_free_object,
94 .dumb_create = bochs_dumb_create,
95 .dumb_map_offset = bochs_dumb_mmap_offset,
96 };
97
98 /* ---------------------------------------------------------------------- */
99 /* pm interface */
100
101 #ifdef CONFIG_PM_SLEEP
bochs_pm_suspend(struct device * dev)102 static int bochs_pm_suspend(struct device *dev)
103 {
104 struct pci_dev *pdev = to_pci_dev(dev);
105 struct drm_device *drm_dev = pci_get_drvdata(pdev);
106 struct bochs_device *bochs = drm_dev->dev_private;
107
108 drm_kms_helper_poll_disable(drm_dev);
109
110 if (bochs->fb.initialized) {
111 console_lock();
112 drm_fb_helper_set_suspend(&bochs->fb.helper, 1);
113 console_unlock();
114 }
115
116 return 0;
117 }
118
bochs_pm_resume(struct device * dev)119 static int bochs_pm_resume(struct device *dev)
120 {
121 struct pci_dev *pdev = to_pci_dev(dev);
122 struct drm_device *drm_dev = pci_get_drvdata(pdev);
123 struct bochs_device *bochs = drm_dev->dev_private;
124
125 drm_helper_resume_force_mode(drm_dev);
126
127 if (bochs->fb.initialized) {
128 console_lock();
129 drm_fb_helper_set_suspend(&bochs->fb.helper, 0);
130 console_unlock();
131 }
132
133 drm_kms_helper_poll_enable(drm_dev);
134 return 0;
135 }
136 #endif
137
138 static const struct dev_pm_ops bochs_pm_ops = {
139 SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
140 bochs_pm_resume)
141 };
142
143 /* ---------------------------------------------------------------------- */
144 /* pci interface */
145
bochs_kick_out_firmware_fb(struct pci_dev * pdev)146 static int bochs_kick_out_firmware_fb(struct pci_dev *pdev)
147 {
148 struct apertures_struct *ap;
149
150 ap = alloc_apertures(1);
151 if (!ap)
152 return -ENOMEM;
153
154 ap->ranges[0].base = pci_resource_start(pdev, 0);
155 ap->ranges[0].size = pci_resource_len(pdev, 0);
156 drm_fb_helper_remove_conflicting_framebuffers(ap, "bochsdrmfb", false);
157 kfree(ap);
158
159 return 0;
160 }
161
bochs_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)162 static int bochs_pci_probe(struct pci_dev *pdev,
163 const struct pci_device_id *ent)
164 {
165 unsigned long fbsize;
166 int ret;
167
168 fbsize = pci_resource_len(pdev, 0);
169 if (fbsize < 4 * 1024 * 1024) {
170 DRM_ERROR("less than 4 MB video memory, ignoring device\n");
171 return -ENOMEM;
172 }
173
174 ret = bochs_kick_out_firmware_fb(pdev);
175 if (ret)
176 return ret;
177
178 return drm_get_pci_dev(pdev, ent, &bochs_driver);
179 }
180
bochs_pci_remove(struct pci_dev * pdev)181 static void bochs_pci_remove(struct pci_dev *pdev)
182 {
183 struct drm_device *dev = pci_get_drvdata(pdev);
184
185 drm_put_dev(dev);
186 }
187
188 static const struct pci_device_id bochs_pci_tbl[] = {
189 {
190 .vendor = 0x1234,
191 .device = 0x1111,
192 .subvendor = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
193 .subdevice = PCI_SUBDEVICE_ID_QEMU,
194 .driver_data = BOCHS_QEMU_STDVGA,
195 },
196 {
197 .vendor = 0x1234,
198 .device = 0x1111,
199 .subvendor = PCI_ANY_ID,
200 .subdevice = PCI_ANY_ID,
201 .driver_data = BOCHS_UNKNOWN,
202 },
203 { /* end of list */ }
204 };
205
206 static struct pci_driver bochs_pci_driver = {
207 .name = "bochs-drm",
208 .id_table = bochs_pci_tbl,
209 .probe = bochs_pci_probe,
210 .remove = bochs_pci_remove,
211 .driver.pm = &bochs_pm_ops,
212 };
213
214 /* ---------------------------------------------------------------------- */
215 /* module init/exit */
216
bochs_init(void)217 static int __init bochs_init(void)
218 {
219 if (vgacon_text_force() && bochs_modeset == -1)
220 return -EINVAL;
221
222 if (bochs_modeset == 0)
223 return -EINVAL;
224
225 return pci_register_driver(&bochs_pci_driver);
226 }
227
bochs_exit(void)228 static void __exit bochs_exit(void)
229 {
230 pci_unregister_driver(&bochs_pci_driver);
231 }
232
233 module_init(bochs_init);
234 module_exit(bochs_exit);
235
236 MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
237 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
238 MODULE_LICENSE("GPL");
239