1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/module.h>
32#include <linux/console.h>
33
34#include <drm/drmP.h>
35#include <drm/drm.h>
36#include <drm/drm_crtc_helper.h>
37#include "qxl_drv.h"
38#include "qxl_object.h"
39
40static const struct pci_device_id pciidlist[] = {
41 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
42 0xffff00, 0 },
43 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
44 0xffff00, 0 },
45 { 0, 0, 0 },
46};
47MODULE_DEVICE_TABLE(pci, pciidlist);
48
49static int qxl_modeset = -1;
50int qxl_num_crtc = 4;
51
52MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
53module_param_named(modeset, qxl_modeset, int, 0400);
54
55MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
56module_param_named(num_heads, qxl_num_crtc, int, 0400);
57
58static struct drm_driver qxl_driver;
59static struct pci_driver qxl_pci_driver;
60
61static int
62qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
63{
64 struct qxl_device *qdev;
65 int ret;
66
67 if (pdev->revision < 4) {
68 DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
69 " use xf86-video-qxl in user mode");
70 return -EINVAL;
71 }
72
73 qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL);
74 if (!qdev)
75 return -ENOMEM;
76
77 ret = pci_enable_device(pdev);
78 if (ret)
79 goto free_dev;
80
81 ret = qxl_device_init(qdev, &qxl_driver, pdev);
82 if (ret)
83 goto disable_pci;
84
85 ret = qxl_modeset_init(qdev);
86 if (ret)
87 goto unload;
88
89 drm_kms_helper_poll_init(&qdev->ddev);
90
91
92 ret = drm_dev_register(&qdev->ddev, ent->driver_data);
93 if (ret)
94 goto modeset_cleanup;
95
96 return 0;
97
98modeset_cleanup:
99 qxl_modeset_fini(qdev);
100unload:
101 qxl_device_fini(qdev);
102disable_pci:
103 pci_disable_device(pdev);
104free_dev:
105 kfree(qdev);
106 return ret;
107}
108
109static void
110qxl_pci_remove(struct pci_dev *pdev)
111{
112 struct drm_device *dev = pci_get_drvdata(pdev);
113 struct qxl_device *qdev = dev->dev_private;
114
115 drm_dev_unregister(dev);
116
117 qxl_modeset_fini(qdev);
118 qxl_device_fini(qdev);
119
120 dev->dev_private = NULL;
121 kfree(qdev);
122 drm_dev_unref(dev);
123}
124
125static const struct file_operations qxl_fops = {
126 .owner = THIS_MODULE,
127 .open = drm_open,
128 .release = drm_release,
129 .unlocked_ioctl = drm_ioctl,
130 .poll = drm_poll,
131 .read = drm_read,
132 .mmap = qxl_mmap,
133};
134
135static int qxl_drm_freeze(struct drm_device *dev)
136{
137 struct pci_dev *pdev = dev->pdev;
138 struct qxl_device *qdev = dev->dev_private;
139 struct drm_crtc *crtc;
140
141 drm_kms_helper_poll_disable(dev);
142
143 console_lock();
144 qxl_fbdev_set_suspend(qdev, 1);
145 console_unlock();
146
147
148 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
149 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
150 if (crtc->enabled)
151 (*crtc_funcs->disable)(crtc);
152 }
153
154 qxl_destroy_monitors_object(qdev);
155 qxl_surf_evict(qdev);
156 qxl_vram_evict(qdev);
157
158 while (!qxl_check_idle(qdev->command_ring));
159 while (!qxl_check_idle(qdev->release_ring))
160 qxl_queue_garbage_collect(qdev, 1);
161
162 pci_save_state(pdev);
163
164 return 0;
165}
166
167static int qxl_drm_resume(struct drm_device *dev, bool thaw)
168{
169 struct qxl_device *qdev = dev->dev_private;
170
171 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
172 if (!thaw) {
173 qxl_reinit_memslots(qdev);
174 qxl_ring_init_hdr(qdev->release_ring);
175 }
176
177 qxl_create_monitors_object(qdev);
178 drm_helper_resume_force_mode(dev);
179
180 console_lock();
181 qxl_fbdev_set_suspend(qdev, 0);
182 console_unlock();
183
184 drm_kms_helper_poll_enable(dev);
185 return 0;
186}
187
188static int qxl_pm_suspend(struct device *dev)
189{
190 struct pci_dev *pdev = to_pci_dev(dev);
191 struct drm_device *drm_dev = pci_get_drvdata(pdev);
192 int error;
193
194 error = qxl_drm_freeze(drm_dev);
195 if (error)
196 return error;
197
198 pci_disable_device(pdev);
199 pci_set_power_state(pdev, PCI_D3hot);
200 return 0;
201}
202
203static int qxl_pm_resume(struct device *dev)
204{
205 struct pci_dev *pdev = to_pci_dev(dev);
206 struct drm_device *drm_dev = pci_get_drvdata(pdev);
207
208 pci_set_power_state(pdev, PCI_D0);
209 pci_restore_state(pdev);
210 if (pci_enable_device(pdev)) {
211 return -EIO;
212 }
213
214 return qxl_drm_resume(drm_dev, false);
215}
216
217static int qxl_pm_thaw(struct device *dev)
218{
219 struct pci_dev *pdev = to_pci_dev(dev);
220 struct drm_device *drm_dev = pci_get_drvdata(pdev);
221
222 return qxl_drm_resume(drm_dev, true);
223}
224
225static int qxl_pm_freeze(struct device *dev)
226{
227 struct pci_dev *pdev = to_pci_dev(dev);
228 struct drm_device *drm_dev = pci_get_drvdata(pdev);
229
230 return qxl_drm_freeze(drm_dev);
231}
232
233static int qxl_pm_restore(struct device *dev)
234{
235 struct pci_dev *pdev = to_pci_dev(dev);
236 struct drm_device *drm_dev = pci_get_drvdata(pdev);
237 struct qxl_device *qdev = drm_dev->dev_private;
238
239 qxl_io_reset(qdev);
240 return qxl_drm_resume(drm_dev, false);
241}
242
243static const struct dev_pm_ops qxl_pm_ops = {
244 .suspend = qxl_pm_suspend,
245 .resume = qxl_pm_resume,
246 .freeze = qxl_pm_freeze,
247 .thaw = qxl_pm_thaw,
248 .poweroff = qxl_pm_freeze,
249 .restore = qxl_pm_restore,
250};
251static struct pci_driver qxl_pci_driver = {
252 .name = DRIVER_NAME,
253 .id_table = pciidlist,
254 .probe = qxl_pci_probe,
255 .remove = qxl_pci_remove,
256 .driver.pm = &qxl_pm_ops,
257};
258
259static struct drm_driver qxl_driver = {
260 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
261 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED |
262 DRIVER_ATOMIC,
263
264 .dumb_create = qxl_mode_dumb_create,
265 .dumb_map_offset = qxl_mode_dumb_mmap,
266#if defined(CONFIG_DEBUG_FS)
267 .debugfs_init = qxl_debugfs_init,
268#endif
269 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
270 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
271 .gem_prime_export = drm_gem_prime_export,
272 .gem_prime_import = drm_gem_prime_import,
273 .gem_prime_pin = qxl_gem_prime_pin,
274 .gem_prime_unpin = qxl_gem_prime_unpin,
275 .gem_prime_get_sg_table = qxl_gem_prime_get_sg_table,
276 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
277 .gem_prime_vmap = qxl_gem_prime_vmap,
278 .gem_prime_vunmap = qxl_gem_prime_vunmap,
279 .gem_prime_mmap = qxl_gem_prime_mmap,
280 .gem_free_object_unlocked = qxl_gem_object_free,
281 .gem_open_object = qxl_gem_object_open,
282 .gem_close_object = qxl_gem_object_close,
283 .fops = &qxl_fops,
284 .ioctls = qxl_ioctls,
285 .irq_handler = qxl_irq_handler,
286 .name = DRIVER_NAME,
287 .desc = DRIVER_DESC,
288 .date = DRIVER_DATE,
289 .major = 0,
290 .minor = 1,
291 .patchlevel = 0,
292};
293
294static int __init qxl_init(void)
295{
296 if (vgacon_text_force() && qxl_modeset == -1)
297 return -EINVAL;
298
299 if (qxl_modeset == 0)
300 return -EINVAL;
301 qxl_driver.num_ioctls = qxl_max_ioctls;
302 return pci_register_driver(&qxl_pci_driver);
303}
304
305static void __exit qxl_exit(void)
306{
307 pci_unregister_driver(&qxl_pci_driver);
308}
309
310module_init(qxl_init);
311module_exit(qxl_exit);
312
313MODULE_AUTHOR(DRIVER_AUTHOR);
314MODULE_DESCRIPTION(DRIVER_DESC);
315MODULE_LICENSE("GPL and additional rights");
316