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; 419baa3c34SBenoit 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, 87058e9f5cSAndreas Pokorny .read = drm_read, 88f64122c1SDave Airlie .mmap = qxl_mmap, 89f64122c1SDave Airlie }; 90f64122c1SDave Airlie 91d84300bfSDave Airlie static int qxl_drm_freeze(struct drm_device *dev) 92d84300bfSDave Airlie { 93d84300bfSDave Airlie struct pci_dev *pdev = dev->pdev; 94d84300bfSDave Airlie struct qxl_device *qdev = dev->dev_private; 95d84300bfSDave Airlie struct drm_crtc *crtc; 96d84300bfSDave Airlie 97d84300bfSDave Airlie drm_kms_helper_poll_disable(dev); 98d84300bfSDave Airlie 99d84300bfSDave Airlie console_lock(); 100d84300bfSDave Airlie qxl_fbdev_set_suspend(qdev, 1); 101d84300bfSDave Airlie console_unlock(); 102d84300bfSDave Airlie 103d84300bfSDave Airlie /* unpin the front buffers */ 104d84300bfSDave Airlie list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 10571279a11SJani Nikula const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 106d84300bfSDave Airlie if (crtc->enabled) 107d84300bfSDave Airlie (*crtc_funcs->disable)(crtc); 108d84300bfSDave Airlie } 109d84300bfSDave Airlie 110d84300bfSDave Airlie qxl_destroy_monitors_object(qdev); 111d84300bfSDave Airlie qxl_surf_evict(qdev); 112d84300bfSDave Airlie qxl_vram_evict(qdev); 113d84300bfSDave Airlie 114d84300bfSDave Airlie while (!qxl_check_idle(qdev->command_ring)); 115d84300bfSDave Airlie while (!qxl_check_idle(qdev->release_ring)) 116d84300bfSDave Airlie qxl_queue_garbage_collect(qdev, 1); 117d84300bfSDave Airlie 118d84300bfSDave Airlie pci_save_state(pdev); 119d84300bfSDave Airlie 120d84300bfSDave Airlie return 0; 121d84300bfSDave Airlie } 122d84300bfSDave Airlie 123d84300bfSDave Airlie static int qxl_drm_resume(struct drm_device *dev, bool thaw) 124d84300bfSDave Airlie { 125d84300bfSDave Airlie struct qxl_device *qdev = dev->dev_private; 126d84300bfSDave Airlie 127d84300bfSDave Airlie qdev->ram_header->int_mask = QXL_INTERRUPT_MASK; 128d84300bfSDave Airlie if (!thaw) { 129d84300bfSDave Airlie qxl_reinit_memslots(qdev); 130d84300bfSDave Airlie qxl_ring_init_hdr(qdev->release_ring); 131d84300bfSDave Airlie } 132d84300bfSDave Airlie 133d84300bfSDave Airlie qxl_create_monitors_object(qdev); 134d84300bfSDave Airlie drm_helper_resume_force_mode(dev); 135d84300bfSDave Airlie 136d84300bfSDave Airlie console_lock(); 137d84300bfSDave Airlie qxl_fbdev_set_suspend(qdev, 0); 138d84300bfSDave Airlie console_unlock(); 139d84300bfSDave Airlie 140d84300bfSDave Airlie drm_kms_helper_poll_enable(dev); 141d84300bfSDave Airlie return 0; 142d84300bfSDave Airlie } 143d84300bfSDave Airlie 144d84300bfSDave Airlie static int qxl_pm_suspend(struct device *dev) 145d84300bfSDave Airlie { 146d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 147d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 148d84300bfSDave Airlie int error; 149d84300bfSDave Airlie 150d84300bfSDave Airlie error = qxl_drm_freeze(drm_dev); 151d84300bfSDave Airlie if (error) 152d84300bfSDave Airlie return error; 153d84300bfSDave Airlie 154d84300bfSDave Airlie pci_disable_device(pdev); 155d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D3hot); 156d84300bfSDave Airlie return 0; 157d84300bfSDave Airlie } 158d84300bfSDave Airlie 159d84300bfSDave Airlie static int qxl_pm_resume(struct device *dev) 160d84300bfSDave Airlie { 161d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 162d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 163d84300bfSDave Airlie 164d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D0); 165d84300bfSDave Airlie pci_restore_state(pdev); 166d84300bfSDave Airlie if (pci_enable_device(pdev)) { 167d84300bfSDave Airlie return -EIO; 168d84300bfSDave Airlie } 169d84300bfSDave Airlie 170d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 171d84300bfSDave Airlie } 172d84300bfSDave Airlie 173d84300bfSDave Airlie static int qxl_pm_thaw(struct device *dev) 174d84300bfSDave Airlie { 175d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 176d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 177d84300bfSDave Airlie 178d84300bfSDave Airlie return qxl_drm_resume(drm_dev, true); 179d84300bfSDave Airlie } 180d84300bfSDave Airlie 181d84300bfSDave Airlie static int qxl_pm_freeze(struct device *dev) 182d84300bfSDave Airlie { 183d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 184d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 185d84300bfSDave Airlie 186d84300bfSDave Airlie return qxl_drm_freeze(drm_dev); 187d84300bfSDave Airlie } 188d84300bfSDave Airlie 189d84300bfSDave Airlie static int qxl_pm_restore(struct device *dev) 190d84300bfSDave Airlie { 191d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 192d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 193d84300bfSDave Airlie struct qxl_device *qdev = drm_dev->dev_private; 194d84300bfSDave Airlie 195d84300bfSDave Airlie qxl_io_reset(qdev); 196d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 197d84300bfSDave Airlie } 198d84300bfSDave Airlie 199*88e72717SThierry Reding static u32 qxl_noop_get_vblank_counter(struct drm_device *dev, 200*88e72717SThierry Reding unsigned int pipe) 201058e9f5cSAndreas Pokorny { 202337eb43cSMario Kleiner return 0; 203058e9f5cSAndreas Pokorny } 204058e9f5cSAndreas Pokorny 205*88e72717SThierry Reding static int qxl_noop_enable_vblank(struct drm_device *dev, unsigned int pipe) 206058e9f5cSAndreas Pokorny { 207058e9f5cSAndreas Pokorny return 0; 208058e9f5cSAndreas Pokorny } 209058e9f5cSAndreas Pokorny 210*88e72717SThierry Reding static void qxl_noop_disable_vblank(struct drm_device *dev, unsigned int pipe) 211058e9f5cSAndreas Pokorny { 212058e9f5cSAndreas Pokorny } 213058e9f5cSAndreas Pokorny 214d84300bfSDave Airlie static const struct dev_pm_ops qxl_pm_ops = { 215d84300bfSDave Airlie .suspend = qxl_pm_suspend, 216d84300bfSDave Airlie .resume = qxl_pm_resume, 217d84300bfSDave Airlie .freeze = qxl_pm_freeze, 218d84300bfSDave Airlie .thaw = qxl_pm_thaw, 219d84300bfSDave Airlie .poweroff = qxl_pm_freeze, 220d84300bfSDave Airlie .restore = qxl_pm_restore, 221d84300bfSDave Airlie }; 222d84300bfSDave Airlie static struct pci_driver qxl_pci_driver = { 223d84300bfSDave Airlie .name = DRIVER_NAME, 224d84300bfSDave Airlie .id_table = pciidlist, 225d84300bfSDave Airlie .probe = qxl_pci_probe, 226d84300bfSDave Airlie .remove = qxl_pci_remove, 227d84300bfSDave Airlie .driver.pm = &qxl_pm_ops, 228d84300bfSDave Airlie }; 229d84300bfSDave Airlie 230f64122c1SDave Airlie static struct drm_driver qxl_driver = { 23147c12968SAndreas Pokorny .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | 232f64122c1SDave Airlie DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED, 233f64122c1SDave Airlie .load = qxl_driver_load, 234f64122c1SDave Airlie .unload = qxl_driver_unload, 235058e9f5cSAndreas Pokorny .get_vblank_counter = qxl_noop_get_vblank_counter, 236058e9f5cSAndreas Pokorny .enable_vblank = qxl_noop_enable_vblank, 237058e9f5cSAndreas Pokorny .disable_vblank = qxl_noop_disable_vblank, 238f64122c1SDave Airlie 239915b4d11SDavid Herrmann .set_busid = drm_pci_set_busid, 240915b4d11SDavid Herrmann 241f64122c1SDave Airlie .dumb_create = qxl_mode_dumb_create, 242f64122c1SDave Airlie .dumb_map_offset = qxl_mode_dumb_mmap, 24343387b37SDaniel Vetter .dumb_destroy = drm_gem_dumb_destroy, 244f64122c1SDave Airlie #if defined(CONFIG_DEBUG_FS) 245f64122c1SDave Airlie .debugfs_init = qxl_debugfs_init, 246f64122c1SDave Airlie .debugfs_cleanup = qxl_debugfs_takedown, 247f64122c1SDave Airlie #endif 24847c12968SAndreas Pokorny .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 24947c12968SAndreas Pokorny .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 25047c12968SAndreas Pokorny .gem_prime_export = drm_gem_prime_export, 25147c12968SAndreas Pokorny .gem_prime_import = drm_gem_prime_import, 25247c12968SAndreas Pokorny .gem_prime_pin = qxl_gem_prime_pin, 25347c12968SAndreas Pokorny .gem_prime_unpin = qxl_gem_prime_unpin, 25447c12968SAndreas Pokorny .gem_prime_get_sg_table = qxl_gem_prime_get_sg_table, 25547c12968SAndreas Pokorny .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table, 25647c12968SAndreas Pokorny .gem_prime_vmap = qxl_gem_prime_vmap, 25747c12968SAndreas Pokorny .gem_prime_vunmap = qxl_gem_prime_vunmap, 25847c12968SAndreas Pokorny .gem_prime_mmap = qxl_gem_prime_mmap, 259f64122c1SDave Airlie .gem_free_object = qxl_gem_object_free, 260f64122c1SDave Airlie .gem_open_object = qxl_gem_object_open, 261f64122c1SDave Airlie .gem_close_object = qxl_gem_object_close, 262f64122c1SDave Airlie .fops = &qxl_fops, 263f64122c1SDave Airlie .ioctls = qxl_ioctls, 264f64122c1SDave Airlie .irq_handler = qxl_irq_handler, 265f64122c1SDave Airlie .name = DRIVER_NAME, 266f64122c1SDave Airlie .desc = DRIVER_DESC, 267f64122c1SDave Airlie .date = DRIVER_DATE, 268f64122c1SDave Airlie .major = 0, 269f64122c1SDave Airlie .minor = 1, 270f64122c1SDave Airlie .patchlevel = 0, 271f64122c1SDave Airlie }; 272f64122c1SDave Airlie 273f64122c1SDave Airlie static int __init qxl_init(void) 274f64122c1SDave Airlie { 275f64122c1SDave Airlie #ifdef CONFIG_VGA_CONSOLE 276f64122c1SDave Airlie if (vgacon_text_force() && qxl_modeset == -1) 277f64122c1SDave Airlie return -EINVAL; 278f64122c1SDave Airlie #endif 279f64122c1SDave Airlie 280f64122c1SDave Airlie if (qxl_modeset == 0) 281f64122c1SDave Airlie return -EINVAL; 282f64122c1SDave Airlie qxl_driver.num_ioctls = qxl_max_ioctls; 283f64122c1SDave Airlie return drm_pci_init(&qxl_driver, &qxl_pci_driver); 284f64122c1SDave Airlie } 285f64122c1SDave Airlie 286f64122c1SDave Airlie static void __exit qxl_exit(void) 287f64122c1SDave Airlie { 288f64122c1SDave Airlie drm_pci_exit(&qxl_driver, &qxl_pci_driver); 289f64122c1SDave Airlie } 290f64122c1SDave Airlie 291f64122c1SDave Airlie module_init(qxl_init); 292f64122c1SDave Airlie module_exit(qxl_exit); 293f64122c1SDave Airlie 294f64122c1SDave Airlie MODULE_AUTHOR(DRIVER_AUTHOR); 295f64122c1SDave Airlie MODULE_DESCRIPTION(DRIVER_DESC); 296f64122c1SDave Airlie MODULE_LICENSE("GPL and additional rights"); 297