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

source code of linux/drivers/gpu/drm/qxl/qxl_drv.c