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 
32 #include <linux/console.h>
33 #include <linux/module.h>
34 #include <linux/pci.h>
35 #include <linux/vgaarb.h>
36 
37 #include <drm/drm.h>
38 #include <drm/drm_aperture.h>
39 #include <drm/drm_atomic_helper.h>
40 #include <drm/drm_drv.h>
41 #include <drm/drm_file.h>
42 #include <drm/drm_gem_ttm_helper.h>
43 #include <drm/drm_modeset_helper.h>
44 #include <drm/drm_prime.h>
45 #include <drm/drm_probe_helper.h>
46 
47 #include "qxl_object.h"
48 
49 static const struct pci_device_id pciidlist[] = {
50 	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
51 	  0xffff00, 0 },
52 	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
53 	  0xffff00, 0 },
54 	{ 0, 0, 0 },
55 };
56 MODULE_DEVICE_TABLE(pci, pciidlist);
57 
58 static int qxl_modeset = -1;
59 int qxl_num_crtc = 4;
60 
61 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
62 module_param_named(modeset, qxl_modeset, int, 0400);
63 
64 MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
65 module_param_named(num_heads, qxl_num_crtc, int, 0400);
66 
67 static struct drm_driver qxl_driver;
68 static struct pci_driver qxl_pci_driver;
69 
is_vga(struct pci_dev * pdev)70 static bool is_vga(struct pci_dev *pdev)
71 {
72 	return pdev->class == PCI_CLASS_DISPLAY_VGA << 8;
73 }
74 
75 static int
qxl_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)76 qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
77 {
78 	struct qxl_device *qdev;
79 	int ret;
80 
81 	if (pdev->revision < 4) {
82 		DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
83 			  " use xf86-video-qxl in user mode");
84 		return -EINVAL; /* TODO: ENODEV ? */
85 	}
86 
87 	qdev = devm_drm_dev_alloc(&pdev->dev, &qxl_driver,
88 				  struct qxl_device, ddev);
89 	if (IS_ERR(qdev)) {
90 		pr_err("Unable to init drm dev");
91 		return -ENOMEM;
92 	}
93 
94 	ret = pci_enable_device(pdev);
95 	if (ret)
96 		return ret;
97 
98 	ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &qxl_driver);
99 	if (ret)
100 		goto disable_pci;
101 
102 	if (is_vga(pdev) && pdev->revision < 5) {
103 		ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
104 		if (ret) {
105 			DRM_ERROR("can't get legacy vga ioports\n");
106 			goto disable_pci;
107 		}
108 	}
109 
110 	ret = qxl_device_init(qdev, pdev);
111 	if (ret)
112 		goto put_vga;
113 
114 	ret = qxl_modeset_init(qdev);
115 	if (ret)
116 		goto unload;
117 
118 	drm_kms_helper_poll_init(&qdev->ddev);
119 
120 	/* Complete initialization. */
121 	ret = drm_dev_register(&qdev->ddev, ent->driver_data);
122 	if (ret)
123 		goto modeset_cleanup;
124 
125 	drm_fbdev_generic_setup(&qdev->ddev, 32);
126 	return 0;
127 
128 modeset_cleanup:
129 	qxl_modeset_fini(qdev);
130 unload:
131 	qxl_device_fini(qdev);
132 put_vga:
133 	if (is_vga(pdev) && pdev->revision < 5)
134 		vga_put(pdev, VGA_RSRC_LEGACY_IO);
135 disable_pci:
136 	pci_disable_device(pdev);
137 
138 	return ret;
139 }
140 
qxl_drm_release(struct drm_device * dev)141 static void qxl_drm_release(struct drm_device *dev)
142 {
143 	struct qxl_device *qdev = to_qxl(dev);
144 
145 	/*
146 	 * TODO: qxl_device_fini() call should be in qxl_pci_remove(),
147 	 * reordering qxl_modeset_fini() + qxl_device_fini() calls is
148 	 * non-trivial though.
149 	 */
150 	qxl_modeset_fini(qdev);
151 	qxl_device_fini(qdev);
152 }
153 
154 static void
qxl_pci_remove(struct pci_dev * pdev)155 qxl_pci_remove(struct pci_dev *pdev)
156 {
157 	struct drm_device *dev = pci_get_drvdata(pdev);
158 
159 	drm_dev_unregister(dev);
160 	drm_atomic_helper_shutdown(dev);
161 	if (is_vga(pdev) && pdev->revision < 5)
162 		vga_put(pdev, VGA_RSRC_LEGACY_IO);
163 }
164 
165 DEFINE_DRM_GEM_FOPS(qxl_fops);
166 
qxl_drm_freeze(struct drm_device * dev)167 static int qxl_drm_freeze(struct drm_device *dev)
168 {
169 	struct pci_dev *pdev = to_pci_dev(dev->dev);
170 	struct qxl_device *qdev = to_qxl(dev);
171 	int ret;
172 
173 	ret = drm_mode_config_helper_suspend(dev);
174 	if (ret)
175 		return ret;
176 
177 	qxl_destroy_monitors_object(qdev);
178 	qxl_surf_evict(qdev);
179 	qxl_vram_evict(qdev);
180 
181 	while (!qxl_check_idle(qdev->command_ring));
182 	while (!qxl_check_idle(qdev->release_ring))
183 		qxl_queue_garbage_collect(qdev, 1);
184 
185 	pci_save_state(pdev);
186 
187 	return 0;
188 }
189 
qxl_drm_resume(struct drm_device * dev,bool thaw)190 static int qxl_drm_resume(struct drm_device *dev, bool thaw)
191 {
192 	struct qxl_device *qdev = to_qxl(dev);
193 
194 	qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
195 	if (!thaw) {
196 		qxl_reinit_memslots(qdev);
197 		qxl_ring_init_hdr(qdev->release_ring);
198 	}
199 
200 	qxl_create_monitors_object(qdev);
201 	return drm_mode_config_helper_resume(dev);
202 }
203 
qxl_pm_suspend(struct device * dev)204 static int qxl_pm_suspend(struct device *dev)
205 {
206 	struct pci_dev *pdev = to_pci_dev(dev);
207 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
208 	int error;
209 
210 	error = qxl_drm_freeze(drm_dev);
211 	if (error)
212 		return error;
213 
214 	pci_disable_device(pdev);
215 	pci_set_power_state(pdev, PCI_D3hot);
216 	return 0;
217 }
218 
qxl_pm_resume(struct device * dev)219 static int qxl_pm_resume(struct device *dev)
220 {
221 	struct pci_dev *pdev = to_pci_dev(dev);
222 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
223 
224 	pci_set_power_state(pdev, PCI_D0);
225 	pci_restore_state(pdev);
226 	if (pci_enable_device(pdev)) {
227 		return -EIO;
228 	}
229 
230 	return qxl_drm_resume(drm_dev, false);
231 }
232 
qxl_pm_thaw(struct device * dev)233 static int qxl_pm_thaw(struct device *dev)
234 {
235 	struct drm_device *drm_dev = dev_get_drvdata(dev);
236 
237 	return qxl_drm_resume(drm_dev, true);
238 }
239 
qxl_pm_freeze(struct device * dev)240 static int qxl_pm_freeze(struct device *dev)
241 {
242 	struct drm_device *drm_dev = dev_get_drvdata(dev);
243 
244 	return qxl_drm_freeze(drm_dev);
245 }
246 
qxl_pm_restore(struct device * dev)247 static int qxl_pm_restore(struct device *dev)
248 {
249 	struct pci_dev *pdev = to_pci_dev(dev);
250 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
251 	struct qxl_device *qdev = to_qxl(drm_dev);
252 
253 	qxl_io_reset(qdev);
254 	return qxl_drm_resume(drm_dev, false);
255 }
256 
257 static const struct dev_pm_ops qxl_pm_ops = {
258 	.suspend = qxl_pm_suspend,
259 	.resume = qxl_pm_resume,
260 	.freeze = qxl_pm_freeze,
261 	.thaw = qxl_pm_thaw,
262 	.poweroff = qxl_pm_freeze,
263 	.restore = qxl_pm_restore,
264 };
265 static struct pci_driver qxl_pci_driver = {
266 	 .name = DRIVER_NAME,
267 	 .id_table = pciidlist,
268 	 .probe = qxl_pci_probe,
269 	 .remove = qxl_pci_remove,
270 	 .driver.pm = &qxl_pm_ops,
271 };
272 
273 static struct drm_driver qxl_driver = {
274 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
275 
276 	.dumb_create = qxl_mode_dumb_create,
277 	.dumb_map_offset = drm_gem_ttm_dumb_map_offset,
278 #if defined(CONFIG_DEBUG_FS)
279 	.debugfs_init = qxl_debugfs_init,
280 #endif
281 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
282 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
283 	.gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
284 	.fops = &qxl_fops,
285 	.ioctls = qxl_ioctls,
286 	.name = DRIVER_NAME,
287 	.desc = DRIVER_DESC,
288 	.date = DRIVER_DATE,
289 	.major = 0,
290 	.minor = 1,
291 	.patchlevel = 0,
292 
293 	.release = qxl_drm_release,
294 };
295 
qxl_init(void)296 static int __init qxl_init(void)
297 {
298 	if (vgacon_text_force() && qxl_modeset == -1)
299 		return -EINVAL;
300 
301 	if (qxl_modeset == 0)
302 		return -EINVAL;
303 	qxl_driver.num_ioctls = qxl_max_ioctls;
304 	return pci_register_driver(&qxl_pci_driver);
305 }
306 
qxl_exit(void)307 static void __exit qxl_exit(void)
308 {
309 	pci_unregister_driver(&qxl_pci_driver);
310 }
311 
312 module_init(qxl_init);
313 module_exit(qxl_exit);
314 
315 MODULE_AUTHOR(DRIVER_AUTHOR);
316 MODULE_DESCRIPTION(DRIVER_DESC);
317 MODULE_LICENSE("GPL and additional rights");
318