Merge airlied/drm-next into drm-misc-next
[linux-2.6-block.git] / drivers / gpu / drm / bochs / bochs_drv.c
CommitLineData
0a6659bd
GH
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>
44adece5 11#include <drm/drm_fb_helper.h>
0a6659bd
GH
12
13#include "bochs.h"
14
1acf5661
MS
15static int bochs_modeset = -1;
16module_param_named(modeset, bochs_modeset, int, 0444);
17MODULE_PARM_DESC(modeset, "enable/disable kernel modesetting");
18
0a6659bd
GH
19static bool enable_fbdev = true;
20module_param_named(fbdev, enable_fbdev, bool, 0444);
21MODULE_PARM_DESC(fbdev, "register fbdev device");
22
23/* ---------------------------------------------------------------------- */
24/* drm interface */
25
11b3c20b 26static void bochs_unload(struct drm_device *dev)
0a6659bd
GH
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;
0a6659bd
GH
36}
37
38static 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
66err:
67 bochs_unload(dev);
68 return ret;
69}
70
71static const struct file_operations bochs_fops = {
72 .owner = THIS_MODULE,
73 .open = drm_open,
74 .release = drm_release,
75 .unlocked_ioctl = drm_ioctl,
0a6659bd 76 .compat_ioctl = drm_compat_ioctl,
0a6659bd
GH
77 .poll = drm_poll,
78 .read = drm_read,
79 .llseek = no_llseek,
80 .mmap = bochs_mmap,
81};
82
83static struct drm_driver bochs_driver = {
84 .driver_features = DRIVER_GEM | DRIVER_MODESET,
85 .load = bochs_load,
86 .unload = bochs_unload,
915b4d11 87 .set_busid = drm_pci_set_busid,
0a6659bd
GH
88 .fops = &bochs_fops,
89 .name = "bochs-drm",
90 .desc = "bochs dispi vga interface (qemu stdvga)",
91 .date = "20130925",
92 .major = 1,
93 .minor = 0,
6f2c1c15 94 .gem_free_object_unlocked = bochs_gem_free_object,
0a6659bd
GH
95 .dumb_create = bochs_dumb_create,
96 .dumb_map_offset = bochs_dumb_mmap_offset,
97 .dumb_destroy = drm_gem_dumb_destroy,
98};
99
b8ccd70f
GH
100/* ---------------------------------------------------------------------- */
101/* pm interface */
102
150cee9c 103#ifdef CONFIG_PM_SLEEP
b8ccd70f
GH
104static int bochs_pm_suspend(struct device *dev)
105{
106 struct pci_dev *pdev = to_pci_dev(dev);
107 struct drm_device *drm_dev = pci_get_drvdata(pdev);
108 struct bochs_device *bochs = drm_dev->dev_private;
109
110 drm_kms_helper_poll_disable(drm_dev);
111
112 if (bochs->fb.initialized) {
113 console_lock();
6a752972 114 drm_fb_helper_set_suspend(&bochs->fb.helper, 1);
b8ccd70f
GH
115 console_unlock();
116 }
117
118 return 0;
119}
120
121static int bochs_pm_resume(struct device *dev)
122{
123 struct pci_dev *pdev = to_pci_dev(dev);
124 struct drm_device *drm_dev = pci_get_drvdata(pdev);
125 struct bochs_device *bochs = drm_dev->dev_private;
126
127 drm_helper_resume_force_mode(drm_dev);
128
129 if (bochs->fb.initialized) {
130 console_lock();
6a752972 131 drm_fb_helper_set_suspend(&bochs->fb.helper, 0);
b8ccd70f
GH
132 console_unlock();
133 }
134
135 drm_kms_helper_poll_enable(drm_dev);
136 return 0;
137}
150cee9c 138#endif
b8ccd70f
GH
139
140static const struct dev_pm_ops bochs_pm_ops = {
141 SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
142 bochs_pm_resume)
143};
144
0a6659bd
GH
145/* ---------------------------------------------------------------------- */
146/* pci interface */
147
148static int bochs_kick_out_firmware_fb(struct pci_dev *pdev)
149{
150 struct apertures_struct *ap;
151
152 ap = alloc_apertures(1);
153 if (!ap)
154 return -ENOMEM;
155
156 ap->ranges[0].base = pci_resource_start(pdev, 0);
157 ap->ranges[0].size = pci_resource_len(pdev, 0);
44adece5 158 drm_fb_helper_remove_conflicting_framebuffers(ap, "bochsdrmfb", false);
0a6659bd
GH
159 kfree(ap);
160
161 return 0;
162}
163
164static int bochs_pci_probe(struct pci_dev *pdev,
165 const struct pci_device_id *ent)
166{
de595762 167 unsigned long fbsize;
0a6659bd
GH
168 int ret;
169
de595762
GH
170 fbsize = pci_resource_len(pdev, 0);
171 if (fbsize < 4 * 1024 * 1024) {
172 DRM_ERROR("less than 4 MB video memory, ignoring device\n");
173 return -ENOMEM;
174 }
175
0a6659bd
GH
176 ret = bochs_kick_out_firmware_fb(pdev);
177 if (ret)
178 return ret;
179
180 return drm_get_pci_dev(pdev, ent, &bochs_driver);
181}
182
183static void bochs_pci_remove(struct pci_dev *pdev)
184{
185 struct drm_device *dev = pci_get_drvdata(pdev);
186
187 drm_put_dev(dev);
188}
189
9baa3c34 190static const struct pci_device_id bochs_pci_tbl[] = {
0a6659bd
GH
191 {
192 .vendor = 0x1234,
193 .device = 0x1111,
caf02abf
RJ
194 .subvendor = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
195 .subdevice = PCI_SUBDEVICE_ID_QEMU,
0a6659bd
GH
196 .driver_data = BOCHS_QEMU_STDVGA,
197 },
198 {
199 .vendor = 0x1234,
200 .device = 0x1111,
201 .subvendor = PCI_ANY_ID,
202 .subdevice = PCI_ANY_ID,
203 .driver_data = BOCHS_UNKNOWN,
204 },
205 { /* end of list */ }
206};
207
208static struct pci_driver bochs_pci_driver = {
209 .name = "bochs-drm",
210 .id_table = bochs_pci_tbl,
211 .probe = bochs_pci_probe,
212 .remove = bochs_pci_remove,
b8ccd70f 213 .driver.pm = &bochs_pm_ops,
0a6659bd
GH
214};
215
216/* ---------------------------------------------------------------------- */
217/* module init/exit */
218
219static int __init bochs_init(void)
220{
1acf5661
MS
221 if (vgacon_text_force() && bochs_modeset == -1)
222 return -EINVAL;
223
224 if (bochs_modeset == 0)
225 return -EINVAL;
226
0a6659bd
GH
227 return drm_pci_init(&bochs_driver, &bochs_pci_driver);
228}
229
230static void __exit bochs_exit(void)
231{
232 drm_pci_exit(&bochs_driver, &bochs_pci_driver);
233}
234
235module_init(bochs_init);
236module_exit(bochs_exit);
237
238MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
239MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
240MODULE_LICENSE("GPL");