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