1 /* 2 * \author Rickard E. (Rik) Faith <faith@valinux.com> 3 * \author Daryll Strauss <daryll@valinux.com> 4 * \author Gareth Hughes <gareth@valinux.com> 5 */ 6 7 /* 8 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com 9 * 10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 12 * All Rights Reserved. 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a 15 * copy of this software and associated documentation files (the "Software"), 16 * to deal in the Software without restriction, including without limitation 17 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 18 * and/or sell copies of the Software, and to permit persons to whom the 19 * Software is furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice (including the next 22 * paragraph) shall be included in all copies or substantial portions of the 23 * Software. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 31 * OTHER DEALINGS IN THE SOFTWARE. 32 */ 33 34 #include <linux/poll.h> 35 #include <linux/slab.h> 36 #include <linux/module.h> 37 38 #include <drm/drm_client.h> 39 #include <drm/drm_file.h> 40 #include <drm/drmP.h> 41 42 #include "drm_legacy.h" 43 #include "drm_internal.h" 44 #include "drm_crtc_internal.h" 45 46 /* from BKL pushdown */ 47 DEFINE_MUTEX(drm_global_mutex); 48 49 /** 50 * DOC: file operations 51 * 52 * Drivers must define the file operations structure that forms the DRM 53 * userspace API entry point, even though most of those operations are 54 * implemented in the DRM core. The resulting &struct file_operations must be 55 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(), 56 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled 57 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no 58 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls 59 * that require 32/64 bit compatibility support must provide their own 60 * &file_operations.compat_ioctl handler that processes private ioctls and calls 61 * drm_compat_ioctl() for core ioctls. 62 * 63 * In addition drm_read() and drm_poll() provide support for DRM events. DRM 64 * events are a generic and extensible means to send asynchronous events to 65 * userspace through the file descriptor. They are used to send vblank event and 66 * page flip completions by the KMS API. But drivers can also use it for their 67 * own needs, e.g. to signal completion of rendering. 68 * 69 * For the driver-side event interface see drm_event_reserve_init() and 70 * drm_send_event() as the main starting points. 71 * 72 * The memory mapping implementation will vary depending on how the driver 73 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap() 74 * function, modern drivers should use one of the provided memory-manager 75 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and 76 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap(). 77 * 78 * No other file operations are supported by the DRM userspace API. Overall the 79 * following is an example &file_operations structure:: 80 * 81 * static const example_drm_fops = { 82 * .owner = THIS_MODULE, 83 * .open = drm_open, 84 * .release = drm_release, 85 * .unlocked_ioctl = drm_ioctl, 86 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n 87 * .poll = drm_poll, 88 * .read = drm_read, 89 * .llseek = no_llseek, 90 * .mmap = drm_gem_mmap, 91 * }; 92 * 93 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for 94 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this 95 * simpler. 96 * 97 * The driver's &file_operations must be stored in &drm_driver.fops. 98 * 99 * For driver-private IOCTL handling see the more detailed discussion in 100 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`. 101 */ 102 103 static int drm_open_helper(struct file *filp, struct drm_minor *minor); 104 105 /** 106 * drm_file_alloc - allocate file context 107 * @minor: minor to allocate on 108 * 109 * This allocates a new DRM file context. It is not linked into any context and 110 * can be used by the caller freely. Note that the context keeps a pointer to 111 * @minor, so it must be freed before @minor is. 112 * 113 * RETURNS: 114 * Pointer to newly allocated context, ERR_PTR on failure. 115 */ 116 struct drm_file *drm_file_alloc(struct drm_minor *minor) 117 { 118 struct drm_device *dev = minor->dev; 119 struct drm_file *file; 120 int ret; 121 122 file = kzalloc(sizeof(*file), GFP_KERNEL); 123 if (!file) 124 return ERR_PTR(-ENOMEM); 125 126 file->pid = get_pid(task_pid(current)); 127 file->minor = minor; 128 129 /* for compatibility root is always authenticated */ 130 file->authenticated = capable(CAP_SYS_ADMIN); 131 file->lock_count = 0; 132 133 INIT_LIST_HEAD(&file->lhead); 134 INIT_LIST_HEAD(&file->fbs); 135 mutex_init(&file->fbs_lock); 136 INIT_LIST_HEAD(&file->blobs); 137 INIT_LIST_HEAD(&file->pending_event_list); 138 INIT_LIST_HEAD(&file->event_list); 139 init_waitqueue_head(&file->event_wait); 140 file->event_space = 4096; /* set aside 4k for event buffer */ 141 142 mutex_init(&file->event_read_lock); 143 144 if (drm_core_check_feature(dev, DRIVER_GEM)) 145 drm_gem_open(dev, file); 146 147 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) 148 drm_syncobj_open(file); 149 150 if (drm_core_check_feature(dev, DRIVER_PRIME)) 151 drm_prime_init_file_private(&file->prime); 152 153 if (dev->driver->open) { 154 ret = dev->driver->open(dev, file); 155 if (ret < 0) 156 goto out_prime_destroy; 157 } 158 159 return file; 160 161 out_prime_destroy: 162 if (drm_core_check_feature(dev, DRIVER_PRIME)) 163 drm_prime_destroy_file_private(&file->prime); 164 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) 165 drm_syncobj_release(file); 166 if (drm_core_check_feature(dev, DRIVER_GEM)) 167 drm_gem_release(dev, file); 168 put_pid(file->pid); 169 kfree(file); 170 171 return ERR_PTR(ret); 172 } 173 174 static void drm_events_release(struct drm_file *file_priv) 175 { 176 struct drm_device *dev = file_priv->minor->dev; 177 struct drm_pending_event *e, *et; 178 unsigned long flags; 179 180 spin_lock_irqsave(&dev->event_lock, flags); 181 182 /* Unlink pending events */ 183 list_for_each_entry_safe(e, et, &file_priv->pending_event_list, 184 pending_link) { 185 list_del(&e->pending_link); 186 e->file_priv = NULL; 187 } 188 189 /* Remove unconsumed events */ 190 list_for_each_entry_safe(e, et, &file_priv->event_list, link) { 191 list_del(&e->link); 192 kfree(e); 193 } 194 195 spin_unlock_irqrestore(&dev->event_lock, flags); 196 } 197 198 /** 199 * drm_file_free - free file context 200 * @file: context to free, or NULL 201 * 202 * This destroys and deallocates a DRM file context previously allocated via 203 * drm_file_alloc(). The caller must make sure to unlink it from any contexts 204 * before calling this. 205 * 206 * If NULL is passed, this is a no-op. 207 * 208 * RETURNS: 209 * 0 on success, or error code on failure. 210 */ 211 void drm_file_free(struct drm_file *file) 212 { 213 struct drm_device *dev; 214 215 if (!file) 216 return; 217 218 dev = file->minor->dev; 219 220 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", 221 task_pid_nr(current), 222 (long)old_encode_dev(file->minor->kdev->devt), 223 dev->open_count); 224 225 if (drm_core_check_feature(dev, DRIVER_LEGACY) && 226 dev->driver->preclose) 227 dev->driver->preclose(dev, file); 228 229 if (drm_core_check_feature(dev, DRIVER_LEGACY)) 230 drm_legacy_lock_release(dev, file->filp); 231 232 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 233 drm_legacy_reclaim_buffers(dev, file); 234 235 drm_events_release(file); 236 237 if (drm_core_check_feature(dev, DRIVER_MODESET)) { 238 drm_fb_release(file); 239 drm_property_destroy_user_blobs(dev, file); 240 } 241 242 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) 243 drm_syncobj_release(file); 244 245 if (drm_core_check_feature(dev, DRIVER_GEM)) 246 drm_gem_release(dev, file); 247 248 drm_legacy_ctxbitmap_flush(dev, file); 249 250 if (drm_is_primary_client(file)) 251 drm_master_release(file); 252 253 if (dev->driver->postclose) 254 dev->driver->postclose(dev, file); 255 256 if (drm_core_check_feature(dev, DRIVER_PRIME)) 257 drm_prime_destroy_file_private(&file->prime); 258 259 WARN_ON(!list_empty(&file->event_list)); 260 261 put_pid(file->pid); 262 kfree(file); 263 } 264 265 static void drm_close_helper(struct file *filp) 266 { 267 struct drm_file *file_priv = filp->private_data; 268 struct drm_device *dev = file_priv->minor->dev; 269 270 mutex_lock(&dev->filelist_mutex); 271 list_del(&file_priv->lhead); 272 mutex_unlock(&dev->filelist_mutex); 273 274 drm_file_free(file_priv); 275 } 276 277 static int drm_setup(struct drm_device * dev) 278 { 279 int ret; 280 281 if (dev->driver->firstopen && 282 drm_core_check_feature(dev, DRIVER_LEGACY)) { 283 ret = dev->driver->firstopen(dev); 284 if (ret != 0) 285 return ret; 286 } 287 288 ret = drm_legacy_dma_setup(dev); 289 if (ret < 0) 290 return ret; 291 292 293 DRM_DEBUG("\n"); 294 return 0; 295 } 296 297 /** 298 * drm_open - open method for DRM file 299 * @inode: device inode 300 * @filp: file pointer. 301 * 302 * This function must be used by drivers as their &file_operations.open method. 303 * It looks up the correct DRM device and instantiates all the per-file 304 * resources for it. It also calls the &drm_driver.open driver callback. 305 * 306 * RETURNS: 307 * 308 * 0 on success or negative errno value on falure. 309 */ 310 int drm_open(struct inode *inode, struct file *filp) 311 { 312 struct drm_device *dev; 313 struct drm_minor *minor; 314 int retcode; 315 int need_setup = 0; 316 317 minor = drm_minor_acquire(iminor(inode)); 318 if (IS_ERR(minor)) 319 return PTR_ERR(minor); 320 321 dev = minor->dev; 322 if (!dev->open_count++) 323 need_setup = 1; 324 325 /* share address_space across all char-devs of a single device */ 326 filp->f_mapping = dev->anon_inode->i_mapping; 327 328 retcode = drm_open_helper(filp, minor); 329 if (retcode) 330 goto err_undo; 331 if (need_setup) { 332 retcode = drm_setup(dev); 333 if (retcode) { 334 drm_close_helper(filp); 335 goto err_undo; 336 } 337 } 338 return 0; 339 340 err_undo: 341 dev->open_count--; 342 drm_minor_release(minor); 343 return retcode; 344 } 345 EXPORT_SYMBOL(drm_open); 346 347 /* 348 * Check whether DRI will run on this CPU. 349 * 350 * \return non-zero if the DRI will run on this CPU, or zero otherwise. 351 */ 352 static int drm_cpu_valid(void) 353 { 354 #if defined(__sparc__) && !defined(__sparc_v9__) 355 return 0; /* No cmpxchg before v9 sparc. */ 356 #endif 357 return 1; 358 } 359 360 /* 361 * Called whenever a process opens /dev/drm. 362 * 363 * \param filp file pointer. 364 * \param minor acquired minor-object. 365 * \return zero on success or a negative number on failure. 366 * 367 * Creates and initializes a drm_file structure for the file private data in \p 368 * filp and add it into the double linked list in \p dev. 369 */ 370 static int drm_open_helper(struct file *filp, struct drm_minor *minor) 371 { 372 struct drm_device *dev = minor->dev; 373 struct drm_file *priv; 374 int ret; 375 376 if (filp->f_flags & O_EXCL) 377 return -EBUSY; /* No exclusive opens */ 378 if (!drm_cpu_valid()) 379 return -EINVAL; 380 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF) 381 return -EINVAL; 382 383 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index); 384 385 priv = drm_file_alloc(minor); 386 if (IS_ERR(priv)) 387 return PTR_ERR(priv); 388 389 if (drm_is_primary_client(priv)) { 390 ret = drm_master_open(priv); 391 if (ret) { 392 drm_file_free(priv); 393 return ret; 394 } 395 } 396 397 filp->private_data = priv; 398 filp->f_mode |= FMODE_UNSIGNED_OFFSET; 399 priv->filp = filp; 400 401 mutex_lock(&dev->filelist_mutex); 402 list_add(&priv->lhead, &dev->filelist); 403 mutex_unlock(&dev->filelist_mutex); 404 405 #ifdef __alpha__ 406 /* 407 * Default the hose 408 */ 409 if (!dev->hose) { 410 struct pci_dev *pci_dev; 411 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); 412 if (pci_dev) { 413 dev->hose = pci_dev->sysdata; 414 pci_dev_put(pci_dev); 415 } 416 if (!dev->hose) { 417 struct pci_bus *b = list_entry(pci_root_buses.next, 418 struct pci_bus, node); 419 if (b) 420 dev->hose = b->sysdata; 421 } 422 } 423 #endif 424 425 return 0; 426 } 427 428 static void drm_legacy_dev_reinit(struct drm_device *dev) 429 { 430 if (dev->irq_enabled) 431 drm_irq_uninstall(dev); 432 433 mutex_lock(&dev->struct_mutex); 434 435 drm_legacy_agp_clear(dev); 436 437 drm_legacy_sg_cleanup(dev); 438 drm_legacy_vma_flush(dev); 439 drm_legacy_dma_takedown(dev); 440 441 mutex_unlock(&dev->struct_mutex); 442 443 dev->sigdata.lock = NULL; 444 445 dev->context_flag = 0; 446 dev->last_context = 0; 447 dev->if_version = 0; 448 449 DRM_DEBUG("lastclose completed\n"); 450 } 451 452 void drm_lastclose(struct drm_device * dev) 453 { 454 DRM_DEBUG("\n"); 455 456 if (dev->driver->lastclose) 457 dev->driver->lastclose(dev); 458 DRM_DEBUG("driver lastclose completed\n"); 459 460 if (drm_core_check_feature(dev, DRIVER_LEGACY)) 461 drm_legacy_dev_reinit(dev); 462 463 drm_client_dev_restore(dev); 464 } 465 466 /** 467 * drm_release - release method for DRM file 468 * @inode: device inode 469 * @filp: file pointer. 470 * 471 * This function must be used by drivers as their &file_operations.release 472 * method. It frees any resources associated with the open file, and calls the 473 * &drm_driver.postclose driver callback. If this is the last open file for the 474 * DRM device also proceeds to call the &drm_driver.lastclose driver callback. 475 * 476 * RETURNS: 477 * 478 * Always succeeds and returns 0. 479 */ 480 int drm_release(struct inode *inode, struct file *filp) 481 { 482 struct drm_file *file_priv = filp->private_data; 483 struct drm_minor *minor = file_priv->minor; 484 struct drm_device *dev = minor->dev; 485 486 mutex_lock(&drm_global_mutex); 487 488 DRM_DEBUG("open_count = %d\n", dev->open_count); 489 490 drm_close_helper(filp); 491 492 if (!--dev->open_count) { 493 drm_lastclose(dev); 494 if (drm_dev_is_unplugged(dev)) 495 drm_put_dev(dev); 496 } 497 mutex_unlock(&drm_global_mutex); 498 499 drm_minor_release(minor); 500 501 return 0; 502 } 503 EXPORT_SYMBOL(drm_release); 504 505 /** 506 * drm_read - read method for DRM file 507 * @filp: file pointer 508 * @buffer: userspace destination pointer for the read 509 * @count: count in bytes to read 510 * @offset: offset to read 511 * 512 * This function must be used by drivers as their &file_operations.read 513 * method iff they use DRM events for asynchronous signalling to userspace. 514 * Since events are used by the KMS API for vblank and page flip completion this 515 * means all modern display drivers must use it. 516 * 517 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also 518 * must set the &file_operation.llseek to no_llseek(). Polling support is 519 * provided by drm_poll(). 520 * 521 * This function will only ever read a full event. Therefore userspace must 522 * supply a big enough buffer to fit any event to ensure forward progress. Since 523 * the maximum event space is currently 4K it's recommended to just use that for 524 * safety. 525 * 526 * RETURNS: 527 * 528 * Number of bytes read (always aligned to full events, and can be 0) or a 529 * negative error code on failure. 530 */ 531 ssize_t drm_read(struct file *filp, char __user *buffer, 532 size_t count, loff_t *offset) 533 { 534 struct drm_file *file_priv = filp->private_data; 535 struct drm_device *dev = file_priv->minor->dev; 536 ssize_t ret; 537 538 if (!access_ok(buffer, count)) 539 return -EFAULT; 540 541 ret = mutex_lock_interruptible(&file_priv->event_read_lock); 542 if (ret) 543 return ret; 544 545 for (;;) { 546 struct drm_pending_event *e = NULL; 547 548 spin_lock_irq(&dev->event_lock); 549 if (!list_empty(&file_priv->event_list)) { 550 e = list_first_entry(&file_priv->event_list, 551 struct drm_pending_event, link); 552 file_priv->event_space += e->event->length; 553 list_del(&e->link); 554 } 555 spin_unlock_irq(&dev->event_lock); 556 557 if (e == NULL) { 558 if (ret) 559 break; 560 561 if (filp->f_flags & O_NONBLOCK) { 562 ret = -EAGAIN; 563 break; 564 } 565 566 mutex_unlock(&file_priv->event_read_lock); 567 ret = wait_event_interruptible(file_priv->event_wait, 568 !list_empty(&file_priv->event_list)); 569 if (ret >= 0) 570 ret = mutex_lock_interruptible(&file_priv->event_read_lock); 571 if (ret) 572 return ret; 573 } else { 574 unsigned length = e->event->length; 575 576 if (length > count - ret) { 577 put_back_event: 578 spin_lock_irq(&dev->event_lock); 579 file_priv->event_space -= length; 580 list_add(&e->link, &file_priv->event_list); 581 spin_unlock_irq(&dev->event_lock); 582 break; 583 } 584 585 if (copy_to_user(buffer + ret, e->event, length)) { 586 if (ret == 0) 587 ret = -EFAULT; 588 goto put_back_event; 589 } 590 591 ret += length; 592 kfree(e); 593 } 594 } 595 mutex_unlock(&file_priv->event_read_lock); 596 597 return ret; 598 } 599 EXPORT_SYMBOL(drm_read); 600 601 /** 602 * drm_poll - poll method for DRM file 603 * @filp: file pointer 604 * @wait: poll waiter table 605 * 606 * This function must be used by drivers as their &file_operations.read method 607 * iff they use DRM events for asynchronous signalling to userspace. Since 608 * events are used by the KMS API for vblank and page flip completion this means 609 * all modern display drivers must use it. 610 * 611 * See also drm_read(). 612 * 613 * RETURNS: 614 * 615 * Mask of POLL flags indicating the current status of the file. 616 */ 617 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait) 618 { 619 struct drm_file *file_priv = filp->private_data; 620 __poll_t mask = 0; 621 622 poll_wait(filp, &file_priv->event_wait, wait); 623 624 if (!list_empty(&file_priv->event_list)) 625 mask |= EPOLLIN | EPOLLRDNORM; 626 627 return mask; 628 } 629 EXPORT_SYMBOL(drm_poll); 630 631 /** 632 * drm_event_reserve_init_locked - init a DRM event and reserve space for it 633 * @dev: DRM device 634 * @file_priv: DRM file private data 635 * @p: tracking structure for the pending event 636 * @e: actual event data to deliver to userspace 637 * 638 * This function prepares the passed in event for eventual delivery. If the event 639 * doesn't get delivered (because the IOCTL fails later on, before queuing up 640 * anything) then the even must be cancelled and freed using 641 * drm_event_cancel_free(). Successfully initialized events should be sent out 642 * using drm_send_event() or drm_send_event_locked() to signal completion of the 643 * asynchronous event to userspace. 644 * 645 * If callers embedded @p into a larger structure it must be allocated with 646 * kmalloc and @p must be the first member element. 647 * 648 * This is the locked version of drm_event_reserve_init() for callers which 649 * already hold &drm_device.event_lock. 650 * 651 * RETURNS: 652 * 653 * 0 on success or a negative error code on failure. 654 */ 655 int drm_event_reserve_init_locked(struct drm_device *dev, 656 struct drm_file *file_priv, 657 struct drm_pending_event *p, 658 struct drm_event *e) 659 { 660 if (file_priv->event_space < e->length) 661 return -ENOMEM; 662 663 file_priv->event_space -= e->length; 664 665 p->event = e; 666 list_add(&p->pending_link, &file_priv->pending_event_list); 667 p->file_priv = file_priv; 668 669 return 0; 670 } 671 EXPORT_SYMBOL(drm_event_reserve_init_locked); 672 673 /** 674 * drm_event_reserve_init - init a DRM event and reserve space for it 675 * @dev: DRM device 676 * @file_priv: DRM file private data 677 * @p: tracking structure for the pending event 678 * @e: actual event data to deliver to userspace 679 * 680 * This function prepares the passed in event for eventual delivery. If the event 681 * doesn't get delivered (because the IOCTL fails later on, before queuing up 682 * anything) then the even must be cancelled and freed using 683 * drm_event_cancel_free(). Successfully initialized events should be sent out 684 * using drm_send_event() or drm_send_event_locked() to signal completion of the 685 * asynchronous event to userspace. 686 * 687 * If callers embedded @p into a larger structure it must be allocated with 688 * kmalloc and @p must be the first member element. 689 * 690 * Callers which already hold &drm_device.event_lock should use 691 * drm_event_reserve_init_locked() instead. 692 * 693 * RETURNS: 694 * 695 * 0 on success or a negative error code on failure. 696 */ 697 int drm_event_reserve_init(struct drm_device *dev, 698 struct drm_file *file_priv, 699 struct drm_pending_event *p, 700 struct drm_event *e) 701 { 702 unsigned long flags; 703 int ret; 704 705 spin_lock_irqsave(&dev->event_lock, flags); 706 ret = drm_event_reserve_init_locked(dev, file_priv, p, e); 707 spin_unlock_irqrestore(&dev->event_lock, flags); 708 709 return ret; 710 } 711 EXPORT_SYMBOL(drm_event_reserve_init); 712 713 /** 714 * drm_event_cancel_free - free a DRM event and release its space 715 * @dev: DRM device 716 * @p: tracking structure for the pending event 717 * 718 * This function frees the event @p initialized with drm_event_reserve_init() 719 * and releases any allocated space. It is used to cancel an event when the 720 * nonblocking operation could not be submitted and needed to be aborted. 721 */ 722 void drm_event_cancel_free(struct drm_device *dev, 723 struct drm_pending_event *p) 724 { 725 unsigned long flags; 726 spin_lock_irqsave(&dev->event_lock, flags); 727 if (p->file_priv) { 728 p->file_priv->event_space += p->event->length; 729 list_del(&p->pending_link); 730 } 731 spin_unlock_irqrestore(&dev->event_lock, flags); 732 733 if (p->fence) 734 dma_fence_put(p->fence); 735 736 kfree(p); 737 } 738 EXPORT_SYMBOL(drm_event_cancel_free); 739 740 /** 741 * drm_send_event_locked - send DRM event to file descriptor 742 * @dev: DRM device 743 * @e: DRM event to deliver 744 * 745 * This function sends the event @e, initialized with drm_event_reserve_init(), 746 * to its associated userspace DRM file. Callers must already hold 747 * &drm_device.event_lock, see drm_send_event() for the unlocked version. 748 * 749 * Note that the core will take care of unlinking and disarming events when the 750 * corresponding DRM file is closed. Drivers need not worry about whether the 751 * DRM file for this event still exists and can call this function upon 752 * completion of the asynchronous work unconditionally. 753 */ 754 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e) 755 { 756 assert_spin_locked(&dev->event_lock); 757 758 if (e->completion) { 759 complete_all(e->completion); 760 e->completion_release(e->completion); 761 e->completion = NULL; 762 } 763 764 if (e->fence) { 765 dma_fence_signal(e->fence); 766 dma_fence_put(e->fence); 767 } 768 769 if (!e->file_priv) { 770 kfree(e); 771 return; 772 } 773 774 list_del(&e->pending_link); 775 list_add_tail(&e->link, 776 &e->file_priv->event_list); 777 wake_up_interruptible(&e->file_priv->event_wait); 778 } 779 EXPORT_SYMBOL(drm_send_event_locked); 780 781 /** 782 * drm_send_event - send DRM event to file descriptor 783 * @dev: DRM device 784 * @e: DRM event to deliver 785 * 786 * This function sends the event @e, initialized with drm_event_reserve_init(), 787 * to its associated userspace DRM file. This function acquires 788 * &drm_device.event_lock, see drm_send_event_locked() for callers which already 789 * hold this lock. 790 * 791 * Note that the core will take care of unlinking and disarming events when the 792 * corresponding DRM file is closed. Drivers need not worry about whether the 793 * DRM file for this event still exists and can call this function upon 794 * completion of the asynchronous work unconditionally. 795 */ 796 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) 797 { 798 unsigned long irqflags; 799 800 spin_lock_irqsave(&dev->event_lock, irqflags); 801 drm_send_event_locked(dev, e); 802 spin_unlock_irqrestore(&dev->event_lock, irqflags); 803 } 804 EXPORT_SYMBOL(drm_send_event); 805