1 /* vim: set ts=8 sw=8 tw=78 ai noexpandtab */ 2 /* qxl_drv.c -- QXL driver -*- linux-c -*- 3 * 4 * Copyright 2011 Red Hat, Inc. 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the next 15 * paragraph) shall be included in all copies or substantial portions of the 16 * Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. 25 * 26 * Authors: 27 * Dave Airlie <airlie@redhat.com> 28 * Alon Levy <alevy@redhat.com> 29 */ 30 31 #include "qxl_drv.h" 32 #include <linux/console.h> 33 #include <linux/module.h> 34 #include <linux/pci.h> 35 36 #include <drm/drm.h> 37 #include <drm/drm_drv.h> 38 #include <drm/drm_file.h> 39 #include <drm/drm_modeset_helper.h> 40 #include <drm/drm_prime.h> 41 #include <drm/drm_probe_helper.h> 42 43 #include "qxl_object.h" 44 45 static const struct pci_device_id pciidlist[] = { 46 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, 47 0xffff00, 0 }, 48 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8, 49 0xffff00, 0 }, 50 { 0, 0, 0 }, 51 }; 52 MODULE_DEVICE_TABLE(pci, pciidlist); 53 54 static int qxl_modeset = -1; 55 int qxl_num_crtc = 4; 56 57 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting"); 58 module_param_named(modeset, qxl_modeset, int, 0400); 59 60 MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)"); 61 module_param_named(num_heads, qxl_num_crtc, int, 0400); 62 63 static struct drm_driver qxl_driver; 64 static struct pci_driver qxl_pci_driver; 65 66 static bool is_vga(struct pci_dev *pdev) 67 { 68 return pdev->class == PCI_CLASS_DISPLAY_VGA << 8; 69 } 70 71 static int 72 qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 73 { 74 struct qxl_device *qdev; 75 int ret; 76 77 if (pdev->revision < 4) { 78 DRM_ERROR("qxl too old, doesn't support client_monitors_config," 79 " use xf86-video-qxl in user mode"); 80 return -EINVAL; /* TODO: ENODEV ? */ 81 } 82 83 qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL); 84 if (!qdev) 85 return -ENOMEM; 86 87 ret = pci_enable_device(pdev); 88 if (ret) 89 goto free_dev; 90 91 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "qxl"); 92 if (ret) 93 goto disable_pci; 94 95 if (is_vga(pdev)) { 96 ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO); 97 if (ret) { 98 DRM_ERROR("can't get legacy vga ioports\n"); 99 goto disable_pci; 100 } 101 } 102 103 ret = qxl_device_init(qdev, &qxl_driver, pdev); 104 if (ret) 105 goto put_vga; 106 107 ret = qxl_modeset_init(qdev); 108 if (ret) 109 goto unload; 110 111 drm_kms_helper_poll_init(&qdev->ddev); 112 113 /* Complete initialization. */ 114 ret = drm_dev_register(&qdev->ddev, ent->driver_data); 115 if (ret) 116 goto modeset_cleanup; 117 118 drm_fbdev_generic_setup(&qdev->ddev, 32); 119 return 0; 120 121 modeset_cleanup: 122 qxl_modeset_fini(qdev); 123 unload: 124 qxl_device_fini(qdev); 125 put_vga: 126 if (is_vga(pdev)) 127 vga_put(pdev, VGA_RSRC_LEGACY_IO); 128 disable_pci: 129 pci_disable_device(pdev); 130 free_dev: 131 kfree(qdev); 132 return ret; 133 } 134 135 static void 136 qxl_pci_remove(struct pci_dev *pdev) 137 { 138 struct drm_device *dev = pci_get_drvdata(pdev); 139 struct qxl_device *qdev = dev->dev_private; 140 141 drm_dev_unregister(dev); 142 143 qxl_modeset_fini(qdev); 144 qxl_device_fini(qdev); 145 if (is_vga(pdev)) 146 vga_put(pdev, VGA_RSRC_LEGACY_IO); 147 148 dev->dev_private = NULL; 149 kfree(qdev); 150 drm_dev_put(dev); 151 } 152 153 DEFINE_DRM_GEM_FOPS(qxl_fops); 154 155 static int qxl_drm_freeze(struct drm_device *dev) 156 { 157 struct pci_dev *pdev = dev->pdev; 158 struct qxl_device *qdev = dev->dev_private; 159 int ret; 160 161 ret = drm_mode_config_helper_suspend(dev); 162 if (ret) 163 return ret; 164 165 qxl_destroy_monitors_object(qdev); 166 qxl_surf_evict(qdev); 167 qxl_vram_evict(qdev); 168 169 while (!qxl_check_idle(qdev->command_ring)); 170 while (!qxl_check_idle(qdev->release_ring)) 171 qxl_queue_garbage_collect(qdev, 1); 172 173 pci_save_state(pdev); 174 175 return 0; 176 } 177 178 static int qxl_drm_resume(struct drm_device *dev, bool thaw) 179 { 180 struct qxl_device *qdev = dev->dev_private; 181 182 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK; 183 if (!thaw) { 184 qxl_reinit_memslots(qdev); 185 qxl_ring_init_hdr(qdev->release_ring); 186 } 187 188 qxl_create_monitors_object(qdev); 189 return drm_mode_config_helper_resume(dev); 190 } 191 192 static int qxl_pm_suspend(struct device *dev) 193 { 194 struct pci_dev *pdev = to_pci_dev(dev); 195 struct drm_device *drm_dev = pci_get_drvdata(pdev); 196 int error; 197 198 error = qxl_drm_freeze(drm_dev); 199 if (error) 200 return error; 201 202 pci_disable_device(pdev); 203 pci_set_power_state(pdev, PCI_D3hot); 204 return 0; 205 } 206 207 static int qxl_pm_resume(struct device *dev) 208 { 209 struct pci_dev *pdev = to_pci_dev(dev); 210 struct drm_device *drm_dev = pci_get_drvdata(pdev); 211 212 pci_set_power_state(pdev, PCI_D0); 213 pci_restore_state(pdev); 214 if (pci_enable_device(pdev)) { 215 return -EIO; 216 } 217 218 return qxl_drm_resume(drm_dev, false); 219 } 220 221 static int qxl_pm_thaw(struct device *dev) 222 { 223 struct drm_device *drm_dev = dev_get_drvdata(dev); 224 225 return qxl_drm_resume(drm_dev, true); 226 } 227 228 static int qxl_pm_freeze(struct device *dev) 229 { 230 struct drm_device *drm_dev = dev_get_drvdata(dev); 231 232 return qxl_drm_freeze(drm_dev); 233 } 234 235 static int qxl_pm_restore(struct device *dev) 236 { 237 struct pci_dev *pdev = to_pci_dev(dev); 238 struct drm_device *drm_dev = pci_get_drvdata(pdev); 239 struct qxl_device *qdev = drm_dev->dev_private; 240 241 qxl_io_reset(qdev); 242 return qxl_drm_resume(drm_dev, false); 243 } 244 245 static const struct dev_pm_ops qxl_pm_ops = { 246 .suspend = qxl_pm_suspend, 247 .resume = qxl_pm_resume, 248 .freeze = qxl_pm_freeze, 249 .thaw = qxl_pm_thaw, 250 .poweroff = qxl_pm_freeze, 251 .restore = qxl_pm_restore, 252 }; 253 static struct pci_driver qxl_pci_driver = { 254 .name = DRIVER_NAME, 255 .id_table = pciidlist, 256 .probe = qxl_pci_probe, 257 .remove = qxl_pci_remove, 258 .driver.pm = &qxl_pm_ops, 259 }; 260 261 static struct drm_driver qxl_driver = { 262 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 263 264 .dumb_create = qxl_mode_dumb_create, 265 .dumb_map_offset = qxl_mode_dumb_mmap, 266 #if defined(CONFIG_DEBUG_FS) 267 .debugfs_init = qxl_debugfs_init, 268 #endif 269 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 270 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 271 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table, 272 .gem_prime_mmap = qxl_gem_prime_mmap, 273 .fops = &qxl_fops, 274 .ioctls = qxl_ioctls, 275 .irq_handler = qxl_irq_handler, 276 .name = DRIVER_NAME, 277 .desc = DRIVER_DESC, 278 .date = DRIVER_DATE, 279 .major = 0, 280 .minor = 1, 281 .patchlevel = 0, 282 }; 283 284 static int __init qxl_init(void) 285 { 286 if (vgacon_text_force() && qxl_modeset == -1) 287 return -EINVAL; 288 289 if (qxl_modeset == 0) 290 return -EINVAL; 291 qxl_driver.num_ioctls = qxl_max_ioctls; 292 return pci_register_driver(&qxl_pci_driver); 293 } 294 295 static void __exit qxl_exit(void) 296 { 297 pci_unregister_driver(&qxl_pci_driver); 298 } 299 300 module_init(qxl_init); 301 module_exit(qxl_exit); 302 303 MODULE_AUTHOR(DRIVER_AUTHOR); 304 MODULE_DESCRIPTION(DRIVER_DESC); 305 MODULE_LICENSE("GPL and additional rights"); 306