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