xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_drv.c (revision d3964221)
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 <linux/module.h>
32 #include <linux/console.h>
33 
34 #include <drm/drmP.h>
35 #include <drm/drm.h>
36 #include <drm/drm_crtc_helper.h>
37 #include "qxl_drv.h"
38 #include "qxl_object.h"
39 
40 static const struct pci_device_id pciidlist[] = {
41 	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
42 	  0xffff00, 0 },
43 	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
44 	  0xffff00, 0 },
45 	{ 0, 0, 0 },
46 };
47 MODULE_DEVICE_TABLE(pci, pciidlist);
48 
49 static int qxl_modeset = -1;
50 int qxl_num_crtc = 4;
51 
52 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
53 module_param_named(modeset, qxl_modeset, int, 0400);
54 
55 MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
56 module_param_named(num_heads, qxl_num_crtc, int, 0400);
57 
58 static struct drm_driver qxl_driver;
59 static struct pci_driver qxl_pci_driver;
60 
61 static int
62 qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
63 {
64 	struct qxl_device *qdev;
65 	int ret;
66 
67 	if (pdev->revision < 4) {
68 		DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
69 			  " use xf86-video-qxl in user mode");
70 		return -EINVAL; /* TODO: ENODEV ? */
71 	}
72 
73 	qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL);
74 	if (!qdev)
75 		return -ENOMEM;
76 
77 	ret = pci_enable_device(pdev);
78 	if (ret)
79 		goto free_dev;
80 
81 	ret = qxl_device_init(qdev, &qxl_driver, pdev);
82 	if (ret)
83 		goto disable_pci;
84 
85 	ret = qxl_modeset_init(qdev);
86 	if (ret)
87 		goto unload;
88 
89 	drm_kms_helper_poll_init(&qdev->ddev);
90 
91 	/* Complete initialization. */
92 	ret = drm_dev_register(&qdev->ddev, ent->driver_data);
93 	if (ret)
94 		goto modeset_cleanup;
95 
96 	return 0;
97 
98 modeset_cleanup:
99 	qxl_modeset_fini(qdev);
100 unload:
101 	qxl_device_fini(qdev);
102 disable_pci:
103 	pci_disable_device(pdev);
104 free_dev:
105 	kfree(qdev);
106 	return ret;
107 }
108 
109 static void
110 qxl_pci_remove(struct pci_dev *pdev)
111 {
112 	struct drm_device *dev = pci_get_drvdata(pdev);
113 	struct qxl_device *qdev = dev->dev_private;
114 
115 	drm_dev_unregister(dev);
116 
117 	qxl_modeset_fini(qdev);
118 	qxl_device_fini(qdev);
119 
120 	dev->dev_private = NULL;
121 	kfree(qdev);
122 	drm_dev_unref(dev);
123 }
124 
125 static const struct file_operations qxl_fops = {
126 	.owner = THIS_MODULE,
127 	.open = drm_open,
128 	.release = drm_release,
129 	.unlocked_ioctl = drm_ioctl,
130 	.poll = drm_poll,
131 	.read = drm_read,
132 	.mmap = qxl_mmap,
133 };
134 
135 static int qxl_drm_freeze(struct drm_device *dev)
136 {
137 	struct pci_dev *pdev = dev->pdev;
138 	struct qxl_device *qdev = dev->dev_private;
139 	struct drm_crtc *crtc;
140 
141 	drm_kms_helper_poll_disable(dev);
142 
143 	console_lock();
144 	qxl_fbdev_set_suspend(qdev, 1);
145 	console_unlock();
146 
147 	/* unpin the front buffers */
148 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
149 		const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
150 		if (crtc->enabled)
151 			(*crtc_funcs->disable)(crtc);
152 	}
153 
154 	qxl_destroy_monitors_object(qdev);
155 	qxl_surf_evict(qdev);
156 	qxl_vram_evict(qdev);
157 
158 	while (!qxl_check_idle(qdev->command_ring));
159 	while (!qxl_check_idle(qdev->release_ring))
160 		qxl_queue_garbage_collect(qdev, 1);
161 
162 	pci_save_state(pdev);
163 
164 	return 0;
165 }
166 
167 static int qxl_drm_resume(struct drm_device *dev, bool thaw)
168 {
169 	struct qxl_device *qdev = dev->dev_private;
170 
171 	qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
172 	if (!thaw) {
173 		qxl_reinit_memslots(qdev);
174 		qxl_ring_init_hdr(qdev->release_ring);
175 	}
176 
177 	qxl_create_monitors_object(qdev);
178 	drm_helper_resume_force_mode(dev);
179 
180 	console_lock();
181 	qxl_fbdev_set_suspend(qdev, 0);
182 	console_unlock();
183 
184 	drm_kms_helper_poll_enable(dev);
185 	return 0;
186 }
187 
188 static int qxl_pm_suspend(struct device *dev)
189 {
190 	struct pci_dev *pdev = to_pci_dev(dev);
191 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
192 	int error;
193 
194 	error = qxl_drm_freeze(drm_dev);
195 	if (error)
196 		return error;
197 
198 	pci_disable_device(pdev);
199 	pci_set_power_state(pdev, PCI_D3hot);
200 	return 0;
201 }
202 
203 static int qxl_pm_resume(struct device *dev)
204 {
205 	struct pci_dev *pdev = to_pci_dev(dev);
206 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
207 
208 	pci_set_power_state(pdev, PCI_D0);
209 	pci_restore_state(pdev);
210 	if (pci_enable_device(pdev)) {
211 		return -EIO;
212 	}
213 
214 	return qxl_drm_resume(drm_dev, false);
215 }
216 
217 static int qxl_pm_thaw(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 	return qxl_drm_resume(drm_dev, true);
223 }
224 
225 static int qxl_pm_freeze(struct device *dev)
226 {
227 	struct pci_dev *pdev = to_pci_dev(dev);
228 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
229 
230 	return qxl_drm_freeze(drm_dev);
231 }
232 
233 static int qxl_pm_restore(struct device *dev)
234 {
235 	struct pci_dev *pdev = to_pci_dev(dev);
236 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
237 	struct qxl_device *qdev = drm_dev->dev_private;
238 
239 	qxl_io_reset(qdev);
240 	return qxl_drm_resume(drm_dev, false);
241 }
242 
243 static const struct dev_pm_ops qxl_pm_ops = {
244 	.suspend = qxl_pm_suspend,
245 	.resume = qxl_pm_resume,
246 	.freeze = qxl_pm_freeze,
247 	.thaw = qxl_pm_thaw,
248 	.poweroff = qxl_pm_freeze,
249 	.restore = qxl_pm_restore,
250 };
251 static struct pci_driver qxl_pci_driver = {
252 	 .name = DRIVER_NAME,
253 	 .id_table = pciidlist,
254 	 .probe = qxl_pci_probe,
255 	 .remove = qxl_pci_remove,
256 	 .driver.pm = &qxl_pm_ops,
257 };
258 
259 static struct drm_driver qxl_driver = {
260 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
261 			   DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED |
262 			   DRIVER_ATOMIC,
263 
264 	.dumb_create = qxl_mode_dumb_create,
265 	.dumb_map_offset = qxl_mode_dumb_mmap,
266 #if defined(CONFIG_DEBUG_FS)
267 	.debugfs_init = qxl_debugfs_init,
268 #endif
269 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
270 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
271 	.gem_prime_export = drm_gem_prime_export,
272 	.gem_prime_import = drm_gem_prime_import,
273 	.gem_prime_pin = qxl_gem_prime_pin,
274 	.gem_prime_unpin = qxl_gem_prime_unpin,
275 	.gem_prime_get_sg_table = qxl_gem_prime_get_sg_table,
276 	.gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
277 	.gem_prime_vmap = qxl_gem_prime_vmap,
278 	.gem_prime_vunmap = qxl_gem_prime_vunmap,
279 	.gem_prime_mmap = qxl_gem_prime_mmap,
280 	.gem_free_object_unlocked = qxl_gem_object_free,
281 	.gem_open_object = qxl_gem_object_open,
282 	.gem_close_object = qxl_gem_object_close,
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 
294 static int __init qxl_init(void)
295 {
296 	if (vgacon_text_force() && qxl_modeset == -1)
297 		return -EINVAL;
298 
299 	if (qxl_modeset == 0)
300 		return -EINVAL;
301 	qxl_driver.num_ioctls = qxl_max_ioctls;
302 	return pci_register_driver(&qxl_pci_driver);
303 }
304 
305 static void __exit qxl_exit(void)
306 {
307 	pci_unregister_driver(&qxl_pci_driver);
308 }
309 
310 module_init(qxl_init);
311 module_exit(qxl_exit);
312 
313 MODULE_AUTHOR(DRIVER_AUTHOR);
314 MODULE_DESCRIPTION(DRIVER_DESC);
315 MODULE_LICENSE("GPL and additional rights");
316