blob: b05f7eae32ce756e39f656adcbadc2ddfb55893c [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;
Takashi Iwai7f551b12015-02-03 17:51:23 +010019int cirrus_bpp = 24;
Dave Airlief9aa76a2012-04-17 14:12:29 +010020
21MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
22module_param_named(modeset, cirrus_modeset, int, 0400);
Takashi Iwai7f551b12015-02-03 17:51:23 +010023MODULE_PARM_DESC(bpp, "Max bits-per-pixel (default:24)");
24module_param_named(bpp, cirrus_bpp, int, 0400);
Dave Airlief9aa76a2012-04-17 14:12:29 +010025
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
32static struct drm_driver driver;
33
34/* only bind to the cirrus chip in qemu */
Benoit Taine9baa3c32014-08-08 15:56:03 +020035static const struct pci_device_id pciidlist[] = {
Robin H. Johnsoncaf02ab2016-03-06 22:02:30 +000036 { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446,
37 PCI_SUBVENDOR_ID_REDHAT_QUMRANET, PCI_SUBDEVICE_ID_QEMU,
38 0, 0, 0 },
Olaf Heringc0c3e732014-04-11 08:56:23 +020039 { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, PCI_VENDOR_ID_XEN,
40 0x0001, 0, 0, 0 },
Dave Airlief9aa76a2012-04-17 14:12:29 +010041 {0,}
42};
43
Dave Airliededc14e2012-06-01 11:11:09 +010044
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000045static int cirrus_kick_out_firmware_fb(struct pci_dev *pdev)
Dave Airliededc14e2012-06-01 11:11:09 +010046{
47 struct apertures_struct *ap;
48 bool primary = false;
49
50 ap = alloc_apertures(1);
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000051 if (!ap)
52 return -ENOMEM;
53
Dave Airliededc14e2012-06-01 11:11:09 +010054 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 remove_conflicting_framebuffers(ap, "cirrusdrmfb", primary);
61 kfree(ap);
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000062
63 return 0;
Dave Airliededc14e2012-06-01 11:11:09 +010064}
65
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -080066static int cirrus_pci_probe(struct pci_dev *pdev,
67 const struct pci_device_id *ent)
Dave Airlief9aa76a2012-04-17 14:12:29 +010068{
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000069 int ret;
70
71 ret = cirrus_kick_out_firmware_fb(pdev);
72 if (ret)
73 return ret;
Dave Airliededc14e2012-06-01 11:11:09 +010074
Dave Airlief9aa76a2012-04-17 14:12:29 +010075 return drm_get_pci_dev(pdev, ent, &driver);
76}
77
78static 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
Russell King8f8e7e12014-07-12 11:01:43 +010085#ifdef CONFIG_PM_SLEEP
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +020086static 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();
Archit Taneja2b9e6e32015-07-31 16:21:44 +053096 drm_fb_helper_set_suspend(&cdev->mode_info.gfbdev->helper, 1);
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +020097 console_unlock();
98 }
99
100 return 0;
101}
102
103static 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();
Archit Taneja2b9e6e32015-07-31 16:21:44 +0530113 drm_fb_helper_set_suspend(&cdev->mode_info.gfbdev->helper, 0);
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200114 console_unlock();
115 }
116
117 drm_kms_helper_poll_enable(drm_dev);
118 return 0;
119}
Russell King8f8e7e12014-07-12 11:01:43 +0100120#endif
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200121
Dave Airlief9aa76a2012-04-17 14:12:29 +0100122static 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,
Keith Packard804d74a2012-07-09 15:40:07 -0700129#ifdef CONFIG_COMPAT
130 .compat_ioctl = drm_compat_ioctl,
131#endif
Dave Airlief9aa76a2012-04-17 14:12:29 +0100132};
133static struct drm_driver driver = {
Daniel Vetter28185642013-08-08 15:41:27 +0200134 .driver_features = DRIVER_MODESET | DRIVER_GEM,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100135 .load = cirrus_driver_load,
136 .unload = cirrus_driver_unload,
David Herrmann915b4d12014-08-29 12:12:43 +0200137 .set_busid = drm_pci_set_busid,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100138 .fops = &cirrus_driver_fops,
139 .name = DRIVER_NAME,
140 .desc = DRIVER_DESC,
141 .date = DRIVER_DATE,
142 .major = DRIVER_MAJOR,
143 .minor = DRIVER_MINOR,
144 .patchlevel = DRIVER_PATCHLEVEL,
Daniel Vetterf5993ee2016-05-30 19:52:57 +0200145 .gem_free_object_unlocked = cirrus_gem_free_object,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100146 .dumb_create = cirrus_dumb_create,
147 .dumb_map_offset = cirrus_dumb_mmap_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200148 .dumb_destroy = drm_gem_dumb_destroy,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100149};
150
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200151static const struct dev_pm_ops cirrus_pm_ops = {
152 SET_SYSTEM_SLEEP_PM_OPS(cirrus_pm_suspend,
153 cirrus_pm_resume)
154};
155
Dave Airlief9aa76a2012-04-17 14:12:29 +0100156static struct pci_driver cirrus_pci_driver = {
157 .name = DRIVER_NAME,
158 .id_table = pciidlist,
159 .probe = cirrus_pci_probe,
160 .remove = cirrus_pci_remove,
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200161 .driver.pm = &cirrus_pm_ops,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100162};
163
164static int __init cirrus_init(void)
165{
166 if (vgacon_text_force() && cirrus_modeset == -1)
167 return -EINVAL;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100168
169 if (cirrus_modeset == 0)
170 return -EINVAL;
171 return drm_pci_init(&driver, &cirrus_pci_driver);
172}
173
174static void __exit cirrus_exit(void)
175{
176 drm_pci_exit(&driver, &cirrus_pci_driver);
177}
178
179module_init(cirrus_init);
180module_exit(cirrus_exit);
181
182MODULE_DEVICE_TABLE(pci, pciidlist);
183MODULE_AUTHOR(DRIVER_AUTHOR);
184MODULE_DESCRIPTION(DRIVER_DESC);
185MODULE_LICENSE("GPL");