1f64122c1SDave Airlie /* vim: set ts=8 sw=8 tw=78 ai noexpandtab */ 2f64122c1SDave Airlie /* qxl_drv.c -- QXL driver -*- linux-c -*- 3f64122c1SDave Airlie * 4f64122c1SDave Airlie * Copyright 2011 Red Hat, Inc. 5f64122c1SDave Airlie * All Rights Reserved. 6f64122c1SDave Airlie * 7f64122c1SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a 8f64122c1SDave Airlie * copy of this software and associated documentation files (the "Software"), 9f64122c1SDave Airlie * to deal in the Software without restriction, including without limitation 10f64122c1SDave Airlie * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11f64122c1SDave Airlie * and/or sell copies of the Software, and to permit persons to whom the 12f64122c1SDave Airlie * Software is furnished to do so, subject to the following conditions: 13f64122c1SDave Airlie * 14f64122c1SDave Airlie * The above copyright notice and this permission notice (including the next 15f64122c1SDave Airlie * paragraph) shall be included in all copies or substantial portions of the 16f64122c1SDave Airlie * Software. 17f64122c1SDave Airlie * 18f64122c1SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19f64122c1SDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20f64122c1SDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21f64122c1SDave Airlie * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 22f64122c1SDave Airlie * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23f64122c1SDave Airlie * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24f64122c1SDave Airlie * OTHER DEALINGS IN THE SOFTWARE. 25f64122c1SDave Airlie * 26f64122c1SDave Airlie * Authors: 27f64122c1SDave Airlie * Dave Airlie <airlie@redhat.com> 28f64122c1SDave Airlie * Alon Levy <alevy@redhat.com> 29f64122c1SDave Airlie */ 30f64122c1SDave Airlie 31f64122c1SDave Airlie #include <linux/module.h> 32f64122c1SDave Airlie #include <linux/console.h> 33f64122c1SDave Airlie 34f64122c1SDave Airlie #include "drmP.h" 35f64122c1SDave Airlie #include "drm/drm.h" 36d84300bfSDave Airlie #include "drm_crtc_helper.h" 37f64122c1SDave Airlie #include "qxl_drv.h" 38d84300bfSDave Airlie #include "qxl_object.h" 39f64122c1SDave Airlie 40f64122c1SDave Airlie extern int qxl_max_ioctls; 41*9baa3c34SBenoit Taine static const struct pci_device_id pciidlist[] = { 42f64122c1SDave Airlie { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, 43f64122c1SDave Airlie 0xffff00, 0 }, 44f64122c1SDave Airlie { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8, 45f64122c1SDave Airlie 0xffff00, 0 }, 46f64122c1SDave Airlie { 0, 0, 0 }, 47f64122c1SDave Airlie }; 48f64122c1SDave Airlie MODULE_DEVICE_TABLE(pci, pciidlist); 49f64122c1SDave Airlie 506d01f1f5SDave Airlie static int qxl_modeset = -1; 5107f8d9bdSDave Airlie int qxl_num_crtc = 4; 52f64122c1SDave Airlie 53f64122c1SDave Airlie MODULE_PARM_DESC(modeset, "Disable/Enable modesetting"); 54f64122c1SDave Airlie module_param_named(modeset, qxl_modeset, int, 0400); 55f64122c1SDave Airlie 5607f8d9bdSDave Airlie MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)"); 5707f8d9bdSDave Airlie module_param_named(num_heads, qxl_num_crtc, int, 0400); 5807f8d9bdSDave Airlie 59f64122c1SDave Airlie static struct drm_driver qxl_driver; 60f64122c1SDave Airlie static struct pci_driver qxl_pci_driver; 61f64122c1SDave Airlie 62f64122c1SDave Airlie static int 63f64122c1SDave Airlie qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 64f64122c1SDave Airlie { 65f64122c1SDave Airlie if (pdev->revision < 4) { 66f64122c1SDave Airlie DRM_ERROR("qxl too old, doesn't support client_monitors_config," 67f64122c1SDave Airlie " use xf86-video-qxl in user mode"); 68f64122c1SDave Airlie return -EINVAL; /* TODO: ENODEV ? */ 69f64122c1SDave Airlie } 70f64122c1SDave Airlie return drm_get_pci_dev(pdev, ent, &qxl_driver); 71f64122c1SDave Airlie } 72f64122c1SDave Airlie 73f64122c1SDave Airlie static void 74f64122c1SDave Airlie qxl_pci_remove(struct pci_dev *pdev) 75f64122c1SDave Airlie { 76f64122c1SDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 77f64122c1SDave Airlie 78f64122c1SDave Airlie drm_put_dev(dev); 79f64122c1SDave Airlie } 80f64122c1SDave Airlie 81f64122c1SDave Airlie static const struct file_operations qxl_fops = { 82f64122c1SDave Airlie .owner = THIS_MODULE, 83f64122c1SDave Airlie .open = drm_open, 84f64122c1SDave Airlie .release = drm_release, 85f64122c1SDave Airlie .unlocked_ioctl = drm_ioctl, 86f64122c1SDave Airlie .poll = drm_poll, 87f64122c1SDave Airlie .mmap = qxl_mmap, 88f64122c1SDave Airlie }; 89f64122c1SDave Airlie 90d84300bfSDave Airlie static int qxl_drm_freeze(struct drm_device *dev) 91d84300bfSDave Airlie { 92d84300bfSDave Airlie struct pci_dev *pdev = dev->pdev; 93d84300bfSDave Airlie struct qxl_device *qdev = dev->dev_private; 94d84300bfSDave Airlie struct drm_crtc *crtc; 95d84300bfSDave Airlie 96d84300bfSDave Airlie drm_kms_helper_poll_disable(dev); 97d84300bfSDave Airlie 98d84300bfSDave Airlie console_lock(); 99d84300bfSDave Airlie qxl_fbdev_set_suspend(qdev, 1); 100d84300bfSDave Airlie console_unlock(); 101d84300bfSDave Airlie 102d84300bfSDave Airlie /* unpin the front buffers */ 103d84300bfSDave Airlie list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 104d84300bfSDave Airlie struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 105d84300bfSDave Airlie if (crtc->enabled) 106d84300bfSDave Airlie (*crtc_funcs->disable)(crtc); 107d84300bfSDave Airlie } 108d84300bfSDave Airlie 109d84300bfSDave Airlie qxl_destroy_monitors_object(qdev); 110d84300bfSDave Airlie qxl_surf_evict(qdev); 111d84300bfSDave Airlie qxl_vram_evict(qdev); 112d84300bfSDave Airlie 113d84300bfSDave Airlie while (!qxl_check_idle(qdev->command_ring)); 114d84300bfSDave Airlie while (!qxl_check_idle(qdev->release_ring)) 115d84300bfSDave Airlie qxl_queue_garbage_collect(qdev, 1); 116d84300bfSDave Airlie 117d84300bfSDave Airlie pci_save_state(pdev); 118d84300bfSDave Airlie 119d84300bfSDave Airlie return 0; 120d84300bfSDave Airlie } 121d84300bfSDave Airlie 122d84300bfSDave Airlie static int qxl_drm_resume(struct drm_device *dev, bool thaw) 123d84300bfSDave Airlie { 124d84300bfSDave Airlie struct qxl_device *qdev = dev->dev_private; 125d84300bfSDave Airlie 126d84300bfSDave Airlie qdev->ram_header->int_mask = QXL_INTERRUPT_MASK; 127d84300bfSDave Airlie if (!thaw) { 128d84300bfSDave Airlie qxl_reinit_memslots(qdev); 129d84300bfSDave Airlie qxl_ring_init_hdr(qdev->release_ring); 130d84300bfSDave Airlie } 131d84300bfSDave Airlie 132d84300bfSDave Airlie qxl_create_monitors_object(qdev); 133d84300bfSDave Airlie drm_helper_resume_force_mode(dev); 134d84300bfSDave Airlie 135d84300bfSDave Airlie console_lock(); 136d84300bfSDave Airlie qxl_fbdev_set_suspend(qdev, 0); 137d84300bfSDave Airlie console_unlock(); 138d84300bfSDave Airlie 139d84300bfSDave Airlie drm_kms_helper_poll_enable(dev); 140d84300bfSDave Airlie return 0; 141d84300bfSDave Airlie } 142d84300bfSDave Airlie 143d84300bfSDave Airlie static int qxl_pm_suspend(struct device *dev) 144d84300bfSDave Airlie { 145d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 146d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 147d84300bfSDave Airlie int error; 148d84300bfSDave Airlie 149d84300bfSDave Airlie error = qxl_drm_freeze(drm_dev); 150d84300bfSDave Airlie if (error) 151d84300bfSDave Airlie return error; 152d84300bfSDave Airlie 153d84300bfSDave Airlie pci_disable_device(pdev); 154d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D3hot); 155d84300bfSDave Airlie return 0; 156d84300bfSDave Airlie } 157d84300bfSDave Airlie 158d84300bfSDave Airlie static int qxl_pm_resume(struct device *dev) 159d84300bfSDave Airlie { 160d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 161d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 162d84300bfSDave Airlie 163d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D0); 164d84300bfSDave Airlie pci_restore_state(pdev); 165d84300bfSDave Airlie if (pci_enable_device(pdev)) { 166d84300bfSDave Airlie return -EIO; 167d84300bfSDave Airlie } 168d84300bfSDave Airlie 169d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 170d84300bfSDave Airlie } 171d84300bfSDave Airlie 172d84300bfSDave Airlie static int qxl_pm_thaw(struct device *dev) 173d84300bfSDave Airlie { 174d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 175d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 176d84300bfSDave Airlie 177d84300bfSDave Airlie return qxl_drm_resume(drm_dev, true); 178d84300bfSDave Airlie } 179d84300bfSDave Airlie 180d84300bfSDave Airlie static int qxl_pm_freeze(struct device *dev) 181d84300bfSDave Airlie { 182d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 183d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 184d84300bfSDave Airlie 185d84300bfSDave Airlie return qxl_drm_freeze(drm_dev); 186d84300bfSDave Airlie } 187d84300bfSDave Airlie 188d84300bfSDave Airlie static int qxl_pm_restore(struct device *dev) 189d84300bfSDave Airlie { 190d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 191d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 192d84300bfSDave Airlie struct qxl_device *qdev = drm_dev->dev_private; 193d84300bfSDave Airlie 194d84300bfSDave Airlie qxl_io_reset(qdev); 195d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 196d84300bfSDave Airlie } 197d84300bfSDave Airlie 198d84300bfSDave Airlie static const struct dev_pm_ops qxl_pm_ops = { 199d84300bfSDave Airlie .suspend = qxl_pm_suspend, 200d84300bfSDave Airlie .resume = qxl_pm_resume, 201d84300bfSDave Airlie .freeze = qxl_pm_freeze, 202d84300bfSDave Airlie .thaw = qxl_pm_thaw, 203d84300bfSDave Airlie .poweroff = qxl_pm_freeze, 204d84300bfSDave Airlie .restore = qxl_pm_restore, 205d84300bfSDave Airlie }; 206d84300bfSDave Airlie static struct pci_driver qxl_pci_driver = { 207d84300bfSDave Airlie .name = DRIVER_NAME, 208d84300bfSDave Airlie .id_table = pciidlist, 209d84300bfSDave Airlie .probe = qxl_pci_probe, 210d84300bfSDave Airlie .remove = qxl_pci_remove, 211d84300bfSDave Airlie .driver.pm = &qxl_pm_ops, 212d84300bfSDave Airlie }; 213d84300bfSDave Airlie 214f64122c1SDave Airlie static struct drm_driver qxl_driver = { 215f64122c1SDave Airlie .driver_features = DRIVER_GEM | DRIVER_MODESET | 216f64122c1SDave Airlie DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED, 217f64122c1SDave Airlie .load = qxl_driver_load, 218f64122c1SDave Airlie .unload = qxl_driver_unload, 219f64122c1SDave Airlie 220f64122c1SDave Airlie .dumb_create = qxl_mode_dumb_create, 221f64122c1SDave Airlie .dumb_map_offset = qxl_mode_dumb_mmap, 22243387b37SDaniel Vetter .dumb_destroy = drm_gem_dumb_destroy, 223f64122c1SDave Airlie #if defined(CONFIG_DEBUG_FS) 224f64122c1SDave Airlie .debugfs_init = qxl_debugfs_init, 225f64122c1SDave Airlie .debugfs_cleanup = qxl_debugfs_takedown, 226f64122c1SDave Airlie #endif 227f64122c1SDave Airlie .gem_free_object = qxl_gem_object_free, 228f64122c1SDave Airlie .gem_open_object = qxl_gem_object_open, 229f64122c1SDave Airlie .gem_close_object = qxl_gem_object_close, 230f64122c1SDave Airlie .fops = &qxl_fops, 231f64122c1SDave Airlie .ioctls = qxl_ioctls, 232f64122c1SDave Airlie .irq_handler = qxl_irq_handler, 233f64122c1SDave Airlie .name = DRIVER_NAME, 234f64122c1SDave Airlie .desc = DRIVER_DESC, 235f64122c1SDave Airlie .date = DRIVER_DATE, 236f64122c1SDave Airlie .major = 0, 237f64122c1SDave Airlie .minor = 1, 238f64122c1SDave Airlie .patchlevel = 0, 239f64122c1SDave Airlie }; 240f64122c1SDave Airlie 241f64122c1SDave Airlie static int __init qxl_init(void) 242f64122c1SDave Airlie { 243f64122c1SDave Airlie #ifdef CONFIG_VGA_CONSOLE 244f64122c1SDave Airlie if (vgacon_text_force() && qxl_modeset == -1) 245f64122c1SDave Airlie return -EINVAL; 246f64122c1SDave Airlie #endif 247f64122c1SDave Airlie 248f64122c1SDave Airlie if (qxl_modeset == 0) 249f64122c1SDave Airlie return -EINVAL; 250f64122c1SDave Airlie qxl_driver.num_ioctls = qxl_max_ioctls; 251f64122c1SDave Airlie return drm_pci_init(&qxl_driver, &qxl_pci_driver); 252f64122c1SDave Airlie } 253f64122c1SDave Airlie 254f64122c1SDave Airlie static void __exit qxl_exit(void) 255f64122c1SDave Airlie { 256f64122c1SDave Airlie drm_pci_exit(&qxl_driver, &qxl_pci_driver); 257f64122c1SDave Airlie } 258f64122c1SDave Airlie 259f64122c1SDave Airlie module_init(qxl_init); 260f64122c1SDave Airlie module_exit(qxl_exit); 261f64122c1SDave Airlie 262f64122c1SDave Airlie MODULE_AUTHOR(DRIVER_AUTHOR); 263f64122c1SDave Airlie MODULE_DESCRIPTION(DRIVER_DESC); 264f64122c1SDave Airlie MODULE_LICENSE("GPL and additional rights"); 265