blob: 862db495d11191461209dfc6014639085a6ac0b4 [file] [log] [blame]
Hans de Goedeacc962c2018-10-18 17:03:32 +02001// SPDX-License-Identifier: MIT
Hans de Goededd55d442017-07-06 16:06:01 +02002/*
3 * Copyright (C) 2013-2017 Oracle Corporation
4 * This file is based on ast_drv.c
5 * Copyright 2012 Red Hat Inc.
Hans de Goededd55d442017-07-06 16:06:01 +02006 * Authors: Dave Airlie <airlied@redhat.com>
7 * Michael Thayer <michael.thayer@oracle.com,
8 * Hans de Goede <hdegoede@redhat.com>
9 */
Hans de Goededd55d442017-07-06 16:06:01 +020010#include <linux/console.h>
Sam Ravnborg96bae042019-01-26 13:25:23 +010011#include <linux/module.h>
12#include <linux/pci.h>
Hans de Goededd55d442017-07-06 16:06:01 +020013#include <linux/vt_kern.h>
14
Hans de Goededd55d442017-07-06 16:06:01 +020015#include <drm/drm_crtc_helper.h>
Sam Ravnborg96bae042019-01-26 13:25:23 +010016#include <drm/drm_drv.h>
17#include <drm/drm_file.h>
18#include <drm/drm_ioctl.h>
Hans de Goededd55d442017-07-06 16:06:01 +020019
20#include "vbox_drv.h"
21
Colin Ian Kingcb67fa12017-07-19 10:36:16 +010022static int vbox_modeset = -1;
Hans de Goededd55d442017-07-06 16:06:01 +020023
24MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
25module_param_named(modeset, vbox_modeset, int, 0400);
26
27static struct drm_driver driver;
28
29static const struct pci_device_id pciidlist[] = {
Hans de Goede650d4aa2018-10-22 16:57:46 +020030 { PCI_DEVICE(0x80ee, 0xbeef) },
31 { }
Hans de Goededd55d442017-07-06 16:06:01 +020032};
33MODULE_DEVICE_TABLE(pci, pciidlist);
34
Nishka Dasgupta596cb852019-08-13 11:55:48 +053035static const struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
Hans de Goedee2c38602018-09-29 14:18:25 +020036 .fb_probe = vboxfb_create,
37};
38
Hans de Goededd55d442017-07-06 16:06:01 +020039static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
40{
Hans de Goede01648892018-09-18 19:44:30 +020041 struct vbox_private *vbox;
Fabio Rafael da Rosa1daddbc2018-06-03 23:24:45 -030042 int ret = 0;
43
Hans de Goeded4670902018-09-18 19:44:29 +020044 if (!vbox_check_supported(VBE_DISPI_ID_HGSMI))
45 return -ENODEV;
46
Hans de Goede01648892018-09-18 19:44:30 +020047 vbox = kzalloc(sizeof(*vbox), GFP_KERNEL);
48 if (!vbox)
49 return -ENOMEM;
50
51 ret = drm_dev_init(&vbox->ddev, &driver, &pdev->dev);
52 if (ret) {
53 kfree(vbox);
54 return ret;
55 }
56
57 vbox->ddev.pdev = pdev;
58 vbox->ddev.dev_private = vbox;
59 pci_set_drvdata(pdev, vbox);
60 mutex_init(&vbox->hw_mutex);
Hans de Goede1ebafd152018-09-10 20:30:38 +020061
62 ret = pci_enable_device(pdev);
63 if (ret)
Hans de Goeded4670902018-09-18 19:44:29 +020064 goto err_dev_put;
Hans de Goede1ebafd152018-09-10 20:30:38 +020065
Hans de Goeded4670902018-09-18 19:44:29 +020066 ret = vbox_hw_init(vbox);
Fabio Rafael da Rosa1daddbc2018-06-03 23:24:45 -030067 if (ret)
Hans de Goeded4670902018-09-18 19:44:29 +020068 goto err_pci_disable;
69
70 ret = vbox_mm_init(vbox);
71 if (ret)
72 goto err_hw_fini;
73
Hans de Goede01648892018-09-18 19:44:30 +020074 ret = vbox_mode_init(vbox);
Hans de Goeded4670902018-09-18 19:44:29 +020075 if (ret)
76 goto err_mm_fini;
77
78 ret = vbox_irq_init(vbox);
79 if (ret)
80 goto err_mode_fini;
81
Hans de Goedee2c38602018-09-29 14:18:25 +020082 ret = drm_fb_helper_fbdev_setup(&vbox->ddev, &vbox->fb_helper,
83 &vbox_fb_helper_funcs, 32,
84 vbox->num_crtcs);
Hans de Goeded4670902018-09-18 19:44:29 +020085 if (ret)
86 goto err_irq_fini;
Fabio Rafael da Rosa1daddbc2018-06-03 23:24:45 -030087
Hans de Goede01648892018-09-18 19:44:30 +020088 ret = drm_dev_register(&vbox->ddev, 0);
Fabio Rafael da Rosa1daddbc2018-06-03 23:24:45 -030089 if (ret)
Hans de Goeded4670902018-09-18 19:44:29 +020090 goto err_fbdev_fini;
Fabio Rafael da Rosa1daddbc2018-06-03 23:24:45 -030091
Hans de Goeded4670902018-09-18 19:44:29 +020092 return 0;
Fabio Rafael da Rosa1daddbc2018-06-03 23:24:45 -030093
Hans de Goeded4670902018-09-18 19:44:29 +020094err_fbdev_fini:
Hans de Goede01648892018-09-18 19:44:30 +020095 vbox_fbdev_fini(vbox);
Hans de Goeded4670902018-09-18 19:44:29 +020096err_irq_fini:
97 vbox_irq_fini(vbox);
98err_mode_fini:
Hans de Goede01648892018-09-18 19:44:30 +020099 vbox_mode_fini(vbox);
Hans de Goeded4670902018-09-18 19:44:29 +0200100err_mm_fini:
101 vbox_mm_fini(vbox);
102err_hw_fini:
103 vbox_hw_fini(vbox);
104err_pci_disable:
Hans de Goede1ebafd152018-09-10 20:30:38 +0200105 pci_disable_device(pdev);
Hans de Goeded4670902018-09-18 19:44:29 +0200106err_dev_put:
Hans de Goede01648892018-09-18 19:44:30 +0200107 drm_dev_put(&vbox->ddev);
Fabio Rafael da Rosa1daddbc2018-06-03 23:24:45 -0300108 return ret;
Hans de Goededd55d442017-07-06 16:06:01 +0200109}
110
111static void vbox_pci_remove(struct pci_dev *pdev)
112{
Hans de Goede01648892018-09-18 19:44:30 +0200113 struct vbox_private *vbox = pci_get_drvdata(pdev);
Hans de Goededd55d442017-07-06 16:06:01 +0200114
Hans de Goede01648892018-09-18 19:44:30 +0200115 drm_dev_unregister(&vbox->ddev);
116 vbox_fbdev_fini(vbox);
Hans de Goeded4670902018-09-18 19:44:29 +0200117 vbox_irq_fini(vbox);
Hans de Goede01648892018-09-18 19:44:30 +0200118 vbox_mode_fini(vbox);
Hans de Goeded4670902018-09-18 19:44:29 +0200119 vbox_mm_fini(vbox);
120 vbox_hw_fini(vbox);
Hans de Goede01648892018-09-18 19:44:30 +0200121 drm_dev_put(&vbox->ddev);
Hans de Goededd55d442017-07-06 16:06:01 +0200122}
123
Hans de Goede35a703d2018-10-22 16:57:47 +0200124#ifdef CONFIG_PM_SLEEP
Hans de Goededd55d442017-07-06 16:06:01 +0200125static int vbox_pm_suspend(struct device *dev)
126{
Hans de Goede01648892018-09-18 19:44:30 +0200127 struct vbox_private *vbox = dev_get_drvdata(dev);
Hans de Goededd55d442017-07-06 16:06:01 +0200128 int error;
129
Hans de Goede438340a2018-09-29 14:18:19 +0200130 error = drm_mode_config_helper_suspend(&vbox->ddev);
Hans de Goededd55d442017-07-06 16:06:01 +0200131 if (error)
132 return error;
133
Hans de Goede438340a2018-09-29 14:18:19 +0200134 pci_save_state(vbox->ddev.pdev);
Hans de Goede01648892018-09-18 19:44:30 +0200135 pci_disable_device(vbox->ddev.pdev);
136 pci_set_power_state(vbox->ddev.pdev, PCI_D3hot);
Hans de Goededd55d442017-07-06 16:06:01 +0200137
138 return 0;
139}
140
141static int vbox_pm_resume(struct device *dev)
142{
Hans de Goede01648892018-09-18 19:44:30 +0200143 struct vbox_private *vbox = dev_get_drvdata(dev);
Hans de Goededd55d442017-07-06 16:06:01 +0200144
Hans de Goede3498ea82018-09-18 19:44:31 +0200145 if (pci_enable_device(vbox->ddev.pdev))
146 return -EIO;
147
Hans de Goede438340a2018-09-29 14:18:19 +0200148 return drm_mode_config_helper_resume(&vbox->ddev);
Hans de Goededd55d442017-07-06 16:06:01 +0200149}
150
151static int vbox_pm_freeze(struct device *dev)
152{
Hans de Goede01648892018-09-18 19:44:30 +0200153 struct vbox_private *vbox = dev_get_drvdata(dev);
Hans de Goededd55d442017-07-06 16:06:01 +0200154
Hans de Goede438340a2018-09-29 14:18:19 +0200155 return drm_mode_config_helper_suspend(&vbox->ddev);
Hans de Goededd55d442017-07-06 16:06:01 +0200156}
157
158static int vbox_pm_thaw(struct device *dev)
159{
Hans de Goede01648892018-09-18 19:44:30 +0200160 struct vbox_private *vbox = dev_get_drvdata(dev);
Hans de Goededd55d442017-07-06 16:06:01 +0200161
Hans de Goede438340a2018-09-29 14:18:19 +0200162 return drm_mode_config_helper_resume(&vbox->ddev);
Hans de Goededd55d442017-07-06 16:06:01 +0200163}
164
165static int vbox_pm_poweroff(struct device *dev)
166{
Hans de Goede01648892018-09-18 19:44:30 +0200167 struct vbox_private *vbox = dev_get_drvdata(dev);
Hans de Goededd55d442017-07-06 16:06:01 +0200168
Hans de Goede438340a2018-09-29 14:18:19 +0200169 return drm_mode_config_helper_suspend(&vbox->ddev);
Hans de Goededd55d442017-07-06 16:06:01 +0200170}
171
172static const struct dev_pm_ops vbox_pm_ops = {
173 .suspend = vbox_pm_suspend,
174 .resume = vbox_pm_resume,
175 .freeze = vbox_pm_freeze,
176 .thaw = vbox_pm_thaw,
177 .poweroff = vbox_pm_poweroff,
178 .restore = vbox_pm_resume,
179};
Hans de Goede35a703d2018-10-22 16:57:47 +0200180#endif
Hans de Goededd55d442017-07-06 16:06:01 +0200181
182static struct pci_driver vbox_pci_driver = {
183 .name = DRIVER_NAME,
184 .id_table = pciidlist,
185 .probe = vbox_pci_probe,
186 .remove = vbox_pci_remove,
Hans de Goede35a703d2018-10-22 16:57:47 +0200187#ifdef CONFIG_PM_SLEEP
Hans de Goededd55d442017-07-06 16:06:01 +0200188 .driver.pm = &vbox_pm_ops,
Hans de Goede35a703d2018-10-22 16:57:47 +0200189#endif
Hans de Goededd55d442017-07-06 16:06:01 +0200190};
191
192static const struct file_operations vbox_fops = {
193 .owner = THIS_MODULE,
Thomas Zimmermann94065bf2019-05-08 10:26:28 +0200194 DRM_VRAM_MM_FILE_OPERATIONS
Hans de Goededd55d442017-07-06 16:06:01 +0200195};
196
Hans de Goededd55d442017-07-06 16:06:01 +0200197static struct drm_driver driver = {
198 .driver_features =
Daniel Vetter0424fda2019-06-17 17:39:24 +0200199 DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
Hans de Goededd55d442017-07-06 16:06:01 +0200200
Hans de Goedee2c38602018-09-29 14:18:25 +0200201 .lastclose = drm_fb_helper_lastclose,
Hans de Goededd55d442017-07-06 16:06:01 +0200202
203 .fops = &vbox_fops,
204 .irq_handler = vbox_irq_handler,
205 .name = DRIVER_NAME,
206 .desc = DRIVER_DESC,
207 .date = DRIVER_DATE,
208 .major = DRIVER_MAJOR,
209 .minor = DRIVER_MINOR,
210 .patchlevel = DRIVER_PATCHLEVEL,
211
Thomas Zimmermann94065bf2019-05-08 10:26:28 +0200212 DRM_GEM_VRAM_DRIVER,
Hans de Goededd55d442017-07-06 16:06:01 +0200213};
214
215static int __init vbox_init(void)
216{
217#ifdef CONFIG_VGA_CONSOLE
218 if (vgacon_text_force() && vbox_modeset == -1)
219 return -EINVAL;
220#endif
221
222 if (vbox_modeset == 0)
223 return -EINVAL;
224
Stephen Rothwelle6742e12017-07-19 11:46:57 +1000225 return pci_register_driver(&vbox_pci_driver);
Hans de Goededd55d442017-07-06 16:06:01 +0200226}
227
228static void __exit vbox_exit(void)
229{
Stephen Rothwelle6742e12017-07-19 11:46:57 +1000230 pci_unregister_driver(&vbox_pci_driver);
Hans de Goededd55d442017-07-06 16:06:01 +0200231}
232
233module_init(vbox_init);
234module_exit(vbox_exit);
235
236MODULE_AUTHOR("Oracle Corporation");
Hans de Goedee46b06e12018-10-22 16:57:48 +0200237MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
Hans de Goededd55d442017-07-06 16:06:01 +0200238MODULE_DESCRIPTION(DRIVER_DESC);
239MODULE_LICENSE("GPL and additional rights");