Merge branch 'i2c/for-current-fixed' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / gpu / drm / qxl / qxl_drv.c
CommitLineData
f64122c1
DA
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
edaf492c
MY
34#include <drm/drmP.h>
35#include <drm/drm.h>
fcd70cd3
DV
36#include <drm/drm_modeset_helper.h>
37#include <drm/drm_probe_helper.h>
f64122c1 38#include "qxl_drv.h"
d84300bf 39#include "qxl_object.h"
f64122c1 40
9baa3c34 41static const struct pci_device_id pciidlist[] = {
f64122c1
DA
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
6d01f1f5 50static int qxl_modeset = -1;
07f8d9bd 51int qxl_num_crtc = 4;
f64122c1
DA
52
53MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
54module_param_named(modeset, qxl_modeset, int, 0400);
55
07f8d9bd
DA
56MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
57module_param_named(num_heads, qxl_num_crtc, int, 0400);
58
f64122c1
DA
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{
2b65d567
GKB
65 struct qxl_device *qdev;
66 int ret;
67
f64122c1
DA
68 if (pdev->revision < 4) {
69 DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
70 " use xf86-video-qxl in user mode");
71 return -EINVAL; /* TODO: ENODEV ? */
72 }
2b65d567 73
2b65d567 74 qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL);
cbdded7f
GKB
75 if (!qdev)
76 return -ENOMEM;
2b65d567
GKB
77
78 ret = pci_enable_device(pdev);
79 if (ret)
cbdded7f 80 goto free_dev;
2b65d567 81
c34674a2
GH
82 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, 0, "qxl");
83 if (ret)
84 goto disable_pci;
85
aa5b62ba 86 ret = qxl_device_init(qdev, &qxl_driver, pdev);
2b65d567
GKB
87 if (ret)
88 goto disable_pci;
89
2b65d567
GKB
90 ret = qxl_modeset_init(qdev);
91 if (ret)
64ca824e 92 goto unload;
2b65d567 93
cbdded7f 94 drm_kms_helper_poll_init(&qdev->ddev);
2b65d567
GKB
95
96 /* Complete initialization. */
cbdded7f 97 ret = drm_dev_register(&qdev->ddev, ent->driver_data);
2b65d567
GKB
98 if (ret)
99 goto modeset_cleanup;
100
26d4707d 101 drm_fbdev_generic_setup(&qdev->ddev, 32);
2b65d567
GKB
102 return 0;
103
104modeset_cleanup:
105 qxl_modeset_fini(qdev);
2b65d567
GKB
106unload:
107 qxl_device_fini(qdev);
108disable_pci:
109 pci_disable_device(pdev);
cbdded7f 110free_dev:
2b65d567 111 kfree(qdev);
2b65d567 112 return ret;
f64122c1
DA
113}
114
115static void
116qxl_pci_remove(struct pci_dev *pdev)
117{
118 struct drm_device *dev = pci_get_drvdata(pdev);
6f897f51
GKB
119 struct qxl_device *qdev = dev->dev_private;
120
121 drm_dev_unregister(dev);
122
123 qxl_modeset_fini(qdev);
124 qxl_device_fini(qdev);
f64122c1 125
6f897f51
GKB
126 dev->dev_private = NULL;
127 kfree(qdev);
b0d146ac 128 drm_dev_put(dev);
f64122c1
DA
129}
130
f64122c1
DA
131static const struct file_operations qxl_fops = {
132 .owner = THIS_MODULE,
133 .open = drm_open,
134 .release = drm_release,
135 .unlocked_ioctl = drm_ioctl,
136 .poll = drm_poll,
058e9f5c 137 .read = drm_read,
f64122c1
DA
138 .mmap = qxl_mmap,
139};
140
d84300bf
DA
141static int qxl_drm_freeze(struct drm_device *dev)
142{
143 struct pci_dev *pdev = dev->pdev;
144 struct qxl_device *qdev = dev->dev_private;
7948a2b1 145 int ret;
d84300bf 146
7948a2b1
PW
147 ret = drm_mode_config_helper_suspend(dev);
148 if (ret)
149 return ret;
d84300bf
DA
150
151 qxl_destroy_monitors_object(qdev);
152 qxl_surf_evict(qdev);
153 qxl_vram_evict(qdev);
154
155 while (!qxl_check_idle(qdev->command_ring));
156 while (!qxl_check_idle(qdev->release_ring))
157 qxl_queue_garbage_collect(qdev, 1);
158
159 pci_save_state(pdev);
160
161 return 0;
162}
163
164static int qxl_drm_resume(struct drm_device *dev, bool thaw)
165{
166 struct qxl_device *qdev = dev->dev_private;
167
168 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
169 if (!thaw) {
170 qxl_reinit_memslots(qdev);
171 qxl_ring_init_hdr(qdev->release_ring);
172 }
173
174 qxl_create_monitors_object(qdev);
7948a2b1 175 return drm_mode_config_helper_resume(dev);
d84300bf
DA
176}
177
178static int qxl_pm_suspend(struct device *dev)
179{
180 struct pci_dev *pdev = to_pci_dev(dev);
181 struct drm_device *drm_dev = pci_get_drvdata(pdev);
182 int error;
183
184 error = qxl_drm_freeze(drm_dev);
185 if (error)
186 return error;
187
188 pci_disable_device(pdev);
189 pci_set_power_state(pdev, PCI_D3hot);
190 return 0;
191}
192
193static int qxl_pm_resume(struct device *dev)
194{
195 struct pci_dev *pdev = to_pci_dev(dev);
196 struct drm_device *drm_dev = pci_get_drvdata(pdev);
197
198 pci_set_power_state(pdev, PCI_D0);
199 pci_restore_state(pdev);
200 if (pci_enable_device(pdev)) {
201 return -EIO;
202 }
203
204 return qxl_drm_resume(drm_dev, false);
205}
206
207static int qxl_pm_thaw(struct device *dev)
208{
209 struct pci_dev *pdev = to_pci_dev(dev);
210 struct drm_device *drm_dev = pci_get_drvdata(pdev);
211
212 return qxl_drm_resume(drm_dev, true);
213}
214
215static int qxl_pm_freeze(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 return qxl_drm_freeze(drm_dev);
221}
222
223static int qxl_pm_restore(struct device *dev)
224{
225 struct pci_dev *pdev = to_pci_dev(dev);
226 struct drm_device *drm_dev = pci_get_drvdata(pdev);
227 struct qxl_device *qdev = drm_dev->dev_private;
228
229 qxl_io_reset(qdev);
230 return qxl_drm_resume(drm_dev, false);
231}
232
233static const struct dev_pm_ops qxl_pm_ops = {
234 .suspend = qxl_pm_suspend,
235 .resume = qxl_pm_resume,
236 .freeze = qxl_pm_freeze,
237 .thaw = qxl_pm_thaw,
238 .poweroff = qxl_pm_freeze,
239 .restore = qxl_pm_restore,
240};
241static struct pci_driver qxl_pci_driver = {
242 .name = DRIVER_NAME,
243 .id_table = pciidlist,
244 .probe = qxl_pci_probe,
245 .remove = qxl_pci_remove,
246 .driver.pm = &qxl_pm_ops,
247};
248
f64122c1 249static struct drm_driver qxl_driver = {
47c12968 250 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
fb4fe33f 251 DRIVER_ATOMIC,
f64122c1
DA
252
253 .dumb_create = qxl_mode_dumb_create,
254 .dumb_map_offset = qxl_mode_dumb_mmap,
f64122c1
DA
255#if defined(CONFIG_DEBUG_FS)
256 .debugfs_init = qxl_debugfs_init,
f64122c1 257#endif
47c12968
AP
258 .gem_prime_export = drm_gem_prime_export,
259 .gem_prime_import = drm_gem_prime_import,
260 .gem_prime_pin = qxl_gem_prime_pin,
261 .gem_prime_unpin = qxl_gem_prime_unpin,
47c12968
AP
262 .gem_prime_vmap = qxl_gem_prime_vmap,
263 .gem_prime_vunmap = qxl_gem_prime_vunmap,
264 .gem_prime_mmap = qxl_gem_prime_mmap,
9f4eebd7 265 .gem_free_object_unlocked = qxl_gem_object_free,
f64122c1
DA
266 .gem_open_object = qxl_gem_object_open,
267 .gem_close_object = qxl_gem_object_close,
268 .fops = &qxl_fops,
269 .ioctls = qxl_ioctls,
270 .irq_handler = qxl_irq_handler,
271 .name = DRIVER_NAME,
272 .desc = DRIVER_DESC,
273 .date = DRIVER_DATE,
274 .major = 0,
275 .minor = 1,
276 .patchlevel = 0,
277};
278
279static int __init qxl_init(void)
280{
f64122c1
DA
281 if (vgacon_text_force() && qxl_modeset == -1)
282 return -EINVAL;
f64122c1
DA
283
284 if (qxl_modeset == 0)
285 return -EINVAL;
286 qxl_driver.num_ioctls = qxl_max_ioctls;
10631d72 287 return pci_register_driver(&qxl_pci_driver);
f64122c1
DA
288}
289
290static void __exit qxl_exit(void)
291{
10631d72 292 pci_unregister_driver(&qxl_pci_driver);
f64122c1
DA
293}
294
295module_init(qxl_init);
296module_exit(qxl_exit);
297
298MODULE_AUTHOR(DRIVER_AUTHOR);
299MODULE_DESCRIPTION(DRIVER_DESC);
300MODULE_LICENSE("GPL and additional rights");