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 <linux/module.h> 32 #include <linux/console.h> 33 34 #include "drmP.h" 35 #include "drm/drm.h" 36 #include "drm_crtc_helper.h" 37 #include "qxl_drv.h" 38 #include "qxl_object.h" 39 40 extern int qxl_max_ioctls; 41 static const struct pci_device_id pciidlist[] = { 42 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, 43 0xffff00, 0 }, 44 { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8, 45 0xffff00, 0 }, 46 { 0, 0, 0 }, 47 }; 48 MODULE_DEVICE_TABLE(pci, pciidlist); 49 50 static int qxl_modeset = -1; 51 int qxl_num_crtc = 4; 52 53 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting"); 54 module_param_named(modeset, qxl_modeset, int, 0400); 55 56 MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)"); 57 module_param_named(num_heads, qxl_num_crtc, int, 0400); 58 59 static struct drm_driver qxl_driver; 60 static struct pci_driver qxl_pci_driver; 61 62 static int 63 qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 64 { 65 struct qxl_device *qdev; 66 int ret; 67 68 if (pdev->revision < 4) { 69 DRM_ERROR("qxl too old, doesn't support client_monitors_config," 70 " use xf86-video-qxl in user mode"); 71 return -EINVAL; /* TODO: ENODEV ? */ 72 } 73 74 qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL); 75 if (!qdev) 76 return -ENOMEM; 77 78 ret = pci_enable_device(pdev); 79 if (ret) 80 goto free_dev; 81 82 ret = qxl_device_init(qdev, &qxl_driver, pdev); 83 if (ret) 84 goto disable_pci; 85 86 ret = qxl_modeset_init(qdev); 87 if (ret) 88 goto unload; 89 90 drm_kms_helper_poll_init(&qdev->ddev); 91 92 /* Complete initialization. */ 93 ret = drm_dev_register(&qdev->ddev, ent->driver_data); 94 if (ret) 95 goto modeset_cleanup; 96 97 return 0; 98 99 modeset_cleanup: 100 qxl_modeset_fini(qdev); 101 unload: 102 qxl_device_fini(qdev); 103 disable_pci: 104 pci_disable_device(pdev); 105 free_dev: 106 kfree(qdev); 107 return ret; 108 } 109 110 static void 111 qxl_pci_remove(struct pci_dev *pdev) 112 { 113 struct drm_device *dev = pci_get_drvdata(pdev); 114 struct qxl_device *qdev = dev->dev_private; 115 116 drm_dev_unregister(dev); 117 118 qxl_modeset_fini(qdev); 119 qxl_device_fini(qdev); 120 121 dev->dev_private = NULL; 122 kfree(qdev); 123 drm_dev_unref(dev); 124 } 125 126 static const struct file_operations qxl_fops = { 127 .owner = THIS_MODULE, 128 .open = drm_open, 129 .release = drm_release, 130 .unlocked_ioctl = drm_ioctl, 131 .poll = drm_poll, 132 .read = drm_read, 133 .mmap = qxl_mmap, 134 }; 135 136 static int qxl_drm_freeze(struct drm_device *dev) 137 { 138 struct pci_dev *pdev = dev->pdev; 139 struct qxl_device *qdev = dev->dev_private; 140 struct drm_crtc *crtc; 141 142 drm_kms_helper_poll_disable(dev); 143 144 console_lock(); 145 qxl_fbdev_set_suspend(qdev, 1); 146 console_unlock(); 147 148 /* unpin the front buffers */ 149 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 150 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 151 if (crtc->enabled) 152 (*crtc_funcs->disable)(crtc); 153 } 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 drm_helper_resume_force_mode(dev); 180 181 console_lock(); 182 qxl_fbdev_set_suspend(qdev, 0); 183 console_unlock(); 184 185 drm_kms_helper_poll_enable(dev); 186 return 0; 187 } 188 189 static int qxl_pm_suspend(struct device *dev) 190 { 191 struct pci_dev *pdev = to_pci_dev(dev); 192 struct drm_device *drm_dev = pci_get_drvdata(pdev); 193 int error; 194 195 error = qxl_drm_freeze(drm_dev); 196 if (error) 197 return error; 198 199 pci_disable_device(pdev); 200 pci_set_power_state(pdev, PCI_D3hot); 201 return 0; 202 } 203 204 static int qxl_pm_resume(struct device *dev) 205 { 206 struct pci_dev *pdev = to_pci_dev(dev); 207 struct drm_device *drm_dev = pci_get_drvdata(pdev); 208 209 pci_set_power_state(pdev, PCI_D0); 210 pci_restore_state(pdev); 211 if (pci_enable_device(pdev)) { 212 return -EIO; 213 } 214 215 return qxl_drm_resume(drm_dev, false); 216 } 217 218 static int qxl_pm_thaw(struct device *dev) 219 { 220 struct pci_dev *pdev = to_pci_dev(dev); 221 struct drm_device *drm_dev = pci_get_drvdata(pdev); 222 223 return qxl_drm_resume(drm_dev, true); 224 } 225 226 static int qxl_pm_freeze(struct device *dev) 227 { 228 struct pci_dev *pdev = to_pci_dev(dev); 229 struct drm_device *drm_dev = pci_get_drvdata(pdev); 230 231 return qxl_drm_freeze(drm_dev); 232 } 233 234 static int qxl_pm_restore(struct device *dev) 235 { 236 struct pci_dev *pdev = to_pci_dev(dev); 237 struct drm_device *drm_dev = pci_get_drvdata(pdev); 238 struct qxl_device *qdev = drm_dev->dev_private; 239 240 qxl_io_reset(qdev); 241 return qxl_drm_resume(drm_dev, false); 242 } 243 244 static const struct dev_pm_ops qxl_pm_ops = { 245 .suspend = qxl_pm_suspend, 246 .resume = qxl_pm_resume, 247 .freeze = qxl_pm_freeze, 248 .thaw = qxl_pm_thaw, 249 .poweroff = qxl_pm_freeze, 250 .restore = qxl_pm_restore, 251 }; 252 static struct pci_driver qxl_pci_driver = { 253 .name = DRIVER_NAME, 254 .id_table = pciidlist, 255 .probe = qxl_pci_probe, 256 .remove = qxl_pci_remove, 257 .driver.pm = &qxl_pm_ops, 258 }; 259 260 static struct drm_driver qxl_driver = { 261 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | 262 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | 263 DRIVER_ATOMIC, 264 265 .set_busid = drm_pci_set_busid, 266 267 .dumb_create = qxl_mode_dumb_create, 268 .dumb_map_offset = qxl_mode_dumb_mmap, 269 .dumb_destroy = drm_gem_dumb_destroy, 270 #if defined(CONFIG_DEBUG_FS) 271 .debugfs_init = qxl_debugfs_init, 272 #endif 273 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 274 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 275 .gem_prime_export = drm_gem_prime_export, 276 .gem_prime_import = drm_gem_prime_import, 277 .gem_prime_pin = qxl_gem_prime_pin, 278 .gem_prime_unpin = qxl_gem_prime_unpin, 279 .gem_prime_get_sg_table = qxl_gem_prime_get_sg_table, 280 .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table, 281 .gem_prime_vmap = qxl_gem_prime_vmap, 282 .gem_prime_vunmap = qxl_gem_prime_vunmap, 283 .gem_prime_mmap = qxl_gem_prime_mmap, 284 .gem_free_object_unlocked = qxl_gem_object_free, 285 .gem_open_object = qxl_gem_object_open, 286 .gem_close_object = qxl_gem_object_close, 287 .fops = &qxl_fops, 288 .ioctls = qxl_ioctls, 289 .irq_handler = qxl_irq_handler, 290 .name = DRIVER_NAME, 291 .desc = DRIVER_DESC, 292 .date = DRIVER_DATE, 293 .major = 0, 294 .minor = 1, 295 .patchlevel = 0, 296 }; 297 298 static int __init qxl_init(void) 299 { 300 if (vgacon_text_force() && qxl_modeset == -1) 301 return -EINVAL; 302 303 if (qxl_modeset == 0) 304 return -EINVAL; 305 qxl_driver.num_ioctls = qxl_max_ioctls; 306 return drm_pci_init(&qxl_driver, &qxl_pci_driver); 307 } 308 309 static void __exit qxl_exit(void) 310 { 311 drm_pci_exit(&qxl_driver, &qxl_pci_driver); 312 } 313 314 module_init(qxl_init); 315 module_exit(qxl_exit); 316 317 MODULE_AUTHOR(DRIVER_AUTHOR); 318 MODULE_DESCRIPTION(DRIVER_DESC); 319 MODULE_LICENSE("GPL and additional rights"); 320