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 { 652b65d567SGabriel Krisman Bertazi struct qxl_device *qdev; 662b65d567SGabriel Krisman Bertazi int ret; 672b65d567SGabriel Krisman Bertazi 68f64122c1SDave Airlie if (pdev->revision < 4) { 69f64122c1SDave Airlie DRM_ERROR("qxl too old, doesn't support client_monitors_config," 70f64122c1SDave Airlie " use xf86-video-qxl in user mode"); 71f64122c1SDave Airlie return -EINVAL; /* TODO: ENODEV ? */ 72f64122c1SDave Airlie } 732b65d567SGabriel Krisman Bertazi 742b65d567SGabriel Krisman Bertazi qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL); 75*cbdded7fSGabriel Krisman Bertazi if (!qdev) 76*cbdded7fSGabriel Krisman Bertazi return -ENOMEM; 772b65d567SGabriel Krisman Bertazi 782b65d567SGabriel Krisman Bertazi ret = pci_enable_device(pdev); 792b65d567SGabriel Krisman Bertazi if (ret) 80*cbdded7fSGabriel Krisman Bertazi goto free_dev; 812b65d567SGabriel Krisman Bertazi 82*cbdded7fSGabriel Krisman Bertazi ret = qxl_device_init(qdev, &qxl_driver, pdev, ent->driver_data); 832b65d567SGabriel Krisman Bertazi if (ret) 842b65d567SGabriel Krisman Bertazi goto disable_pci; 852b65d567SGabriel Krisman Bertazi 86*cbdded7fSGabriel Krisman Bertazi ret = drm_vblank_init(&qdev->ddev, 1); 872b65d567SGabriel Krisman Bertazi if (ret) 882b65d567SGabriel Krisman Bertazi goto unload; 892b65d567SGabriel Krisman Bertazi 902b65d567SGabriel Krisman Bertazi ret = qxl_modeset_init(qdev); 912b65d567SGabriel Krisman Bertazi if (ret) 922b65d567SGabriel Krisman Bertazi goto vblank_cleanup; 932b65d567SGabriel Krisman Bertazi 94*cbdded7fSGabriel Krisman Bertazi drm_kms_helper_poll_init(&qdev->ddev); 952b65d567SGabriel Krisman Bertazi 962b65d567SGabriel Krisman Bertazi /* Complete initialization. */ 97*cbdded7fSGabriel Krisman Bertazi ret = drm_dev_register(&qdev->ddev, ent->driver_data); 982b65d567SGabriel Krisman Bertazi if (ret) 992b65d567SGabriel Krisman Bertazi goto modeset_cleanup; 1002b65d567SGabriel Krisman Bertazi 1012b65d567SGabriel Krisman Bertazi return 0; 1022b65d567SGabriel Krisman Bertazi 1032b65d567SGabriel Krisman Bertazi modeset_cleanup: 1042b65d567SGabriel Krisman Bertazi qxl_modeset_fini(qdev); 1052b65d567SGabriel Krisman Bertazi vblank_cleanup: 106*cbdded7fSGabriel Krisman Bertazi drm_vblank_cleanup(&qdev->ddev); 1072b65d567SGabriel Krisman Bertazi unload: 1082b65d567SGabriel Krisman Bertazi qxl_device_fini(qdev); 1092b65d567SGabriel Krisman Bertazi disable_pci: 1102b65d567SGabriel Krisman Bertazi pci_disable_device(pdev); 111*cbdded7fSGabriel Krisman Bertazi free_dev: 1122b65d567SGabriel Krisman Bertazi kfree(qdev); 1132b65d567SGabriel Krisman Bertazi return ret; 114f64122c1SDave Airlie } 115f64122c1SDave Airlie 116f64122c1SDave Airlie static void 117f64122c1SDave Airlie qxl_pci_remove(struct pci_dev *pdev) 118f64122c1SDave Airlie { 119f64122c1SDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 1206f897f51SGabriel Krisman Bertazi struct qxl_device *qdev = dev->dev_private; 121f64122c1SDave Airlie 1226f897f51SGabriel Krisman Bertazi drm_dev_unregister(dev); 1236f897f51SGabriel Krisman Bertazi 1246f897f51SGabriel Krisman Bertazi qxl_modeset_fini(qdev); 1256f897f51SGabriel Krisman Bertazi qxl_device_fini(qdev); 1266f897f51SGabriel Krisman Bertazi 1276f897f51SGabriel Krisman Bertazi dev->dev_private = NULL; 1286f897f51SGabriel Krisman Bertazi kfree(qdev); 1296f897f51SGabriel Krisman Bertazi drm_dev_unref(dev); 130f64122c1SDave Airlie } 131f64122c1SDave Airlie 132f64122c1SDave Airlie static const struct file_operations qxl_fops = { 133f64122c1SDave Airlie .owner = THIS_MODULE, 134f64122c1SDave Airlie .open = drm_open, 135f64122c1SDave Airlie .release = drm_release, 136f64122c1SDave Airlie .unlocked_ioctl = drm_ioctl, 137f64122c1SDave Airlie .poll = drm_poll, 138058e9f5cSAndreas Pokorny .read = drm_read, 139f64122c1SDave Airlie .mmap = qxl_mmap, 140f64122c1SDave Airlie }; 141f64122c1SDave Airlie 142d84300bfSDave Airlie static int qxl_drm_freeze(struct drm_device *dev) 143d84300bfSDave Airlie { 144d84300bfSDave Airlie struct pci_dev *pdev = dev->pdev; 145d84300bfSDave Airlie struct qxl_device *qdev = dev->dev_private; 146d84300bfSDave Airlie struct drm_crtc *crtc; 147d84300bfSDave Airlie 148d84300bfSDave Airlie drm_kms_helper_poll_disable(dev); 149d84300bfSDave Airlie 150d84300bfSDave Airlie console_lock(); 151d84300bfSDave Airlie qxl_fbdev_set_suspend(qdev, 1); 152d84300bfSDave Airlie console_unlock(); 153d84300bfSDave Airlie 154d84300bfSDave Airlie /* unpin the front buffers */ 155d84300bfSDave Airlie list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 15671279a11SJani Nikula const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 157d84300bfSDave Airlie if (crtc->enabled) 158d84300bfSDave Airlie (*crtc_funcs->disable)(crtc); 159d84300bfSDave Airlie } 160d84300bfSDave Airlie 161d84300bfSDave Airlie qxl_destroy_monitors_object(qdev); 162d84300bfSDave Airlie qxl_surf_evict(qdev); 163d84300bfSDave Airlie qxl_vram_evict(qdev); 164d84300bfSDave Airlie 165d84300bfSDave Airlie while (!qxl_check_idle(qdev->command_ring)); 166d84300bfSDave Airlie while (!qxl_check_idle(qdev->release_ring)) 167d84300bfSDave Airlie qxl_queue_garbage_collect(qdev, 1); 168d84300bfSDave Airlie 169d84300bfSDave Airlie pci_save_state(pdev); 170d84300bfSDave Airlie 171d84300bfSDave Airlie return 0; 172d84300bfSDave Airlie } 173d84300bfSDave Airlie 174d84300bfSDave Airlie static int qxl_drm_resume(struct drm_device *dev, bool thaw) 175d84300bfSDave Airlie { 176d84300bfSDave Airlie struct qxl_device *qdev = dev->dev_private; 177d84300bfSDave Airlie 178d84300bfSDave Airlie qdev->ram_header->int_mask = QXL_INTERRUPT_MASK; 179d84300bfSDave Airlie if (!thaw) { 180d84300bfSDave Airlie qxl_reinit_memslots(qdev); 181d84300bfSDave Airlie qxl_ring_init_hdr(qdev->release_ring); 182d84300bfSDave Airlie } 183d84300bfSDave Airlie 184d84300bfSDave Airlie qxl_create_monitors_object(qdev); 185d84300bfSDave Airlie drm_helper_resume_force_mode(dev); 186d84300bfSDave Airlie 187d84300bfSDave Airlie console_lock(); 188d84300bfSDave Airlie qxl_fbdev_set_suspend(qdev, 0); 189d84300bfSDave Airlie console_unlock(); 190d84300bfSDave Airlie 191d84300bfSDave Airlie drm_kms_helper_poll_enable(dev); 192d84300bfSDave Airlie return 0; 193d84300bfSDave Airlie } 194d84300bfSDave Airlie 195d84300bfSDave Airlie static int qxl_pm_suspend(struct device *dev) 196d84300bfSDave Airlie { 197d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 198d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 199d84300bfSDave Airlie int error; 200d84300bfSDave Airlie 201d84300bfSDave Airlie error = qxl_drm_freeze(drm_dev); 202d84300bfSDave Airlie if (error) 203d84300bfSDave Airlie return error; 204d84300bfSDave Airlie 205d84300bfSDave Airlie pci_disable_device(pdev); 206d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D3hot); 207d84300bfSDave Airlie return 0; 208d84300bfSDave Airlie } 209d84300bfSDave Airlie 210d84300bfSDave Airlie static int qxl_pm_resume(struct device *dev) 211d84300bfSDave Airlie { 212d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 213d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 214d84300bfSDave Airlie 215d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D0); 216d84300bfSDave Airlie pci_restore_state(pdev); 217d84300bfSDave Airlie if (pci_enable_device(pdev)) { 218d84300bfSDave Airlie return -EIO; 219d84300bfSDave Airlie } 220d84300bfSDave Airlie 221d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 222d84300bfSDave Airlie } 223d84300bfSDave Airlie 224d84300bfSDave Airlie static int qxl_pm_thaw(struct device *dev) 225d84300bfSDave Airlie { 226d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 227d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 228d84300bfSDave Airlie 229d84300bfSDave Airlie return qxl_drm_resume(drm_dev, true); 230d84300bfSDave Airlie } 231d84300bfSDave Airlie 232d84300bfSDave Airlie static int qxl_pm_freeze(struct device *dev) 233d84300bfSDave Airlie { 234d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 235d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 236d84300bfSDave Airlie 237d84300bfSDave Airlie return qxl_drm_freeze(drm_dev); 238d84300bfSDave Airlie } 239d84300bfSDave Airlie 240d84300bfSDave Airlie static int qxl_pm_restore(struct device *dev) 241d84300bfSDave Airlie { 242d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 243d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 244d84300bfSDave Airlie struct qxl_device *qdev = drm_dev->dev_private; 245d84300bfSDave Airlie 246d84300bfSDave Airlie qxl_io_reset(qdev); 247d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 248d84300bfSDave Airlie } 249d84300bfSDave Airlie 25088e72717SThierry Reding static u32 qxl_noop_get_vblank_counter(struct drm_device *dev, 25188e72717SThierry Reding unsigned int pipe) 252058e9f5cSAndreas Pokorny { 253337eb43cSMario Kleiner return 0; 254058e9f5cSAndreas Pokorny } 255058e9f5cSAndreas Pokorny 25688e72717SThierry Reding static int qxl_noop_enable_vblank(struct drm_device *dev, unsigned int pipe) 257058e9f5cSAndreas Pokorny { 258058e9f5cSAndreas Pokorny return 0; 259058e9f5cSAndreas Pokorny } 260058e9f5cSAndreas Pokorny 26188e72717SThierry Reding static void qxl_noop_disable_vblank(struct drm_device *dev, unsigned int pipe) 262058e9f5cSAndreas Pokorny { 263058e9f5cSAndreas Pokorny } 264058e9f5cSAndreas Pokorny 265d84300bfSDave Airlie static const struct dev_pm_ops qxl_pm_ops = { 266d84300bfSDave Airlie .suspend = qxl_pm_suspend, 267d84300bfSDave Airlie .resume = qxl_pm_resume, 268d84300bfSDave Airlie .freeze = qxl_pm_freeze, 269d84300bfSDave Airlie .thaw = qxl_pm_thaw, 270d84300bfSDave Airlie .poweroff = qxl_pm_freeze, 271d84300bfSDave Airlie .restore = qxl_pm_restore, 272d84300bfSDave Airlie }; 273d84300bfSDave Airlie static struct pci_driver qxl_pci_driver = { 274d84300bfSDave Airlie .name = DRIVER_NAME, 275d84300bfSDave Airlie .id_table = pciidlist, 276d84300bfSDave Airlie .probe = qxl_pci_probe, 277d84300bfSDave Airlie .remove = qxl_pci_remove, 278d84300bfSDave Airlie .driver.pm = &qxl_pm_ops, 279d84300bfSDave Airlie }; 280d84300bfSDave Airlie 281f64122c1SDave Airlie static struct drm_driver qxl_driver = { 28247c12968SAndreas Pokorny .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | 283f64122c1SDave Airlie DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED, 284058e9f5cSAndreas Pokorny .get_vblank_counter = qxl_noop_get_vblank_counter, 285058e9f5cSAndreas Pokorny .enable_vblank = qxl_noop_enable_vblank, 286058e9f5cSAndreas Pokorny .disable_vblank = qxl_noop_disable_vblank, 287f64122c1SDave Airlie 288915b4d11SDavid Herrmann .set_busid = drm_pci_set_busid, 289915b4d11SDavid Herrmann 290f64122c1SDave Airlie .dumb_create = qxl_mode_dumb_create, 291f64122c1SDave Airlie .dumb_map_offset = qxl_mode_dumb_mmap, 29243387b37SDaniel Vetter .dumb_destroy = drm_gem_dumb_destroy, 293f64122c1SDave Airlie #if defined(CONFIG_DEBUG_FS) 294f64122c1SDave Airlie .debugfs_init = qxl_debugfs_init, 295f64122c1SDave Airlie .debugfs_cleanup = qxl_debugfs_takedown, 296f64122c1SDave Airlie #endif 29747c12968SAndreas Pokorny .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 29847c12968SAndreas Pokorny .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 29947c12968SAndreas Pokorny .gem_prime_export = drm_gem_prime_export, 30047c12968SAndreas Pokorny .gem_prime_import = drm_gem_prime_import, 30147c12968SAndreas Pokorny .gem_prime_pin = qxl_gem_prime_pin, 30247c12968SAndreas Pokorny .gem_prime_unpin = qxl_gem_prime_unpin, 30347c12968SAndreas Pokorny .gem_prime_get_sg_table = qxl_gem_prime_get_sg_table, 30447c12968SAndreas Pokorny .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table, 30547c12968SAndreas Pokorny .gem_prime_vmap = qxl_gem_prime_vmap, 30647c12968SAndreas Pokorny .gem_prime_vunmap = qxl_gem_prime_vunmap, 30747c12968SAndreas Pokorny .gem_prime_mmap = qxl_gem_prime_mmap, 3089f4eebd7SDaniel Vetter .gem_free_object_unlocked = qxl_gem_object_free, 309f64122c1SDave Airlie .gem_open_object = qxl_gem_object_open, 310f64122c1SDave Airlie .gem_close_object = qxl_gem_object_close, 311f64122c1SDave Airlie .fops = &qxl_fops, 312f64122c1SDave Airlie .ioctls = qxl_ioctls, 313f64122c1SDave Airlie .irq_handler = qxl_irq_handler, 314f64122c1SDave Airlie .name = DRIVER_NAME, 315f64122c1SDave Airlie .desc = DRIVER_DESC, 316f64122c1SDave Airlie .date = DRIVER_DATE, 317f64122c1SDave Airlie .major = 0, 318f64122c1SDave Airlie .minor = 1, 319f64122c1SDave Airlie .patchlevel = 0, 320f64122c1SDave Airlie }; 321f64122c1SDave Airlie 322f64122c1SDave Airlie static int __init qxl_init(void) 323f64122c1SDave Airlie { 324f64122c1SDave Airlie if (vgacon_text_force() && qxl_modeset == -1) 325f64122c1SDave Airlie return -EINVAL; 326f64122c1SDave Airlie 327f64122c1SDave Airlie if (qxl_modeset == 0) 328f64122c1SDave Airlie return -EINVAL; 329f64122c1SDave Airlie qxl_driver.num_ioctls = qxl_max_ioctls; 330f64122c1SDave Airlie return drm_pci_init(&qxl_driver, &qxl_pci_driver); 331f64122c1SDave Airlie } 332f64122c1SDave Airlie 333f64122c1SDave Airlie static void __exit qxl_exit(void) 334f64122c1SDave Airlie { 335f64122c1SDave Airlie drm_pci_exit(&qxl_driver, &qxl_pci_driver); 336f64122c1SDave Airlie } 337f64122c1SDave Airlie 338f64122c1SDave Airlie module_init(qxl_init); 339f64122c1SDave Airlie module_exit(qxl_exit); 340f64122c1SDave Airlie 341f64122c1SDave Airlie MODULE_AUTHOR(DRIVER_AUTHOR); 342f64122c1SDave Airlie MODULE_DESCRIPTION(DRIVER_DESC); 343f64122c1SDave Airlie MODULE_LICENSE("GPL and additional rights"); 344