Loading...
1/* qxl_drv.c -- QXL driver -*- linux-c -*-
2 *
3 * Copyright 2011 Red Hat, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Dave Airlie <airlie@redhat.com>
27 * Alon Levy <alevy@redhat.com>
28 */
29
30#include "qxl_drv.h"
31
32#include <linux/module.h>
33#include <linux/pci.h>
34#include <linux/vgaarb.h>
35
36#include <drm/drm.h>
37#include <drm/drm_aperture.h>
38#include <drm/drm_atomic_helper.h>
39#include <drm/drm_drv.h>
40#include <drm/drm_fbdev_generic.h>
41#include <drm/drm_file.h>
42#include <drm/drm_gem_ttm_helper.h>
43#include <drm/drm_module.h>
44#include <drm/drm_modeset_helper.h>
45#include <drm/drm_prime.h>
46#include <drm/drm_probe_helper.h>
47
48#include "qxl_object.h"
49
50static const struct pci_device_id pciidlist[] = {
51 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
52 0xffff00, 0 },
53 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
54 0xffff00, 0 },
55 { 0, 0, 0 },
56};
57MODULE_DEVICE_TABLE(pci, pciidlist);
58
59static int qxl_modeset = -1;
60int qxl_num_crtc = 4;
61
62MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
63module_param_named(modeset, qxl_modeset, int, 0400);
64
65MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
66module_param_named(num_heads, qxl_num_crtc, int, 0400);
67
68static struct drm_driver qxl_driver;
69static struct pci_driver qxl_pci_driver;
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 = devm_drm_dev_alloc(&pdev->dev, &qxl_driver,
84 struct qxl_device, ddev);
85 if (IS_ERR(qdev)) {
86 pr_err("Unable to init drm dev");
87 return -ENOMEM;
88 }
89
90 ret = pci_enable_device(pdev);
91 if (ret)
92 return ret;
93
94 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &qxl_driver);
95 if (ret)
96 goto disable_pci;
97
98 if (pci_is_vga(pdev) && pdev->revision < 5) {
99 ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
100 if (ret) {
101 DRM_ERROR("can't get legacy vga ioports\n");
102 goto disable_pci;
103 }
104 }
105
106 ret = qxl_device_init(qdev, pdev);
107 if (ret)
108 goto put_vga;
109
110 ret = qxl_modeset_init(qdev);
111 if (ret)
112 goto unload;
113
114 drm_kms_helper_poll_init(&qdev->ddev);
115
116 /* Complete initialization. */
117 ret = drm_dev_register(&qdev->ddev, ent->driver_data);
118 if (ret)
119 goto modeset_cleanup;
120
121 drm_fbdev_generic_setup(&qdev->ddev, 32);
122 return 0;
123
124modeset_cleanup:
125 qxl_modeset_fini(qdev);
126unload:
127 qxl_device_fini(qdev);
128put_vga:
129 if (pci_is_vga(pdev) && pdev->revision < 5)
130 vga_put(pdev, VGA_RSRC_LEGACY_IO);
131disable_pci:
132 pci_disable_device(pdev);
133
134 return ret;
135}
136
137static void qxl_drm_release(struct drm_device *dev)
138{
139 struct qxl_device *qdev = to_qxl(dev);
140
141 /*
142 * TODO: qxl_device_fini() call should be in qxl_pci_remove(),
143 * reordering qxl_modeset_fini() + qxl_device_fini() calls is
144 * non-trivial though.
145 */
146 qxl_modeset_fini(qdev);
147 qxl_device_fini(qdev);
148}
149
150static void
151qxl_pci_remove(struct pci_dev *pdev)
152{
153 struct drm_device *dev = pci_get_drvdata(pdev);
154
155 drm_dev_unregister(dev);
156 drm_atomic_helper_shutdown(dev);
157 if (pci_is_vga(pdev) && pdev->revision < 5)
158 vga_put(pdev, VGA_RSRC_LEGACY_IO);
159}
160
161static void
162qxl_pci_shutdown(struct pci_dev *pdev)
163{
164 drm_atomic_helper_shutdown(pci_get_drvdata(pdev));
165}
166
167DEFINE_DRM_GEM_FOPS(qxl_fops);
168
169static int qxl_drm_freeze(struct drm_device *dev)
170{
171 struct pci_dev *pdev = to_pci_dev(dev->dev);
172 struct qxl_device *qdev = to_qxl(dev);
173 int ret;
174
175 ret = drm_mode_config_helper_suspend(dev);
176 if (ret)
177 return ret;
178
179 qxl_destroy_monitors_object(qdev);
180 qxl_surf_evict(qdev);
181 qxl_vram_evict(qdev);
182
183 while (!qxl_check_idle(qdev->command_ring));
184 while (!qxl_check_idle(qdev->release_ring))
185 qxl_queue_garbage_collect(qdev, 1);
186
187 pci_save_state(pdev);
188
189 return 0;
190}
191
192static int qxl_drm_resume(struct drm_device *dev, bool thaw)
193{
194 struct qxl_device *qdev = to_qxl(dev);
195
196 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
197 if (!thaw) {
198 qxl_reinit_memslots(qdev);
199 }
200
201 qxl_create_monitors_object(qdev);
202 return drm_mode_config_helper_resume(dev);
203}
204
205static int qxl_pm_suspend(struct device *dev)
206{
207 struct pci_dev *pdev = to_pci_dev(dev);
208 struct drm_device *drm_dev = pci_get_drvdata(pdev);
209 int error;
210
211 error = qxl_drm_freeze(drm_dev);
212 if (error)
213 return error;
214
215 pci_disable_device(pdev);
216 pci_set_power_state(pdev, PCI_D3hot);
217 return 0;
218}
219
220static int qxl_pm_resume(struct device *dev)
221{
222 struct pci_dev *pdev = to_pci_dev(dev);
223 struct drm_device *drm_dev = pci_get_drvdata(pdev);
224 struct qxl_device *qdev = to_qxl(drm_dev);
225
226 pci_set_power_state(pdev, PCI_D0);
227 pci_restore_state(pdev);
228 if (pci_enable_device(pdev)) {
229 return -EIO;
230 }
231
232 qxl_io_reset(qdev);
233 return qxl_drm_resume(drm_dev, false);
234}
235
236static int qxl_pm_thaw(struct device *dev)
237{
238 struct drm_device *drm_dev = dev_get_drvdata(dev);
239
240 return qxl_drm_resume(drm_dev, true);
241}
242
243static int qxl_pm_freeze(struct device *dev)
244{
245 struct drm_device *drm_dev = dev_get_drvdata(dev);
246
247 return qxl_drm_freeze(drm_dev);
248}
249
250static int qxl_pm_restore(struct device *dev)
251{
252 struct pci_dev *pdev = to_pci_dev(dev);
253 struct drm_device *drm_dev = pci_get_drvdata(pdev);
254 struct qxl_device *qdev = to_qxl(drm_dev);
255
256 qxl_io_reset(qdev);
257 return qxl_drm_resume(drm_dev, false);
258}
259
260static const struct dev_pm_ops qxl_pm_ops = {
261 .suspend = qxl_pm_suspend,
262 .resume = qxl_pm_resume,
263 .freeze = qxl_pm_freeze,
264 .thaw = qxl_pm_thaw,
265 .poweroff = qxl_pm_freeze,
266 .restore = qxl_pm_restore,
267};
268static struct pci_driver qxl_pci_driver = {
269 .name = DRIVER_NAME,
270 .id_table = pciidlist,
271 .probe = qxl_pci_probe,
272 .remove = qxl_pci_remove,
273 .shutdown = qxl_pci_shutdown,
274 .driver.pm = &qxl_pm_ops,
275};
276
277static const struct drm_ioctl_desc qxl_ioctls[] = {
278 DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH),
279 DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH),
280 DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl, DRM_AUTH),
281 DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl, DRM_AUTH),
282 DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl, DRM_AUTH),
283 DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl, DRM_AUTH),
284 DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl, DRM_AUTH),
285};
286
287static struct drm_driver qxl_driver = {
288 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_CURSOR_HOTSPOT,
289
290 .dumb_create = qxl_mode_dumb_create,
291 .dumb_map_offset = drm_gem_ttm_dumb_map_offset,
292#if defined(CONFIG_DEBUG_FS)
293 .debugfs_init = qxl_debugfs_init,
294#endif
295 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
296 .fops = &qxl_fops,
297 .ioctls = qxl_ioctls,
298 .num_ioctls = ARRAY_SIZE(qxl_ioctls),
299 .name = DRIVER_NAME,
300 .desc = DRIVER_DESC,
301 .date = DRIVER_DATE,
302 .major = 0,
303 .minor = 1,
304 .patchlevel = 0,
305
306 .release = qxl_drm_release,
307};
308
309drm_module_pci_driver_if_modeset(qxl_pci_driver, qxl_modeset);
310
311MODULE_AUTHOR(DRIVER_AUTHOR);
312MODULE_DESCRIPTION(DRIVER_DESC);
313MODULE_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 "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_atomic_helper.h>
38#include <drm/drm_drv.h>
39#include <drm/drm_file.h>
40#include <drm/drm_modeset_helper.h>
41#include <drm/drm_prime.h>
42#include <drm/drm_probe_helper.h>
43
44#include "qxl_object.h"
45
46static const struct pci_device_id pciidlist[] = {
47 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
48 0xffff00, 0 },
49 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
50 0xffff00, 0 },
51 { 0, 0, 0 },
52};
53MODULE_DEVICE_TABLE(pci, pciidlist);
54
55static int qxl_modeset = -1;
56int qxl_num_crtc = 4;
57
58MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
59module_param_named(modeset, qxl_modeset, int, 0400);
60
61MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
62module_param_named(num_heads, qxl_num_crtc, int, 0400);
63
64static struct drm_driver qxl_driver;
65static struct pci_driver qxl_pci_driver;
66
67static bool is_vga(struct pci_dev *pdev)
68{
69 return pdev->class == PCI_CLASS_DISPLAY_VGA << 8;
70}
71
72static int
73qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
74{
75 struct qxl_device *qdev;
76 int ret;
77
78 if (pdev->revision < 4) {
79 DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
80 " use xf86-video-qxl in user mode");
81 return -EINVAL; /* TODO: ENODEV ? */
82 }
83
84 qdev = devm_drm_dev_alloc(&pdev->dev, &qxl_driver,
85 struct qxl_device, ddev);
86 if (IS_ERR(qdev)) {
87 pr_err("Unable to init drm dev");
88 return -ENOMEM;
89 }
90
91 ret = pci_enable_device(pdev);
92 if (ret)
93 return ret;
94
95 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "qxl");
96 if (ret)
97 goto disable_pci;
98
99 if (is_vga(pdev)) {
100 ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
101 if (ret) {
102 DRM_ERROR("can't get legacy vga ioports\n");
103 goto disable_pci;
104 }
105 }
106
107 ret = qxl_device_init(qdev, pdev);
108 if (ret)
109 goto put_vga;
110
111 ret = qxl_modeset_init(qdev);
112 if (ret)
113 goto unload;
114
115 drm_kms_helper_poll_init(&qdev->ddev);
116
117 /* Complete initialization. */
118 ret = drm_dev_register(&qdev->ddev, ent->driver_data);
119 if (ret)
120 goto modeset_cleanup;
121
122 drm_fbdev_generic_setup(&qdev->ddev, 32);
123 return 0;
124
125modeset_cleanup:
126 qxl_modeset_fini(qdev);
127unload:
128 qxl_device_fini(qdev);
129put_vga:
130 if (is_vga(pdev))
131 vga_put(pdev, VGA_RSRC_LEGACY_IO);
132disable_pci:
133 pci_disable_device(pdev);
134
135 return ret;
136}
137
138static void qxl_drm_release(struct drm_device *dev)
139{
140 struct qxl_device *qdev = to_qxl(dev);
141
142 /*
143 * TODO: qxl_device_fini() call should be in qxl_pci_remove(),
144 * reodering qxl_modeset_fini() + qxl_device_fini() calls is
145 * non-trivial though.
146 */
147 qxl_modeset_fini(qdev);
148 qxl_device_fini(qdev);
149}
150
151static void
152qxl_pci_remove(struct pci_dev *pdev)
153{
154 struct drm_device *dev = pci_get_drvdata(pdev);
155
156 drm_dev_unregister(dev);
157 drm_atomic_helper_shutdown(dev);
158 if (is_vga(pdev))
159 vga_put(pdev, VGA_RSRC_LEGACY_IO);
160}
161
162DEFINE_DRM_GEM_FOPS(qxl_fops);
163
164static int qxl_drm_freeze(struct drm_device *dev)
165{
166 struct pci_dev *pdev = dev->pdev;
167 struct qxl_device *qdev = to_qxl(dev);
168 int ret;
169
170 ret = drm_mode_config_helper_suspend(dev);
171 if (ret)
172 return ret;
173
174 qxl_destroy_monitors_object(qdev);
175 qxl_surf_evict(qdev);
176 qxl_vram_evict(qdev);
177
178 while (!qxl_check_idle(qdev->command_ring));
179 while (!qxl_check_idle(qdev->release_ring))
180 qxl_queue_garbage_collect(qdev, 1);
181
182 pci_save_state(pdev);
183
184 return 0;
185}
186
187static int qxl_drm_resume(struct drm_device *dev, bool thaw)
188{
189 struct qxl_device *qdev = to_qxl(dev);
190
191 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
192 if (!thaw) {
193 qxl_reinit_memslots(qdev);
194 qxl_ring_init_hdr(qdev->release_ring);
195 }
196
197 qxl_create_monitors_object(qdev);
198 return drm_mode_config_helper_resume(dev);
199}
200
201static int qxl_pm_suspend(struct device *dev)
202{
203 struct pci_dev *pdev = to_pci_dev(dev);
204 struct drm_device *drm_dev = pci_get_drvdata(pdev);
205 int error;
206
207 error = qxl_drm_freeze(drm_dev);
208 if (error)
209 return error;
210
211 pci_disable_device(pdev);
212 pci_set_power_state(pdev, PCI_D3hot);
213 return 0;
214}
215
216static int qxl_pm_resume(struct device *dev)
217{
218 struct pci_dev *pdev = to_pci_dev(dev);
219 struct drm_device *drm_dev = pci_get_drvdata(pdev);
220
221 pci_set_power_state(pdev, PCI_D0);
222 pci_restore_state(pdev);
223 if (pci_enable_device(pdev)) {
224 return -EIO;
225 }
226
227 return qxl_drm_resume(drm_dev, false);
228}
229
230static int qxl_pm_thaw(struct device *dev)
231{
232 struct drm_device *drm_dev = dev_get_drvdata(dev);
233
234 return qxl_drm_resume(drm_dev, true);
235}
236
237static int qxl_pm_freeze(struct device *dev)
238{
239 struct drm_device *drm_dev = dev_get_drvdata(dev);
240
241 return qxl_drm_freeze(drm_dev);
242}
243
244static int qxl_pm_restore(struct device *dev)
245{
246 struct pci_dev *pdev = to_pci_dev(dev);
247 struct drm_device *drm_dev = pci_get_drvdata(pdev);
248 struct qxl_device *qdev = to_qxl(drm_dev);
249
250 qxl_io_reset(qdev);
251 return qxl_drm_resume(drm_dev, false);
252}
253
254static const struct dev_pm_ops qxl_pm_ops = {
255 .suspend = qxl_pm_suspend,
256 .resume = qxl_pm_resume,
257 .freeze = qxl_pm_freeze,
258 .thaw = qxl_pm_thaw,
259 .poweroff = qxl_pm_freeze,
260 .restore = qxl_pm_restore,
261};
262static struct pci_driver qxl_pci_driver = {
263 .name = DRIVER_NAME,
264 .id_table = pciidlist,
265 .probe = qxl_pci_probe,
266 .remove = qxl_pci_remove,
267 .driver.pm = &qxl_pm_ops,
268};
269
270static struct drm_driver qxl_driver = {
271 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
272
273 .dumb_create = qxl_mode_dumb_create,
274 .dumb_map_offset = qxl_mode_dumb_mmap,
275#if defined(CONFIG_DEBUG_FS)
276 .debugfs_init = qxl_debugfs_init,
277#endif
278 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
279 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
280 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
281 .gem_prime_mmap = qxl_gem_prime_mmap,
282 .fops = &qxl_fops,
283 .ioctls = qxl_ioctls,
284 .irq_handler = qxl_irq_handler,
285 .name = DRIVER_NAME,
286 .desc = DRIVER_DESC,
287 .date = DRIVER_DATE,
288 .major = 0,
289 .minor = 1,
290 .patchlevel = 0,
291
292 .release = qxl_drm_release,
293};
294
295static int __init qxl_init(void)
296{
297 if (vgacon_text_force() && qxl_modeset == -1)
298 return -EINVAL;
299
300 if (qxl_modeset == 0)
301 return -EINVAL;
302 qxl_driver.num_ioctls = qxl_max_ioctls;
303 return pci_register_driver(&qxl_pci_driver);
304}
305
306static void __exit qxl_exit(void)
307{
308 pci_unregister_driver(&qxl_pci_driver);
309}
310
311module_init(qxl_init);
312module_exit(qxl_exit);
313
314MODULE_AUTHOR(DRIVER_AUTHOR);
315MODULE_DESCRIPTION(DRIVER_DESC);
316MODULE_LICENSE("GPL and additional rights");