Loading...
1/* 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 "qxl_drv.h"
32#include <linux/console.h>
33#include <linux/module.h>
34#include <linux/pci.h>
35
36#include <drm/drm.h>
37#include <drm/drm_drv.h>
38#include <drm/drm_file.h>
39#include <drm/drm_modeset_helper.h>
40#include <drm/drm_prime.h>
41#include <drm/drm_probe_helper.h>
42
43#include "qxl_object.h"
44
45static const struct pci_device_id pciidlist[] = {
46 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
47 0xffff00, 0 },
48 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
49 0xffff00, 0 },
50 { 0, 0, 0 },
51};
52MODULE_DEVICE_TABLE(pci, pciidlist);
53
54static int qxl_modeset = -1;
55int qxl_num_crtc = 4;
56
57MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
58module_param_named(modeset, qxl_modeset, int, 0400);
59
60MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
61module_param_named(num_heads, qxl_num_crtc, int, 0400);
62
63static struct drm_driver qxl_driver;
64static struct pci_driver qxl_pci_driver;
65
66static bool is_vga(struct pci_dev *pdev)
67{
68 return pdev->class == PCI_CLASS_DISPLAY_VGA << 8;
69}
70
71static int
72qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
73{
74 struct qxl_device *qdev;
75 int ret;
76
77 if (pdev->revision < 4) {
78 DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
79 " use xf86-video-qxl in user mode");
80 return -EINVAL; /* TODO: ENODEV ? */
81 }
82
83 qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL);
84 if (!qdev)
85 return -ENOMEM;
86
87 ret = pci_enable_device(pdev);
88 if (ret)
89 goto free_dev;
90
91 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, 0, "qxl");
92 if (ret)
93 goto disable_pci;
94
95 if (is_vga(pdev)) {
96 ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
97 if (ret) {
98 DRM_ERROR("can't get legacy vga ioports\n");
99 goto disable_pci;
100 }
101 }
102
103 ret = qxl_device_init(qdev, &qxl_driver, pdev);
104 if (ret)
105 goto put_vga;
106
107 ret = qxl_modeset_init(qdev);
108 if (ret)
109 goto unload;
110
111 drm_kms_helper_poll_init(&qdev->ddev);
112
113 /* Complete initialization. */
114 ret = drm_dev_register(&qdev->ddev, ent->driver_data);
115 if (ret)
116 goto modeset_cleanup;
117
118 drm_fbdev_generic_setup(&qdev->ddev, 32);
119 return 0;
120
121modeset_cleanup:
122 qxl_modeset_fini(qdev);
123unload:
124 qxl_device_fini(qdev);
125put_vga:
126 if (is_vga(pdev))
127 vga_put(pdev, VGA_RSRC_LEGACY_IO);
128disable_pci:
129 pci_disable_device(pdev);
130free_dev:
131 kfree(qdev);
132 return ret;
133}
134
135static void
136qxl_pci_remove(struct pci_dev *pdev)
137{
138 struct drm_device *dev = pci_get_drvdata(pdev);
139 struct qxl_device *qdev = dev->dev_private;
140
141 drm_dev_unregister(dev);
142
143 qxl_modeset_fini(qdev);
144 qxl_device_fini(qdev);
145 if (is_vga(pdev))
146 vga_put(pdev, VGA_RSRC_LEGACY_IO);
147
148 dev->dev_private = NULL;
149 kfree(qdev);
150 drm_dev_put(dev);
151}
152
153static const struct file_operations qxl_fops = {
154 .owner = THIS_MODULE,
155 .open = drm_open,
156 .release = drm_release,
157 .unlocked_ioctl = drm_ioctl,
158 .poll = drm_poll,
159 .read = drm_read,
160 .mmap = qxl_mmap,
161};
162
163static int qxl_drm_freeze(struct drm_device *dev)
164{
165 struct pci_dev *pdev = dev->pdev;
166 struct qxl_device *qdev = dev->dev_private;
167 int ret;
168
169 ret = drm_mode_config_helper_suspend(dev);
170 if (ret)
171 return ret;
172
173 qxl_destroy_monitors_object(qdev);
174 qxl_surf_evict(qdev);
175 qxl_vram_evict(qdev);
176
177 while (!qxl_check_idle(qdev->command_ring));
178 while (!qxl_check_idle(qdev->release_ring))
179 qxl_queue_garbage_collect(qdev, 1);
180
181 pci_save_state(pdev);
182
183 return 0;
184}
185
186static int qxl_drm_resume(struct drm_device *dev, bool thaw)
187{
188 struct qxl_device *qdev = dev->dev_private;
189
190 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
191 if (!thaw) {
192 qxl_reinit_memslots(qdev);
193 qxl_ring_init_hdr(qdev->release_ring);
194 }
195
196 qxl_create_monitors_object(qdev);
197 return drm_mode_config_helper_resume(dev);
198}
199
200static int qxl_pm_suspend(struct device *dev)
201{
202 struct pci_dev *pdev = to_pci_dev(dev);
203 struct drm_device *drm_dev = pci_get_drvdata(pdev);
204 int error;
205
206 error = qxl_drm_freeze(drm_dev);
207 if (error)
208 return error;
209
210 pci_disable_device(pdev);
211 pci_set_power_state(pdev, PCI_D3hot);
212 return 0;
213}
214
215static int qxl_pm_resume(struct device *dev)
216{
217 struct pci_dev *pdev = to_pci_dev(dev);
218 struct drm_device *drm_dev = pci_get_drvdata(pdev);
219
220 pci_set_power_state(pdev, PCI_D0);
221 pci_restore_state(pdev);
222 if (pci_enable_device(pdev)) {
223 return -EIO;
224 }
225
226 return qxl_drm_resume(drm_dev, false);
227}
228
229static int qxl_pm_thaw(struct device *dev)
230{
231 struct drm_device *drm_dev = dev_get_drvdata(dev);
232
233 return qxl_drm_resume(drm_dev, true);
234}
235
236static int qxl_pm_freeze(struct device *dev)
237{
238 struct drm_device *drm_dev = dev_get_drvdata(dev);
239
240 return qxl_drm_freeze(drm_dev);
241}
242
243static int qxl_pm_restore(struct device *dev)
244{
245 struct pci_dev *pdev = to_pci_dev(dev);
246 struct drm_device *drm_dev = pci_get_drvdata(pdev);
247 struct qxl_device *qdev = drm_dev->dev_private;
248
249 qxl_io_reset(qdev);
250 return qxl_drm_resume(drm_dev, false);
251}
252
253static const struct dev_pm_ops qxl_pm_ops = {
254 .suspend = qxl_pm_suspend,
255 .resume = qxl_pm_resume,
256 .freeze = qxl_pm_freeze,
257 .thaw = qxl_pm_thaw,
258 .poweroff = qxl_pm_freeze,
259 .restore = qxl_pm_restore,
260};
261static struct pci_driver qxl_pci_driver = {
262 .name = DRIVER_NAME,
263 .id_table = pciidlist,
264 .probe = qxl_pci_probe,
265 .remove = qxl_pci_remove,
266 .driver.pm = &qxl_pm_ops,
267};
268
269static struct drm_driver qxl_driver = {
270 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
271
272 .dumb_create = qxl_mode_dumb_create,
273 .dumb_map_offset = qxl_mode_dumb_mmap,
274#if defined(CONFIG_DEBUG_FS)
275 .debugfs_init = qxl_debugfs_init,
276#endif
277 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
278 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
279 .gem_prime_pin = qxl_gem_prime_pin,
280 .gem_prime_unpin = qxl_gem_prime_unpin,
281 .gem_prime_get_sg_table = qxl_gem_prime_get_sg_table,
282 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
283 .gem_prime_vmap = qxl_gem_prime_vmap,
284 .gem_prime_vunmap = qxl_gem_prime_vunmap,
285 .gem_prime_mmap = qxl_gem_prime_mmap,
286 .gem_free_object_unlocked = qxl_gem_object_free,
287 .gem_open_object = qxl_gem_object_open,
288 .gem_close_object = qxl_gem_object_close,
289 .fops = &qxl_fops,
290 .ioctls = qxl_ioctls,
291 .irq_handler = qxl_irq_handler,
292 .name = DRIVER_NAME,
293 .desc = DRIVER_DESC,
294 .date = DRIVER_DATE,
295 .major = 0,
296 .minor = 1,
297 .patchlevel = 0,
298};
299
300static int __init qxl_init(void)
301{
302 if (vgacon_text_force() && qxl_modeset == -1)
303 return -EINVAL;
304
305 if (qxl_modeset == 0)
306 return -EINVAL;
307 qxl_driver.num_ioctls = qxl_max_ioctls;
308 return pci_register_driver(&qxl_pci_driver);
309}
310
311static void __exit qxl_exit(void)
312{
313 pci_unregister_driver(&qxl_pci_driver);
314}
315
316module_init(qxl_init);
317module_exit(qxl_exit);
318
319MODULE_AUTHOR(DRIVER_AUTHOR);
320MODULE_DESCRIPTION(DRIVER_DESC);
321MODULE_LICENSE("GPL and additional rights");
1/* 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"
36#include "drm_crtc_helper.h"
37#include "qxl_drv.h"
38#include "qxl_object.h"
39
40extern int qxl_max_ioctls;
41static DEFINE_PCI_DEVICE_TABLE(pciidlist) = {
42 { 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
50static int qxl_modeset = -1;
51int qxl_num_crtc = 4;
52
53MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
54module_param_named(modeset, qxl_modeset, int, 0400);
55
56MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
57module_param_named(num_heads, qxl_num_crtc, int, 0400);
58
59static 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
81static 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,
87 .mmap = qxl_mmap,
88};
89
90static int qxl_drm_freeze(struct drm_device *dev)
91{
92 struct pci_dev *pdev = dev->pdev;
93 struct qxl_device *qdev = dev->dev_private;
94 struct drm_crtc *crtc;
95
96 drm_kms_helper_poll_disable(dev);
97
98 console_lock();
99 qxl_fbdev_set_suspend(qdev, 1);
100 console_unlock();
101
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;
105 if (crtc->enabled)
106 (*crtc_funcs->disable)(crtc);
107 }
108
109 qxl_destroy_monitors_object(qdev);
110 qxl_surf_evict(qdev);
111 qxl_vram_evict(qdev);
112
113 while (!qxl_check_idle(qdev->command_ring));
114 while (!qxl_check_idle(qdev->release_ring))
115 qxl_queue_garbage_collect(qdev, 1);
116
117 pci_save_state(pdev);
118
119 return 0;
120}
121
122static int qxl_drm_resume(struct drm_device *dev, bool thaw)
123{
124 struct qxl_device *qdev = dev->dev_private;
125
126 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
127 if (!thaw) {
128 qxl_reinit_memslots(qdev);
129 qxl_ring_init_hdr(qdev->release_ring);
130 }
131
132 qxl_create_monitors_object(qdev);
133 drm_helper_resume_force_mode(dev);
134
135 console_lock();
136 qxl_fbdev_set_suspend(qdev, 0);
137 console_unlock();
138
139 drm_kms_helper_poll_enable(dev);
140 return 0;
141}
142
143static int qxl_pm_suspend(struct device *dev)
144{
145 struct pci_dev *pdev = to_pci_dev(dev);
146 struct drm_device *drm_dev = pci_get_drvdata(pdev);
147 int error;
148
149 error = qxl_drm_freeze(drm_dev);
150 if (error)
151 return error;
152
153 pci_disable_device(pdev);
154 pci_set_power_state(pdev, PCI_D3hot);
155 return 0;
156}
157
158static int qxl_pm_resume(struct device *dev)
159{
160 struct pci_dev *pdev = to_pci_dev(dev);
161 struct drm_device *drm_dev = pci_get_drvdata(pdev);
162
163 pci_set_power_state(pdev, PCI_D0);
164 pci_restore_state(pdev);
165 if (pci_enable_device(pdev)) {
166 return -EIO;
167 }
168
169 return qxl_drm_resume(drm_dev, false);
170}
171
172static int qxl_pm_thaw(struct device *dev)
173{
174 struct pci_dev *pdev = to_pci_dev(dev);
175 struct drm_device *drm_dev = pci_get_drvdata(pdev);
176
177 return qxl_drm_resume(drm_dev, true);
178}
179
180static int qxl_pm_freeze(struct device *dev)
181{
182 struct pci_dev *pdev = to_pci_dev(dev);
183 struct drm_device *drm_dev = pci_get_drvdata(pdev);
184
185 return qxl_drm_freeze(drm_dev);
186}
187
188static int qxl_pm_restore(struct device *dev)
189{
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;
193
194 qxl_io_reset(qdev);
195 return qxl_drm_resume(drm_dev, false);
196}
197
198static const struct dev_pm_ops qxl_pm_ops = {
199 .suspend = qxl_pm_suspend,
200 .resume = qxl_pm_resume,
201 .freeze = qxl_pm_freeze,
202 .thaw = qxl_pm_thaw,
203 .poweroff = qxl_pm_freeze,
204 .restore = qxl_pm_restore,
205};
206static struct pci_driver qxl_pci_driver = {
207 .name = DRIVER_NAME,
208 .id_table = pciidlist,
209 .probe = qxl_pci_probe,
210 .remove = qxl_pci_remove,
211 .driver.pm = &qxl_pm_ops,
212};
213
214static struct drm_driver qxl_driver = {
215 .driver_features = DRIVER_GEM | DRIVER_MODESET |
216 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED,
217 .dev_priv_size = 0,
218 .load = qxl_driver_load,
219 .unload = qxl_driver_unload,
220
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,
227#endif
228 .gem_free_object = qxl_gem_object_free,
229 .gem_open_object = qxl_gem_object_open,
230 .gem_close_object = qxl_gem_object_close,
231 .fops = &qxl_fops,
232 .ioctls = qxl_ioctls,
233 .irq_handler = qxl_irq_handler,
234 .name = DRIVER_NAME,
235 .desc = DRIVER_DESC,
236 .date = DRIVER_DATE,
237 .major = 0,
238 .minor = 1,
239 .patchlevel = 0,
240};
241
242static int __init qxl_init(void)
243{
244#ifdef CONFIG_VGA_CONSOLE
245 if (vgacon_text_force() && qxl_modeset == -1)
246 return -EINVAL;
247#endif
248
249 if (qxl_modeset == 0)
250 return -EINVAL;
251 qxl_driver.num_ioctls = qxl_max_ioctls;
252 return drm_pci_init(&qxl_driver, &qxl_pci_driver);
253}
254
255static void __exit qxl_exit(void)
256{
257 drm_pci_exit(&qxl_driver, &qxl_pci_driver);
258}
259
260module_init(qxl_init);
261module_exit(qxl_exit);
262
263MODULE_AUTHOR(DRIVER_AUTHOR);
264MODULE_DESCRIPTION(DRIVER_DESC);
265MODULE_LICENSE("GPL and additional rights");