drm: Split out drm_probe_helper.h
[linux-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>
fcd70cd3 12#include <drm/drm_probe_helper.h>
0a6659bd
GH
13
14#include "bochs.h"
15
1acf5661
MS
16static int bochs_modeset = -1;
17module_param_named(modeset, bochs_modeset, int, 0444);
18MODULE_PARM_DESC(modeset, "enable/disable kernel modesetting");
19
0a6659bd
GH
20/* ---------------------------------------------------------------------- */
21/* drm interface */
22
11b3c20b 23static void bochs_unload(struct drm_device *dev)
0a6659bd
GH
24{
25 struct bochs_device *bochs = dev->dev_private;
26
0a6659bd
GH
27 bochs_kms_fini(bochs);
28 bochs_mm_fini(bochs);
29 bochs_hw_fini(dev);
30 kfree(bochs);
31 dev->dev_private = NULL;
0a6659bd
GH
32}
33
7780eb9c 34static int bochs_load(struct drm_device *dev)
0a6659bd
GH
35{
36 struct bochs_device *bochs;
37 int ret;
38
39 bochs = kzalloc(sizeof(*bochs), GFP_KERNEL);
40 if (bochs == NULL)
41 return -ENOMEM;
42 dev->dev_private = bochs;
43 bochs->dev = dev;
44
7780eb9c 45 ret = bochs_hw_init(dev);
0a6659bd
GH
46 if (ret)
47 goto err;
48
49 ret = bochs_mm_init(bochs);
50 if (ret)
51 goto err;
52
53 ret = bochs_kms_init(bochs);
54 if (ret)
55 goto err;
56
0a6659bd
GH
57 return 0;
58
59err:
60 bochs_unload(dev);
61 return ret;
62}
63
64static const struct file_operations bochs_fops = {
65 .owner = THIS_MODULE,
66 .open = drm_open,
67 .release = drm_release,
68 .unlocked_ioctl = drm_ioctl,
0a6659bd 69 .compat_ioctl = drm_compat_ioctl,
0a6659bd
GH
70 .poll = drm_poll,
71 .read = drm_read,
72 .llseek = no_llseek,
73 .mmap = bochs_mmap,
74};
75
76static struct drm_driver bochs_driver = {
132a4534
GH
77 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC |
78 DRIVER_PRIME,
0a6659bd
GH
79 .fops = &bochs_fops,
80 .name = "bochs-drm",
81 .desc = "bochs dispi vga interface (qemu stdvga)",
82 .date = "20130925",
83 .major = 1,
84 .minor = 0,
6f2c1c15 85 .gem_free_object_unlocked = bochs_gem_free_object,
0a6659bd
GH
86 .dumb_create = bochs_dumb_create,
87 .dumb_map_offset = bochs_dumb_mmap_offset,
132a4534
GH
88
89 .gem_prime_export = drm_gem_prime_export,
90 .gem_prime_import = drm_gem_prime_import,
91 .gem_prime_pin = bochs_gem_prime_pin,
92 .gem_prime_unpin = bochs_gem_prime_unpin,
93 .gem_prime_vmap = bochs_gem_prime_vmap,
94 .gem_prime_vunmap = bochs_gem_prime_vunmap,
95 .gem_prime_mmap = bochs_gem_prime_mmap,
0a6659bd
GH
96};
97
b8ccd70f
GH
98/* ---------------------------------------------------------------------- */
99/* pm interface */
100
150cee9c 101#ifdef CONFIG_PM_SLEEP
b8ccd70f
GH
102static 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);
b8ccd70f 106
568a4c72 107 return drm_mode_config_helper_suspend(drm_dev);
b8ccd70f
GH
108}
109
110static int bochs_pm_resume(struct device *dev)
111{
112 struct pci_dev *pdev = to_pci_dev(dev);
113 struct drm_device *drm_dev = pci_get_drvdata(pdev);
b8ccd70f 114
568a4c72 115 return drm_mode_config_helper_resume(drm_dev);
b8ccd70f 116}
150cee9c 117#endif
b8ccd70f
GH
118
119static const struct dev_pm_ops bochs_pm_ops = {
120 SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
121 bochs_pm_resume)
122};
123
0a6659bd
GH
124/* ---------------------------------------------------------------------- */
125/* pci interface */
126
0a6659bd
GH
127static int bochs_pci_probe(struct pci_dev *pdev,
128 const struct pci_device_id *ent)
129{
7780eb9c 130 struct drm_device *dev;
de595762 131 unsigned long fbsize;
0a6659bd
GH
132 int ret;
133
de595762
GH
134 fbsize = pci_resource_len(pdev, 0);
135 if (fbsize < 4 * 1024 * 1024) {
136 DRM_ERROR("less than 4 MB video memory, ignoring device\n");
137 return -ENOMEM;
138 }
139
77d1ed41 140 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, 0, "bochsdrmfb");
0a6659bd
GH
141 if (ret)
142 return ret;
143
7780eb9c
PW
144 dev = drm_dev_alloc(&bochs_driver, &pdev->dev);
145 if (IS_ERR(dev))
146 return PTR_ERR(dev);
147
148 dev->pdev = pdev;
149 pci_set_drvdata(pdev, dev);
150
151 ret = bochs_load(dev);
152 if (ret)
153 goto err_free_dev;
154
155 ret = drm_dev_register(dev, 0);
156 if (ret)
157 goto err_unload;
158
7a0483ac 159 drm_fbdev_generic_setup(dev, 32);
7780eb9c
PW
160 return ret;
161
162err_unload:
163 bochs_unload(dev);
164err_free_dev:
165 drm_dev_put(dev);
166 return ret;
0a6659bd
GH
167}
168
169static void bochs_pci_remove(struct pci_dev *pdev)
170{
171 struct drm_device *dev = pci_get_drvdata(pdev);
172
7780eb9c
PW
173 drm_dev_unregister(dev);
174 bochs_unload(dev);
175 drm_dev_put(dev);
0a6659bd
GH
176}
177
9baa3c34 178static const struct pci_device_id bochs_pci_tbl[] = {
0a6659bd
GH
179 {
180 .vendor = 0x1234,
181 .device = 0x1111,
caf02abf
RJ
182 .subvendor = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
183 .subdevice = PCI_SUBDEVICE_ID_QEMU,
0a6659bd
GH
184 .driver_data = BOCHS_QEMU_STDVGA,
185 },
186 {
187 .vendor = 0x1234,
188 .device = 0x1111,
189 .subvendor = PCI_ANY_ID,
190 .subdevice = PCI_ANY_ID,
191 .driver_data = BOCHS_UNKNOWN,
192 },
193 { /* end of list */ }
194};
195
196static struct pci_driver bochs_pci_driver = {
197 .name = "bochs-drm",
198 .id_table = bochs_pci_tbl,
199 .probe = bochs_pci_probe,
200 .remove = bochs_pci_remove,
b8ccd70f 201 .driver.pm = &bochs_pm_ops,
0a6659bd
GH
202};
203
204/* ---------------------------------------------------------------------- */
205/* module init/exit */
206
207static int __init bochs_init(void)
208{
1acf5661
MS
209 if (vgacon_text_force() && bochs_modeset == -1)
210 return -EINVAL;
211
212 if (bochs_modeset == 0)
213 return -EINVAL;
214
10631d72 215 return pci_register_driver(&bochs_pci_driver);
0a6659bd
GH
216}
217
218static void __exit bochs_exit(void)
219{
10631d72 220 pci_unregister_driver(&bochs_pci_driver);
0a6659bd
GH
221}
222
223module_init(bochs_init);
224module_exit(bochs_exit);
225
226MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
227MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
228MODULE_LICENSE("GPL");