1 /* qxl_drv.c -- QXL driver -*- linux-c -*-
2 *
3 * Copyright 2011 Red Hat, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Dave Airlie <airlie@redhat.com>
27 * Alon Levy <alevy@redhat.com>
28 */
29
30 #include "qxl_drv.h"
31 #include <linux/console.h>
32 #include <linux/module.h>
33 #include <linux/pci.h>
34
35 #include <drm/drm.h>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_drv.h>
38 #include <drm/drm_file.h>
39 #include <drm/drm_modeset_helper.h>
40 #include <drm/drm_prime.h>
41 #include <drm/drm_probe_helper.h>
42
43 #include "qxl_object.h"
44
45 static const struct pci_device_id pciidlist[] = {
46 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
47 0xffff00, 0 },
48 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
49 0xffff00, 0 },
50 { 0, 0, 0 },
51 };
52 MODULE_DEVICE_TABLE(pci, pciidlist);
53
54 static int qxl_modeset = -1;
55 int qxl_num_crtc = 4;
56
57 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
58 module_param_named(modeset, qxl_modeset, int, 0400);
59
60 MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
61 module_param_named(num_heads, qxl_num_crtc, int, 0400);
62
63 static struct drm_driver qxl_driver;
64 static struct pci_driver qxl_pci_driver;
65
is_vga(struct pci_dev * pdev)66 static bool is_vga(struct pci_dev *pdev)
67 {
68 return pdev->class == PCI_CLASS_DISPLAY_VGA << 8;
69 }
70
71 static int
qxl_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)72 qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
73 {
74 struct qxl_device *qdev;
75 int ret;
76
77 if (pdev->revision < 4) {
78 DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
79 " use xf86-video-qxl in user mode");
80 return -EINVAL; /* TODO: ENODEV ? */
81 }
82
83 qdev = devm_drm_dev_alloc(&pdev->dev, &qxl_driver,
84 struct qxl_device, ddev);
85 if (IS_ERR(qdev)) {
86 pr_err("Unable to init drm dev");
87 return -ENOMEM;
88 }
89
90 ret = pci_enable_device(pdev);
91 if (ret)
92 return ret;
93
94 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "qxl");
95 if (ret)
96 goto disable_pci;
97
98 if (is_vga(pdev) && pdev->revision < 5) {
99 ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
100 if (ret) {
101 DRM_ERROR("can't get legacy vga ioports\n");
102 goto disable_pci;
103 }
104 }
105
106 ret = qxl_device_init(qdev, pdev);
107 if (ret)
108 goto put_vga;
109
110 ret = qxl_modeset_init(qdev);
111 if (ret)
112 goto unload;
113
114 drm_kms_helper_poll_init(&qdev->ddev);
115
116 /* Complete initialization. */
117 ret = drm_dev_register(&qdev->ddev, ent->driver_data);
118 if (ret)
119 goto modeset_cleanup;
120
121 drm_fbdev_generic_setup(&qdev->ddev, 32);
122 return 0;
123
124 modeset_cleanup:
125 qxl_modeset_fini(qdev);
126 unload:
127 qxl_device_fini(qdev);
128 put_vga:
129 if (is_vga(pdev) && pdev->revision < 5)
130 vga_put(pdev, VGA_RSRC_LEGACY_IO);
131 disable_pci:
132 pci_disable_device(pdev);
133
134 return ret;
135 }
136
qxl_drm_release(struct drm_device * dev)137 static void qxl_drm_release(struct drm_device *dev)
138 {
139 struct qxl_device *qdev = to_qxl(dev);
140
141 /*
142 * TODO: qxl_device_fini() call should be in qxl_pci_remove(),
143 * reordering qxl_modeset_fini() + qxl_device_fini() calls is
144 * non-trivial though.
145 */
146 qxl_modeset_fini(qdev);
147 qxl_device_fini(qdev);
148 }
149
150 static void
qxl_pci_remove(struct pci_dev * pdev)151 qxl_pci_remove(struct pci_dev *pdev)
152 {
153 struct drm_device *dev = pci_get_drvdata(pdev);
154
155 drm_dev_unregister(dev);
156 drm_atomic_helper_shutdown(dev);
157 if (is_vga(pdev) && pdev->revision < 5)
158 vga_put(pdev, VGA_RSRC_LEGACY_IO);
159 }
160
161 DEFINE_DRM_GEM_FOPS(qxl_fops);
162
qxl_drm_freeze(struct drm_device * dev)163 static int qxl_drm_freeze(struct drm_device *dev)
164 {
165 struct pci_dev *pdev = to_pci_dev(dev->dev);
166 struct qxl_device *qdev = to_qxl(dev);
167 int ret;
168
169 ret = drm_mode_config_helper_suspend(dev);
170 if (ret)
171 return ret;
172
173 qxl_destroy_monitors_object(qdev);
174 qxl_surf_evict(qdev);
175 qxl_vram_evict(qdev);
176
177 while (!qxl_check_idle(qdev->command_ring));
178 while (!qxl_check_idle(qdev->release_ring))
179 qxl_queue_garbage_collect(qdev, 1);
180
181 pci_save_state(pdev);
182
183 return 0;
184 }
185
qxl_drm_resume(struct drm_device * dev,bool thaw)186 static int qxl_drm_resume(struct drm_device *dev, bool thaw)
187 {
188 struct qxl_device *qdev = to_qxl(dev);
189
190 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
191 if (!thaw) {
192 qxl_reinit_memslots(qdev);
193 qxl_ring_init_hdr(qdev->release_ring);
194 }
195
196 qxl_create_monitors_object(qdev);
197 return drm_mode_config_helper_resume(dev);
198 }
199
qxl_pm_suspend(struct device * dev)200 static int qxl_pm_suspend(struct device *dev)
201 {
202 struct pci_dev *pdev = to_pci_dev(dev);
203 struct drm_device *drm_dev = pci_get_drvdata(pdev);
204 int error;
205
206 error = qxl_drm_freeze(drm_dev);
207 if (error)
208 return error;
209
210 pci_disable_device(pdev);
211 pci_set_power_state(pdev, PCI_D3hot);
212 return 0;
213 }
214
qxl_pm_resume(struct device * dev)215 static int qxl_pm_resume(struct device *dev)
216 {
217 struct pci_dev *pdev = to_pci_dev(dev);
218 struct drm_device *drm_dev = pci_get_drvdata(pdev);
219
220 pci_set_power_state(pdev, PCI_D0);
221 pci_restore_state(pdev);
222 if (pci_enable_device(pdev)) {
223 return -EIO;
224 }
225
226 return qxl_drm_resume(drm_dev, false);
227 }
228
qxl_pm_thaw(struct device * dev)229 static int qxl_pm_thaw(struct device *dev)
230 {
231 struct drm_device *drm_dev = dev_get_drvdata(dev);
232
233 return qxl_drm_resume(drm_dev, true);
234 }
235
qxl_pm_freeze(struct device * dev)236 static int qxl_pm_freeze(struct device *dev)
237 {
238 struct drm_device *drm_dev = dev_get_drvdata(dev);
239
240 return qxl_drm_freeze(drm_dev);
241 }
242
qxl_pm_restore(struct device * dev)243 static int qxl_pm_restore(struct device *dev)
244 {
245 struct pci_dev *pdev = to_pci_dev(dev);
246 struct drm_device *drm_dev = pci_get_drvdata(pdev);
247 struct qxl_device *qdev = to_qxl(drm_dev);
248
249 qxl_io_reset(qdev);
250 return qxl_drm_resume(drm_dev, false);
251 }
252
253 static const struct dev_pm_ops qxl_pm_ops = {
254 .suspend = qxl_pm_suspend,
255 .resume = qxl_pm_resume,
256 .freeze = qxl_pm_freeze,
257 .thaw = qxl_pm_thaw,
258 .poweroff = qxl_pm_freeze,
259 .restore = qxl_pm_restore,
260 };
261 static struct pci_driver qxl_pci_driver = {
262 .name = DRIVER_NAME,
263 .id_table = pciidlist,
264 .probe = qxl_pci_probe,
265 .remove = qxl_pci_remove,
266 .driver.pm = &qxl_pm_ops,
267 };
268
269 static struct drm_driver qxl_driver = {
270 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
271
272 .dumb_create = qxl_mode_dumb_create,
273 .dumb_map_offset = qxl_mode_dumb_mmap,
274 #if defined(CONFIG_DEBUG_FS)
275 .debugfs_init = qxl_debugfs_init,
276 #endif
277 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
278 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
279 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
280 .gem_prime_mmap = qxl_gem_prime_mmap,
281 .fops = &qxl_fops,
282 .ioctls = qxl_ioctls,
283 .irq_handler = qxl_irq_handler,
284 .name = DRIVER_NAME,
285 .desc = DRIVER_DESC,
286 .date = DRIVER_DATE,
287 .major = 0,
288 .minor = 1,
289 .patchlevel = 0,
290
291 .release = qxl_drm_release,
292 };
293
qxl_init(void)294 static 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
qxl_exit(void)305 static void __exit qxl_exit(void)
306 {
307 pci_unregister_driver(&qxl_pci_driver);
308 }
309
310 module_init(qxl_init);
311 module_exit(qxl_exit);
312
313 MODULE_AUTHOR(DRIVER_AUTHOR);
314 MODULE_DESCRIPTION(DRIVER_DESC);
315 MODULE_LICENSE("GPL and additional rights");
316