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 DEFINE_PCI_DEVICE_TABLE(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 if (pdev->revision < 4) {
66 DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
67 " use xf86-video-qxl in user mode");
68 return -EINVAL; /* TODO: ENODEV ? */
70 return drm_get_pci_dev(pdev, ent, &qxl_driver);
74 qxl_pci_remove(struct pci_dev *pdev)
76 struct drm_device *dev = pci_get_drvdata(pdev);
81 static const struct file_operations qxl_fops = {
84 .release = drm_release,
85 .unlocked_ioctl = drm_ioctl,
90 static int qxl_drm_freeze(struct drm_device *dev)
92 struct pci_dev *pdev = dev->pdev;
93 struct qxl_device *qdev = dev->dev_private;
94 struct drm_crtc *crtc;
96 drm_kms_helper_poll_disable(dev);
99 qxl_fbdev_set_suspend(qdev, 1);
102 /* unpin the front buffers */
103 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
104 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
106 (*crtc_funcs->disable)(crtc);
109 qxl_destroy_monitors_object(qdev);
110 qxl_surf_evict(qdev);
111 qxl_vram_evict(qdev);
113 while (!qxl_check_idle(qdev->command_ring));
114 while (!qxl_check_idle(qdev->release_ring))
115 qxl_queue_garbage_collect(qdev, 1);
117 pci_save_state(pdev);
122 static int qxl_drm_resume(struct drm_device *dev, bool thaw)
124 struct qxl_device *qdev = dev->dev_private;
126 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
128 qxl_reinit_memslots(qdev);
129 qxl_ring_init_hdr(qdev->release_ring);
132 qxl_create_monitors_object(qdev);
133 drm_helper_resume_force_mode(dev);
136 qxl_fbdev_set_suspend(qdev, 0);
139 drm_kms_helper_poll_enable(dev);
143 static int qxl_pm_suspend(struct device *dev)
145 struct pci_dev *pdev = to_pci_dev(dev);
146 struct drm_device *drm_dev = pci_get_drvdata(pdev);
149 error = qxl_drm_freeze(drm_dev);
153 pci_disable_device(pdev);
154 pci_set_power_state(pdev, PCI_D3hot);
158 static int qxl_pm_resume(struct device *dev)
160 struct pci_dev *pdev = to_pci_dev(dev);
161 struct drm_device *drm_dev = pci_get_drvdata(pdev);
163 pci_set_power_state(pdev, PCI_D0);
164 pci_restore_state(pdev);
165 if (pci_enable_device(pdev)) {
169 return qxl_drm_resume(drm_dev, false);
172 static int qxl_pm_thaw(struct device *dev)
174 struct pci_dev *pdev = to_pci_dev(dev);
175 struct drm_device *drm_dev = pci_get_drvdata(pdev);
177 return qxl_drm_resume(drm_dev, true);
180 static int qxl_pm_freeze(struct device *dev)
182 struct pci_dev *pdev = to_pci_dev(dev);
183 struct drm_device *drm_dev = pci_get_drvdata(pdev);
185 return qxl_drm_freeze(drm_dev);
188 static int qxl_pm_restore(struct device *dev)
190 struct pci_dev *pdev = to_pci_dev(dev);
191 struct drm_device *drm_dev = pci_get_drvdata(pdev);
192 struct qxl_device *qdev = drm_dev->dev_private;
195 return qxl_drm_resume(drm_dev, false);
198 static const struct dev_pm_ops qxl_pm_ops = {
199 .suspend = qxl_pm_suspend,
200 .resume = qxl_pm_resume,
201 .freeze = qxl_pm_freeze,
203 .poweroff = qxl_pm_freeze,
204 .restore = qxl_pm_restore,
206 static struct pci_driver qxl_pci_driver = {
208 .id_table = pciidlist,
209 .probe = qxl_pci_probe,
210 .remove = qxl_pci_remove,
211 .driver.pm = &qxl_pm_ops,
214 static struct drm_driver qxl_driver = {
215 .driver_features = DRIVER_GEM | DRIVER_MODESET |
216 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED,
218 .load = qxl_driver_load,
219 .unload = qxl_driver_unload,
221 .dumb_create = qxl_mode_dumb_create,
222 .dumb_map_offset = qxl_mode_dumb_mmap,
223 .dumb_destroy = drm_gem_dumb_destroy,
224 #if defined(CONFIG_DEBUG_FS)
225 .debugfs_init = qxl_debugfs_init,
226 .debugfs_cleanup = qxl_debugfs_takedown,
228 .gem_init_object = qxl_gem_object_init,
229 .gem_free_object = qxl_gem_object_free,
230 .gem_open_object = qxl_gem_object_open,
231 .gem_close_object = qxl_gem_object_close,
233 .ioctls = qxl_ioctls,
234 .irq_handler = qxl_irq_handler,
243 static int __init qxl_init(void)
245 #ifdef CONFIG_VGA_CONSOLE
246 if (vgacon_text_force() && qxl_modeset == -1)
250 if (qxl_modeset == 0)
252 qxl_driver.num_ioctls = qxl_max_ioctls;
253 return drm_pci_init(&qxl_driver, &qxl_pci_driver);
256 static void __exit qxl_exit(void)
258 drm_pci_exit(&qxl_driver, &qxl_pci_driver);
261 module_init(qxl_init);
262 module_exit(qxl_exit);
264 MODULE_AUTHOR(DRIVER_AUTHOR);
265 MODULE_DESCRIPTION(DRIVER_DESC);
266 MODULE_LICENSE("GPL and additional rights");