1 /* vim: set ts=8 sw=8 tw=78 ai noexpandtab */
2 /* qxl_drv.c -- QXL driver -*- linux-c -*-
4 * Copyright 2011 Red Hat, Inc.
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:
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
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.
27 * Dave Airlie <airlie@redhat.com>
28 * Alon Levy <alevy@redhat.com>
31 #include <linux/module.h>
32 #include <linux/console.h>
36 #include <drm/drm_modeset_helper.h>
37 #include <drm/drm_probe_helper.h>
39 #include "qxl_object.h"
41 static const struct pci_device_id pciidlist
[] = {
42 { 0x1b36, 0x100, PCI_ANY_ID
, PCI_ANY_ID
, PCI_CLASS_DISPLAY_VGA
<< 8,
44 { 0x1b36, 0x100, PCI_ANY_ID
, PCI_ANY_ID
, PCI_CLASS_DISPLAY_OTHER
<< 8,
48 MODULE_DEVICE_TABLE(pci
, pciidlist
);
50 static int qxl_modeset
= -1;
53 MODULE_PARM_DESC(modeset
, "Disable/Enable modesetting");
54 module_param_named(modeset
, qxl_modeset
, int, 0400);
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);
59 static struct drm_driver qxl_driver
;
60 static struct pci_driver qxl_pci_driver
;
63 qxl_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
65 struct qxl_device
*qdev
;
68 if (pdev
->revision
< 4) {
69 DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
70 " use xf86-video-qxl in user mode");
71 return -EINVAL
; /* TODO: ENODEV ? */
74 qdev
= kzalloc(sizeof(struct qxl_device
), GFP_KERNEL
);
78 ret
= pci_enable_device(pdev
);
82 ret
= qxl_device_init(qdev
, &qxl_driver
, pdev
);
86 ret
= qxl_modeset_init(qdev
);
90 drm_kms_helper_poll_init(&qdev
->ddev
);
92 /* Complete initialization. */
93 ret
= drm_dev_register(&qdev
->ddev
, ent
->driver_data
);
97 drm_fb_helper_remove_conflicting_pci_framebuffers(pdev
, 0, "qxl");
98 drm_fbdev_generic_setup(&qdev
->ddev
, 32);
102 qxl_modeset_fini(qdev
);
104 qxl_device_fini(qdev
);
106 pci_disable_device(pdev
);
113 qxl_pci_remove(struct pci_dev
*pdev
)
115 struct drm_device
*dev
= pci_get_drvdata(pdev
);
116 struct qxl_device
*qdev
= dev
->dev_private
;
118 drm_dev_unregister(dev
);
120 qxl_modeset_fini(qdev
);
121 qxl_device_fini(qdev
);
123 dev
->dev_private
= NULL
;
128 static const struct file_operations qxl_fops
= {
129 .owner
= THIS_MODULE
,
131 .release
= drm_release
,
132 .unlocked_ioctl
= drm_ioctl
,
138 static int qxl_drm_freeze(struct drm_device
*dev
)
140 struct pci_dev
*pdev
= dev
->pdev
;
141 struct qxl_device
*qdev
= dev
->dev_private
;
144 ret
= drm_mode_config_helper_suspend(dev
);
148 qxl_destroy_monitors_object(qdev
);
149 qxl_surf_evict(qdev
);
150 qxl_vram_evict(qdev
);
152 while (!qxl_check_idle(qdev
->command_ring
));
153 while (!qxl_check_idle(qdev
->release_ring
))
154 qxl_queue_garbage_collect(qdev
, 1);
156 pci_save_state(pdev
);
161 static int qxl_drm_resume(struct drm_device
*dev
, bool thaw
)
163 struct qxl_device
*qdev
= dev
->dev_private
;
165 qdev
->ram_header
->int_mask
= QXL_INTERRUPT_MASK
;
167 qxl_reinit_memslots(qdev
);
168 qxl_ring_init_hdr(qdev
->release_ring
);
171 qxl_create_monitors_object(qdev
);
172 return drm_mode_config_helper_resume(dev
);
175 static int qxl_pm_suspend(struct device
*dev
)
177 struct pci_dev
*pdev
= to_pci_dev(dev
);
178 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
181 error
= qxl_drm_freeze(drm_dev
);
185 pci_disable_device(pdev
);
186 pci_set_power_state(pdev
, PCI_D3hot
);
190 static int qxl_pm_resume(struct device
*dev
)
192 struct pci_dev
*pdev
= to_pci_dev(dev
);
193 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
195 pci_set_power_state(pdev
, PCI_D0
);
196 pci_restore_state(pdev
);
197 if (pci_enable_device(pdev
)) {
201 return qxl_drm_resume(drm_dev
, false);
204 static int qxl_pm_thaw(struct device
*dev
)
206 struct pci_dev
*pdev
= to_pci_dev(dev
);
207 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
209 return qxl_drm_resume(drm_dev
, true);
212 static int qxl_pm_freeze(struct device
*dev
)
214 struct pci_dev
*pdev
= to_pci_dev(dev
);
215 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
217 return qxl_drm_freeze(drm_dev
);
220 static int qxl_pm_restore(struct device
*dev
)
222 struct pci_dev
*pdev
= to_pci_dev(dev
);
223 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
224 struct qxl_device
*qdev
= drm_dev
->dev_private
;
227 return qxl_drm_resume(drm_dev
, false);
230 static const struct dev_pm_ops qxl_pm_ops
= {
231 .suspend
= qxl_pm_suspend
,
232 .resume
= qxl_pm_resume
,
233 .freeze
= qxl_pm_freeze
,
235 .poweroff
= qxl_pm_freeze
,
236 .restore
= qxl_pm_restore
,
238 static struct pci_driver qxl_pci_driver
= {
240 .id_table
= pciidlist
,
241 .probe
= qxl_pci_probe
,
242 .remove
= qxl_pci_remove
,
243 .driver
.pm
= &qxl_pm_ops
,
246 static struct drm_driver qxl_driver
= {
247 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
|
250 .dumb_create
= qxl_mode_dumb_create
,
251 .dumb_map_offset
= qxl_mode_dumb_mmap
,
252 #if defined(CONFIG_DEBUG_FS)
253 .debugfs_init
= qxl_debugfs_init
,
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_vmap
= qxl_gem_prime_vmap
,
260 .gem_prime_vunmap
= qxl_gem_prime_vunmap
,
261 .gem_prime_mmap
= qxl_gem_prime_mmap
,
262 .gem_free_object_unlocked
= qxl_gem_object_free
,
263 .gem_open_object
= qxl_gem_object_open
,
264 .gem_close_object
= qxl_gem_object_close
,
266 .ioctls
= qxl_ioctls
,
267 .irq_handler
= qxl_irq_handler
,
276 static int __init
qxl_init(void)
278 if (vgacon_text_force() && qxl_modeset
== -1)
281 if (qxl_modeset
== 0)
283 qxl_driver
.num_ioctls
= qxl_max_ioctls
;
284 return pci_register_driver(&qxl_pci_driver
);
287 static void __exit
qxl_exit(void)
289 pci_unregister_driver(&qxl_pci_driver
);
292 module_init(qxl_init
);
293 module_exit(qxl_exit
);
295 MODULE_AUTHOR(DRIVER_AUTHOR
);
296 MODULE_DESCRIPTION(DRIVER_DESC
);
297 MODULE_LICENSE("GPL and additional rights");