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> 40c0f4b75cSSam Ravnborg #include <drm/drm_modeset_helper.h> 41c0f4b75cSSam Ravnborg #include <drm/drm_prime.h> 42c0f4b75cSSam Ravnborg #include <drm/drm_probe_helper.h> 43c0f4b75cSSam Ravnborg 44d84300bfSDave Airlie #include "qxl_object.h" 45f64122c1SDave Airlie 469baa3c34SBenoit Taine static const struct pci_device_id pciidlist[] = { 47f64122c1SDave Airlie { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, 48f64122c1SDave Airlie 0xffff00, 0 }, 49f64122c1SDave Airlie { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8, 50f64122c1SDave Airlie 0xffff00, 0 }, 51f64122c1SDave Airlie { 0, 0, 0 }, 52f64122c1SDave Airlie }; 53f64122c1SDave Airlie MODULE_DEVICE_TABLE(pci, pciidlist); 54f64122c1SDave Airlie 556d01f1f5SDave Airlie static int qxl_modeset = -1; 5607f8d9bdSDave Airlie int qxl_num_crtc = 4; 57f64122c1SDave Airlie 58f64122c1SDave Airlie MODULE_PARM_DESC(modeset, "Disable/Enable modesetting"); 59f64122c1SDave Airlie module_param_named(modeset, qxl_modeset, int, 0400); 60f64122c1SDave Airlie 6107f8d9bdSDave Airlie MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)"); 6207f8d9bdSDave Airlie module_param_named(num_heads, qxl_num_crtc, int, 0400); 6307f8d9bdSDave Airlie 64f64122c1SDave Airlie static struct drm_driver qxl_driver; 65f64122c1SDave Airlie static struct pci_driver qxl_pci_driver; 66f64122c1SDave Airlie 67fbf0a7f4SGerd Hoffmann static bool is_vga(struct pci_dev *pdev) 68fbf0a7f4SGerd Hoffmann { 69fbf0a7f4SGerd Hoffmann return pdev->class == PCI_CLASS_DISPLAY_VGA << 8; 70fbf0a7f4SGerd Hoffmann } 71fbf0a7f4SGerd Hoffmann 72f64122c1SDave Airlie static int 73f64122c1SDave Airlie qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 74f64122c1SDave Airlie { 752b65d567SGabriel Krisman Bertazi struct qxl_device *qdev; 762b65d567SGabriel Krisman Bertazi int ret; 772b65d567SGabriel Krisman Bertazi 78f64122c1SDave Airlie if (pdev->revision < 4) { 79f64122c1SDave Airlie DRM_ERROR("qxl too old, doesn't support client_monitors_config," 80f64122c1SDave Airlie " use xf86-video-qxl in user mode"); 81f64122c1SDave Airlie return -EINVAL; /* TODO: ENODEV ? */ 82f64122c1SDave Airlie } 832b65d567SGabriel Krisman Bertazi 84a9b0b24aSDaniel Vetter qdev = devm_drm_dev_alloc(&pdev->dev, &qxl_driver, 85a9b0b24aSDaniel Vetter struct qxl_device, ddev); 86a9b0b24aSDaniel Vetter if (IS_ERR(qdev)) { 87a9b0b24aSDaniel Vetter pr_err("Unable to init drm dev"); 88cbdded7fSGabriel Krisman Bertazi return -ENOMEM; 89a9b0b24aSDaniel Vetter } 902b65d567SGabriel Krisman Bertazi 912b65d567SGabriel Krisman Bertazi ret = pci_enable_device(pdev); 922b65d567SGabriel Krisman Bertazi if (ret) 93a9b0b24aSDaniel Vetter return ret; 942b65d567SGabriel Krisman Bertazi 9535616a4aSGerd Hoffmann ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "qxl"); 96c34674a2SGerd Hoffmann if (ret) 97c34674a2SGerd Hoffmann goto disable_pci; 98c34674a2SGerd Hoffmann 99*66c8ef90SGerd Hoffmann if (is_vga(pdev) && pdev->revision < 5) { 100fbf0a7f4SGerd Hoffmann ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO); 101fbf0a7f4SGerd Hoffmann if (ret) { 102fbf0a7f4SGerd Hoffmann DRM_ERROR("can't get legacy vga ioports\n"); 103fbf0a7f4SGerd Hoffmann goto disable_pci; 104fbf0a7f4SGerd Hoffmann } 105fbf0a7f4SGerd Hoffmann } 106fbf0a7f4SGerd Hoffmann 107a9b0b24aSDaniel Vetter ret = qxl_device_init(qdev, pdev); 1082b65d567SGabriel Krisman Bertazi if (ret) 109fbf0a7f4SGerd Hoffmann goto put_vga; 1102b65d567SGabriel Krisman Bertazi 1112b65d567SGabriel Krisman Bertazi ret = qxl_modeset_init(qdev); 1122b65d567SGabriel Krisman Bertazi if (ret) 11364ca824eSGabriel Krisman Bertazi goto unload; 1142b65d567SGabriel Krisman Bertazi 115cbdded7fSGabriel Krisman Bertazi drm_kms_helper_poll_init(&qdev->ddev); 1162b65d567SGabriel Krisman Bertazi 1172b65d567SGabriel Krisman Bertazi /* Complete initialization. */ 118cbdded7fSGabriel Krisman Bertazi ret = drm_dev_register(&qdev->ddev, ent->driver_data); 1192b65d567SGabriel Krisman Bertazi if (ret) 1202b65d567SGabriel Krisman Bertazi goto modeset_cleanup; 1212b65d567SGabriel Krisman Bertazi 12226d4707dSGerd Hoffmann drm_fbdev_generic_setup(&qdev->ddev, 32); 1232b65d567SGabriel Krisman Bertazi return 0; 1242b65d567SGabriel Krisman Bertazi 1252b65d567SGabriel Krisman Bertazi modeset_cleanup: 1262b65d567SGabriel Krisman Bertazi qxl_modeset_fini(qdev); 1272b65d567SGabriel Krisman Bertazi unload: 1282b65d567SGabriel Krisman Bertazi qxl_device_fini(qdev); 129fbf0a7f4SGerd Hoffmann put_vga: 130*66c8ef90SGerd Hoffmann if (is_vga(pdev) && pdev->revision < 5) 131fbf0a7f4SGerd Hoffmann vga_put(pdev, VGA_RSRC_LEGACY_IO); 1322b65d567SGabriel Krisman Bertazi disable_pci: 1332b65d567SGabriel Krisman Bertazi pci_disable_device(pdev); 134a9b0b24aSDaniel Vetter 1352b65d567SGabriel Krisman Bertazi return ret; 136f64122c1SDave Airlie } 137f64122c1SDave Airlie 13823575e7aSGerd Hoffmann static void qxl_drm_release(struct drm_device *dev) 13923575e7aSGerd Hoffmann { 140e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev); 14123575e7aSGerd Hoffmann 14223575e7aSGerd Hoffmann /* 14323575e7aSGerd Hoffmann * TODO: qxl_device_fini() call should be in qxl_pci_remove(), 14423575e7aSGerd Hoffmann * reodering qxl_modeset_fini() + qxl_device_fini() calls is 14523575e7aSGerd Hoffmann * non-trivial though. 14623575e7aSGerd Hoffmann */ 14723575e7aSGerd Hoffmann qxl_modeset_fini(qdev); 14823575e7aSGerd Hoffmann qxl_device_fini(qdev); 14923575e7aSGerd Hoffmann } 15023575e7aSGerd Hoffmann 151f64122c1SDave Airlie static void 152f64122c1SDave Airlie qxl_pci_remove(struct pci_dev *pdev) 153f64122c1SDave Airlie { 154f64122c1SDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 155f64122c1SDave Airlie 1566f897f51SGabriel Krisman Bertazi drm_dev_unregister(dev); 15723575e7aSGerd Hoffmann drm_atomic_helper_shutdown(dev); 158*66c8ef90SGerd Hoffmann if (is_vga(pdev) && pdev->revision < 5) 159fbf0a7f4SGerd Hoffmann vga_put(pdev, VGA_RSRC_LEGACY_IO); 160f64122c1SDave Airlie } 161f64122c1SDave Airlie 162db262087SGerd Hoffmann DEFINE_DRM_GEM_FOPS(qxl_fops); 163f64122c1SDave Airlie 164d84300bfSDave Airlie static int qxl_drm_freeze(struct drm_device *dev) 165d84300bfSDave Airlie { 166d84300bfSDave Airlie struct pci_dev *pdev = dev->pdev; 167e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev); 1687948a2b1SPeter Wu int ret; 169d84300bfSDave Airlie 1707948a2b1SPeter Wu ret = drm_mode_config_helper_suspend(dev); 1717948a2b1SPeter Wu if (ret) 1727948a2b1SPeter Wu return ret; 173d84300bfSDave Airlie 174d84300bfSDave Airlie qxl_destroy_monitors_object(qdev); 175d84300bfSDave Airlie qxl_surf_evict(qdev); 176d84300bfSDave Airlie qxl_vram_evict(qdev); 177d84300bfSDave Airlie 178d84300bfSDave Airlie while (!qxl_check_idle(qdev->command_ring)); 179d84300bfSDave Airlie while (!qxl_check_idle(qdev->release_ring)) 180d84300bfSDave Airlie qxl_queue_garbage_collect(qdev, 1); 181d84300bfSDave Airlie 182d84300bfSDave Airlie pci_save_state(pdev); 183d84300bfSDave Airlie 184d84300bfSDave Airlie return 0; 185d84300bfSDave Airlie } 186d84300bfSDave Airlie 187d84300bfSDave Airlie static int qxl_drm_resume(struct drm_device *dev, bool thaw) 188d84300bfSDave Airlie { 189e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev); 190d84300bfSDave Airlie 191d84300bfSDave Airlie qdev->ram_header->int_mask = QXL_INTERRUPT_MASK; 192d84300bfSDave Airlie if (!thaw) { 193d84300bfSDave Airlie qxl_reinit_memslots(qdev); 194d84300bfSDave Airlie qxl_ring_init_hdr(qdev->release_ring); 195d84300bfSDave Airlie } 196d84300bfSDave Airlie 197d84300bfSDave Airlie qxl_create_monitors_object(qdev); 1987948a2b1SPeter Wu return drm_mode_config_helper_resume(dev); 199d84300bfSDave Airlie } 200d84300bfSDave Airlie 201d84300bfSDave Airlie static int qxl_pm_suspend(struct device *dev) 202d84300bfSDave Airlie { 203d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 204d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 205d84300bfSDave Airlie int error; 206d84300bfSDave Airlie 207d84300bfSDave Airlie error = qxl_drm_freeze(drm_dev); 208d84300bfSDave Airlie if (error) 209d84300bfSDave Airlie return error; 210d84300bfSDave Airlie 211d84300bfSDave Airlie pci_disable_device(pdev); 212d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D3hot); 213d84300bfSDave Airlie return 0; 214d84300bfSDave Airlie } 215d84300bfSDave Airlie 216d84300bfSDave Airlie static int qxl_pm_resume(struct device *dev) 217d84300bfSDave Airlie { 218d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 219d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 220d84300bfSDave Airlie 221d84300bfSDave Airlie pci_set_power_state(pdev, PCI_D0); 222d84300bfSDave Airlie pci_restore_state(pdev); 223d84300bfSDave Airlie if (pci_enable_device(pdev)) { 224d84300bfSDave Airlie return -EIO; 225d84300bfSDave Airlie } 226d84300bfSDave Airlie 227d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 228d84300bfSDave Airlie } 229d84300bfSDave Airlie 230d84300bfSDave Airlie static int qxl_pm_thaw(struct device *dev) 231d84300bfSDave Airlie { 232f6d2b63aSChuhong Yuan struct drm_device *drm_dev = dev_get_drvdata(dev); 233d84300bfSDave Airlie 234d84300bfSDave Airlie return qxl_drm_resume(drm_dev, true); 235d84300bfSDave Airlie } 236d84300bfSDave Airlie 237d84300bfSDave Airlie static int qxl_pm_freeze(struct device *dev) 238d84300bfSDave Airlie { 239f6d2b63aSChuhong Yuan struct drm_device *drm_dev = dev_get_drvdata(dev); 240d84300bfSDave Airlie 241d84300bfSDave Airlie return qxl_drm_freeze(drm_dev); 242d84300bfSDave Airlie } 243d84300bfSDave Airlie 244d84300bfSDave Airlie static int qxl_pm_restore(struct device *dev) 245d84300bfSDave Airlie { 246d84300bfSDave Airlie struct pci_dev *pdev = to_pci_dev(dev); 247d84300bfSDave Airlie struct drm_device *drm_dev = pci_get_drvdata(pdev); 248e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(drm_dev); 249d84300bfSDave Airlie 250d84300bfSDave Airlie qxl_io_reset(qdev); 251d84300bfSDave Airlie return qxl_drm_resume(drm_dev, false); 252d84300bfSDave Airlie } 253d84300bfSDave Airlie 254d84300bfSDave Airlie static const struct dev_pm_ops qxl_pm_ops = { 255d84300bfSDave Airlie .suspend = qxl_pm_suspend, 256d84300bfSDave Airlie .resume = qxl_pm_resume, 257d84300bfSDave Airlie .freeze = qxl_pm_freeze, 258d84300bfSDave Airlie .thaw = qxl_pm_thaw, 259d84300bfSDave Airlie .poweroff = qxl_pm_freeze, 260d84300bfSDave Airlie .restore = qxl_pm_restore, 261d84300bfSDave Airlie }; 262d84300bfSDave Airlie static struct pci_driver qxl_pci_driver = { 263d84300bfSDave Airlie .name = DRIVER_NAME, 264d84300bfSDave Airlie .id_table = pciidlist, 265d84300bfSDave Airlie .probe = qxl_pci_probe, 266d84300bfSDave Airlie .remove = qxl_pci_remove, 267d84300bfSDave Airlie .driver.pm = &qxl_pm_ops, 268d84300bfSDave Airlie }; 269d84300bfSDave Airlie 270f64122c1SDave Airlie static struct drm_driver qxl_driver = { 2710424fdafSDaniel Vetter .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 272f64122c1SDave Airlie 273f64122c1SDave Airlie .dumb_create = qxl_mode_dumb_create, 274f64122c1SDave Airlie .dumb_map_offset = qxl_mode_dumb_mmap, 275f64122c1SDave Airlie #if defined(CONFIG_DEBUG_FS) 276f64122c1SDave Airlie .debugfs_init = qxl_debugfs_init, 277f64122c1SDave Airlie #endif 278ab042b82SGerd Hoffmann .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 279ab042b82SGerd Hoffmann .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 280ab042b82SGerd Hoffmann .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table, 28147c12968SAndreas Pokorny .gem_prime_mmap = qxl_gem_prime_mmap, 282f64122c1SDave Airlie .fops = &qxl_fops, 283f64122c1SDave Airlie .ioctls = qxl_ioctls, 284f64122c1SDave Airlie .irq_handler = qxl_irq_handler, 285f64122c1SDave Airlie .name = DRIVER_NAME, 286f64122c1SDave Airlie .desc = DRIVER_DESC, 287f64122c1SDave Airlie .date = DRIVER_DATE, 288f64122c1SDave Airlie .major = 0, 289f64122c1SDave Airlie .minor = 1, 290f64122c1SDave Airlie .patchlevel = 0, 29123575e7aSGerd Hoffmann 29223575e7aSGerd Hoffmann .release = qxl_drm_release, 293f64122c1SDave Airlie }; 294f64122c1SDave Airlie 295f64122c1SDave Airlie static int __init qxl_init(void) 296f64122c1SDave Airlie { 297f64122c1SDave Airlie if (vgacon_text_force() && qxl_modeset == -1) 298f64122c1SDave Airlie return -EINVAL; 299f64122c1SDave Airlie 300f64122c1SDave Airlie if (qxl_modeset == 0) 301f64122c1SDave Airlie return -EINVAL; 302f64122c1SDave Airlie qxl_driver.num_ioctls = qxl_max_ioctls; 30310631d72SDaniel Vetter return pci_register_driver(&qxl_pci_driver); 304f64122c1SDave Airlie } 305f64122c1SDave Airlie 306f64122c1SDave Airlie static void __exit qxl_exit(void) 307f64122c1SDave Airlie { 30810631d72SDaniel Vetter pci_unregister_driver(&qxl_pci_driver); 309f64122c1SDave Airlie } 310f64122c1SDave Airlie 311f64122c1SDave Airlie module_init(qxl_init); 312f64122c1SDave Airlie module_exit(qxl_exit); 313f64122c1SDave Airlie 314f64122c1SDave Airlie MODULE_AUTHOR(DRIVER_AUTHOR); 315f64122c1SDave Airlie MODULE_DESCRIPTION(DRIVER_DESC); 316f64122c1SDave Airlie MODULE_LICENSE("GPL and additional rights"); 317