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