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 "qxl_drv.h" 32c0f4b75cSSam Ravnborg #include <linux/console.h> 33c0f4b75cSSam Ravnborg #include <linux/module.h> 34c0f4b75cSSam Ravnborg #include <linux/pci.h> 35c0f4b75cSSam Ravnborg 36c0f4b75cSSam Ravnborg #include <drm/drm.h> 3723575e7aSGerd Hoffmann #include <drm/drm_atomic_helper.h> 38c0f4b75cSSam Ravnborg #include <drm/drm_drv.h> 39c0f4b75cSSam Ravnborg #include <drm/drm_file.h> 40*f4268a4bSThomas Zimmermann #include <drm/drm_gem_ttm_helper.h> 41c0f4b75cSSam Ravnborg #include <drm/drm_modeset_helper.h> 42c0f4b75cSSam Ravnborg #include <drm/drm_prime.h> 43c0f4b75cSSam Ravnborg #include <drm/drm_probe_helper.h> 44c0f4b75cSSam Ravnborg 45d84300bfSDave Airlie #include "qxl_object.h" 46f64122c1SDave Airlie 479baa3c34SBenoit Taine static const struct pci_device_id pciidlist[] = { 48f64122c1SDave Airlie { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, 49f64122c1SDave Airlie 0xffff00, 0 }, 50f64122c1SDave Airlie { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8, 51f64122c1SDave Airlie 0xffff00, 0 }, 52f64122c1SDave Airlie { 0, 0, 0 }, 53f64122c1SDave Airlie }; 54f64122c1SDave Airlie MODULE_DEVICE_TABLE(pci, pciidlist); 55f64122c1SDave Airlie 566d01f1f5SDave Airlie static int qxl_modeset = -1; 5707f8d9bdSDave Airlie int qxl_num_crtc = 4; 58f64122c1SDave Airlie 59f64122c1SDave Airlie MODULE_PARM_DESC(modeset, "Disable/Enable modesetting"); 60f64122c1SDave Airlie module_param_named(modeset, qxl_modeset, int, 0400); 61f64122c1SDave Airlie 6207f8d9bdSDave Airlie MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)"); 6307f8d9bdSDave Airlie module_param_named(num_heads, qxl_num_crtc, int, 0400); 6407f8d9bdSDave Airlie 65f64122c1SDave Airlie static struct drm_driver qxl_driver; 66f64122c1SDave Airlie static struct pci_driver qxl_pci_driver; 67f64122c1SDave Airlie 68fbf0a7f4SGerd Hoffmann static bool is_vga(struct pci_dev *pdev) 69fbf0a7f4SGerd Hoffmann { 70fbf0a7f4SGerd Hoffmann return pdev->class == PCI_CLASS_DISPLAY_VGA << 8; 71fbf0a7f4SGerd Hoffmann } 72fbf0a7f4SGerd Hoffmann 73f64122c1SDave Airlie static int 74f64122c1SDave Airlie qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 75f64122c1SDave Airlie { 762b65d567SGabriel Krisman Bertazi struct qxl_device *qdev; 772b65d567SGabriel Krisman Bertazi int ret; 782b65d567SGabriel Krisman Bertazi 79f64122c1SDave Airlie if (pdev->revision < 4) { 80f64122c1SDave Airlie DRM_ERROR("qxl too old, doesn't support client_monitors_config," 81f64122c1SDave Airlie " use xf86-video-qxl in user mode"); 82f64122c1SDave Airlie return -EINVAL; /* TODO: ENODEV ? */ 83f64122c1SDave Airlie } 842b65d567SGabriel Krisman Bertazi 85a9b0b24aSDaniel Vetter qdev = devm_drm_dev_alloc(&pdev->dev, &qxl_driver, 86a9b0b24aSDaniel Vetter struct qxl_device, ddev); 87a9b0b24aSDaniel Vetter if (IS_ERR(qdev)) { 88a9b0b24aSDaniel Vetter pr_err("Unable to init drm dev"); 89cbdded7fSGabriel Krisman Bertazi return -ENOMEM; 90a9b0b24aSDaniel Vetter } 912b65d567SGabriel Krisman Bertazi 922b65d567SGabriel Krisman Bertazi ret = pci_enable_device(pdev); 932b65d567SGabriel Krisman Bertazi if (ret) 94a9b0b24aSDaniel Vetter return ret; 952b65d567SGabriel Krisman Bertazi 9635616a4aSGerd Hoffmann ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "qxl"); 97c34674a2SGerd Hoffmann if (ret) 98c34674a2SGerd Hoffmann goto disable_pci; 99c34674a2SGerd Hoffmann 10066c8ef90SGerd Hoffmann if (is_vga(pdev) && pdev->revision < 5) { 101fbf0a7f4SGerd Hoffmann ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO); 102fbf0a7f4SGerd Hoffmann if (ret) { 103fbf0a7f4SGerd Hoffmann DRM_ERROR("can't get legacy vga ioports\n"); 104fbf0a7f4SGerd Hoffmann goto disable_pci; 105fbf0a7f4SGerd Hoffmann } 106fbf0a7f4SGerd Hoffmann } 107fbf0a7f4SGerd Hoffmann 108a9b0b24aSDaniel Vetter ret = qxl_device_init(qdev, pdev); 1092b65d567SGabriel Krisman Bertazi if (ret) 110fbf0a7f4SGerd Hoffmann goto put_vga; 1112b65d567SGabriel Krisman Bertazi 1122b65d567SGabriel Krisman Bertazi ret = qxl_modeset_init(qdev); 1132b65d567SGabriel Krisman Bertazi if (ret) 11464ca824eSGabriel Krisman Bertazi goto unload; 1152b65d567SGabriel Krisman Bertazi 116cbdded7fSGabriel Krisman Bertazi drm_kms_helper_poll_init(&qdev->ddev); 1172b65d567SGabriel Krisman Bertazi 1182b65d567SGabriel Krisman Bertazi /* Complete initialization. */ 119cbdded7fSGabriel Krisman Bertazi ret = drm_dev_register(&qdev->ddev, ent->driver_data); 1202b65d567SGabriel Krisman Bertazi if (ret) 1212b65d567SGabriel Krisman Bertazi goto modeset_cleanup; 1222b65d567SGabriel Krisman Bertazi 12326d4707dSGerd Hoffmann drm_fbdev_generic_setup(&qdev->ddev, 32); 1242b65d567SGabriel Krisman Bertazi return 0; 1252b65d567SGabriel Krisman Bertazi 1262b65d567SGabriel Krisman Bertazi modeset_cleanup: 1272b65d567SGabriel Krisman Bertazi qxl_modeset_fini(qdev); 1282b65d567SGabriel Krisman Bertazi unload: 1292b65d567SGabriel Krisman Bertazi qxl_device_fini(qdev); 130fbf0a7f4SGerd Hoffmann put_vga: 13166c8ef90SGerd Hoffmann if (is_vga(pdev) && pdev->revision < 5) 132fbf0a7f4SGerd Hoffmann vga_put(pdev, VGA_RSRC_LEGACY_IO); 1332b65d567SGabriel Krisman Bertazi disable_pci: 1342b65d567SGabriel Krisman Bertazi pci_disable_device(pdev); 135a9b0b24aSDaniel Vetter 1362b65d567SGabriel Krisman Bertazi return ret; 137f64122c1SDave Airlie } 138f64122c1SDave Airlie 13923575e7aSGerd Hoffmann static void qxl_drm_release(struct drm_device *dev) 14023575e7aSGerd Hoffmann { 141e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev); 14223575e7aSGerd Hoffmann 14323575e7aSGerd Hoffmann /* 14423575e7aSGerd Hoffmann * TODO: qxl_device_fini() call should be in qxl_pci_remove(), 14587525610SBjorn Helgaas * reordering qxl_modeset_fini() + qxl_device_fini() calls is 14623575e7aSGerd Hoffmann * non-trivial though. 14723575e7aSGerd Hoffmann */ 14823575e7aSGerd Hoffmann qxl_modeset_fini(qdev); 14923575e7aSGerd Hoffmann qxl_device_fini(qdev); 15023575e7aSGerd Hoffmann } 15123575e7aSGerd Hoffmann 152f64122c1SDave Airlie static void 153f64122c1SDave Airlie qxl_pci_remove(struct pci_dev *pdev) 154f64122c1SDave Airlie { 155f64122c1SDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 156f64122c1SDave Airlie 1576f897f51SGabriel Krisman Bertazi drm_dev_unregister(dev); 15823575e7aSGerd Hoffmann drm_atomic_helper_shutdown(dev); 15966c8ef90SGerd Hoffmann if (is_vga(pdev) && pdev->revision < 5) 160fbf0a7f4SGerd Hoffmann vga_put(pdev, VGA_RSRC_LEGACY_IO); 161f64122c1SDave Airlie } 162f64122c1SDave Airlie 163db262087SGerd Hoffmann DEFINE_DRM_GEM_FOPS(qxl_fops); 164f64122c1SDave Airlie 165d84300bfSDave Airlie static int qxl_drm_freeze(struct drm_device *dev) 166d84300bfSDave Airlie { 167dc3629abSThomas Zimmermann struct pci_dev *pdev = to_pci_dev(dev->dev); 168e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev); 1697948a2b1SPeter Wu int ret; 170d84300bfSDave Airlie 1717948a2b1SPeter Wu ret = drm_mode_config_helper_suspend(dev); 1727948a2b1SPeter Wu if (ret) 1737948a2b1SPeter Wu return ret; 174d84300bfSDave Airlie 175d84300bfSDave Airlie qxl_destroy_monitors_object(qdev); 176d84300bfSDave Airlie qxl_surf_evict(qdev); 177d84300bfSDave Airlie qxl_vram_evict(qdev); 178d84300bfSDave Airlie 179d84300bfSDave Airlie while (!qxl_check_idle(qdev->command_ring)); 180d84300bfSDave Airlie while (!qxl_check_idle(qdev->release_ring)) 181d84300bfSDave Airlie qxl_queue_garbage_collect(qdev, 1); 182d84300bfSDave Airlie 183d84300bfSDave Airlie pci_save_state(pdev); 184d84300bfSDave Airlie 185d84300bfSDave Airlie return 0; 186d84300bfSDave Airlie } 187d84300bfSDave Airlie 188d84300bfSDave Airlie static int qxl_drm_resume(struct drm_device *dev, bool thaw) 189d84300bfSDave Airlie { 190e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev); 191d84300bfSDave Airlie 192d84300bfSDave Airlie qdev->ram_header->int_mask = QXL_INTERRUPT_MASK; 193d84300bfSDave Airlie if (!thaw) { 194d84300bfSDave Airlie qxl_reinit_memslots(qdev); 195d84300bfSDave Airlie qxl_ring_init_hdr(qdev->release_ring); 196d84300bfSDave Airlie } 197d84300bfSDave Airlie 198d84300bfSDave Airlie qxl_create_monitors_object(qdev); 1997948a2b1SPeter Wu return drm_mode_config_helper_resume(dev); 200d84300bfSDave Airlie } 201d84300bfSDave Airlie 202d84300bfSDave Airlie static int qxl_pm_suspend(struct device *dev) 203d84300bfSDave Airlie { 204d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 205d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 206d84300bfSDave Airlie int error; 207d84300bfSDave Airlie 208d84300bfSDave Airlie error = qxl_drm_freeze(drm_dev); 209d84300bfSDave Airlie if (error) 210d84300bfSDave Airlie return error; 211d84300bfSDave Airlie 212d84300bfSDave Airlie pci_disable_device(pdev); 213d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D3hot); 214d84300bfSDave Airlie return 0; 215d84300bfSDave Airlie } 216d84300bfSDave Airlie 217d84300bfSDave Airlie static int qxl_pm_resume(struct device *dev) 218d84300bfSDave Airlie { 219d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 220d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 221d84300bfSDave Airlie 222d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D0); 223d84300bfSDave Airlie pci_restore_state(pdev); 224d84300bfSDave Airlie if (pci_enable_device(pdev)) { 225d84300bfSDave Airlie return -EIO; 226d84300bfSDave Airlie } 227d84300bfSDave Airlie 228d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 229d84300bfSDave Airlie } 230d84300bfSDave Airlie 231d84300bfSDave Airlie static int qxl_pm_thaw(struct device *dev) 232d84300bfSDave Airlie { 233f6d2b63aSChuhong Yuan struct drm_device *drm_dev = dev_get_drvdata(dev); 234d84300bfSDave Airlie 235d84300bfSDave Airlie return qxl_drm_resume(drm_dev, true); 236d84300bfSDave Airlie } 237d84300bfSDave Airlie 238d84300bfSDave Airlie static int qxl_pm_freeze(struct device *dev) 239d84300bfSDave Airlie { 240f6d2b63aSChuhong Yuan struct drm_device *drm_dev = dev_get_drvdata(dev); 241d84300bfSDave Airlie 242d84300bfSDave Airlie return qxl_drm_freeze(drm_dev); 243d84300bfSDave Airlie } 244d84300bfSDave Airlie 245d84300bfSDave Airlie static int qxl_pm_restore(struct device *dev) 246d84300bfSDave Airlie { 247d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 248d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 249e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(drm_dev); 250d84300bfSDave Airlie 251d84300bfSDave Airlie qxl_io_reset(qdev); 252d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 253d84300bfSDave Airlie } 254d84300bfSDave Airlie 255d84300bfSDave Airlie static const struct dev_pm_ops qxl_pm_ops = { 256d84300bfSDave Airlie .suspend = qxl_pm_suspend, 257d84300bfSDave Airlie .resume = qxl_pm_resume, 258d84300bfSDave Airlie .freeze = qxl_pm_freeze, 259d84300bfSDave Airlie .thaw = qxl_pm_thaw, 260d84300bfSDave Airlie .poweroff = qxl_pm_freeze, 261d84300bfSDave Airlie .restore = qxl_pm_restore, 262d84300bfSDave Airlie }; 263d84300bfSDave Airlie static struct pci_driver qxl_pci_driver = { 264d84300bfSDave Airlie .name = DRIVER_NAME, 265d84300bfSDave Airlie .id_table = pciidlist, 266d84300bfSDave Airlie .probe = qxl_pci_probe, 267d84300bfSDave Airlie .remove = qxl_pci_remove, 268d84300bfSDave Airlie .driver.pm = &qxl_pm_ops, 269d84300bfSDave Airlie }; 270d84300bfSDave Airlie 271f64122c1SDave Airlie static struct drm_driver qxl_driver = { 2720424fdafSDaniel Vetter .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 273f64122c1SDave Airlie 274f64122c1SDave Airlie .dumb_create = qxl_mode_dumb_create, 275*f4268a4bSThomas Zimmermann .dumb_map_offset = drm_gem_ttm_dumb_map_offset, 276f64122c1SDave Airlie #if defined(CONFIG_DEBUG_FS) 277f64122c1SDave Airlie .debugfs_init = qxl_debugfs_init, 278f64122c1SDave Airlie #endif 279ab042b82SGerd Hoffmann .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 280ab042b82SGerd Hoffmann .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 281ab042b82SGerd Hoffmann .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table, 28247c12968SAndreas Pokorny .gem_prime_mmap = qxl_gem_prime_mmap, 283f64122c1SDave Airlie .fops = &qxl_fops, 284f64122c1SDave Airlie .ioctls = qxl_ioctls, 285f64122c1SDave Airlie .irq_handler = qxl_irq_handler, 286f64122c1SDave Airlie .name = DRIVER_NAME, 287f64122c1SDave Airlie .desc = DRIVER_DESC, 288f64122c1SDave Airlie .date = DRIVER_DATE, 289f64122c1SDave Airlie .major = 0, 290f64122c1SDave Airlie .minor = 1, 291f64122c1SDave Airlie .patchlevel = 0, 29223575e7aSGerd Hoffmann 29323575e7aSGerd Hoffmann .release = qxl_drm_release, 294f64122c1SDave Airlie }; 295f64122c1SDave Airlie 296f64122c1SDave Airlie static int __init qxl_init(void) 297f64122c1SDave Airlie { 298f64122c1SDave Airlie if (vgacon_text_force() && qxl_modeset == -1) 299f64122c1SDave Airlie return -EINVAL; 300f64122c1SDave Airlie 301f64122c1SDave Airlie if (qxl_modeset == 0) 302f64122c1SDave Airlie return -EINVAL; 303f64122c1SDave Airlie qxl_driver.num_ioctls = qxl_max_ioctls; 30410631d72SDaniel Vetter return pci_register_driver(&qxl_pci_driver); 305f64122c1SDave Airlie } 306f64122c1SDave Airlie 307f64122c1SDave Airlie static void __exit qxl_exit(void) 308f64122c1SDave Airlie { 30910631d72SDaniel Vetter pci_unregister_driver(&qxl_pci_driver); 310f64122c1SDave Airlie } 311f64122c1SDave Airlie 312f64122c1SDave Airlie module_init(qxl_init); 313f64122c1SDave Airlie module_exit(qxl_exit); 314f64122c1SDave Airlie 315f64122c1SDave Airlie MODULE_AUTHOR(DRIVER_AUTHOR); 316f64122c1SDave Airlie MODULE_DESCRIPTION(DRIVER_DESC); 317f64122c1SDave Airlie MODULE_LICENSE("GPL and additional rights"); 318