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 int 67 qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 68 { 69 struct qxl_device *qdev; 70 int ret; 71 72 if (pdev->revision < 4) { 73 DRM_ERROR("qxl too old, doesn't support client_monitors_config," 74 " use xf86-video-qxl in user mode"); 75 return -EINVAL; /* TODO: ENODEV ? */ 76 } 77 78 qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL); 79 if (!qdev) 80 return -ENOMEM; 81 82 ret = pci_enable_device(pdev); 83 if (ret) 84 goto free_dev; 85 86 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, 0, "qxl"); 87 if (ret) 88 goto disable_pci; 89 90 ret = qxl_device_init(qdev, &qxl_driver, pdev); 91 if (ret) 92 goto disable_pci; 93 94 ret = qxl_modeset_init(qdev); 95 if (ret) 96 goto unload; 97 98 drm_kms_helper_poll_init(&qdev->ddev); 99 100 /* Complete initialization. */ 101 ret = drm_dev_register(&qdev->ddev, ent->driver_data); 102 if (ret) 103 goto modeset_cleanup; 104 105 drm_fbdev_generic_setup(&qdev->ddev, 32); 106 return 0; 107 108 modeset_cleanup: 109 qxl_modeset_fini(qdev); 110 unload: 111 qxl_device_fini(qdev); 112 disable_pci: 113 pci_disable_device(pdev); 114 free_dev: 115 kfree(qdev); 116 return ret; 117 } 118 119 static void 120 qxl_pci_remove(struct pci_dev *pdev) 121 { 122 struct drm_device *dev = pci_get_drvdata(pdev); 123 struct qxl_device *qdev = dev->dev_private; 124 125 drm_dev_unregister(dev); 126 127 qxl_modeset_fini(qdev); 128 qxl_device_fini(qdev); 129 130 dev->dev_private = NULL; 131 kfree(qdev); 132 drm_dev_put(dev); 133 } 134 135 static const struct file_operations qxl_fops = { 136 .owner = THIS_MODULE, 137 .open = drm_open, 138 .release = drm_release, 139 .unlocked_ioctl = drm_ioctl, 140 .poll = drm_poll, 141 .read = drm_read, 142 .mmap = qxl_mmap, 143 }; 144 145 static int qxl_drm_freeze(struct drm_device *dev) 146 { 147 struct pci_dev *pdev = dev->pdev; 148 struct qxl_device *qdev = dev->dev_private; 149 int ret; 150 151 ret = drm_mode_config_helper_suspend(dev); 152 if (ret) 153 return ret; 154 155 qxl_destroy_monitors_object(qdev); 156 qxl_surf_evict(qdev); 157 qxl_vram_evict(qdev); 158 159 while (!qxl_check_idle(qdev->command_ring)); 160 while (!qxl_check_idle(qdev->release_ring)) 161 qxl_queue_garbage_collect(qdev, 1); 162 163 pci_save_state(pdev); 164 165 return 0; 166 } 167 168 static int qxl_drm_resume(struct drm_device *dev, bool thaw) 169 { 170 struct qxl_device *qdev = dev->dev_private; 171 172 qdev->ram_header->int_mask = QXL_INTERRUPT_MASK; 173 if (!thaw) { 174 qxl_reinit_memslots(qdev); 175 qxl_ring_init_hdr(qdev->release_ring); 176 } 177 178 qxl_create_monitors_object(qdev); 179 return drm_mode_config_helper_resume(dev); 180 } 181 182 static int qxl_pm_suspend(struct device *dev) 183 { 184 struct pci_dev *pdev = to_pci_dev(dev); 185 struct drm_device *drm_dev = pci_get_drvdata(pdev); 186 int error; 187 188 error = qxl_drm_freeze(drm_dev); 189 if (error) 190 return error; 191 192 pci_disable_device(pdev); 193 pci_set_power_state(pdev, PCI_D3hot); 194 return 0; 195 } 196 197 static int qxl_pm_resume(struct device *dev) 198 { 199 struct pci_dev *pdev = to_pci_dev(dev); 200 struct drm_device *drm_dev = pci_get_drvdata(pdev); 201 202 pci_set_power_state(pdev, PCI_D0); 203 pci_restore_state(pdev); 204 if (pci_enable_device(pdev)) { 205 return -EIO; 206 } 207 208 return qxl_drm_resume(drm_dev, false); 209 } 210 211 static int qxl_pm_thaw(struct device *dev) 212 { 213 struct drm_device *drm_dev = dev_get_drvdata(dev); 214 215 return qxl_drm_resume(drm_dev, true); 216 } 217 218 static int qxl_pm_freeze(struct device *dev) 219 { 220 struct drm_device *drm_dev = dev_get_drvdata(dev); 221 222 return qxl_drm_freeze(drm_dev); 223 } 224 225 static int qxl_pm_restore(struct device *dev) 226 { 227 struct pci_dev *pdev = to_pci_dev(dev); 228 struct drm_device *drm_dev = pci_get_drvdata(pdev); 229 struct qxl_device *qdev = drm_dev->dev_private; 230 231 qxl_io_reset(qdev); 232 return qxl_drm_resume(drm_dev, false); 233 } 234 235 static const struct dev_pm_ops qxl_pm_ops = { 236 .suspend = qxl_pm_suspend, 237 .resume = qxl_pm_resume, 238 .freeze = qxl_pm_freeze, 239 .thaw = qxl_pm_thaw, 240 .poweroff = qxl_pm_freeze, 241 .restore = qxl_pm_restore, 242 }; 243 static struct pci_driver qxl_pci_driver = { 244 .name = DRIVER_NAME, 245 .id_table = pciidlist, 246 .probe = qxl_pci_probe, 247 .remove = qxl_pci_remove, 248 .driver.pm = &qxl_pm_ops, 249 }; 250 251 static struct drm_driver qxl_driver = { 252 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 253 254 .dumb_create = qxl_mode_dumb_create, 255 .dumb_map_offset = qxl_mode_dumb_mmap, 256 #if defined(CONFIG_DEBUG_FS) 257 .debugfs_init = qxl_debugfs_init, 258 #endif 259 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 260 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 261 .gem_prime_pin = qxl_gem_prime_pin, 262 .gem_prime_unpin = qxl_gem_prime_unpin, 263 .gem_prime_get_sg_table = qxl_gem_prime_get_sg_table, 264 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table, 265 .gem_prime_vmap = qxl_gem_prime_vmap, 266 .gem_prime_vunmap = qxl_gem_prime_vunmap, 267 .gem_prime_mmap = qxl_gem_prime_mmap, 268 .gem_free_object_unlocked = qxl_gem_object_free, 269 .gem_open_object = qxl_gem_object_open, 270 .gem_close_object = qxl_gem_object_close, 271 .fops = &qxl_fops, 272 .ioctls = qxl_ioctls, 273 .irq_handler = qxl_irq_handler, 274 .name = DRIVER_NAME, 275 .desc = DRIVER_DESC, 276 .date = DRIVER_DATE, 277 .major = 0, 278 .minor = 1, 279 .patchlevel = 0, 280 }; 281 282 static int __init qxl_init(void) 283 { 284 if (vgacon_text_force() && qxl_modeset == -1) 285 return -EINVAL; 286 287 if (qxl_modeset == 0) 288 return -EINVAL; 289 qxl_driver.num_ioctls = qxl_max_ioctls; 290 return pci_register_driver(&qxl_pci_driver); 291 } 292 293 static void __exit qxl_exit(void) 294 { 295 pci_unregister_driver(&qxl_pci_driver); 296 } 297 298 module_init(qxl_init); 299 module_exit(qxl_exit); 300 301 MODULE_AUTHOR(DRIVER_AUTHOR); 302 MODULE_DESCRIPTION(DRIVER_DESC); 303 MODULE_LICENSE("GPL and additional rights"); 304