• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 Red Hat <mjg@redhat.com>
3  *
4  * This file is subject to the terms and conditions of the GNU General
5  * Public License version 2. See the file COPYING in the main
6  * directory of this archive for more details.
7  *
8  * Authors: Matthew Garrett
9  *          Dave Airlie
10  */
11 #include <linux/module.h>
12 #include <linux/console.h>
13 #include <drm/drmP.h>
14 #include <drm/drm_crtc_helper.h>
15 
16 #include "cirrus_drv.h"
17 
18 int cirrus_modeset = -1;
19 
20 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
21 module_param_named(modeset, cirrus_modeset, int, 0400);
22 
23 /*
24  * This is the generic driver code. This binds the driver to the drm core,
25  * which then performs further device association and calls our graphics init
26  * functions
27  */
28 
29 static struct drm_driver driver;
30 
31 /* only bind to the cirrus chip in qemu */
32 static const struct pci_device_id pciidlist[] = {
33 	{ PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, 0x1af4, 0x1100, 0,
34 	  0, 0 },
35 	{ PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, PCI_VENDOR_ID_XEN,
36 	  0x0001, 0, 0, 0 },
37 	{0,}
38 };
39 
40 
cirrus_kick_out_firmware_fb(struct pci_dev * pdev)41 static int cirrus_kick_out_firmware_fb(struct pci_dev *pdev)
42 {
43 	struct apertures_struct *ap;
44 	bool primary = false;
45 
46 	ap = alloc_apertures(1);
47 	if (!ap)
48 		return -ENOMEM;
49 
50 	ap->ranges[0].base = pci_resource_start(pdev, 0);
51 	ap->ranges[0].size = pci_resource_len(pdev, 0);
52 
53 #ifdef CONFIG_X86
54 	primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
55 #endif
56 	remove_conflicting_framebuffers(ap, "cirrusdrmfb", primary);
57 	kfree(ap);
58 
59 	return 0;
60 }
61 
cirrus_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)62 static int cirrus_pci_probe(struct pci_dev *pdev,
63 			    const struct pci_device_id *ent)
64 {
65 	int ret;
66 
67 	ret = cirrus_kick_out_firmware_fb(pdev);
68 	if (ret)
69 		return ret;
70 
71 	return drm_get_pci_dev(pdev, ent, &driver);
72 }
73 
cirrus_pci_remove(struct pci_dev * pdev)74 static void cirrus_pci_remove(struct pci_dev *pdev)
75 {
76 	struct drm_device *dev = pci_get_drvdata(pdev);
77 
78 	drm_put_dev(dev);
79 }
80 
81 #ifdef CONFIG_PM_SLEEP
cirrus_pm_suspend(struct device * dev)82 static int cirrus_pm_suspend(struct device *dev)
83 {
84 	struct pci_dev *pdev = to_pci_dev(dev);
85 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
86 	struct cirrus_device *cdev = drm_dev->dev_private;
87 
88 	drm_kms_helper_poll_disable(drm_dev);
89 
90 	if (cdev->mode_info.gfbdev) {
91 		console_lock();
92 		fb_set_suspend(cdev->mode_info.gfbdev->helper.fbdev, 1);
93 		console_unlock();
94 	}
95 
96 	return 0;
97 }
98 
cirrus_pm_resume(struct device * dev)99 static int cirrus_pm_resume(struct device *dev)
100 {
101 	struct pci_dev *pdev = to_pci_dev(dev);
102 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
103 	struct cirrus_device *cdev = drm_dev->dev_private;
104 
105 	drm_helper_resume_force_mode(drm_dev);
106 
107 	if (cdev->mode_info.gfbdev) {
108 		console_lock();
109 		fb_set_suspend(cdev->mode_info.gfbdev->helper.fbdev, 0);
110 		console_unlock();
111 	}
112 
113 	drm_kms_helper_poll_enable(drm_dev);
114 	return 0;
115 }
116 #endif
117 
118 static const struct file_operations cirrus_driver_fops = {
119 	.owner = THIS_MODULE,
120 	.open = drm_open,
121 	.release = drm_release,
122 	.unlocked_ioctl = drm_ioctl,
123 	.mmap = cirrus_mmap,
124 	.poll = drm_poll,
125 #ifdef CONFIG_COMPAT
126 	.compat_ioctl = drm_compat_ioctl,
127 #endif
128 };
129 static struct drm_driver driver = {
130 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
131 	.load = cirrus_driver_load,
132 	.unload = cirrus_driver_unload,
133 	.set_busid = drm_pci_set_busid,
134 	.fops = &cirrus_driver_fops,
135 	.name = DRIVER_NAME,
136 	.desc = DRIVER_DESC,
137 	.date = DRIVER_DATE,
138 	.major = DRIVER_MAJOR,
139 	.minor = DRIVER_MINOR,
140 	.patchlevel = DRIVER_PATCHLEVEL,
141 	.gem_free_object = cirrus_gem_free_object,
142 	.dumb_create = cirrus_dumb_create,
143 	.dumb_map_offset = cirrus_dumb_mmap_offset,
144 	.dumb_destroy = drm_gem_dumb_destroy,
145 };
146 
147 static const struct dev_pm_ops cirrus_pm_ops = {
148 	SET_SYSTEM_SLEEP_PM_OPS(cirrus_pm_suspend,
149 				cirrus_pm_resume)
150 };
151 
152 static struct pci_driver cirrus_pci_driver = {
153 	.name = DRIVER_NAME,
154 	.id_table = pciidlist,
155 	.probe = cirrus_pci_probe,
156 	.remove = cirrus_pci_remove,
157 	.driver.pm = &cirrus_pm_ops,
158 };
159 
cirrus_init(void)160 static int __init cirrus_init(void)
161 {
162 #ifdef CONFIG_VGA_CONSOLE
163 	if (vgacon_text_force() && cirrus_modeset == -1)
164 		return -EINVAL;
165 #endif
166 
167 	if (cirrus_modeset == 0)
168 		return -EINVAL;
169 	return drm_pci_init(&driver, &cirrus_pci_driver);
170 }
171 
cirrus_exit(void)172 static void __exit cirrus_exit(void)
173 {
174 	drm_pci_exit(&driver, &cirrus_pci_driver);
175 }
176 
177 module_init(cirrus_init);
178 module_exit(cirrus_exit);
179 
180 MODULE_DEVICE_TABLE(pci, pciidlist);
181 MODULE_AUTHOR(DRIVER_AUTHOR);
182 MODULE_DESCRIPTION(DRIVER_DESC);
183 MODULE_LICENSE("GPL");
184