xref: /openbmc/linux/drivers/gpu/drm/ast/ast_drv.c (revision 34fa67e7)
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18  * USE OR OTHER DEALINGS IN THE SOFTWARE.
19  *
20  * The above copyright notice and this permission notice (including the
21  * next paragraph) shall be included in all copies or substantial portions
22  * of the Software.
23  *
24  */
25 /*
26  * Authors: Dave Airlie <airlied@redhat.com>
27  */
28 
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 
32 #include <drm/drm_aperture.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_crtc_helper.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_gem_vram_helper.h>
37 #include <drm/drm_probe_helper.h>
38 
39 #include "ast_drv.h"
40 
41 int ast_modeset = -1;
42 
43 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
44 module_param_named(modeset, ast_modeset, int, 0400);
45 
46 /*
47  * DRM driver
48  */
49 
50 DEFINE_DRM_GEM_FOPS(ast_fops);
51 
52 static const struct drm_driver ast_driver = {
53 	.driver_features = DRIVER_ATOMIC |
54 			   DRIVER_GEM |
55 			   DRIVER_MODESET,
56 
57 	.fops = &ast_fops,
58 	.name = DRIVER_NAME,
59 	.desc = DRIVER_DESC,
60 	.date = DRIVER_DATE,
61 	.major = DRIVER_MAJOR,
62 	.minor = DRIVER_MINOR,
63 	.patchlevel = DRIVER_PATCHLEVEL,
64 
65 	DRM_GEM_VRAM_DRIVER
66 };
67 
68 /*
69  * PCI driver
70  */
71 
72 #define PCI_VENDOR_ASPEED 0x1a03
73 
74 #define AST_VGA_DEVICE(id, info) {		\
75 	.class = PCI_BASE_CLASS_DISPLAY << 16,	\
76 	.class_mask = 0xff0000,			\
77 	.vendor = PCI_VENDOR_ASPEED,			\
78 	.device = id,				\
79 	.subvendor = PCI_ANY_ID,		\
80 	.subdevice = PCI_ANY_ID,		\
81 	.driver_data = (unsigned long) info }
82 
83 static const struct pci_device_id ast_pciidlist[] = {
84 	AST_VGA_DEVICE(PCI_CHIP_AST2000, NULL),
85 	AST_VGA_DEVICE(PCI_CHIP_AST2100, NULL),
86 	{0, 0, 0},
87 };
88 
89 MODULE_DEVICE_TABLE(pci, ast_pciidlist);
90 
91 static int ast_remove_conflicting_framebuffers(struct pci_dev *pdev)
92 {
93 	bool primary = false;
94 	resource_size_t base, size;
95 
96 	base = pci_resource_start(pdev, 0);
97 	size = pci_resource_len(pdev, 0);
98 #ifdef CONFIG_X86
99 	primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
100 #endif
101 
102 	return drm_aperture_remove_conflicting_framebuffers(base, size, primary, &ast_driver);
103 }
104 
105 static int ast_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
106 {
107 	struct ast_private *ast;
108 	struct drm_device *dev;
109 	int ret;
110 
111 	ret = ast_remove_conflicting_framebuffers(pdev);
112 	if (ret)
113 		return ret;
114 
115 	ret = pcim_enable_device(pdev);
116 	if (ret)
117 		return ret;
118 
119 	ast = ast_device_create(&ast_driver, pdev, ent->driver_data);
120 	if (IS_ERR(ast))
121 		return PTR_ERR(ast);
122 	dev = &ast->base;
123 
124 	ret = drm_dev_register(dev, ent->driver_data);
125 	if (ret)
126 		return ret;
127 
128 	drm_fbdev_generic_setup(dev, 32);
129 
130 	return 0;
131 }
132 
133 static void ast_pci_remove(struct pci_dev *pdev)
134 {
135 	struct drm_device *dev = pci_get_drvdata(pdev);
136 
137 	drm_dev_unregister(dev);
138 	drm_atomic_helper_shutdown(dev);
139 }
140 
141 static int ast_drm_freeze(struct drm_device *dev)
142 {
143 	int error;
144 
145 	error = drm_mode_config_helper_suspend(dev);
146 	if (error)
147 		return error;
148 	pci_save_state(to_pci_dev(dev->dev));
149 	return 0;
150 }
151 
152 static int ast_drm_thaw(struct drm_device *dev)
153 {
154 	ast_post_gpu(dev);
155 
156 	return drm_mode_config_helper_resume(dev);
157 }
158 
159 static int ast_drm_resume(struct drm_device *dev)
160 {
161 	int ret;
162 
163 	if (pci_enable_device(to_pci_dev(dev->dev)))
164 		return -EIO;
165 
166 	ret = ast_drm_thaw(dev);
167 	if (ret)
168 		return ret;
169 	return 0;
170 }
171 
172 static int ast_pm_suspend(struct device *dev)
173 {
174 	struct pci_dev *pdev = to_pci_dev(dev);
175 	struct drm_device *ddev = pci_get_drvdata(pdev);
176 	int error;
177 
178 	error = ast_drm_freeze(ddev);
179 	if (error)
180 		return error;
181 
182 	pci_disable_device(pdev);
183 	pci_set_power_state(pdev, PCI_D3hot);
184 	return 0;
185 }
186 
187 static int ast_pm_resume(struct device *dev)
188 {
189 	struct pci_dev *pdev = to_pci_dev(dev);
190 	struct drm_device *ddev = pci_get_drvdata(pdev);
191 	return ast_drm_resume(ddev);
192 }
193 
194 static int ast_pm_freeze(struct device *dev)
195 {
196 	struct pci_dev *pdev = to_pci_dev(dev);
197 	struct drm_device *ddev = pci_get_drvdata(pdev);
198 	return ast_drm_freeze(ddev);
199 }
200 
201 static int ast_pm_thaw(struct device *dev)
202 {
203 	struct pci_dev *pdev = to_pci_dev(dev);
204 	struct drm_device *ddev = pci_get_drvdata(pdev);
205 	return ast_drm_thaw(ddev);
206 }
207 
208 static int ast_pm_poweroff(struct device *dev)
209 {
210 	struct pci_dev *pdev = to_pci_dev(dev);
211 	struct drm_device *ddev = pci_get_drvdata(pdev);
212 
213 	return ast_drm_freeze(ddev);
214 }
215 
216 static const struct dev_pm_ops ast_pm_ops = {
217 	.suspend = ast_pm_suspend,
218 	.resume = ast_pm_resume,
219 	.freeze = ast_pm_freeze,
220 	.thaw = ast_pm_thaw,
221 	.poweroff = ast_pm_poweroff,
222 	.restore = ast_pm_resume,
223 };
224 
225 static struct pci_driver ast_pci_driver = {
226 	.name = DRIVER_NAME,
227 	.id_table = ast_pciidlist,
228 	.probe = ast_pci_probe,
229 	.remove = ast_pci_remove,
230 	.driver.pm = &ast_pm_ops,
231 };
232 
233 static int __init ast_init(void)
234 {
235 	if (drm_firmware_drivers_only() && ast_modeset == -1)
236 		return -EINVAL;
237 
238 	if (ast_modeset == 0)
239 		return -EINVAL;
240 	return pci_register_driver(&ast_pci_driver);
241 }
242 static void __exit ast_exit(void)
243 {
244 	pci_unregister_driver(&ast_pci_driver);
245 }
246 
247 module_init(ast_init);
248 module_exit(ast_exit);
249 
250 MODULE_AUTHOR(DRIVER_AUTHOR);
251 MODULE_DESCRIPTION(DRIVER_DESC);
252 MODULE_LICENSE("GPL and additional rights");
253