]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/rpmsg.git/blob - drivers/staging/vboxvideo/vbox_drv.c
Merge tag 'alloc-args-v4.19-rc8' of https://git.kernel.org/pub/scm/linux/kernel/git...
[rpmsg/rpmsg.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>
34 #include <drm/drmP.h>
35 #include <drm/drm_crtc_helper.h>
37 #include "vbox_drv.h"
39 static int vbox_modeset = -1;
41 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
42 module_param_named(modeset, vbox_modeset, int, 0400);
44 static struct drm_driver driver;
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);
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;
57         dev = drm_dev_alloc(&driver, &pdev->dev);
58         if (IS_ERR(dev)) {
59                 ret = PTR_ERR(dev);
60                 goto err_drv_alloc;
61         }
63         ret = pci_enable_device(pdev);
64         if (ret)
65                 goto err_pci_enable;
67         dev->pdev = pdev;
68         pci_set_drvdata(pdev, dev);
70         ret = vbox_driver_load(dev);
71         if (ret)
72                 goto err_vbox_driver_load;
74         ret = drm_dev_register(dev, 0);
75         if (ret)
76                 goto err_drv_dev_register;
78         return ret;
80  err_drv_dev_register:
81         vbox_driver_unload(dev);
82  err_vbox_driver_load:
83         pci_disable_device(pdev);
84  err_pci_enable:
85         drm_dev_put(dev);
86  err_drv_alloc:
87         return ret;
88 }
90 static void vbox_pci_remove(struct pci_dev *pdev)
91 {
92         struct drm_device *dev = pci_get_drvdata(pdev);
94         drm_dev_unregister(dev);
95         vbox_driver_unload(dev);
96         drm_dev_put(dev);
97 }
99 static int vbox_drm_freeze(struct drm_device *dev)
101         struct vbox_private *vbox = dev->dev_private;
103         drm_kms_helper_poll_disable(dev);
105         pci_save_state(dev->pdev);
107         drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, true);
109         return 0;
112 static int vbox_drm_thaw(struct drm_device *dev)
114         struct vbox_private *vbox = dev->dev_private;
116         drm_mode_config_reset(dev);
117         drm_helper_resume_force_mode(dev);
118         drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, false);
120         return 0;
123 static int vbox_drm_resume(struct drm_device *dev)
125         int ret;
127         if (pci_enable_device(dev->pdev))
128                 return -EIO;
130         ret = vbox_drm_thaw(dev);
131         if (ret)
132                 return ret;
134         drm_kms_helper_poll_enable(dev);
136         return 0;
139 static int vbox_pm_suspend(struct device *dev)
141         struct pci_dev *pdev = to_pci_dev(dev);
142         struct drm_device *ddev = pci_get_drvdata(pdev);
143         int error;
145         error = vbox_drm_freeze(ddev);
146         if (error)
147                 return error;
149         pci_disable_device(pdev);
150         pci_set_power_state(pdev, PCI_D3hot);
152         return 0;
155 static int vbox_pm_resume(struct device *dev)
157         struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
159         return vbox_drm_resume(ddev);
162 static int vbox_pm_freeze(struct device *dev)
164         struct pci_dev *pdev = to_pci_dev(dev);
165         struct drm_device *ddev = pci_get_drvdata(pdev);
167         if (!ddev || !ddev->dev_private)
168                 return -ENODEV;
170         return vbox_drm_freeze(ddev);
173 static int vbox_pm_thaw(struct device *dev)
175         struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
177         return vbox_drm_thaw(ddev);
180 static int vbox_pm_poweroff(struct device *dev)
182         struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
184         return vbox_drm_freeze(ddev);
187 static const struct dev_pm_ops vbox_pm_ops = {
188         .suspend = vbox_pm_suspend,
189         .resume = vbox_pm_resume,
190         .freeze = vbox_pm_freeze,
191         .thaw = vbox_pm_thaw,
192         .poweroff = vbox_pm_poweroff,
193         .restore = vbox_pm_resume,
194 };
196 static struct pci_driver vbox_pci_driver = {
197         .name = DRIVER_NAME,
198         .id_table = pciidlist,
199         .probe = vbox_pci_probe,
200         .remove = vbox_pci_remove,
201         .driver.pm = &vbox_pm_ops,
202 };
204 static const struct file_operations vbox_fops = {
205         .owner = THIS_MODULE,
206         .open = drm_open,
207         .release = drm_release,
208         .unlocked_ioctl = drm_ioctl,
209         .mmap = vbox_mmap,
210         .poll = drm_poll,
211 #ifdef CONFIG_COMPAT
212         .compat_ioctl = drm_compat_ioctl,
213 #endif
214         .read = drm_read,
215 };
217 static int vbox_master_set(struct drm_device *dev,
218                            struct drm_file *file_priv, bool from_open)
220         struct vbox_private *vbox = dev->dev_private;
222         /*
223          * We do not yet know whether the new owner can handle hotplug, so we
224          * do not advertise dynamic modes on the first query and send a
225          * tentative hotplug notification after that to see if they query again.
226          */
227         vbox->initial_mode_queried = false;
229         mutex_lock(&vbox->hw_mutex);
230         /*
231          * Disable VBVA when someone releases master in case the next person
232          * tries tries to do VESA.
233          */
234         /** @todo work out if anyone is likely to and whether it will work. */
235         /*
236          * Update: we also disable it because if the new master does not do
237          * dirty rectangle reporting (e.g. old versions of Plymouth) then at
238          * least the first screen will still be updated. We enable it as soon
239          * as we receive a dirty rectangle report.
240          */
241         vbox_disable_accel(vbox);
242         mutex_unlock(&vbox->hw_mutex);
244         return 0;
247 static void vbox_master_drop(struct drm_device *dev, struct drm_file *file_priv)
249         struct vbox_private *vbox = dev->dev_private;
251         /* See vbox_master_set() */
252         vbox->initial_mode_queried = false;
254         mutex_lock(&vbox->hw_mutex);
255         vbox_disable_accel(vbox);
256         mutex_unlock(&vbox->hw_mutex);
259 static struct drm_driver driver = {
260         .driver_features =
261             DRIVER_MODESET | DRIVER_GEM | DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED |
262             DRIVER_PRIME,
263         .dev_priv_size = 0,
265         .lastclose = vbox_driver_lastclose,
266         .master_set = vbox_master_set,
267         .master_drop = vbox_master_drop,
269         .fops = &vbox_fops,
270         .irq_handler = vbox_irq_handler,
271         .name = DRIVER_NAME,
272         .desc = DRIVER_DESC,
273         .date = DRIVER_DATE,
274         .major = DRIVER_MAJOR,
275         .minor = DRIVER_MINOR,
276         .patchlevel = DRIVER_PATCHLEVEL,
278         .gem_free_object_unlocked = vbox_gem_free_object,
279         .dumb_create = vbox_dumb_create,
280         .dumb_map_offset = vbox_dumb_mmap_offset,
281         .dumb_destroy = drm_gem_dumb_destroy,
282         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
283         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
284         .gem_prime_export = drm_gem_prime_export,
285         .gem_prime_import = drm_gem_prime_import,
286         .gem_prime_pin = vbox_gem_prime_pin,
287         .gem_prime_unpin = vbox_gem_prime_unpin,
288         .gem_prime_get_sg_table = vbox_gem_prime_get_sg_table,
289         .gem_prime_import_sg_table = vbox_gem_prime_import_sg_table,
290         .gem_prime_vmap = vbox_gem_prime_vmap,
291         .gem_prime_vunmap = vbox_gem_prime_vunmap,
292         .gem_prime_mmap = vbox_gem_prime_mmap,
293 };
295 static int __init vbox_init(void)
297 #ifdef CONFIG_VGA_CONSOLE
298         if (vgacon_text_force() && vbox_modeset == -1)
299                 return -EINVAL;
300 #endif
302         if (vbox_modeset == 0)
303                 return -EINVAL;
305         return pci_register_driver(&vbox_pci_driver);
308 static void __exit vbox_exit(void)
310         pci_unregister_driver(&vbox_pci_driver);
313 module_init(vbox_init);
314 module_exit(vbox_exit);
316 MODULE_AUTHOR("Oracle Corporation");
317 MODULE_DESCRIPTION(DRIVER_DESC);
318 MODULE_LICENSE("GPL and additional rights");