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_crtc_helper.h"
38 #include "qxl_object.h"
40 extern int qxl_max_ioctls
;
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
, ent
->driver_data
);
86 ret
= drm_vblank_init(&qdev
->ddev
, 1);
90 ret
= qxl_modeset_init(qdev
);
94 drm_kms_helper_poll_init(&qdev
->ddev
);
96 /* Complete initialization. */
97 ret
= drm_dev_register(&qdev
->ddev
, ent
->driver_data
);
104 qxl_modeset_fini(qdev
);
106 drm_vblank_cleanup(&qdev
->ddev
);
108 qxl_device_fini(qdev
);
110 pci_disable_device(pdev
);
117 qxl_pci_remove(struct pci_dev
*pdev
)
119 struct drm_device
*dev
= pci_get_drvdata(pdev
);
120 struct qxl_device
*qdev
= dev
->dev_private
;
122 drm_dev_unregister(dev
);
124 qxl_modeset_fini(qdev
);
125 qxl_device_fini(qdev
);
127 dev
->dev_private
= NULL
;
132 static const struct file_operations qxl_fops
= {
133 .owner
= THIS_MODULE
,
135 .release
= drm_release
,
136 .unlocked_ioctl
= drm_ioctl
,
142 static int qxl_drm_freeze(struct drm_device
*dev
)
144 struct pci_dev
*pdev
= dev
->pdev
;
145 struct qxl_device
*qdev
= dev
->dev_private
;
146 struct drm_crtc
*crtc
;
148 drm_kms_helper_poll_disable(dev
);
151 qxl_fbdev_set_suspend(qdev
, 1);
154 /* unpin the front buffers */
155 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
156 const struct drm_crtc_helper_funcs
*crtc_funcs
= crtc
->helper_private
;
158 (*crtc_funcs
->disable
)(crtc
);
161 qxl_destroy_monitors_object(qdev
);
162 qxl_surf_evict(qdev
);
163 qxl_vram_evict(qdev
);
165 while (!qxl_check_idle(qdev
->command_ring
));
166 while (!qxl_check_idle(qdev
->release_ring
))
167 qxl_queue_garbage_collect(qdev
, 1);
169 pci_save_state(pdev
);
174 static int qxl_drm_resume(struct drm_device
*dev
, bool thaw
)
176 struct qxl_device
*qdev
= dev
->dev_private
;
178 qdev
->ram_header
->int_mask
= QXL_INTERRUPT_MASK
;
180 qxl_reinit_memslots(qdev
);
181 qxl_ring_init_hdr(qdev
->release_ring
);
184 qxl_create_monitors_object(qdev
);
185 drm_helper_resume_force_mode(dev
);
188 qxl_fbdev_set_suspend(qdev
, 0);
191 drm_kms_helper_poll_enable(dev
);
195 static int qxl_pm_suspend(struct device
*dev
)
197 struct pci_dev
*pdev
= to_pci_dev(dev
);
198 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
201 error
= qxl_drm_freeze(drm_dev
);
205 pci_disable_device(pdev
);
206 pci_set_power_state(pdev
, PCI_D3hot
);
210 static int qxl_pm_resume(struct device
*dev
)
212 struct pci_dev
*pdev
= to_pci_dev(dev
);
213 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
215 pci_set_power_state(pdev
, PCI_D0
);
216 pci_restore_state(pdev
);
217 if (pci_enable_device(pdev
)) {
221 return qxl_drm_resume(drm_dev
, false);
224 static int qxl_pm_thaw(struct device
*dev
)
226 struct pci_dev
*pdev
= to_pci_dev(dev
);
227 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
229 return qxl_drm_resume(drm_dev
, true);
232 static int qxl_pm_freeze(struct device
*dev
)
234 struct pci_dev
*pdev
= to_pci_dev(dev
);
235 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
237 return qxl_drm_freeze(drm_dev
);
240 static int qxl_pm_restore(struct device
*dev
)
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
;
247 return qxl_drm_resume(drm_dev
, false);
250 static u32
qxl_noop_get_vblank_counter(struct drm_device
*dev
,
256 static int qxl_noop_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
261 static void qxl_noop_disable_vblank(struct drm_device
*dev
, unsigned int pipe
)
265 static const struct dev_pm_ops qxl_pm_ops
= {
266 .suspend
= qxl_pm_suspend
,
267 .resume
= qxl_pm_resume
,
268 .freeze
= qxl_pm_freeze
,
270 .poweroff
= qxl_pm_freeze
,
271 .restore
= qxl_pm_restore
,
273 static struct pci_driver qxl_pci_driver
= {
275 .id_table
= pciidlist
,
276 .probe
= qxl_pci_probe
,
277 .remove
= qxl_pci_remove
,
278 .driver
.pm
= &qxl_pm_ops
,
281 static struct drm_driver qxl_driver
= {
282 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
|
283 DRIVER_HAVE_IRQ
| DRIVER_IRQ_SHARED
,
284 .get_vblank_counter
= qxl_noop_get_vblank_counter
,
285 .enable_vblank
= qxl_noop_enable_vblank
,
286 .disable_vblank
= qxl_noop_disable_vblank
,
288 .set_busid
= drm_pci_set_busid
,
290 .dumb_create
= qxl_mode_dumb_create
,
291 .dumb_map_offset
= qxl_mode_dumb_mmap
,
292 .dumb_destroy
= drm_gem_dumb_destroy
,
293 #if defined(CONFIG_DEBUG_FS)
294 .debugfs_init
= qxl_debugfs_init
,
295 .debugfs_cleanup
= qxl_debugfs_takedown
,
297 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
298 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
299 .gem_prime_export
= drm_gem_prime_export
,
300 .gem_prime_import
= drm_gem_prime_import
,
301 .gem_prime_pin
= qxl_gem_prime_pin
,
302 .gem_prime_unpin
= qxl_gem_prime_unpin
,
303 .gem_prime_get_sg_table
= qxl_gem_prime_get_sg_table
,
304 .gem_prime_import_sg_table
= qxl_gem_prime_import_sg_table
,
305 .gem_prime_vmap
= qxl_gem_prime_vmap
,
306 .gem_prime_vunmap
= qxl_gem_prime_vunmap
,
307 .gem_prime_mmap
= qxl_gem_prime_mmap
,
308 .gem_free_object_unlocked
= qxl_gem_object_free
,
309 .gem_open_object
= qxl_gem_object_open
,
310 .gem_close_object
= qxl_gem_object_close
,
312 .ioctls
= qxl_ioctls
,
313 .irq_handler
= qxl_irq_handler
,
322 static int __init
qxl_init(void)
324 if (vgacon_text_force() && qxl_modeset
== -1)
327 if (qxl_modeset
== 0)
329 qxl_driver
.num_ioctls
= qxl_max_ioctls
;
330 return drm_pci_init(&qxl_driver
, &qxl_pci_driver
);
333 static void __exit
qxl_exit(void)
335 drm_pci_exit(&qxl_driver
, &qxl_pci_driver
);
338 module_init(qxl_init
);
339 module_exit(qxl_exit
);
341 MODULE_AUTHOR(DRIVER_AUTHOR
);
342 MODULE_DESCRIPTION(DRIVER_DESC
);
343 MODULE_LICENSE("GPL and additional rights");