blob: 83f6f0b5e9efa292e83fae0c2b9ea45d4c7241b2 [file] [log] [blame]
Dave Airlief64122c2013-02-25 14:47:55 +10001/* vim: set ts=8 sw=8 tw=78 ai noexpandtab */
2/* qxl_drv.c -- QXL driver -*- linux-c -*-
3 *
4 * Copyright 2011 Red Hat, Inc.
5 * All Rights Reserved.
6 *
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:
13 *
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.
17 *
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.
25 *
26 * Authors:
27 * Dave Airlie <airlie@redhat.com>
28 * Alon Levy <alevy@redhat.com>
29 */
30
31#include <linux/module.h>
32#include <linux/console.h>
33
34#include "drmP.h"
35#include "drm/drm.h"
Dave Airlied84300b2013-07-04 15:02:33 +100036#include "drm_crtc_helper.h"
Dave Airlief64122c2013-02-25 14:47:55 +100037#include "qxl_drv.h"
Dave Airlied84300b2013-07-04 15:02:33 +100038#include "qxl_object.h"
Dave Airlief64122c2013-02-25 14:47:55 +100039
40extern int qxl_max_ioctls;
Benoit Taine9baa3c32014-08-08 15:56:03 +020041static const struct pci_device_id pciidlist[] = {
Dave Airlief64122c2013-02-25 14:47:55 +100042 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
43 0xffff00, 0 },
44 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
45 0xffff00, 0 },
46 { 0, 0, 0 },
47};
48MODULE_DEVICE_TABLE(pci, pciidlist);
49
Dave Airlie6d01f1f2013-04-16 13:24:25 +100050static int qxl_modeset = -1;
Dave Airlie07f8d9b2013-07-02 06:37:13 +010051int qxl_num_crtc = 4;
Dave Airlief64122c2013-02-25 14:47:55 +100052
53MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
54module_param_named(modeset, qxl_modeset, int, 0400);
55
Dave Airlie07f8d9b2013-07-02 06:37:13 +010056MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
57module_param_named(num_heads, qxl_num_crtc, int, 0400);
58
Dave Airlief64122c2013-02-25 14:47:55 +100059static struct drm_driver qxl_driver;
60static struct pci_driver qxl_pci_driver;
61
62static int
63qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
64{
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 ? */
69 }
70 return drm_get_pci_dev(pdev, ent, &qxl_driver);
71}
72
73static void
74qxl_pci_remove(struct pci_dev *pdev)
75{
76 struct drm_device *dev = pci_get_drvdata(pdev);
77
78 drm_put_dev(dev);
79}
80
Dave Airlief64122c2013-02-25 14:47:55 +100081static const struct file_operations qxl_fops = {
82 .owner = THIS_MODULE,
83 .open = drm_open,
84 .release = drm_release,
85 .unlocked_ioctl = drm_ioctl,
86 .poll = drm_poll,
Andreas Pokorny058e9f52014-08-08 10:40:55 +020087 .read = drm_read,
Dave Airlief64122c2013-02-25 14:47:55 +100088 .mmap = qxl_mmap,
89};
90
Dave Airlied84300b2013-07-04 15:02:33 +100091static int qxl_drm_freeze(struct drm_device *dev)
92{
93 struct pci_dev *pdev = dev->pdev;
94 struct qxl_device *qdev = dev->dev_private;
95 struct drm_crtc *crtc;
96
97 drm_kms_helper_poll_disable(dev);
98
99 console_lock();
100 qxl_fbdev_set_suspend(qdev, 1);
101 console_unlock();
102
103 /* unpin the front buffers */
104 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jani Nikula71279a12015-03-11 11:51:05 +0200105 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
Dave Airlied84300b2013-07-04 15:02:33 +1000106 if (crtc->enabled)
107 (*crtc_funcs->disable)(crtc);
108 }
109
110 qxl_destroy_monitors_object(qdev);
111 qxl_surf_evict(qdev);
112 qxl_vram_evict(qdev);
113
114 while (!qxl_check_idle(qdev->command_ring));
115 while (!qxl_check_idle(qdev->release_ring))
116 qxl_queue_garbage_collect(qdev, 1);
117
118 pci_save_state(pdev);
119
120 return 0;
121}
122
123static int qxl_drm_resume(struct drm_device *dev, bool thaw)
124{
125 struct qxl_device *qdev = dev->dev_private;
126
127 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
128 if (!thaw) {
129 qxl_reinit_memslots(qdev);
130 qxl_ring_init_hdr(qdev->release_ring);
131 }
132
133 qxl_create_monitors_object(qdev);
134 drm_helper_resume_force_mode(dev);
135
136 console_lock();
137 qxl_fbdev_set_suspend(qdev, 0);
138 console_unlock();
139
140 drm_kms_helper_poll_enable(dev);
141 return 0;
142}
143
144static int qxl_pm_suspend(struct device *dev)
145{
146 struct pci_dev *pdev = to_pci_dev(dev);
147 struct drm_device *drm_dev = pci_get_drvdata(pdev);
148 int error;
149
150 error = qxl_drm_freeze(drm_dev);
151 if (error)
152 return error;
153
154 pci_disable_device(pdev);
155 pci_set_power_state(pdev, PCI_D3hot);
156 return 0;
157}
158
159static int qxl_pm_resume(struct device *dev)
160{
161 struct pci_dev *pdev = to_pci_dev(dev);
162 struct drm_device *drm_dev = pci_get_drvdata(pdev);
163
164 pci_set_power_state(pdev, PCI_D0);
165 pci_restore_state(pdev);
166 if (pci_enable_device(pdev)) {
167 return -EIO;
168 }
169
170 return qxl_drm_resume(drm_dev, false);
171}
172
173static int qxl_pm_thaw(struct device *dev)
174{
175 struct pci_dev *pdev = to_pci_dev(dev);
176 struct drm_device *drm_dev = pci_get_drvdata(pdev);
177
178 return qxl_drm_resume(drm_dev, true);
179}
180
181static int qxl_pm_freeze(struct device *dev)
182{
183 struct pci_dev *pdev = to_pci_dev(dev);
184 struct drm_device *drm_dev = pci_get_drvdata(pdev);
185
186 return qxl_drm_freeze(drm_dev);
187}
188
189static int qxl_pm_restore(struct device *dev)
190{
191 struct pci_dev *pdev = to_pci_dev(dev);
192 struct drm_device *drm_dev = pci_get_drvdata(pdev);
193 struct qxl_device *qdev = drm_dev->dev_private;
194
195 qxl_io_reset(qdev);
196 return qxl_drm_resume(drm_dev, false);
197}
198
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200199static u32 qxl_noop_get_vblank_counter(struct drm_device *dev, int crtc)
200{
Mario Kleiner337eb432015-05-04 06:29:47 +0200201 return 0;
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200202}
203
204static int qxl_noop_enable_vblank(struct drm_device *dev, int crtc)
205{
206 return 0;
207}
208
209static void qxl_noop_disable_vblank(struct drm_device *dev, int crtc)
210{
211}
212
Dave Airlied84300b2013-07-04 15:02:33 +1000213static const struct dev_pm_ops qxl_pm_ops = {
214 .suspend = qxl_pm_suspend,
215 .resume = qxl_pm_resume,
216 .freeze = qxl_pm_freeze,
217 .thaw = qxl_pm_thaw,
218 .poweroff = qxl_pm_freeze,
219 .restore = qxl_pm_restore,
220};
221static struct pci_driver qxl_pci_driver = {
222 .name = DRIVER_NAME,
223 .id_table = pciidlist,
224 .probe = qxl_pci_probe,
225 .remove = qxl_pci_remove,
226 .driver.pm = &qxl_pm_ops,
227};
228
Dave Airlief64122c2013-02-25 14:47:55 +1000229static struct drm_driver qxl_driver = {
Andreas Pokorny47c12962014-08-08 10:40:56 +0200230 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
Dave Airlief64122c2013-02-25 14:47:55 +1000231 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED,
Dave Airlief64122c2013-02-25 14:47:55 +1000232 .load = qxl_driver_load,
233 .unload = qxl_driver_unload,
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200234 .get_vblank_counter = qxl_noop_get_vblank_counter,
235 .enable_vblank = qxl_noop_enable_vblank,
236 .disable_vblank = qxl_noop_disable_vblank,
Dave Airlief64122c2013-02-25 14:47:55 +1000237
David Herrmann915b4d12014-08-29 12:12:43 +0200238 .set_busid = drm_pci_set_busid,
239
Dave Airlief64122c2013-02-25 14:47:55 +1000240 .dumb_create = qxl_mode_dumb_create,
241 .dumb_map_offset = qxl_mode_dumb_mmap,
Daniel Vetter43387b32013-07-16 09:12:04 +0200242 .dumb_destroy = drm_gem_dumb_destroy,
Dave Airlief64122c2013-02-25 14:47:55 +1000243#if defined(CONFIG_DEBUG_FS)
244 .debugfs_init = qxl_debugfs_init,
245 .debugfs_cleanup = qxl_debugfs_takedown,
246#endif
Andreas Pokorny47c12962014-08-08 10:40:56 +0200247 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
248 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
249 .gem_prime_export = drm_gem_prime_export,
250 .gem_prime_import = drm_gem_prime_import,
251 .gem_prime_pin = qxl_gem_prime_pin,
252 .gem_prime_unpin = qxl_gem_prime_unpin,
253 .gem_prime_get_sg_table = qxl_gem_prime_get_sg_table,
254 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
255 .gem_prime_vmap = qxl_gem_prime_vmap,
256 .gem_prime_vunmap = qxl_gem_prime_vunmap,
257 .gem_prime_mmap = qxl_gem_prime_mmap,
Dave Airlief64122c2013-02-25 14:47:55 +1000258 .gem_free_object = qxl_gem_object_free,
259 .gem_open_object = qxl_gem_object_open,
260 .gem_close_object = qxl_gem_object_close,
261 .fops = &qxl_fops,
262 .ioctls = qxl_ioctls,
263 .irq_handler = qxl_irq_handler,
264 .name = DRIVER_NAME,
265 .desc = DRIVER_DESC,
266 .date = DRIVER_DATE,
267 .major = 0,
268 .minor = 1,
269 .patchlevel = 0,
270};
271
272static int __init qxl_init(void)
273{
274#ifdef CONFIG_VGA_CONSOLE
275 if (vgacon_text_force() && qxl_modeset == -1)
276 return -EINVAL;
277#endif
278
279 if (qxl_modeset == 0)
280 return -EINVAL;
281 qxl_driver.num_ioctls = qxl_max_ioctls;
282 return drm_pci_init(&qxl_driver, &qxl_pci_driver);
283}
284
285static void __exit qxl_exit(void)
286{
287 drm_pci_exit(&qxl_driver, &qxl_pci_driver);
288}
289
290module_init(qxl_init);
291module_exit(qxl_exit);
292
293MODULE_AUTHOR(DRIVER_AUTHOR);
294MODULE_DESCRIPTION(DRIVER_DESC);
295MODULE_LICENSE("GPL and additional rights");