blob: c2a1cba1e984546d63f033a4cf4b07ff3279bb16 [file] [log] [blame]
Dave Airlief9aa76a2012-04-17 14:12:29 +01001/*
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>
David Howells760285e2012-10-02 18:01:07 +010013#include <drm/drmP.h>
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +020014#include <drm/drm_crtc_helper.h>
Dave Airlief9aa76a2012-04-17 14:12:29 +010015
16#include "cirrus_drv.h"
17
18int cirrus_modeset = -1;
19
20MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
21module_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
29static struct drm_driver driver;
30
31/* only bind to the cirrus chip in qemu */
Benoit Taine9baa3c32014-08-08 15:56:03 +020032static const struct pci_device_id pciidlist[] = {
Dave Airlief9aa76a2012-04-17 14:12:29 +010033 { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, 0x1af4, 0x1100, 0,
34 0, 0 },
Olaf Heringc0c3e732014-04-11 08:56:23 +020035 { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, PCI_VENDOR_ID_XEN,
36 0x0001, 0, 0, 0 },
Dave Airlief9aa76a2012-04-17 14:12:29 +010037 {0,}
38};
39
Dave Airliededc14e2012-06-01 11:11:09 +010040
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000041static int cirrus_kick_out_firmware_fb(struct pci_dev *pdev)
Dave Airliededc14e2012-06-01 11:11:09 +010042{
43 struct apertures_struct *ap;
44 bool primary = false;
45
46 ap = alloc_apertures(1);
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000047 if (!ap)
48 return -ENOMEM;
49
Dave Airliededc14e2012-06-01 11:11:09 +010050 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);
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000058
59 return 0;
Dave Airliededc14e2012-06-01 11:11:09 +010060}
61
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -080062static int cirrus_pci_probe(struct pci_dev *pdev,
63 const struct pci_device_id *ent)
Dave Airlief9aa76a2012-04-17 14:12:29 +010064{
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000065 int ret;
66
67 ret = cirrus_kick_out_firmware_fb(pdev);
68 if (ret)
69 return ret;
Dave Airliededc14e2012-06-01 11:11:09 +010070
Dave Airlief9aa76a2012-04-17 14:12:29 +010071 return drm_get_pci_dev(pdev, ent, &driver);
72}
73
74static 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
Russell King8f8e7e12014-07-12 11:01:43 +010081#ifdef CONFIG_PM_SLEEP
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +020082static 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
99static 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}
Russell King8f8e7e12014-07-12 11:01:43 +0100116#endif
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200117
Dave Airlief9aa76a2012-04-17 14:12:29 +0100118static 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,
Keith Packard804d74a2012-07-09 15:40:07 -0700125#ifdef CONFIG_COMPAT
126 .compat_ioctl = drm_compat_ioctl,
127#endif
Dave Airlief9aa76a2012-04-17 14:12:29 +0100128};
129static struct drm_driver driver = {
Daniel Vetter28185642013-08-08 15:41:27 +0200130 .driver_features = DRIVER_MODESET | DRIVER_GEM,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100131 .load = cirrus_driver_load,
132 .unload = cirrus_driver_unload,
David Herrmann915b4d12014-08-29 12:12:43 +0200133 .set_busid = drm_pci_set_busid,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100134 .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,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100141 .gem_free_object = cirrus_gem_free_object,
142 .dumb_create = cirrus_dumb_create,
143 .dumb_map_offset = cirrus_dumb_mmap_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200144 .dumb_destroy = drm_gem_dumb_destroy,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100145};
146
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200147static const struct dev_pm_ops cirrus_pm_ops = {
148 SET_SYSTEM_SLEEP_PM_OPS(cirrus_pm_suspend,
149 cirrus_pm_resume)
150};
151
Dave Airlief9aa76a2012-04-17 14:12:29 +0100152static struct pci_driver cirrus_pci_driver = {
153 .name = DRIVER_NAME,
154 .id_table = pciidlist,
155 .probe = cirrus_pci_probe,
156 .remove = cirrus_pci_remove,
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200157 .driver.pm = &cirrus_pm_ops,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100158};
159
160static int __init cirrus_init(void)
161{
Dave Airlie4688a69d2012-05-19 16:33:21 +0100162#ifdef CONFIG_VGA_CONSOLE
Dave Airlief9aa76a2012-04-17 14:12:29 +0100163 if (vgacon_text_force() && cirrus_modeset == -1)
164 return -EINVAL;
Dave Airlie4688a69d2012-05-19 16:33:21 +0100165#endif
Dave Airlief9aa76a2012-04-17 14:12:29 +0100166
167 if (cirrus_modeset == 0)
168 return -EINVAL;
169 return drm_pci_init(&driver, &cirrus_pci_driver);
170}
171
172static void __exit cirrus_exit(void)
173{
174 drm_pci_exit(&driver, &cirrus_pci_driver);
175}
176
177module_init(cirrus_init);
178module_exit(cirrus_exit);
179
180MODULE_DEVICE_TABLE(pci, pciidlist);
181MODULE_AUTHOR(DRIVER_AUTHOR);
182MODULE_DESCRIPTION(DRIVER_DESC);
183MODULE_LICENSE("GPL");