xref: /linux/drivers/gpu/drm/qxl/qxl_drv.c (revision 6848c291a54f8cd1e8b32f4d6e0f681acc8d5095)
1f64122c1SDave Airlie /* vim: set ts=8 sw=8 tw=78 ai noexpandtab */
2f64122c1SDave Airlie /* qxl_drv.c -- QXL driver -*- linux-c -*-
3f64122c1SDave Airlie  *
4f64122c1SDave Airlie  * Copyright 2011 Red Hat, Inc.
5f64122c1SDave Airlie  * All Rights Reserved.
6f64122c1SDave Airlie  *
7f64122c1SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8f64122c1SDave Airlie  * copy of this software and associated documentation files (the "Software"),
9f64122c1SDave Airlie  * to deal in the Software without restriction, including without limitation
10f64122c1SDave Airlie  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11f64122c1SDave Airlie  * and/or sell copies of the Software, and to permit persons to whom the
12f64122c1SDave Airlie  * Software is furnished to do so, subject to the following conditions:
13f64122c1SDave Airlie  *
14f64122c1SDave Airlie  * The above copyright notice and this permission notice (including the next
15f64122c1SDave Airlie  * paragraph) shall be included in all copies or substantial portions of the
16f64122c1SDave Airlie  * Software.
17f64122c1SDave Airlie  *
18f64122c1SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19f64122c1SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20f64122c1SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21f64122c1SDave Airlie  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22f64122c1SDave Airlie  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23f64122c1SDave Airlie  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24f64122c1SDave Airlie  * OTHER DEALINGS IN THE SOFTWARE.
25f64122c1SDave Airlie  *
26f64122c1SDave Airlie  * Authors:
27f64122c1SDave Airlie  *    Dave Airlie <airlie@redhat.com>
28f64122c1SDave Airlie  *    Alon Levy <alevy@redhat.com>
29f64122c1SDave Airlie  */
30f64122c1SDave Airlie 
31f64122c1SDave Airlie #include "qxl_drv.h"
32*6848c291SThomas Zimmermann 
33c0f4b75cSSam Ravnborg #include <linux/console.h>
34c0f4b75cSSam Ravnborg #include <linux/module.h>
35c0f4b75cSSam Ravnborg #include <linux/pci.h>
36*6848c291SThomas Zimmermann #include <linux/vgaarb.h>
37c0f4b75cSSam Ravnborg 
38c0f4b75cSSam Ravnborg #include <drm/drm.h>
39*6848c291SThomas Zimmermann #include <drm/drm_aperture.h>
4023575e7aSGerd Hoffmann #include <drm/drm_atomic_helper.h>
41c0f4b75cSSam Ravnborg #include <drm/drm_drv.h>
42c0f4b75cSSam Ravnborg #include <drm/drm_file.h>
43f4268a4bSThomas Zimmermann #include <drm/drm_gem_ttm_helper.h>
44c0f4b75cSSam Ravnborg #include <drm/drm_modeset_helper.h>
45c0f4b75cSSam Ravnborg #include <drm/drm_prime.h>
46c0f4b75cSSam Ravnborg #include <drm/drm_probe_helper.h>
47c0f4b75cSSam Ravnborg 
48d84300bfSDave Airlie #include "qxl_object.h"
49f64122c1SDave Airlie 
509baa3c34SBenoit Taine static const struct pci_device_id pciidlist[] = {
51f64122c1SDave Airlie 	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
52f64122c1SDave Airlie 	  0xffff00, 0 },
53f64122c1SDave Airlie 	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
54f64122c1SDave Airlie 	  0xffff00, 0 },
55f64122c1SDave Airlie 	{ 0, 0, 0 },
56f64122c1SDave Airlie };
57f64122c1SDave Airlie MODULE_DEVICE_TABLE(pci, pciidlist);
58f64122c1SDave Airlie 
596d01f1f5SDave Airlie static int qxl_modeset = -1;
6007f8d9bdSDave Airlie int qxl_num_crtc = 4;
61f64122c1SDave Airlie 
62f64122c1SDave Airlie MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
63f64122c1SDave Airlie module_param_named(modeset, qxl_modeset, int, 0400);
64f64122c1SDave Airlie 
6507f8d9bdSDave Airlie MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
6607f8d9bdSDave Airlie module_param_named(num_heads, qxl_num_crtc, int, 0400);
6707f8d9bdSDave Airlie 
68f64122c1SDave Airlie static struct drm_driver qxl_driver;
69f64122c1SDave Airlie static struct pci_driver qxl_pci_driver;
70f64122c1SDave Airlie 
71fbf0a7f4SGerd Hoffmann static bool is_vga(struct pci_dev *pdev)
72fbf0a7f4SGerd Hoffmann {
73fbf0a7f4SGerd Hoffmann 	return pdev->class == PCI_CLASS_DISPLAY_VGA << 8;
74fbf0a7f4SGerd Hoffmann }
75fbf0a7f4SGerd Hoffmann 
76f64122c1SDave Airlie static int
77f64122c1SDave Airlie qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
78f64122c1SDave Airlie {
792b65d567SGabriel Krisman Bertazi 	struct qxl_device *qdev;
802b65d567SGabriel Krisman Bertazi 	int ret;
812b65d567SGabriel Krisman Bertazi 
82f64122c1SDave Airlie 	if (pdev->revision < 4) {
83f64122c1SDave Airlie 		DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
84f64122c1SDave Airlie 			  " use xf86-video-qxl in user mode");
85f64122c1SDave Airlie 		return -EINVAL; /* TODO: ENODEV ? */
86f64122c1SDave Airlie 	}
872b65d567SGabriel Krisman Bertazi 
88a9b0b24aSDaniel Vetter 	qdev = devm_drm_dev_alloc(&pdev->dev, &qxl_driver,
89a9b0b24aSDaniel Vetter 				  struct qxl_device, ddev);
90a9b0b24aSDaniel Vetter 	if (IS_ERR(qdev)) {
91a9b0b24aSDaniel Vetter 		pr_err("Unable to init drm dev");
92cbdded7fSGabriel Krisman Bertazi 		return -ENOMEM;
93a9b0b24aSDaniel Vetter 	}
942b65d567SGabriel Krisman Bertazi 
952b65d567SGabriel Krisman Bertazi 	ret = pci_enable_device(pdev);
962b65d567SGabriel Krisman Bertazi 	if (ret)
97a9b0b24aSDaniel Vetter 		return ret;
982b65d567SGabriel Krisman Bertazi 
99*6848c291SThomas Zimmermann 	ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, "qxl");
100c34674a2SGerd Hoffmann 	if (ret)
101c34674a2SGerd Hoffmann 		goto disable_pci;
102c34674a2SGerd Hoffmann 
10366c8ef90SGerd Hoffmann 	if (is_vga(pdev) && pdev->revision < 5) {
104fbf0a7f4SGerd Hoffmann 		ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
105fbf0a7f4SGerd Hoffmann 		if (ret) {
106fbf0a7f4SGerd Hoffmann 			DRM_ERROR("can't get legacy vga ioports\n");
107fbf0a7f4SGerd Hoffmann 			goto disable_pci;
108fbf0a7f4SGerd Hoffmann 		}
109fbf0a7f4SGerd Hoffmann 	}
110fbf0a7f4SGerd Hoffmann 
111a9b0b24aSDaniel Vetter 	ret = qxl_device_init(qdev, pdev);
1122b65d567SGabriel Krisman Bertazi 	if (ret)
113fbf0a7f4SGerd Hoffmann 		goto put_vga;
1142b65d567SGabriel Krisman Bertazi 
1152b65d567SGabriel Krisman Bertazi 	ret = qxl_modeset_init(qdev);
1162b65d567SGabriel Krisman Bertazi 	if (ret)
11764ca824eSGabriel Krisman Bertazi 		goto unload;
1182b65d567SGabriel Krisman Bertazi 
119cbdded7fSGabriel Krisman Bertazi 	drm_kms_helper_poll_init(&qdev->ddev);
1202b65d567SGabriel Krisman Bertazi 
1212b65d567SGabriel Krisman Bertazi 	/* Complete initialization. */
122cbdded7fSGabriel Krisman Bertazi 	ret = drm_dev_register(&qdev->ddev, ent->driver_data);
1232b65d567SGabriel Krisman Bertazi 	if (ret)
1242b65d567SGabriel Krisman Bertazi 		goto modeset_cleanup;
1252b65d567SGabriel Krisman Bertazi 
12626d4707dSGerd Hoffmann 	drm_fbdev_generic_setup(&qdev->ddev, 32);
1272b65d567SGabriel Krisman Bertazi 	return 0;
1282b65d567SGabriel Krisman Bertazi 
1292b65d567SGabriel Krisman Bertazi modeset_cleanup:
1302b65d567SGabriel Krisman Bertazi 	qxl_modeset_fini(qdev);
1312b65d567SGabriel Krisman Bertazi unload:
1322b65d567SGabriel Krisman Bertazi 	qxl_device_fini(qdev);
133fbf0a7f4SGerd Hoffmann put_vga:
13466c8ef90SGerd Hoffmann 	if (is_vga(pdev) && pdev->revision < 5)
135fbf0a7f4SGerd Hoffmann 		vga_put(pdev, VGA_RSRC_LEGACY_IO);
1362b65d567SGabriel Krisman Bertazi disable_pci:
1372b65d567SGabriel Krisman Bertazi 	pci_disable_device(pdev);
138a9b0b24aSDaniel Vetter 
1392b65d567SGabriel Krisman Bertazi 	return ret;
140f64122c1SDave Airlie }
141f64122c1SDave Airlie 
14223575e7aSGerd Hoffmann static void qxl_drm_release(struct drm_device *dev)
14323575e7aSGerd Hoffmann {
144e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
14523575e7aSGerd Hoffmann 
14623575e7aSGerd Hoffmann 	/*
14723575e7aSGerd Hoffmann 	 * TODO: qxl_device_fini() call should be in qxl_pci_remove(),
14887525610SBjorn Helgaas 	 * reordering qxl_modeset_fini() + qxl_device_fini() calls is
14923575e7aSGerd Hoffmann 	 * non-trivial though.
15023575e7aSGerd Hoffmann 	 */
15123575e7aSGerd Hoffmann 	qxl_modeset_fini(qdev);
15223575e7aSGerd Hoffmann 	qxl_device_fini(qdev);
15323575e7aSGerd Hoffmann }
15423575e7aSGerd Hoffmann 
155f64122c1SDave Airlie static void
156f64122c1SDave Airlie qxl_pci_remove(struct pci_dev *pdev)
157f64122c1SDave Airlie {
158f64122c1SDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
159f64122c1SDave Airlie 
1606f897f51SGabriel Krisman Bertazi 	drm_dev_unregister(dev);
16123575e7aSGerd Hoffmann 	drm_atomic_helper_shutdown(dev);
16266c8ef90SGerd Hoffmann 	if (is_vga(pdev) && pdev->revision < 5)
163fbf0a7f4SGerd Hoffmann 		vga_put(pdev, VGA_RSRC_LEGACY_IO);
164f64122c1SDave Airlie }
165f64122c1SDave Airlie 
166db262087SGerd Hoffmann DEFINE_DRM_GEM_FOPS(qxl_fops);
167f64122c1SDave Airlie 
168d84300bfSDave Airlie static int qxl_drm_freeze(struct drm_device *dev)
169d84300bfSDave Airlie {
170dc3629abSThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
171e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
1727948a2b1SPeter Wu 	int ret;
173d84300bfSDave Airlie 
1747948a2b1SPeter Wu 	ret = drm_mode_config_helper_suspend(dev);
1757948a2b1SPeter Wu 	if (ret)
1767948a2b1SPeter Wu 		return ret;
177d84300bfSDave Airlie 
178d84300bfSDave Airlie 	qxl_destroy_monitors_object(qdev);
179d84300bfSDave Airlie 	qxl_surf_evict(qdev);
180d84300bfSDave Airlie 	qxl_vram_evict(qdev);
181d84300bfSDave Airlie 
182d84300bfSDave Airlie 	while (!qxl_check_idle(qdev->command_ring));
183d84300bfSDave Airlie 	while (!qxl_check_idle(qdev->release_ring))
184d84300bfSDave Airlie 		qxl_queue_garbage_collect(qdev, 1);
185d84300bfSDave Airlie 
186d84300bfSDave Airlie 	pci_save_state(pdev);
187d84300bfSDave Airlie 
188d84300bfSDave Airlie 	return 0;
189d84300bfSDave Airlie }
190d84300bfSDave Airlie 
191d84300bfSDave Airlie static int qxl_drm_resume(struct drm_device *dev, bool thaw)
192d84300bfSDave Airlie {
193e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
194d84300bfSDave Airlie 
195d84300bfSDave Airlie 	qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
196d84300bfSDave Airlie 	if (!thaw) {
197d84300bfSDave Airlie 		qxl_reinit_memslots(qdev);
198d84300bfSDave Airlie 		qxl_ring_init_hdr(qdev->release_ring);
199d84300bfSDave Airlie 	}
200d84300bfSDave Airlie 
201d84300bfSDave Airlie 	qxl_create_monitors_object(qdev);
2027948a2b1SPeter Wu 	return drm_mode_config_helper_resume(dev);
203d84300bfSDave Airlie }
204d84300bfSDave Airlie 
205d84300bfSDave Airlie static int qxl_pm_suspend(struct device *dev)
206d84300bfSDave Airlie {
207d84300bfSDave Airlie 	struct pci_dev *pdev = to_pci_dev(dev);
208d84300bfSDave Airlie 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
209d84300bfSDave Airlie 	int error;
210d84300bfSDave Airlie 
211d84300bfSDave Airlie 	error = qxl_drm_freeze(drm_dev);
212d84300bfSDave Airlie 	if (error)
213d84300bfSDave Airlie 		return error;
214d84300bfSDave Airlie 
215d84300bfSDave Airlie 	pci_disable_device(pdev);
216d84300bfSDave Airlie 	pci_set_power_state(pdev, PCI_D3hot);
217d84300bfSDave Airlie 	return 0;
218d84300bfSDave Airlie }
219d84300bfSDave Airlie 
220d84300bfSDave Airlie static int qxl_pm_resume(struct device *dev)
221d84300bfSDave Airlie {
222d84300bfSDave Airlie 	struct pci_dev *pdev = to_pci_dev(dev);
223d84300bfSDave Airlie 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
224d84300bfSDave Airlie 
225d84300bfSDave Airlie 	pci_set_power_state(pdev, PCI_D0);
226d84300bfSDave Airlie 	pci_restore_state(pdev);
227d84300bfSDave Airlie 	if (pci_enable_device(pdev)) {
228d84300bfSDave Airlie 		return -EIO;
229d84300bfSDave Airlie 	}
230d84300bfSDave Airlie 
231d84300bfSDave Airlie 	return qxl_drm_resume(drm_dev, false);
232d84300bfSDave Airlie }
233d84300bfSDave Airlie 
234d84300bfSDave Airlie static int qxl_pm_thaw(struct device *dev)
235d84300bfSDave Airlie {
236f6d2b63aSChuhong Yuan 	struct drm_device *drm_dev = dev_get_drvdata(dev);
237d84300bfSDave Airlie 
238d84300bfSDave Airlie 	return qxl_drm_resume(drm_dev, true);
239d84300bfSDave Airlie }
240d84300bfSDave Airlie 
241d84300bfSDave Airlie static int qxl_pm_freeze(struct device *dev)
242d84300bfSDave Airlie {
243f6d2b63aSChuhong Yuan 	struct drm_device *drm_dev = dev_get_drvdata(dev);
244d84300bfSDave Airlie 
245d84300bfSDave Airlie 	return qxl_drm_freeze(drm_dev);
246d84300bfSDave Airlie }
247d84300bfSDave Airlie 
248d84300bfSDave Airlie static int qxl_pm_restore(struct device *dev)
249d84300bfSDave Airlie {
250d84300bfSDave Airlie 	struct pci_dev *pdev = to_pci_dev(dev);
251d84300bfSDave Airlie 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
252e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(drm_dev);
253d84300bfSDave Airlie 
254d84300bfSDave Airlie 	qxl_io_reset(qdev);
255d84300bfSDave Airlie 	return qxl_drm_resume(drm_dev, false);
256d84300bfSDave Airlie }
257d84300bfSDave Airlie 
258d84300bfSDave Airlie static const struct dev_pm_ops qxl_pm_ops = {
259d84300bfSDave Airlie 	.suspend = qxl_pm_suspend,
260d84300bfSDave Airlie 	.resume = qxl_pm_resume,
261d84300bfSDave Airlie 	.freeze = qxl_pm_freeze,
262d84300bfSDave Airlie 	.thaw = qxl_pm_thaw,
263d84300bfSDave Airlie 	.poweroff = qxl_pm_freeze,
264d84300bfSDave Airlie 	.restore = qxl_pm_restore,
265d84300bfSDave Airlie };
266d84300bfSDave Airlie static struct pci_driver qxl_pci_driver = {
267d84300bfSDave Airlie 	 .name = DRIVER_NAME,
268d84300bfSDave Airlie 	 .id_table = pciidlist,
269d84300bfSDave Airlie 	 .probe = qxl_pci_probe,
270d84300bfSDave Airlie 	 .remove = qxl_pci_remove,
271d84300bfSDave Airlie 	 .driver.pm = &qxl_pm_ops,
272d84300bfSDave Airlie };
273d84300bfSDave Airlie 
274f64122c1SDave Airlie static struct drm_driver qxl_driver = {
2750424fdafSDaniel Vetter 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
276f64122c1SDave Airlie 
277f64122c1SDave Airlie 	.dumb_create = qxl_mode_dumb_create,
278f4268a4bSThomas Zimmermann 	.dumb_map_offset = drm_gem_ttm_dumb_map_offset,
279f64122c1SDave Airlie #if defined(CONFIG_DEBUG_FS)
280f64122c1SDave Airlie 	.debugfs_init = qxl_debugfs_init,
281f64122c1SDave Airlie #endif
282ab042b82SGerd Hoffmann 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
283ab042b82SGerd Hoffmann 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
284ab042b82SGerd Hoffmann 	.gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
28547c12968SAndreas Pokorny 	.gem_prime_mmap = qxl_gem_prime_mmap,
286f64122c1SDave Airlie 	.fops = &qxl_fops,
287f64122c1SDave Airlie 	.ioctls = qxl_ioctls,
288f64122c1SDave Airlie 	.irq_handler = qxl_irq_handler,
289f64122c1SDave Airlie 	.name = DRIVER_NAME,
290f64122c1SDave Airlie 	.desc = DRIVER_DESC,
291f64122c1SDave Airlie 	.date = DRIVER_DATE,
292f64122c1SDave Airlie 	.major = 0,
293f64122c1SDave Airlie 	.minor = 1,
294f64122c1SDave Airlie 	.patchlevel = 0,
29523575e7aSGerd Hoffmann 
29623575e7aSGerd Hoffmann 	.release = qxl_drm_release,
297f64122c1SDave Airlie };
298f64122c1SDave Airlie 
299f64122c1SDave Airlie static int __init qxl_init(void)
300f64122c1SDave Airlie {
301f64122c1SDave Airlie 	if (vgacon_text_force() && qxl_modeset == -1)
302f64122c1SDave Airlie 		return -EINVAL;
303f64122c1SDave Airlie 
304f64122c1SDave Airlie 	if (qxl_modeset == 0)
305f64122c1SDave Airlie 		return -EINVAL;
306f64122c1SDave Airlie 	qxl_driver.num_ioctls = qxl_max_ioctls;
30710631d72SDaniel Vetter 	return pci_register_driver(&qxl_pci_driver);
308f64122c1SDave Airlie }
309f64122c1SDave Airlie 
310f64122c1SDave Airlie static void __exit qxl_exit(void)
311f64122c1SDave Airlie {
31210631d72SDaniel Vetter 	pci_unregister_driver(&qxl_pci_driver);
313f64122c1SDave Airlie }
314f64122c1SDave Airlie 
315f64122c1SDave Airlie module_init(qxl_init);
316f64122c1SDave Airlie module_exit(qxl_exit);
317f64122c1SDave Airlie 
318f64122c1SDave Airlie MODULE_AUTHOR(DRIVER_AUTHOR);
319f64122c1SDave Airlie MODULE_DESCRIPTION(DRIVER_DESC);
320f64122c1SDave Airlie MODULE_LICENSE("GPL and additional rights");
321