Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / qxl / qxl_drv.c
blob2445e75cf7ea6664854325a6383fe0e32e2bdd57
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.
5 * All Rights Reserved.
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
16 * Software.
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.
26 * Authors:
27 * Dave Airlie <airlie@redhat.com>
28 * Alon Levy <alevy@redhat.com>
31 #include <linux/module.h>
32 #include <linux/console.h>
34 #include <drm/drmP.h>
35 #include <drm/drm.h>
36 #include <drm/drm_crtc_helper.h>
37 #include "qxl_drv.h"
38 #include "qxl_object.h"
40 static const struct pci_device_id pciidlist[] = {
41 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
42 0xffff00, 0 },
43 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
44 0xffff00, 0 },
45 { 0, 0, 0 },
47 MODULE_DEVICE_TABLE(pci, pciidlist);
49 static int qxl_modeset = -1;
50 int qxl_num_crtc = 4;
52 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
53 module_param_named(modeset, qxl_modeset, int, 0400);
55 MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
56 module_param_named(num_heads, qxl_num_crtc, int, 0400);
58 static struct drm_driver qxl_driver;
59 static struct pci_driver qxl_pci_driver;
61 static int
62 qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
64 struct qxl_device *qdev;
65 int ret;
67 if (pdev->revision < 4) {
68 DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
69 " use xf86-video-qxl in user mode");
70 return -EINVAL; /* TODO: ENODEV ? */
73 qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL);
74 if (!qdev)
75 return -ENOMEM;
77 ret = pci_enable_device(pdev);
78 if (ret)
79 goto free_dev;
81 ret = qxl_device_init(qdev, &qxl_driver, pdev);
82 if (ret)
83 goto disable_pci;
85 ret = qxl_modeset_init(qdev);
86 if (ret)
87 goto unload;
89 drm_kms_helper_poll_init(&qdev->ddev);
91 /* Complete initialization. */
92 ret = drm_dev_register(&qdev->ddev, ent->driver_data);
93 if (ret)
94 goto modeset_cleanup;
96 return 0;
98 modeset_cleanup:
99 qxl_modeset_fini(qdev);
100 unload:
101 qxl_device_fini(qdev);
102 disable_pci:
103 pci_disable_device(pdev);
104 free_dev:
105 kfree(qdev);
106 return ret;
109 static void
110 qxl_pci_remove(struct pci_dev *pdev)
112 struct drm_device *dev = pci_get_drvdata(pdev);
113 struct qxl_device *qdev = dev->dev_private;
115 drm_dev_unregister(dev);
117 qxl_modeset_fini(qdev);
118 qxl_device_fini(qdev);
120 dev->dev_private = NULL;
121 kfree(qdev);
122 drm_dev_unref(dev);
125 static const struct file_operations qxl_fops = {
126 .owner = THIS_MODULE,
127 .open = drm_open,
128 .release = drm_release,
129 .unlocked_ioctl = drm_ioctl,
130 .poll = drm_poll,
131 .read = drm_read,
132 .mmap = qxl_mmap,
135 static int qxl_drm_freeze(struct drm_device *dev)
137 struct pci_dev *pdev = dev->pdev;
138 struct qxl_device *qdev = dev->dev_private;
139 struct drm_crtc *crtc;
141 drm_kms_helper_poll_disable(dev);
143 console_lock();
144 qxl_fbdev_set_suspend(qdev, 1);
145 console_unlock();
147 /* unpin the front buffers */
148 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
149 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
150 if (crtc->enabled)
151 (*crtc_funcs->disable)(crtc);
154 qxl_destroy_monitors_object(qdev);
155 qxl_surf_evict(qdev);
156 qxl_vram_evict(qdev);
158 while (!qxl_check_idle(qdev->command_ring));
159 while (!qxl_check_idle(qdev->release_ring))
160 qxl_queue_garbage_collect(qdev, 1);
162 pci_save_state(pdev);
164 return 0;
167 static int qxl_drm_resume(struct drm_device *dev, bool thaw)
169 struct qxl_device *qdev = dev->dev_private;
171 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
172 if (!thaw) {
173 qxl_reinit_memslots(qdev);
174 qxl_ring_init_hdr(qdev->release_ring);
177 qxl_create_monitors_object(qdev);
178 drm_helper_resume_force_mode(dev);
180 console_lock();
181 qxl_fbdev_set_suspend(qdev, 0);
182 console_unlock();
184 drm_kms_helper_poll_enable(dev);
185 return 0;
188 static int qxl_pm_suspend(struct device *dev)
190 struct pci_dev *pdev = to_pci_dev(dev);
191 struct drm_device *drm_dev = pci_get_drvdata(pdev);
192 int error;
194 error = qxl_drm_freeze(drm_dev);
195 if (error)
196 return error;
198 pci_disable_device(pdev);
199 pci_set_power_state(pdev, PCI_D3hot);
200 return 0;
203 static int qxl_pm_resume(struct device *dev)
205 struct pci_dev *pdev = to_pci_dev(dev);
206 struct drm_device *drm_dev = pci_get_drvdata(pdev);
208 pci_set_power_state(pdev, PCI_D0);
209 pci_restore_state(pdev);
210 if (pci_enable_device(pdev)) {
211 return -EIO;
214 return qxl_drm_resume(drm_dev, false);
217 static int qxl_pm_thaw(struct device *dev)
219 struct pci_dev *pdev = to_pci_dev(dev);
220 struct drm_device *drm_dev = pci_get_drvdata(pdev);
222 return qxl_drm_resume(drm_dev, true);
225 static int qxl_pm_freeze(struct device *dev)
227 struct pci_dev *pdev = to_pci_dev(dev);
228 struct drm_device *drm_dev = pci_get_drvdata(pdev);
230 return qxl_drm_freeze(drm_dev);
233 static int qxl_pm_restore(struct device *dev)
235 struct pci_dev *pdev = to_pci_dev(dev);
236 struct drm_device *drm_dev = pci_get_drvdata(pdev);
237 struct qxl_device *qdev = drm_dev->dev_private;
239 qxl_io_reset(qdev);
240 return qxl_drm_resume(drm_dev, false);
243 static const struct dev_pm_ops qxl_pm_ops = {
244 .suspend = qxl_pm_suspend,
245 .resume = qxl_pm_resume,
246 .freeze = qxl_pm_freeze,
247 .thaw = qxl_pm_thaw,
248 .poweroff = qxl_pm_freeze,
249 .restore = qxl_pm_restore,
251 static struct pci_driver qxl_pci_driver = {
252 .name = DRIVER_NAME,
253 .id_table = pciidlist,
254 .probe = qxl_pci_probe,
255 .remove = qxl_pci_remove,
256 .driver.pm = &qxl_pm_ops,
259 static struct drm_driver qxl_driver = {
260 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
261 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED |
262 DRIVER_ATOMIC,
264 .dumb_create = qxl_mode_dumb_create,
265 .dumb_map_offset = qxl_mode_dumb_mmap,
266 #if defined(CONFIG_DEBUG_FS)
267 .debugfs_init = qxl_debugfs_init,
268 #endif
269 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
270 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
271 .gem_prime_export = drm_gem_prime_export,
272 .gem_prime_import = drm_gem_prime_import,
273 .gem_prime_pin = qxl_gem_prime_pin,
274 .gem_prime_unpin = qxl_gem_prime_unpin,
275 .gem_prime_get_sg_table = qxl_gem_prime_get_sg_table,
276 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
277 .gem_prime_vmap = qxl_gem_prime_vmap,
278 .gem_prime_vunmap = qxl_gem_prime_vunmap,
279 .gem_prime_mmap = qxl_gem_prime_mmap,
280 .gem_free_object_unlocked = qxl_gem_object_free,
281 .gem_open_object = qxl_gem_object_open,
282 .gem_close_object = qxl_gem_object_close,
283 .fops = &qxl_fops,
284 .ioctls = qxl_ioctls,
285 .irq_handler = qxl_irq_handler,
286 .name = DRIVER_NAME,
287 .desc = DRIVER_DESC,
288 .date = DRIVER_DATE,
289 .major = 0,
290 .minor = 1,
291 .patchlevel = 0,
294 static int __init qxl_init(void)
296 if (vgacon_text_force() && qxl_modeset == -1)
297 return -EINVAL;
299 if (qxl_modeset == 0)
300 return -EINVAL;
301 qxl_driver.num_ioctls = qxl_max_ioctls;
302 return pci_register_driver(&qxl_pci_driver);
305 static void __exit qxl_exit(void)
307 pci_unregister_driver(&qxl_pci_driver);
310 module_init(qxl_init);
311 module_exit(qxl_exit);
313 MODULE_AUTHOR(DRIVER_AUTHOR);
314 MODULE_DESCRIPTION(DRIVER_DESC);
315 MODULE_LICENSE("GPL and additional rights");