Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 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> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 13 | #include <drm/drmP.h> |
Gerd Hoffmann | 2f1e800 | 2014-04-14 11:34:48 +0200 | [diff] [blame] | 14 | #include <drm/drm_crtc_helper.h> |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 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 */ |
Benoit Taine | 9baa3c3 | 2014-08-08 15:56:03 +0200 | [diff] [blame] | 32 | static const struct pci_device_id pciidlist[] = { |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 33 | { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, 0x1af4, 0x1100, 0, |
| 34 | 0, 0 }, |
Olaf Hering | c0c3e73 | 2014-04-11 08:56:23 +0200 | [diff] [blame] | 35 | { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, PCI_VENDOR_ID_XEN, |
| 36 | 0x0001, 0, 0, 0 }, |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 37 | {0,} |
| 38 | }; |
| 39 | |
Dave Airlie | dedc14e | 2012-06-01 11:11:09 +0100 | [diff] [blame] | 40 | |
Tommi Rantala | bfb82ff | 2012-11-09 09:19:35 +0000 | [diff] [blame] | 41 | static int cirrus_kick_out_firmware_fb(struct pci_dev *pdev) |
Dave Airlie | dedc14e | 2012-06-01 11:11:09 +0100 | [diff] [blame] | 42 | { |
| 43 | struct apertures_struct *ap; |
| 44 | bool primary = false; |
| 45 | |
| 46 | ap = alloc_apertures(1); |
Tommi Rantala | bfb82ff | 2012-11-09 09:19:35 +0000 | [diff] [blame] | 47 | if (!ap) |
| 48 | return -ENOMEM; |
| 49 | |
Dave Airlie | dedc14e | 2012-06-01 11:11:09 +0100 | [diff] [blame] | 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); |
Tommi Rantala | bfb82ff | 2012-11-09 09:19:35 +0000 | [diff] [blame] | 58 | |
| 59 | return 0; |
Dave Airlie | dedc14e | 2012-06-01 11:11:09 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Greg Kroah-Hartman | 56550d9 | 2012-12-21 15:09:25 -0800 | [diff] [blame] | 62 | static int cirrus_pci_probe(struct pci_dev *pdev, |
| 63 | const struct pci_device_id *ent) |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 64 | { |
Tommi Rantala | bfb82ff | 2012-11-09 09:19:35 +0000 | [diff] [blame] | 65 | int ret; |
| 66 | |
| 67 | ret = cirrus_kick_out_firmware_fb(pdev); |
| 68 | if (ret) |
| 69 | return ret; |
Dave Airlie | dedc14e | 2012-06-01 11:11:09 +0100 | [diff] [blame] | 70 | |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 71 | return drm_get_pci_dev(pdev, ent, &driver); |
| 72 | } |
| 73 | |
| 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 | |
Russell King | 8f8e7e1 | 2014-07-12 11:01:43 +0100 | [diff] [blame] | 81 | #ifdef CONFIG_PM_SLEEP |
Gerd Hoffmann | 2f1e800 | 2014-04-14 11:34:48 +0200 | [diff] [blame] | 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 | |
| 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 | } |
Russell King | 8f8e7e1 | 2014-07-12 11:01:43 +0100 | [diff] [blame] | 116 | #endif |
Gerd Hoffmann | 2f1e800 | 2014-04-14 11:34:48 +0200 | [diff] [blame] | 117 | |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 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, |
Keith Packard | 804d74a | 2012-07-09 15:40:07 -0700 | [diff] [blame] | 125 | #ifdef CONFIG_COMPAT |
| 126 | .compat_ioctl = drm_compat_ioctl, |
| 127 | #endif |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 128 | }; |
| 129 | static struct drm_driver driver = { |
Daniel Vetter | 2818564 | 2013-08-08 15:41:27 +0200 | [diff] [blame] | 130 | .driver_features = DRIVER_MODESET | DRIVER_GEM, |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 131 | .load = cirrus_driver_load, |
| 132 | .unload = cirrus_driver_unload, |
David Herrmann | 915b4d1 | 2014-08-29 12:12:43 +0200 | [diff] [blame] | 133 | .set_busid = drm_pci_set_busid, |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 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, |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 141 | .gem_free_object = cirrus_gem_free_object, |
| 142 | .dumb_create = cirrus_dumb_create, |
| 143 | .dumb_map_offset = cirrus_dumb_mmap_offset, |
Daniel Vetter | 43387b3 | 2013-07-16 09:12:04 +0200 | [diff] [blame] | 144 | .dumb_destroy = drm_gem_dumb_destroy, |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
Gerd Hoffmann | 2f1e800 | 2014-04-14 11:34:48 +0200 | [diff] [blame] | 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 | |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 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, |
Gerd Hoffmann | 2f1e800 | 2014-04-14 11:34:48 +0200 | [diff] [blame] | 157 | .driver.pm = &cirrus_pm_ops, |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static int __init cirrus_init(void) |
| 161 | { |
Dave Airlie | 4688a69d | 2012-05-19 16:33:21 +0100 | [diff] [blame] | 162 | #ifdef CONFIG_VGA_CONSOLE |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 163 | if (vgacon_text_force() && cirrus_modeset == -1) |
| 164 | return -EINVAL; |
Dave Airlie | 4688a69d | 2012-05-19 16:33:21 +0100 | [diff] [blame] | 165 | #endif |
Dave Airlie | f9aa76a | 2012-04-17 14:12:29 +0100 | [diff] [blame] | 166 | |
| 167 | if (cirrus_modeset == 0) |
| 168 | return -EINVAL; |
| 169 | return drm_pci_init(&driver, &cirrus_pci_driver); |
| 170 | } |
| 171 | |
| 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"); |