xref: /linux/drivers/gpu/drm/vboxvideo/vbox_drv.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright (C) 2013-2017 Oracle Corporation
4  * This file is based on ast_drv.c
5  * Copyright 2012 Red Hat Inc.
6  * Authors: Dave Airlie <airlied@redhat.com>
7  *          Michael Thayer <michael.thayer@oracle.com,
8  *          Hans de Goede <hdegoede@redhat.com>
9  */
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/vt_kern.h>
13 
14 #include <drm/drm_aperture.h>
15 #include <drm/drm_drv.h>
16 #include <drm/drm_fbdev_generic.h>
17 #include <drm/drm_file.h>
18 #include <drm/drm_ioctl.h>
19 #include <drm/drm_managed.h>
20 #include <drm/drm_modeset_helper.h>
21 #include <drm/drm_module.h>
22 
23 #include "vbox_drv.h"
24 
25 static int vbox_modeset = -1;
26 
27 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
28 module_param_named(modeset, vbox_modeset, int, 0400);
29 
30 static const struct drm_driver driver;
31 
32 static const struct pci_device_id pciidlist[] = {
33 	{ PCI_DEVICE(0x80ee, 0xbeef) },
34 	{ }
35 };
36 MODULE_DEVICE_TABLE(pci, pciidlist);
37 
38 static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
39 {
40 	struct vbox_private *vbox;
41 	int ret = 0;
42 
43 	if (!vbox_check_supported(VBE_DISPI_ID_HGSMI))
44 		return -ENODEV;
45 
46 	ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
47 	if (ret)
48 		return ret;
49 
50 	vbox = devm_drm_dev_alloc(&pdev->dev, &driver,
51 				  struct vbox_private, ddev);
52 	if (IS_ERR(vbox))
53 		return PTR_ERR(vbox);
54 
55 	pci_set_drvdata(pdev, vbox);
56 	mutex_init(&vbox->hw_mutex);
57 
58 	ret = pcim_enable_device(pdev);
59 	if (ret)
60 		return ret;
61 
62 	ret = vbox_hw_init(vbox);
63 	if (ret)
64 		return ret;
65 
66 	ret = vbox_mm_init(vbox);
67 	if (ret)
68 		goto err_hw_fini;
69 
70 	ret = vbox_mode_init(vbox);
71 	if (ret)
72 		goto err_hw_fini;
73 
74 	ret = vbox_irq_init(vbox);
75 	if (ret)
76 		goto err_mode_fini;
77 
78 	ret = drm_dev_register(&vbox->ddev, 0);
79 	if (ret)
80 		goto err_irq_fini;
81 
82 	drm_fbdev_generic_setup(&vbox->ddev, 32);
83 
84 	return 0;
85 
86 err_irq_fini:
87 	vbox_irq_fini(vbox);
88 err_mode_fini:
89 	vbox_mode_fini(vbox);
90 err_hw_fini:
91 	vbox_hw_fini(vbox);
92 	return ret;
93 }
94 
95 static void vbox_pci_remove(struct pci_dev *pdev)
96 {
97 	struct vbox_private *vbox = pci_get_drvdata(pdev);
98 
99 	drm_dev_unregister(&vbox->ddev);
100 	vbox_irq_fini(vbox);
101 	vbox_mode_fini(vbox);
102 	vbox_hw_fini(vbox);
103 }
104 
105 static int vbox_pm_suspend(struct device *dev)
106 {
107 	struct vbox_private *vbox = dev_get_drvdata(dev);
108 	struct pci_dev *pdev = to_pci_dev(dev);
109 	int error;
110 
111 	error = drm_mode_config_helper_suspend(&vbox->ddev);
112 	if (error)
113 		return error;
114 
115 	pci_save_state(pdev);
116 	pci_disable_device(pdev);
117 	pci_set_power_state(pdev, PCI_D3hot);
118 
119 	return 0;
120 }
121 
122 static int vbox_pm_resume(struct device *dev)
123 {
124 	struct vbox_private *vbox = dev_get_drvdata(dev);
125 	struct pci_dev *pdev = to_pci_dev(dev);
126 
127 	if (pci_enable_device(pdev))
128 		return -EIO;
129 
130 	return drm_mode_config_helper_resume(&vbox->ddev);
131 }
132 
133 static int vbox_pm_freeze(struct device *dev)
134 {
135 	struct vbox_private *vbox = dev_get_drvdata(dev);
136 
137 	return drm_mode_config_helper_suspend(&vbox->ddev);
138 }
139 
140 static int vbox_pm_thaw(struct device *dev)
141 {
142 	struct vbox_private *vbox = dev_get_drvdata(dev);
143 
144 	return drm_mode_config_helper_resume(&vbox->ddev);
145 }
146 
147 static int vbox_pm_poweroff(struct device *dev)
148 {
149 	struct vbox_private *vbox = dev_get_drvdata(dev);
150 
151 	return drm_mode_config_helper_suspend(&vbox->ddev);
152 }
153 
154 static const struct dev_pm_ops vbox_pm_ops = {
155 	.suspend = vbox_pm_suspend,
156 	.resume = vbox_pm_resume,
157 	.freeze = vbox_pm_freeze,
158 	.thaw = vbox_pm_thaw,
159 	.poweroff = vbox_pm_poweroff,
160 	.restore = vbox_pm_resume,
161 };
162 
163 static struct pci_driver vbox_pci_driver = {
164 	.name = DRIVER_NAME,
165 	.id_table = pciidlist,
166 	.probe = vbox_pci_probe,
167 	.remove = vbox_pci_remove,
168 	.driver.pm = pm_sleep_ptr(&vbox_pm_ops),
169 };
170 
171 DEFINE_DRM_GEM_FOPS(vbox_fops);
172 
173 static const struct drm_driver driver = {
174 	.driver_features =
175 	    DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
176 
177 	.fops = &vbox_fops,
178 	.name = DRIVER_NAME,
179 	.desc = DRIVER_DESC,
180 	.date = DRIVER_DATE,
181 	.major = DRIVER_MAJOR,
182 	.minor = DRIVER_MINOR,
183 	.patchlevel = DRIVER_PATCHLEVEL,
184 
185 	DRM_GEM_VRAM_DRIVER,
186 };
187 
188 drm_module_pci_driver_if_modeset(vbox_pci_driver, vbox_modeset);
189 
190 MODULE_AUTHOR("Oracle Corporation");
191 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
192 MODULE_DESCRIPTION(DRIVER_DESC);
193 MODULE_LICENSE("GPL and additional rights");
194