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/console.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_crtc_helper.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_fb_helper.h>
37 #include <drm/drm_gem_vram_helper.h>
38 #include <drm/drm_probe_helper.h>
39 
40 #include "ast_drv.h"
41 
42 int ast_modeset = -1;
43 
44 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
45 module_param_named(modeset, ast_modeset, int, 0400);
46 
47 /*
48  * DRM driver
49  */
50 
51 DEFINE_DRM_GEM_FOPS(ast_fops);
52 
53 static const struct drm_driver ast_driver = {
54 	.driver_features = DRIVER_ATOMIC |
55 			   DRIVER_GEM |
56 			   DRIVER_MODESET,
57 
58 	.fops = &ast_fops,
59 	.name = DRIVER_NAME,
60 	.desc = DRIVER_DESC,
61 	.date = DRIVER_DATE,
62 	.major = DRIVER_MAJOR,
63 	.minor = DRIVER_MINOR,
64 	.patchlevel = DRIVER_PATCHLEVEL,
65 
66 	DRM_GEM_VRAM_DRIVER
67 };
68 
69 /*
70  * PCI driver
71  */
72 
73 #define PCI_VENDOR_ASPEED 0x1a03
74 
75 #define AST_VGA_DEVICE(id, info) {		\
76 	.class = PCI_BASE_CLASS_DISPLAY << 16,	\
77 	.class_mask = 0xff0000,			\
78 	.vendor = PCI_VENDOR_ASPEED,			\
79 	.device = id,				\
80 	.subvendor = PCI_ANY_ID,		\
81 	.subdevice = PCI_ANY_ID,		\
82 	.driver_data = (unsigned long) info }
83 
84 static const struct pci_device_id ast_pciidlist[] = {
85 	AST_VGA_DEVICE(PCI_CHIP_AST2000, NULL),
86 	AST_VGA_DEVICE(PCI_CHIP_AST2100, NULL),
87 	{0, 0, 0},
88 };
89 
90 MODULE_DEVICE_TABLE(pci, ast_pciidlist);
91 
ast_kick_out_firmware_fb(struct pci_dev * pdev)92 static void ast_kick_out_firmware_fb(struct pci_dev *pdev)
93 {
94 	struct apertures_struct *ap;
95 	bool primary = false;
96 
97 	ap = alloc_apertures(1);
98 	if (!ap)
99 		return;
100 
101 	ap->ranges[0].base = pci_resource_start(pdev, 0);
102 	ap->ranges[0].size = pci_resource_len(pdev, 0);
103 
104 #ifdef CONFIG_X86
105 	primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
106 #endif
107 	drm_fb_helper_remove_conflicting_framebuffers(ap, "astdrmfb", primary);
108 	kfree(ap);
109 }
110 
ast_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)111 static int ast_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
112 {
113 	struct ast_private *ast;
114 	struct drm_device *dev;
115 	int ret;
116 
117 	ast_kick_out_firmware_fb(pdev);
118 
119 	ret = pcim_enable_device(pdev);
120 	if (ret)
121 		return ret;
122 
123 	ast = ast_device_create(&ast_driver, pdev, ent->driver_data);
124 	if (IS_ERR(ast))
125 		return PTR_ERR(ast);
126 	dev = &ast->base;
127 
128 	ret = drm_dev_register(dev, ent->driver_data);
129 	if (ret)
130 		return ret;
131 
132 	drm_fbdev_generic_setup(dev, 32);
133 
134 	return 0;
135 }
136 
ast_pci_remove(struct pci_dev * pdev)137 static void ast_pci_remove(struct pci_dev *pdev)
138 {
139 	struct drm_device *dev = pci_get_drvdata(pdev);
140 
141 	drm_dev_unregister(dev);
142 	drm_atomic_helper_shutdown(dev);
143 }
144 
ast_drm_freeze(struct drm_device * dev)145 static int ast_drm_freeze(struct drm_device *dev)
146 {
147 	int error;
148 
149 	error = drm_mode_config_helper_suspend(dev);
150 	if (error)
151 		return error;
152 	pci_save_state(to_pci_dev(dev->dev));
153 	return 0;
154 }
155 
ast_drm_thaw(struct drm_device * dev)156 static int ast_drm_thaw(struct drm_device *dev)
157 {
158 	ast_post_gpu(dev);
159 
160 	return drm_mode_config_helper_resume(dev);
161 }
162 
ast_drm_resume(struct drm_device * dev)163 static int ast_drm_resume(struct drm_device *dev)
164 {
165 	int ret;
166 
167 	if (pci_enable_device(to_pci_dev(dev->dev)))
168 		return -EIO;
169 
170 	ret = ast_drm_thaw(dev);
171 	if (ret)
172 		return ret;
173 	return 0;
174 }
175 
ast_pm_suspend(struct device * dev)176 static int ast_pm_suspend(struct device *dev)
177 {
178 	struct pci_dev *pdev = to_pci_dev(dev);
179 	struct drm_device *ddev = pci_get_drvdata(pdev);
180 	int error;
181 
182 	error = ast_drm_freeze(ddev);
183 	if (error)
184 		return error;
185 
186 	pci_disable_device(pdev);
187 	pci_set_power_state(pdev, PCI_D3hot);
188 	return 0;
189 }
190 
ast_pm_resume(struct device * dev)191 static int ast_pm_resume(struct device *dev)
192 {
193 	struct pci_dev *pdev = to_pci_dev(dev);
194 	struct drm_device *ddev = pci_get_drvdata(pdev);
195 	return ast_drm_resume(ddev);
196 }
197 
ast_pm_freeze(struct device * dev)198 static int ast_pm_freeze(struct device *dev)
199 {
200 	struct pci_dev *pdev = to_pci_dev(dev);
201 	struct drm_device *ddev = pci_get_drvdata(pdev);
202 	return ast_drm_freeze(ddev);
203 }
204 
ast_pm_thaw(struct device * dev)205 static int ast_pm_thaw(struct device *dev)
206 {
207 	struct pci_dev *pdev = to_pci_dev(dev);
208 	struct drm_device *ddev = pci_get_drvdata(pdev);
209 	return ast_drm_thaw(ddev);
210 }
211 
ast_pm_poweroff(struct device * dev)212 static int ast_pm_poweroff(struct device *dev)
213 {
214 	struct pci_dev *pdev = to_pci_dev(dev);
215 	struct drm_device *ddev = pci_get_drvdata(pdev);
216 
217 	return ast_drm_freeze(ddev);
218 }
219 
220 static const struct dev_pm_ops ast_pm_ops = {
221 	.suspend = ast_pm_suspend,
222 	.resume = ast_pm_resume,
223 	.freeze = ast_pm_freeze,
224 	.thaw = ast_pm_thaw,
225 	.poweroff = ast_pm_poweroff,
226 	.restore = ast_pm_resume,
227 };
228 
229 static struct pci_driver ast_pci_driver = {
230 	.name = DRIVER_NAME,
231 	.id_table = ast_pciidlist,
232 	.probe = ast_pci_probe,
233 	.remove = ast_pci_remove,
234 	.driver.pm = &ast_pm_ops,
235 };
236 
ast_init(void)237 static int __init ast_init(void)
238 {
239 	if (vgacon_text_force() && ast_modeset == -1)
240 		return -EINVAL;
241 
242 	if (ast_modeset == 0)
243 		return -EINVAL;
244 	return pci_register_driver(&ast_pci_driver);
245 }
ast_exit(void)246 static void __exit ast_exit(void)
247 {
248 	pci_unregister_driver(&ast_pci_driver);
249 }
250 
251 module_init(ast_init);
252 module_exit(ast_exit);
253 
254 MODULE_AUTHOR(DRIVER_AUTHOR);
255 MODULE_DESCRIPTION(DRIVER_DESC);
256 MODULE_LICENSE("GPL and additional rights");
257