xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_drv.c (revision 0c874100)
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_put(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 	int ret;
140 
141 	ret = drm_mode_config_helper_suspend(dev);
142 	if (ret)
143 		return ret;
144 
145 	qxl_destroy_monitors_object(qdev);
146 	qxl_surf_evict(qdev);
147 	qxl_vram_evict(qdev);
148 
149 	while (!qxl_check_idle(qdev->command_ring));
150 	while (!qxl_check_idle(qdev->release_ring))
151 		qxl_queue_garbage_collect(qdev, 1);
152 
153 	pci_save_state(pdev);
154 
155 	return 0;
156 }
157 
158 static int qxl_drm_resume(struct drm_device *dev, bool thaw)
159 {
160 	struct qxl_device *qdev = dev->dev_private;
161 
162 	qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
163 	if (!thaw) {
164 		qxl_reinit_memslots(qdev);
165 		qxl_ring_init_hdr(qdev->release_ring);
166 	}
167 
168 	qxl_create_monitors_object(qdev);
169 	return drm_mode_config_helper_resume(dev);
170 }
171 
172 static int qxl_pm_suspend(struct device *dev)
173 {
174 	struct pci_dev *pdev = to_pci_dev(dev);
175 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
176 	int error;
177 
178 	error = qxl_drm_freeze(drm_dev);
179 	if (error)
180 		return error;
181 
182 	pci_disable_device(pdev);
183 	pci_set_power_state(pdev, PCI_D3hot);
184 	return 0;
185 }
186 
187 static int qxl_pm_resume(struct device *dev)
188 {
189 	struct pci_dev *pdev = to_pci_dev(dev);
190 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
191 
192 	pci_set_power_state(pdev, PCI_D0);
193 	pci_restore_state(pdev);
194 	if (pci_enable_device(pdev)) {
195 		return -EIO;
196 	}
197 
198 	return qxl_drm_resume(drm_dev, false);
199 }
200 
201 static int qxl_pm_thaw(struct device *dev)
202 {
203 	struct pci_dev *pdev = to_pci_dev(dev);
204 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
205 
206 	return qxl_drm_resume(drm_dev, true);
207 }
208 
209 static int qxl_pm_freeze(struct device *dev)
210 {
211 	struct pci_dev *pdev = to_pci_dev(dev);
212 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
213 
214 	return qxl_drm_freeze(drm_dev);
215 }
216 
217 static int qxl_pm_restore(struct device *dev)
218 {
219 	struct pci_dev *pdev = to_pci_dev(dev);
220 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
221 	struct qxl_device *qdev = drm_dev->dev_private;
222 
223 	qxl_io_reset(qdev);
224 	return qxl_drm_resume(drm_dev, false);
225 }
226 
227 static const struct dev_pm_ops qxl_pm_ops = {
228 	.suspend = qxl_pm_suspend,
229 	.resume = qxl_pm_resume,
230 	.freeze = qxl_pm_freeze,
231 	.thaw = qxl_pm_thaw,
232 	.poweroff = qxl_pm_freeze,
233 	.restore = qxl_pm_restore,
234 };
235 static struct pci_driver qxl_pci_driver = {
236 	 .name = DRIVER_NAME,
237 	 .id_table = pciidlist,
238 	 .probe = qxl_pci_probe,
239 	 .remove = qxl_pci_remove,
240 	 .driver.pm = &qxl_pm_ops,
241 };
242 
243 static struct drm_driver qxl_driver = {
244 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
245 			   DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED |
246 			   DRIVER_ATOMIC,
247 
248 	.dumb_create = qxl_mode_dumb_create,
249 	.dumb_map_offset = qxl_mode_dumb_mmap,
250 #if defined(CONFIG_DEBUG_FS)
251 	.debugfs_init = qxl_debugfs_init,
252 #endif
253 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
254 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
255 	.gem_prime_export = drm_gem_prime_export,
256 	.gem_prime_import = drm_gem_prime_import,
257 	.gem_prime_pin = qxl_gem_prime_pin,
258 	.gem_prime_unpin = qxl_gem_prime_unpin,
259 	.gem_prime_get_sg_table = qxl_gem_prime_get_sg_table,
260 	.gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
261 	.gem_prime_vmap = qxl_gem_prime_vmap,
262 	.gem_prime_vunmap = qxl_gem_prime_vunmap,
263 	.gem_prime_mmap = qxl_gem_prime_mmap,
264 	.gem_free_object_unlocked = qxl_gem_object_free,
265 	.gem_open_object = qxl_gem_object_open,
266 	.gem_close_object = qxl_gem_object_close,
267 	.fops = &qxl_fops,
268 	.ioctls = qxl_ioctls,
269 	.irq_handler = qxl_irq_handler,
270 	.name = DRIVER_NAME,
271 	.desc = DRIVER_DESC,
272 	.date = DRIVER_DATE,
273 	.major = 0,
274 	.minor = 1,
275 	.patchlevel = 0,
276 };
277 
278 static int __init qxl_init(void)
279 {
280 	if (vgacon_text_force() && qxl_modeset == -1)
281 		return -EINVAL;
282 
283 	if (qxl_modeset == 0)
284 		return -EINVAL;
285 	qxl_driver.num_ioctls = qxl_max_ioctls;
286 	return pci_register_driver(&qxl_pci_driver);
287 }
288 
289 static void __exit qxl_exit(void)
290 {
291 	pci_unregister_driver(&qxl_pci_driver);
292 }
293 
294 module_init(qxl_init);
295 module_exit(qxl_exit);
296 
297 MODULE_AUTHOR(DRIVER_AUTHOR);
298 MODULE_DESCRIPTION(DRIVER_DESC);
299 MODULE_LICENSE("GPL and additional rights");
300