]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/vboxvideo/vbox_drv.c
da92c493f1577ab7d35da20c73dd70f052fffc77
[linux.git] / drivers / staging / vboxvideo / vbox_drv.c
1 /*
2  * Copyright (C) 2013-2017 Oracle Corporation
3  * This file is based on ast_drv.c
4  * Copyright 2012 Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * Authors: Dave Airlie <airlied@redhat.com>
27  *          Michael Thayer <michael.thayer@oracle.com,
28  *          Hans de Goede <hdegoede@redhat.com>
29  */
30 #include <linux/module.h>
31 #include <linux/console.h>
32 #include <linux/vt_kern.h>
33
34 #include <drm/drmP.h>
35 #include <drm/drm_crtc_helper.h>
36
37 #include "vbox_drv.h"
38
39 static int vbox_modeset = -1;
40
41 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
42 module_param_named(modeset, vbox_modeset, int, 0400);
43
44 static struct drm_driver driver;
45
46 static const struct pci_device_id pciidlist[] = {
47         { 0x80ee, 0xbeef, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
48         { 0, 0, 0},
49 };
50 MODULE_DEVICE_TABLE(pci, pciidlist);
51
52 static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
53 {
54         struct drm_device *dev = NULL;
55         int ret = 0;
56
57         dev = drm_dev_alloc(&driver, &pdev->dev);
58         if (IS_ERR(dev)) {
59                 ret = PTR_ERR(dev);
60                 goto err_drv_alloc;
61         }
62         dev->pdev = pdev;
63         pci_set_drvdata(pdev, dev);
64
65         ret = vbox_driver_load(dev);
66         if (ret)
67                 goto err_vbox_driver_load;
68
69         ret = drm_dev_register(dev, 0);
70         if (ret)
71                 goto err_drv_dev_register;
72
73         return ret;
74
75  err_drv_dev_register:
76         vbox_driver_unload(dev);
77  err_vbox_driver_load:
78         drm_dev_put(dev);
79  err_drv_alloc:
80         return ret;
81 }
82
83 static void vbox_pci_remove(struct pci_dev *pdev)
84 {
85         struct drm_device *dev = pci_get_drvdata(pdev);
86
87         drm_dev_unregister(dev);
88         vbox_driver_unload(dev);
89         drm_dev_put(dev);
90 }
91
92 static int vbox_drm_freeze(struct drm_device *dev)
93 {
94         struct vbox_private *vbox = dev->dev_private;
95
96         drm_kms_helper_poll_disable(dev);
97
98         pci_save_state(dev->pdev);
99
100         drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, true);
101
102         return 0;
103 }
104
105 static int vbox_drm_thaw(struct drm_device *dev)
106 {
107         struct vbox_private *vbox = dev->dev_private;
108
109         drm_mode_config_reset(dev);
110         drm_helper_resume_force_mode(dev);
111         drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, false);
112
113         return 0;
114 }
115
116 static int vbox_drm_resume(struct drm_device *dev)
117 {
118         int ret;
119
120         if (pci_enable_device(dev->pdev))
121                 return -EIO;
122
123         ret = vbox_drm_thaw(dev);
124         if (ret)
125                 return ret;
126
127         drm_kms_helper_poll_enable(dev);
128
129         return 0;
130 }
131
132 static int vbox_pm_suspend(struct device *dev)
133 {
134         struct pci_dev *pdev = to_pci_dev(dev);
135         struct drm_device *ddev = pci_get_drvdata(pdev);
136         int error;
137
138         error = vbox_drm_freeze(ddev);
139         if (error)
140                 return error;
141
142         pci_disable_device(pdev);
143         pci_set_power_state(pdev, PCI_D3hot);
144
145         return 0;
146 }
147
148 static int vbox_pm_resume(struct device *dev)
149 {
150         struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
151
152         return vbox_drm_resume(ddev);
153 }
154
155 static int vbox_pm_freeze(struct device *dev)
156 {
157         struct pci_dev *pdev = to_pci_dev(dev);
158         struct drm_device *ddev = pci_get_drvdata(pdev);
159
160         if (!ddev || !ddev->dev_private)
161                 return -ENODEV;
162
163         return vbox_drm_freeze(ddev);
164 }
165
166 static int vbox_pm_thaw(struct device *dev)
167 {
168         struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
169
170         return vbox_drm_thaw(ddev);
171 }
172
173 static int vbox_pm_poweroff(struct device *dev)
174 {
175         struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
176
177         return vbox_drm_freeze(ddev);
178 }
179
180 static const struct dev_pm_ops vbox_pm_ops = {
181         .suspend = vbox_pm_suspend,
182         .resume = vbox_pm_resume,
183         .freeze = vbox_pm_freeze,
184         .thaw = vbox_pm_thaw,
185         .poweroff = vbox_pm_poweroff,
186         .restore = vbox_pm_resume,
187 };
188
189 static struct pci_driver vbox_pci_driver = {
190         .name = DRIVER_NAME,
191         .id_table = pciidlist,
192         .probe = vbox_pci_probe,
193         .remove = vbox_pci_remove,
194         .driver.pm = &vbox_pm_ops,
195 };
196
197 static const struct file_operations vbox_fops = {
198         .owner = THIS_MODULE,
199         .open = drm_open,
200         .release = drm_release,
201         .unlocked_ioctl = drm_ioctl,
202         .mmap = vbox_mmap,
203         .poll = drm_poll,
204 #ifdef CONFIG_COMPAT
205         .compat_ioctl = drm_compat_ioctl,
206 #endif
207         .read = drm_read,
208 };
209
210 static int vbox_master_set(struct drm_device *dev,
211                            struct drm_file *file_priv, bool from_open)
212 {
213         struct vbox_private *vbox = dev->dev_private;
214
215         /*
216          * We do not yet know whether the new owner can handle hotplug, so we
217          * do not advertise dynamic modes on the first query and send a
218          * tentative hotplug notification after that to see if they query again.
219          */
220         vbox->initial_mode_queried = false;
221
222         mutex_lock(&vbox->hw_mutex);
223         /*
224          * Disable VBVA when someone releases master in case the next person
225          * tries tries to do VESA.
226          */
227         /** @todo work out if anyone is likely to and whether it will work. */
228         /*
229          * Update: we also disable it because if the new master does not do
230          * dirty rectangle reporting (e.g. old versions of Plymouth) then at
231          * least the first screen will still be updated. We enable it as soon
232          * as we receive a dirty rectangle report.
233          */
234         vbox_disable_accel(vbox);
235         mutex_unlock(&vbox->hw_mutex);
236
237         return 0;
238 }
239
240 static void vbox_master_drop(struct drm_device *dev, struct drm_file *file_priv)
241 {
242         struct vbox_private *vbox = dev->dev_private;
243
244         /* See vbox_master_set() */
245         vbox->initial_mode_queried = false;
246
247         mutex_lock(&vbox->hw_mutex);
248         vbox_disable_accel(vbox);
249         mutex_unlock(&vbox->hw_mutex);
250 }
251
252 static struct drm_driver driver = {
253         .driver_features =
254             DRIVER_MODESET | DRIVER_GEM | DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED |
255             DRIVER_PRIME,
256         .dev_priv_size = 0,
257
258         .lastclose = vbox_driver_lastclose,
259         .master_set = vbox_master_set,
260         .master_drop = vbox_master_drop,
261
262         .fops = &vbox_fops,
263         .irq_handler = vbox_irq_handler,
264         .name = DRIVER_NAME,
265         .desc = DRIVER_DESC,
266         .date = DRIVER_DATE,
267         .major = DRIVER_MAJOR,
268         .minor = DRIVER_MINOR,
269         .patchlevel = DRIVER_PATCHLEVEL,
270
271         .gem_free_object_unlocked = vbox_gem_free_object,
272         .dumb_create = vbox_dumb_create,
273         .dumb_map_offset = vbox_dumb_mmap_offset,
274         .dumb_destroy = drm_gem_dumb_destroy,
275         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
276         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
277         .gem_prime_export = drm_gem_prime_export,
278         .gem_prime_import = drm_gem_prime_import,
279         .gem_prime_pin = vbox_gem_prime_pin,
280         .gem_prime_unpin = vbox_gem_prime_unpin,
281         .gem_prime_get_sg_table = vbox_gem_prime_get_sg_table,
282         .gem_prime_import_sg_table = vbox_gem_prime_import_sg_table,
283         .gem_prime_vmap = vbox_gem_prime_vmap,
284         .gem_prime_vunmap = vbox_gem_prime_vunmap,
285         .gem_prime_mmap = vbox_gem_prime_mmap,
286 };
287
288 static int __init vbox_init(void)
289 {
290 #ifdef CONFIG_VGA_CONSOLE
291         if (vgacon_text_force() && vbox_modeset == -1)
292                 return -EINVAL;
293 #endif
294
295         if (vbox_modeset == 0)
296                 return -EINVAL;
297
298         return pci_register_driver(&vbox_pci_driver);
299 }
300
301 static void __exit vbox_exit(void)
302 {
303         pci_unregister_driver(&vbox_pci_driver);
304 }
305
306 module_init(vbox_init);
307 module_exit(vbox_exit);
308
309 MODULE_AUTHOR("Oracle Corporation");
310 MODULE_DESCRIPTION(DRIVER_DESC);
311 MODULE_LICENSE("GPL and additional rights");