xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_drv.c (revision 15e3ae36)
1 /* vim: set ts=8 sw=8 tw=78 ai noexpandtab */
2 /* qxl_drv.c -- QXL driver -*- linux-c -*-
3  *
4  * Copyright 2011 Red Hat, Inc.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  *
26  * Authors:
27  *    Dave Airlie <airlie@redhat.com>
28  *    Alon Levy <alevy@redhat.com>
29  */
30 
31 #include "qxl_drv.h"
32 #include <linux/console.h>
33 #include <linux/module.h>
34 #include <linux/pci.h>
35 
36 #include <drm/drm.h>
37 #include <drm/drm_atomic_helper.h>
38 #include <drm/drm_drv.h>
39 #include <drm/drm_file.h>
40 #include <drm/drm_modeset_helper.h>
41 #include <drm/drm_prime.h>
42 #include <drm/drm_probe_helper.h>
43 
44 #include "qxl_object.h"
45 
46 static const struct pci_device_id pciidlist[] = {
47 	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
48 	  0xffff00, 0 },
49 	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
50 	  0xffff00, 0 },
51 	{ 0, 0, 0 },
52 };
53 MODULE_DEVICE_TABLE(pci, pciidlist);
54 
55 static int qxl_modeset = -1;
56 int qxl_num_crtc = 4;
57 
58 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
59 module_param_named(modeset, qxl_modeset, int, 0400);
60 
61 MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
62 module_param_named(num_heads, qxl_num_crtc, int, 0400);
63 
64 static struct drm_driver qxl_driver;
65 static struct pci_driver qxl_pci_driver;
66 
67 static bool is_vga(struct pci_dev *pdev)
68 {
69 	return pdev->class == PCI_CLASS_DISPLAY_VGA << 8;
70 }
71 
72 static int
73 qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
74 {
75 	struct qxl_device *qdev;
76 	int ret;
77 
78 	if (pdev->revision < 4) {
79 		DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
80 			  " use xf86-video-qxl in user mode");
81 		return -EINVAL; /* TODO: ENODEV ? */
82 	}
83 
84 	qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL);
85 	if (!qdev)
86 		return -ENOMEM;
87 
88 	ret = pci_enable_device(pdev);
89 	if (ret)
90 		goto free_dev;
91 
92 	ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "qxl");
93 	if (ret)
94 		goto disable_pci;
95 
96 	if (is_vga(pdev)) {
97 		ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
98 		if (ret) {
99 			DRM_ERROR("can't get legacy vga ioports\n");
100 			goto disable_pci;
101 		}
102 	}
103 
104 	ret = qxl_device_init(qdev, &qxl_driver, pdev);
105 	if (ret)
106 		goto put_vga;
107 
108 	ret = qxl_modeset_init(qdev);
109 	if (ret)
110 		goto unload;
111 
112 	drm_kms_helper_poll_init(&qdev->ddev);
113 
114 	/* Complete initialization. */
115 	ret = drm_dev_register(&qdev->ddev, ent->driver_data);
116 	if (ret)
117 		goto modeset_cleanup;
118 
119 	drm_fbdev_generic_setup(&qdev->ddev, 32);
120 	return 0;
121 
122 modeset_cleanup:
123 	qxl_modeset_fini(qdev);
124 unload:
125 	qxl_device_fini(qdev);
126 put_vga:
127 	if (is_vga(pdev))
128 		vga_put(pdev, VGA_RSRC_LEGACY_IO);
129 disable_pci:
130 	pci_disable_device(pdev);
131 free_dev:
132 	kfree(qdev);
133 	return ret;
134 }
135 
136 static void qxl_drm_release(struct drm_device *dev)
137 {
138 	struct qxl_device *qdev = dev->dev_private;
139 
140 	/*
141 	 * TODO: qxl_device_fini() call should be in qxl_pci_remove(),
142 	 * reodering qxl_modeset_fini() + qxl_device_fini() calls is
143 	 * non-trivial though.
144 	 */
145 	qxl_modeset_fini(qdev);
146 	qxl_device_fini(qdev);
147 	dev->dev_private = NULL;
148 	kfree(qdev);
149 }
150 
151 static void
152 qxl_pci_remove(struct pci_dev *pdev)
153 {
154 	struct drm_device *dev = pci_get_drvdata(pdev);
155 
156 	drm_dev_unregister(dev);
157 	drm_atomic_helper_shutdown(dev);
158 	if (is_vga(pdev))
159 		vga_put(pdev, VGA_RSRC_LEGACY_IO);
160 	drm_dev_put(dev);
161 }
162 
163 DEFINE_DRM_GEM_FOPS(qxl_fops);
164 
165 static int qxl_drm_freeze(struct drm_device *dev)
166 {
167 	struct pci_dev *pdev = dev->pdev;
168 	struct qxl_device *qdev = dev->dev_private;
169 	int ret;
170 
171 	ret = drm_mode_config_helper_suspend(dev);
172 	if (ret)
173 		return ret;
174 
175 	qxl_destroy_monitors_object(qdev);
176 	qxl_surf_evict(qdev);
177 	qxl_vram_evict(qdev);
178 
179 	while (!qxl_check_idle(qdev->command_ring));
180 	while (!qxl_check_idle(qdev->release_ring))
181 		qxl_queue_garbage_collect(qdev, 1);
182 
183 	pci_save_state(pdev);
184 
185 	return 0;
186 }
187 
188 static int qxl_drm_resume(struct drm_device *dev, bool thaw)
189 {
190 	struct qxl_device *qdev = dev->dev_private;
191 
192 	qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
193 	if (!thaw) {
194 		qxl_reinit_memslots(qdev);
195 		qxl_ring_init_hdr(qdev->release_ring);
196 	}
197 
198 	qxl_create_monitors_object(qdev);
199 	return drm_mode_config_helper_resume(dev);
200 }
201 
202 static int qxl_pm_suspend(struct device *dev)
203 {
204 	struct pci_dev *pdev = to_pci_dev(dev);
205 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
206 	int error;
207 
208 	error = qxl_drm_freeze(drm_dev);
209 	if (error)
210 		return error;
211 
212 	pci_disable_device(pdev);
213 	pci_set_power_state(pdev, PCI_D3hot);
214 	return 0;
215 }
216 
217 static int qxl_pm_resume(struct device *dev)
218 {
219 	struct pci_dev *pdev = to_pci_dev(dev);
220 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
221 
222 	pci_set_power_state(pdev, PCI_D0);
223 	pci_restore_state(pdev);
224 	if (pci_enable_device(pdev)) {
225 		return -EIO;
226 	}
227 
228 	return qxl_drm_resume(drm_dev, false);
229 }
230 
231 static int qxl_pm_thaw(struct device *dev)
232 {
233 	struct drm_device *drm_dev = dev_get_drvdata(dev);
234 
235 	return qxl_drm_resume(drm_dev, true);
236 }
237 
238 static int qxl_pm_freeze(struct device *dev)
239 {
240 	struct drm_device *drm_dev = dev_get_drvdata(dev);
241 
242 	return qxl_drm_freeze(drm_dev);
243 }
244 
245 static int qxl_pm_restore(struct device *dev)
246 {
247 	struct pci_dev *pdev = to_pci_dev(dev);
248 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
249 	struct qxl_device *qdev = drm_dev->dev_private;
250 
251 	qxl_io_reset(qdev);
252 	return qxl_drm_resume(drm_dev, false);
253 }
254 
255 static const struct dev_pm_ops qxl_pm_ops = {
256 	.suspend = qxl_pm_suspend,
257 	.resume = qxl_pm_resume,
258 	.freeze = qxl_pm_freeze,
259 	.thaw = qxl_pm_thaw,
260 	.poweroff = qxl_pm_freeze,
261 	.restore = qxl_pm_restore,
262 };
263 static struct pci_driver qxl_pci_driver = {
264 	 .name = DRIVER_NAME,
265 	 .id_table = pciidlist,
266 	 .probe = qxl_pci_probe,
267 	 .remove = qxl_pci_remove,
268 	 .driver.pm = &qxl_pm_ops,
269 };
270 
271 static struct drm_driver qxl_driver = {
272 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
273 
274 	.dumb_create = qxl_mode_dumb_create,
275 	.dumb_map_offset = qxl_mode_dumb_mmap,
276 #if defined(CONFIG_DEBUG_FS)
277 	.debugfs_init = qxl_debugfs_init,
278 #endif
279 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
280 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
281 	.gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
282 	.gem_prime_mmap = qxl_gem_prime_mmap,
283 	.fops = &qxl_fops,
284 	.ioctls = qxl_ioctls,
285 	.irq_handler = qxl_irq_handler,
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 
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 
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