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