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 int cirrus_bpp = 24;
20 
21 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
22 module_param_named(modeset, cirrus_modeset, int, 0400);
23 MODULE_PARM_DESC(bpp, "Max bits-per-pixel (default:24)");
24 module_param_named(bpp, cirrus_bpp, int, 0400);
25 
26 /*
27  * This is the generic driver code. This binds the driver to the drm core,
28  * which then performs further device association and calls our graphics init
29  * functions
30  */
31 
32 static struct drm_driver driver;
33 
34 /* only bind to the cirrus chip in qemu */
35 static const struct pci_device_id pciidlist[] = {
36 	{ PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446,
37 	  PCI_SUBVENDOR_ID_REDHAT_QUMRANET, PCI_SUBDEVICE_ID_QEMU,
38 	  0, 0, 0 },
39 	{ PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, PCI_VENDOR_ID_XEN,
40 	  0x0001, 0, 0, 0 },
41 	{0,}
42 };
43 
44 
cirrus_kick_out_firmware_fb(struct pci_dev * pdev)45 static int cirrus_kick_out_firmware_fb(struct pci_dev *pdev)
46 {
47 	struct apertures_struct *ap;
48 	bool primary = false;
49 
50 	ap = alloc_apertures(1);
51 	if (!ap)
52 		return -ENOMEM;
53 
54 	ap->ranges[0].base = pci_resource_start(pdev, 0);
55 	ap->ranges[0].size = pci_resource_len(pdev, 0);
56 
57 #ifdef CONFIG_X86
58 	primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
59 #endif
60 	drm_fb_helper_remove_conflicting_framebuffers(ap, "cirrusdrmfb", primary);
61 	kfree(ap);
62 
63 	return 0;
64 }
65 
cirrus_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)66 static int cirrus_pci_probe(struct pci_dev *pdev,
67 			    const struct pci_device_id *ent)
68 {
69 	int ret;
70 
71 	ret = cirrus_kick_out_firmware_fb(pdev);
72 	if (ret)
73 		return ret;
74 
75 	return drm_get_pci_dev(pdev, ent, &driver);
76 }
77 
cirrus_pci_remove(struct pci_dev * pdev)78 static void cirrus_pci_remove(struct pci_dev *pdev)
79 {
80 	struct drm_device *dev = pci_get_drvdata(pdev);
81 
82 	drm_put_dev(dev);
83 }
84 
85 #ifdef CONFIG_PM_SLEEP
cirrus_pm_suspend(struct device * dev)86 static int cirrus_pm_suspend(struct device *dev)
87 {
88 	struct pci_dev *pdev = to_pci_dev(dev);
89 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
90 	struct cirrus_device *cdev = drm_dev->dev_private;
91 
92 	drm_kms_helper_poll_disable(drm_dev);
93 
94 	if (cdev->mode_info.gfbdev) {
95 		console_lock();
96 		drm_fb_helper_set_suspend(&cdev->mode_info.gfbdev->helper, 1);
97 		console_unlock();
98 	}
99 
100 	return 0;
101 }
102 
cirrus_pm_resume(struct device * dev)103 static int cirrus_pm_resume(struct device *dev)
104 {
105 	struct pci_dev *pdev = to_pci_dev(dev);
106 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
107 	struct cirrus_device *cdev = drm_dev->dev_private;
108 
109 	drm_helper_resume_force_mode(drm_dev);
110 
111 	if (cdev->mode_info.gfbdev) {
112 		console_lock();
113 		drm_fb_helper_set_suspend(&cdev->mode_info.gfbdev->helper, 0);
114 		console_unlock();
115 	}
116 
117 	drm_kms_helper_poll_enable(drm_dev);
118 	return 0;
119 }
120 #endif
121 
122 static const struct file_operations cirrus_driver_fops = {
123 	.owner = THIS_MODULE,
124 	.open = drm_open,
125 	.release = drm_release,
126 	.unlocked_ioctl = drm_ioctl,
127 	.mmap = cirrus_mmap,
128 	.poll = drm_poll,
129 	.compat_ioctl = drm_compat_ioctl,
130 };
131 static struct drm_driver driver = {
132 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
133 	.load = cirrus_driver_load,
134 	.unload = cirrus_driver_unload,
135 	.fops = &cirrus_driver_fops,
136 	.name = DRIVER_NAME,
137 	.desc = DRIVER_DESC,
138 	.date = DRIVER_DATE,
139 	.major = DRIVER_MAJOR,
140 	.minor = DRIVER_MINOR,
141 	.patchlevel = DRIVER_PATCHLEVEL,
142 	.gem_free_object_unlocked = cirrus_gem_free_object,
143 	.dumb_create = cirrus_dumb_create,
144 	.dumb_map_offset = cirrus_dumb_mmap_offset,
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 	if (vgacon_text_force() && cirrus_modeset == -1)
163 		return -EINVAL;
164 
165 	if (cirrus_modeset == 0)
166 		return -EINVAL;
167 	return pci_register_driver(&cirrus_pci_driver);
168 }
169 
cirrus_exit(void)170 static void __exit cirrus_exit(void)
171 {
172 	pci_unregister_driver(&cirrus_pci_driver);
173 }
174 
175 module_init(cirrus_init);
176 module_exit(cirrus_exit);
177 
178 MODULE_DEVICE_TABLE(pci, pciidlist);
179 MODULE_AUTHOR(DRIVER_AUTHOR);
180 MODULE_DESCRIPTION(DRIVER_DESC);
181 MODULE_LICENSE("GPL");
182