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 
vbox_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)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 
63 	ret = pci_enable_device(pdev);
64 	if (ret)
65 		goto err_pci_enable;
66 
67 	dev->pdev = pdev;
68 	pci_set_drvdata(pdev, dev);
69 
70 	ret = vbox_driver_load(dev);
71 	if (ret)
72 		goto err_vbox_driver_load;
73 
74 	ret = drm_dev_register(dev, 0);
75 	if (ret)
76 		goto err_drv_dev_register;
77 
78 	return ret;
79 
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 }
89 
vbox_pci_remove(struct pci_dev * pdev)90 static void vbox_pci_remove(struct pci_dev *pdev)
91 {
92 	struct drm_device *dev = pci_get_drvdata(pdev);
93 
94 	drm_dev_unregister(dev);
95 	vbox_driver_unload(dev);
96 	drm_dev_put(dev);
97 }
98 
vbox_drm_freeze(struct drm_device * dev)99 static int vbox_drm_freeze(struct drm_device *dev)
100 {
101 	struct vbox_private *vbox = dev->dev_private;
102 
103 	drm_kms_helper_poll_disable(dev);
104 
105 	pci_save_state(dev->pdev);
106 
107 	drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, true);
108 
109 	return 0;
110 }
111 
vbox_drm_thaw(struct drm_device * dev)112 static int vbox_drm_thaw(struct drm_device *dev)
113 {
114 	struct vbox_private *vbox = dev->dev_private;
115 
116 	drm_mode_config_reset(dev);
117 	drm_helper_resume_force_mode(dev);
118 	drm_fb_helper_set_suspend_unlocked(&vbox->fbdev->helper, false);
119 
120 	return 0;
121 }
122 
vbox_drm_resume(struct drm_device * dev)123 static int vbox_drm_resume(struct drm_device *dev)
124 {
125 	int ret;
126 
127 	if (pci_enable_device(dev->pdev))
128 		return -EIO;
129 
130 	ret = vbox_drm_thaw(dev);
131 	if (ret)
132 		return ret;
133 
134 	drm_kms_helper_poll_enable(dev);
135 
136 	return 0;
137 }
138 
vbox_pm_suspend(struct device * dev)139 static int vbox_pm_suspend(struct device *dev)
140 {
141 	struct pci_dev *pdev = to_pci_dev(dev);
142 	struct drm_device *ddev = pci_get_drvdata(pdev);
143 	int error;
144 
145 	error = vbox_drm_freeze(ddev);
146 	if (error)
147 		return error;
148 
149 	pci_disable_device(pdev);
150 	pci_set_power_state(pdev, PCI_D3hot);
151 
152 	return 0;
153 }
154 
vbox_pm_resume(struct device * dev)155 static int vbox_pm_resume(struct device *dev)
156 {
157 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
158 
159 	return vbox_drm_resume(ddev);
160 }
161 
vbox_pm_freeze(struct device * dev)162 static int vbox_pm_freeze(struct device *dev)
163 {
164 	struct pci_dev *pdev = to_pci_dev(dev);
165 	struct drm_device *ddev = pci_get_drvdata(pdev);
166 
167 	if (!ddev || !ddev->dev_private)
168 		return -ENODEV;
169 
170 	return vbox_drm_freeze(ddev);
171 }
172 
vbox_pm_thaw(struct device * dev)173 static int vbox_pm_thaw(struct device *dev)
174 {
175 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
176 
177 	return vbox_drm_thaw(ddev);
178 }
179 
vbox_pm_poweroff(struct device * dev)180 static int vbox_pm_poweroff(struct device *dev)
181 {
182 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
183 
184 	return vbox_drm_freeze(ddev);
185 }
186 
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 };
195 
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 };
203 
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 };
216 
vbox_master_set(struct drm_device * dev,struct drm_file * file_priv,bool from_open)217 static int vbox_master_set(struct drm_device *dev,
218 			   struct drm_file *file_priv, bool from_open)
219 {
220 	struct vbox_private *vbox = dev->dev_private;
221 
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;
228 
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);
243 
244 	return 0;
245 }
246 
vbox_master_drop(struct drm_device * dev,struct drm_file * file_priv)247 static void vbox_master_drop(struct drm_device *dev, struct drm_file *file_priv)
248 {
249 	struct vbox_private *vbox = dev->dev_private;
250 
251 	/* See vbox_master_set() */
252 	vbox->initial_mode_queried = false;
253 
254 	mutex_lock(&vbox->hw_mutex);
255 	vbox_disable_accel(vbox);
256 	mutex_unlock(&vbox->hw_mutex);
257 }
258 
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,
264 
265 	.lastclose = vbox_driver_lastclose,
266 	.master_set = vbox_master_set,
267 	.master_drop = vbox_master_drop,
268 
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,
277 
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 };
294 
vbox_init(void)295 static int __init vbox_init(void)
296 {
297 #ifdef CONFIG_VGA_CONSOLE
298 	if (vgacon_text_force() && vbox_modeset == -1)
299 		return -EINVAL;
300 #endif
301 
302 	if (vbox_modeset == 0)
303 		return -EINVAL;
304 
305 	return pci_register_driver(&vbox_pci_driver);
306 }
307 
vbox_exit(void)308 static void __exit vbox_exit(void)
309 {
310 	pci_unregister_driver(&vbox_pci_driver);
311 }
312 
313 module_init(vbox_init);
314 module_exit(vbox_exit);
315 
316 MODULE_AUTHOR("Oracle Corporation");
317 MODULE_DESCRIPTION(DRIVER_DESC);
318 MODULE_LICENSE("GPL and additional rights");
319