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