xref: /linux/drivers/gpu/drm/qxl/qxl_drv.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1f64122c1SDave Airlie /* qxl_drv.c -- QXL driver -*- linux-c -*-
2f64122c1SDave Airlie  *
3f64122c1SDave Airlie  * Copyright 2011 Red Hat, Inc.
4f64122c1SDave Airlie  * All Rights Reserved.
5f64122c1SDave Airlie  *
6f64122c1SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
7f64122c1SDave Airlie  * copy of this software and associated documentation files (the "Software"),
8f64122c1SDave Airlie  * to deal in the Software without restriction, including without limitation
9f64122c1SDave Airlie  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10f64122c1SDave Airlie  * and/or sell copies of the Software, and to permit persons to whom the
11f64122c1SDave Airlie  * Software is furnished to do so, subject to the following conditions:
12f64122c1SDave Airlie  *
13f64122c1SDave Airlie  * The above copyright notice and this permission notice (including the next
14f64122c1SDave Airlie  * paragraph) shall be included in all copies or substantial portions of the
15f64122c1SDave Airlie  * Software.
16f64122c1SDave Airlie  *
17f64122c1SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18f64122c1SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19f64122c1SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20f64122c1SDave Airlie  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21f64122c1SDave Airlie  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22f64122c1SDave Airlie  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23f64122c1SDave Airlie  * OTHER DEALINGS IN THE SOFTWARE.
24f64122c1SDave Airlie  *
25f64122c1SDave Airlie  * Authors:
26f64122c1SDave Airlie  *    Dave Airlie <airlie@redhat.com>
27f64122c1SDave Airlie  *    Alon Levy <alevy@redhat.com>
28f64122c1SDave Airlie  */
29f64122c1SDave Airlie 
30f64122c1SDave Airlie #include "qxl_drv.h"
316848c291SThomas Zimmermann 
32c0f4b75cSSam Ravnborg #include <linux/module.h>
33c0f4b75cSSam Ravnborg #include <linux/pci.h>
346848c291SThomas Zimmermann #include <linux/vgaarb.h>
35c0f4b75cSSam Ravnborg 
36c0f4b75cSSam Ravnborg #include <drm/drm.h>
376848c291SThomas Zimmermann #include <drm/drm_aperture.h>
3823575e7aSGerd Hoffmann #include <drm/drm_atomic_helper.h>
39c0f4b75cSSam Ravnborg #include <drm/drm_drv.h>
40*aae4682eSThomas Zimmermann #include <drm/drm_fbdev_ttm.h>
41c0f4b75cSSam Ravnborg #include <drm/drm_file.h>
42f4268a4bSThomas Zimmermann #include <drm/drm_gem_ttm_helper.h>
4310dcc831SThomas Zimmermann #include <drm/drm_module.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 
71f64122c1SDave Airlie static int
qxl_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)72f64122c1SDave Airlie qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
73f64122c1SDave Airlie {
742b65d567SGabriel Krisman Bertazi 	struct qxl_device *qdev;
752b65d567SGabriel Krisman Bertazi 	int ret;
762b65d567SGabriel Krisman Bertazi 
77f64122c1SDave Airlie 	if (pdev->revision < 4) {
78f64122c1SDave Airlie 		DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
79f64122c1SDave Airlie 			  " use xf86-video-qxl in user mode");
80f64122c1SDave Airlie 		return -EINVAL; /* TODO: ENODEV ? */
81f64122c1SDave Airlie 	}
822b65d567SGabriel Krisman Bertazi 
83a9b0b24aSDaniel Vetter 	qdev = devm_drm_dev_alloc(&pdev->dev, &qxl_driver,
84a9b0b24aSDaniel Vetter 				  struct qxl_device, ddev);
85a9b0b24aSDaniel Vetter 	if (IS_ERR(qdev)) {
86a9b0b24aSDaniel Vetter 		pr_err("Unable to init drm dev");
87cbdded7fSGabriel Krisman Bertazi 		return -ENOMEM;
88a9b0b24aSDaniel Vetter 	}
892b65d567SGabriel Krisman Bertazi 
902b65d567SGabriel Krisman Bertazi 	ret = pci_enable_device(pdev);
912b65d567SGabriel Krisman Bertazi 	if (ret)
92a9b0b24aSDaniel Vetter 		return ret;
932b65d567SGabriel Krisman Bertazi 
9497c9bfe3SThomas Zimmermann 	ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &qxl_driver);
95c34674a2SGerd Hoffmann 	if (ret)
96c34674a2SGerd Hoffmann 		goto disable_pci;
97c34674a2SGerd Hoffmann 
9894cfada2SSui Jingfeng 	if (pci_is_vga(pdev) && pdev->revision < 5) {
99fbf0a7f4SGerd Hoffmann 		ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
100fbf0a7f4SGerd Hoffmann 		if (ret) {
101fbf0a7f4SGerd Hoffmann 			DRM_ERROR("can't get legacy vga ioports\n");
102fbf0a7f4SGerd Hoffmann 			goto disable_pci;
103fbf0a7f4SGerd Hoffmann 		}
104fbf0a7f4SGerd Hoffmann 	}
105fbf0a7f4SGerd Hoffmann 
106a9b0b24aSDaniel Vetter 	ret = qxl_device_init(qdev, pdev);
1072b65d567SGabriel Krisman Bertazi 	if (ret)
108fbf0a7f4SGerd Hoffmann 		goto put_vga;
1092b65d567SGabriel Krisman Bertazi 
1102b65d567SGabriel Krisman Bertazi 	ret = qxl_modeset_init(qdev);
1112b65d567SGabriel Krisman Bertazi 	if (ret)
11264ca824eSGabriel Krisman Bertazi 		goto unload;
1132b65d567SGabriel Krisman Bertazi 
114cbdded7fSGabriel Krisman Bertazi 	drm_kms_helper_poll_init(&qdev->ddev);
1152b65d567SGabriel Krisman Bertazi 
1162b65d567SGabriel Krisman Bertazi 	/* Complete initialization. */
117cbdded7fSGabriel Krisman Bertazi 	ret = drm_dev_register(&qdev->ddev, ent->driver_data);
1182b65d567SGabriel Krisman Bertazi 	if (ret)
1192b65d567SGabriel Krisman Bertazi 		goto modeset_cleanup;
1202b65d567SGabriel Krisman Bertazi 
121*aae4682eSThomas Zimmermann 	drm_fbdev_ttm_setup(&qdev->ddev, 32);
1222b65d567SGabriel Krisman Bertazi 	return 0;
1232b65d567SGabriel Krisman Bertazi 
1242b65d567SGabriel Krisman Bertazi modeset_cleanup:
1252b65d567SGabriel Krisman Bertazi 	qxl_modeset_fini(qdev);
1262b65d567SGabriel Krisman Bertazi unload:
1272b65d567SGabriel Krisman Bertazi 	qxl_device_fini(qdev);
128fbf0a7f4SGerd Hoffmann put_vga:
12994cfada2SSui Jingfeng 	if (pci_is_vga(pdev) && pdev->revision < 5)
130fbf0a7f4SGerd Hoffmann 		vga_put(pdev, VGA_RSRC_LEGACY_IO);
1312b65d567SGabriel Krisman Bertazi disable_pci:
1322b65d567SGabriel Krisman Bertazi 	pci_disable_device(pdev);
133a9b0b24aSDaniel Vetter 
1342b65d567SGabriel Krisman Bertazi 	return ret;
135f64122c1SDave Airlie }
136f64122c1SDave Airlie 
qxl_drm_release(struct drm_device * dev)13723575e7aSGerd Hoffmann static void qxl_drm_release(struct drm_device *dev)
13823575e7aSGerd Hoffmann {
139e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
14023575e7aSGerd Hoffmann 
14123575e7aSGerd Hoffmann 	/*
14223575e7aSGerd Hoffmann 	 * TODO: qxl_device_fini() call should be in qxl_pci_remove(),
14387525610SBjorn Helgaas 	 * reordering qxl_modeset_fini() + qxl_device_fini() calls is
14423575e7aSGerd Hoffmann 	 * non-trivial though.
14523575e7aSGerd Hoffmann 	 */
14623575e7aSGerd Hoffmann 	qxl_modeset_fini(qdev);
14723575e7aSGerd Hoffmann 	qxl_device_fini(qdev);
14823575e7aSGerd Hoffmann }
14923575e7aSGerd Hoffmann 
150f64122c1SDave Airlie static void
qxl_pci_remove(struct pci_dev * pdev)151f64122c1SDave Airlie qxl_pci_remove(struct pci_dev *pdev)
152f64122c1SDave Airlie {
153f64122c1SDave Airlie 	struct drm_device *dev = pci_get_drvdata(pdev);
154f64122c1SDave Airlie 
1556f897f51SGabriel Krisman Bertazi 	drm_dev_unregister(dev);
15623575e7aSGerd Hoffmann 	drm_atomic_helper_shutdown(dev);
15794cfada2SSui Jingfeng 	if (pci_is_vga(pdev) && pdev->revision < 5)
158fbf0a7f4SGerd Hoffmann 		vga_put(pdev, VGA_RSRC_LEGACY_IO);
159f64122c1SDave Airlie }
160f64122c1SDave Airlie 
161ce3d99c8SDouglas Anderson static void
qxl_pci_shutdown(struct pci_dev * pdev)162ce3d99c8SDouglas Anderson qxl_pci_shutdown(struct pci_dev *pdev)
163ce3d99c8SDouglas Anderson {
164ce3d99c8SDouglas Anderson 	drm_atomic_helper_shutdown(pci_get_drvdata(pdev));
165ce3d99c8SDouglas Anderson }
166ce3d99c8SDouglas Anderson 
167db262087SGerd Hoffmann DEFINE_DRM_GEM_FOPS(qxl_fops);
168f64122c1SDave Airlie 
qxl_drm_freeze(struct drm_device * dev)169d84300bfSDave Airlie static int qxl_drm_freeze(struct drm_device *dev)
170d84300bfSDave Airlie {
171dc3629abSThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
172e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
1737948a2b1SPeter Wu 	int ret;
174d84300bfSDave Airlie 
1757948a2b1SPeter Wu 	ret = drm_mode_config_helper_suspend(dev);
1767948a2b1SPeter Wu 	if (ret)
1777948a2b1SPeter Wu 		return ret;
178d84300bfSDave Airlie 
179d84300bfSDave Airlie 	qxl_destroy_monitors_object(qdev);
180d84300bfSDave Airlie 	qxl_surf_evict(qdev);
181d84300bfSDave Airlie 	qxl_vram_evict(qdev);
182d84300bfSDave Airlie 
183d84300bfSDave Airlie 	while (!qxl_check_idle(qdev->command_ring));
184d84300bfSDave Airlie 	while (!qxl_check_idle(qdev->release_ring))
185d84300bfSDave Airlie 		qxl_queue_garbage_collect(qdev, 1);
186d84300bfSDave Airlie 
187d84300bfSDave Airlie 	pci_save_state(pdev);
188d84300bfSDave Airlie 
189d84300bfSDave Airlie 	return 0;
190d84300bfSDave Airlie }
191d84300bfSDave Airlie 
qxl_drm_resume(struct drm_device * dev,bool thaw)192d84300bfSDave Airlie static int qxl_drm_resume(struct drm_device *dev, bool thaw)
193d84300bfSDave Airlie {
194e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
195d84300bfSDave Airlie 
196d84300bfSDave Airlie 	qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
197d84300bfSDave Airlie 	if (!thaw) {
198d84300bfSDave Airlie 		qxl_reinit_memslots(qdev);
199d84300bfSDave Airlie 	}
200d84300bfSDave Airlie 
201d84300bfSDave Airlie 	qxl_create_monitors_object(qdev);
2027948a2b1SPeter Wu 	return drm_mode_config_helper_resume(dev);
203d84300bfSDave Airlie }
204d84300bfSDave Airlie 
qxl_pm_suspend(struct device * dev)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 
qxl_pm_resume(struct device * dev)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);
2244da7aad4SZongmin Zhou 	struct qxl_device *qdev = to_qxl(drm_dev);
225d84300bfSDave Airlie 
226d84300bfSDave Airlie 	pci_set_power_state(pdev, PCI_D0);
227d84300bfSDave Airlie 	pci_restore_state(pdev);
228d84300bfSDave Airlie 	if (pci_enable_device(pdev)) {
229d84300bfSDave Airlie 		return -EIO;
230d84300bfSDave Airlie 	}
231d84300bfSDave Airlie 
2324da7aad4SZongmin Zhou 	qxl_io_reset(qdev);
233d84300bfSDave Airlie 	return qxl_drm_resume(drm_dev, false);
234d84300bfSDave Airlie }
235d84300bfSDave Airlie 
qxl_pm_thaw(struct device * dev)236d84300bfSDave Airlie static int qxl_pm_thaw(struct device *dev)
237d84300bfSDave Airlie {
238f6d2b63aSChuhong Yuan 	struct drm_device *drm_dev = dev_get_drvdata(dev);
239d84300bfSDave Airlie 
240d84300bfSDave Airlie 	return qxl_drm_resume(drm_dev, true);
241d84300bfSDave Airlie }
242d84300bfSDave Airlie 
qxl_pm_freeze(struct device * dev)243d84300bfSDave Airlie static int qxl_pm_freeze(struct device *dev)
244d84300bfSDave Airlie {
245f6d2b63aSChuhong Yuan 	struct drm_device *drm_dev = dev_get_drvdata(dev);
246d84300bfSDave Airlie 
247d84300bfSDave Airlie 	return qxl_drm_freeze(drm_dev);
248d84300bfSDave Airlie }
249d84300bfSDave Airlie 
qxl_pm_restore(struct device * dev)250d84300bfSDave Airlie static int qxl_pm_restore(struct device *dev)
251d84300bfSDave Airlie {
252d84300bfSDave Airlie 	struct pci_dev *pdev = to_pci_dev(dev);
253d84300bfSDave Airlie 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
254e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(drm_dev);
255d84300bfSDave Airlie 
256d84300bfSDave Airlie 	qxl_io_reset(qdev);
257d84300bfSDave Airlie 	return qxl_drm_resume(drm_dev, false);
258d84300bfSDave Airlie }
259d84300bfSDave Airlie 
260d84300bfSDave Airlie static const struct dev_pm_ops qxl_pm_ops = {
261d84300bfSDave Airlie 	.suspend = qxl_pm_suspend,
262d84300bfSDave Airlie 	.resume = qxl_pm_resume,
263d84300bfSDave Airlie 	.freeze = qxl_pm_freeze,
264d84300bfSDave Airlie 	.thaw = qxl_pm_thaw,
265d84300bfSDave Airlie 	.poweroff = qxl_pm_freeze,
266d84300bfSDave Airlie 	.restore = qxl_pm_restore,
267d84300bfSDave Airlie };
268d84300bfSDave Airlie static struct pci_driver qxl_pci_driver = {
269d84300bfSDave Airlie 	 .name = DRIVER_NAME,
270d84300bfSDave Airlie 	 .id_table = pciidlist,
271d84300bfSDave Airlie 	 .probe = qxl_pci_probe,
272d84300bfSDave Airlie 	 .remove = qxl_pci_remove,
273ce3d99c8SDouglas Anderson 	 .shutdown = qxl_pci_shutdown,
274d84300bfSDave Airlie 	 .driver.pm = &qxl_pm_ops,
275d84300bfSDave Airlie };
276d84300bfSDave Airlie 
27766755b48SThomas Zimmermann static const struct drm_ioctl_desc qxl_ioctls[] = {
27866755b48SThomas Zimmermann 	DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH),
27966755b48SThomas Zimmermann 	DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH),
28066755b48SThomas Zimmermann 	DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl, DRM_AUTH),
28166755b48SThomas Zimmermann 	DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl, DRM_AUTH),
28266755b48SThomas Zimmermann 	DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl, DRM_AUTH),
28366755b48SThomas Zimmermann 	DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl, DRM_AUTH),
28466755b48SThomas Zimmermann 	DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl, DRM_AUTH),
28566755b48SThomas Zimmermann };
28666755b48SThomas Zimmermann 
287f64122c1SDave Airlie static struct drm_driver qxl_driver = {
2884e3b70daSZack Rusin 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_CURSOR_HOTSPOT,
289f64122c1SDave Airlie 
290f64122c1SDave Airlie 	.dumb_create = qxl_mode_dumb_create,
291f4268a4bSThomas Zimmermann 	.dumb_map_offset = drm_gem_ttm_dumb_map_offset,
292f64122c1SDave Airlie #if defined(CONFIG_DEBUG_FS)
293f64122c1SDave Airlie 	.debugfs_init = qxl_debugfs_init,
294f64122c1SDave Airlie #endif
295ab042b82SGerd Hoffmann 	.gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
296f64122c1SDave Airlie 	.fops = &qxl_fops,
297f64122c1SDave Airlie 	.ioctls = qxl_ioctls,
29866755b48SThomas Zimmermann 	.num_ioctls = ARRAY_SIZE(qxl_ioctls),
299f64122c1SDave Airlie 	.name = DRIVER_NAME,
300f64122c1SDave Airlie 	.desc = DRIVER_DESC,
301f64122c1SDave Airlie 	.date = DRIVER_DATE,
302f64122c1SDave Airlie 	.major = 0,
303f64122c1SDave Airlie 	.minor = 1,
304f64122c1SDave Airlie 	.patchlevel = 0,
30523575e7aSGerd Hoffmann 
30623575e7aSGerd Hoffmann 	.release = qxl_drm_release,
307f64122c1SDave Airlie };
308f64122c1SDave Airlie 
30910dcc831SThomas Zimmermann drm_module_pci_driver_if_modeset(qxl_pci_driver, qxl_modeset);
310f64122c1SDave Airlie 
311f64122c1SDave Airlie MODULE_AUTHOR(DRIVER_AUTHOR);
312f64122c1SDave Airlie MODULE_DESCRIPTION(DRIVER_DESC);
313f64122c1SDave Airlie MODULE_LICENSE("GPL and additional rights");
314