blob: e705335101a59ea0a703d9b699d8ea7af9eafcb5 [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 },
35 {0,}
36};
37
Dave Airliededc14e2012-06-01 11:11:09 +010038
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000039static int cirrus_kick_out_firmware_fb(struct pci_dev *pdev)
Dave Airliededc14e2012-06-01 11:11:09 +010040{
41 struct apertures_struct *ap;
42 bool primary = false;
43
44 ap = alloc_apertures(1);
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000045 if (!ap)
46 return -ENOMEM;
47
Dave Airliededc14e2012-06-01 11:11:09 +010048 ap->ranges[0].base = pci_resource_start(pdev, 0);
49 ap->ranges[0].size = pci_resource_len(pdev, 0);
50
51#ifdef CONFIG_X86
52 primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
53#endif
54 remove_conflicting_framebuffers(ap, "cirrusdrmfb", primary);
55 kfree(ap);
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000056
57 return 0;
Dave Airliededc14e2012-06-01 11:11:09 +010058}
59
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -080060static int cirrus_pci_probe(struct pci_dev *pdev,
61 const struct pci_device_id *ent)
Dave Airlief9aa76a2012-04-17 14:12:29 +010062{
Tommi Rantalabfb82ff2012-11-09 09:19:35 +000063 int ret;
64
65 ret = cirrus_kick_out_firmware_fb(pdev);
66 if (ret)
67 return ret;
Dave Airliededc14e2012-06-01 11:11:09 +010068
Dave Airlief9aa76a2012-04-17 14:12:29 +010069 return drm_get_pci_dev(pdev, ent, &driver);
70}
71
72static void cirrus_pci_remove(struct pci_dev *pdev)
73{
74 struct drm_device *dev = pci_get_drvdata(pdev);
75
76 drm_put_dev(dev);
77}
78
Russell King8f8e7e12014-07-12 11:01:43 +010079#ifdef CONFIG_PM_SLEEP
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +020080static int cirrus_pm_suspend(struct device *dev)
81{
82 struct pci_dev *pdev = to_pci_dev(dev);
83 struct drm_device *drm_dev = pci_get_drvdata(pdev);
84 struct cirrus_device *cdev = drm_dev->dev_private;
85
86 drm_kms_helper_poll_disable(drm_dev);
87
88 if (cdev->mode_info.gfbdev) {
89 console_lock();
90 fb_set_suspend(cdev->mode_info.gfbdev->helper.fbdev, 1);
91 console_unlock();
92 }
93
94 return 0;
95}
96
97static int cirrus_pm_resume(struct device *dev)
98{
99 struct pci_dev *pdev = to_pci_dev(dev);
100 struct drm_device *drm_dev = pci_get_drvdata(pdev);
101 struct cirrus_device *cdev = drm_dev->dev_private;
102
103 drm_helper_resume_force_mode(drm_dev);
104
105 if (cdev->mode_info.gfbdev) {
106 console_lock();
107 fb_set_suspend(cdev->mode_info.gfbdev->helper.fbdev, 0);
108 console_unlock();
109 }
110
111 drm_kms_helper_poll_enable(drm_dev);
112 return 0;
113}
Russell King8f8e7e12014-07-12 11:01:43 +0100114#endif
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200115
Dave Airlief9aa76a2012-04-17 14:12:29 +0100116static const struct file_operations cirrus_driver_fops = {
117 .owner = THIS_MODULE,
118 .open = drm_open,
119 .release = drm_release,
120 .unlocked_ioctl = drm_ioctl,
121 .mmap = cirrus_mmap,
122 .poll = drm_poll,
Keith Packard804d74a2012-07-09 15:40:07 -0700123#ifdef CONFIG_COMPAT
124 .compat_ioctl = drm_compat_ioctl,
125#endif
Dave Airlief9aa76a2012-04-17 14:12:29 +0100126};
127static struct drm_driver driver = {
Daniel Vetter28185642013-08-08 15:41:27 +0200128 .driver_features = DRIVER_MODESET | DRIVER_GEM,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100129 .load = cirrus_driver_load,
130 .unload = cirrus_driver_unload,
David Herrmann915b4d12014-08-29 12:12:43 +0200131 .set_busid = drm_pci_set_busid,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100132 .fops = &cirrus_driver_fops,
133 .name = DRIVER_NAME,
134 .desc = DRIVER_DESC,
135 .date = DRIVER_DATE,
136 .major = DRIVER_MAJOR,
137 .minor = DRIVER_MINOR,
138 .patchlevel = DRIVER_PATCHLEVEL,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100139 .gem_free_object = cirrus_gem_free_object,
140 .dumb_create = cirrus_dumb_create,
141 .dumb_map_offset = cirrus_dumb_mmap_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200142 .dumb_destroy = drm_gem_dumb_destroy,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100143};
144
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200145static const struct dev_pm_ops cirrus_pm_ops = {
146 SET_SYSTEM_SLEEP_PM_OPS(cirrus_pm_suspend,
147 cirrus_pm_resume)
148};
149
Dave Airlief9aa76a2012-04-17 14:12:29 +0100150static struct pci_driver cirrus_pci_driver = {
151 .name = DRIVER_NAME,
152 .id_table = pciidlist,
153 .probe = cirrus_pci_probe,
154 .remove = cirrus_pci_remove,
Gerd Hoffmann2f1e8002014-04-14 11:34:48 +0200155 .driver.pm = &cirrus_pm_ops,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100156};
157
158static int __init cirrus_init(void)
159{
Dave Airlie4688a692012-05-19 16:33:21 +0100160#ifdef CONFIG_VGA_CONSOLE
Dave Airlief9aa76a2012-04-17 14:12:29 +0100161 if (vgacon_text_force() && cirrus_modeset == -1)
162 return -EINVAL;
Dave Airlie4688a692012-05-19 16:33:21 +0100163#endif
Dave Airlief9aa76a2012-04-17 14:12:29 +0100164
165 if (cirrus_modeset == 0)
166 return -EINVAL;
167 return drm_pci_init(&driver, &cirrus_pci_driver);
168}
169
170static void __exit cirrus_exit(void)
171{
172 drm_pci_exit(&driver, &cirrus_pci_driver);
173}
174
175module_init(cirrus_init);
176module_exit(cirrus_exit);
177
178MODULE_DEVICE_TABLE(pci, pciidlist);
179MODULE_AUTHOR(DRIVER_AUTHOR);
180MODULE_DESCRIPTION(DRIVER_DESC);
181MODULE_LICENSE("GPL");