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/anon_inodes.h> 35 #include <linux/dma-fence.h> 36 #include <linux/export.h> 37 #include <linux/file.h> 38 #include <linux/module.h> 39 #include <linux/pci.h> 40 #include <linux/poll.h> 41 #include <linux/slab.h> 42 #include <linux/vga_switcheroo.h> 43 44 #include <drm/drm_client_event.h> 45 #include <drm/drm_drv.h> 46 #include <drm/drm_file.h> 47 #include <drm/drm_gem.h> 48 #include <drm/drm_print.h> 49 #include <drm/drm_debugfs.h> 50 51 #include "drm_crtc_internal.h" 52 #include "drm_internal.h" 53 54 /* from BKL pushdown */ 55 DEFINE_MUTEX(drm_global_mutex); 56 57 bool drm_dev_needs_global_mutex(struct drm_device *dev) 58 { 59 /* 60 * The deprecated ->load callback must be called after the driver is 61 * already registered. This means such drivers rely on the BKL to make 62 * sure an open can't proceed until the driver is actually fully set up. 63 * Similar hilarity holds for the unload callback. 64 */ 65 if (dev->driver->load || dev->driver->unload) 66 return true; 67 68 return false; 69 } 70 71 /** 72 * DOC: file operations 73 * 74 * Drivers must define the file operations structure that forms the DRM 75 * userspace API entry point, even though most of those operations are 76 * implemented in the DRM core. The resulting &struct file_operations must be 77 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(), 78 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled 79 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no 80 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls 81 * that require 32/64 bit compatibility support must provide their own 82 * &file_operations.compat_ioctl handler that processes private ioctls and calls 83 * drm_compat_ioctl() for core ioctls. 84 * 85 * In addition drm_read() and drm_poll() provide support for DRM events. DRM 86 * events are a generic and extensible means to send asynchronous events to 87 * userspace through the file descriptor. They are used to send vblank event and 88 * page flip completions by the KMS API. But drivers can also use it for their 89 * own needs, e.g. to signal completion of rendering. 90 * 91 * For the driver-side event interface see drm_event_reserve_init() and 92 * drm_send_event() as the main starting points. 93 * 94 * The memory mapping implementation will vary depending on how the driver 95 * manages memory. For GEM-based drivers this is drm_gem_mmap(). 96 * 97 * No other file operations are supported by the DRM userspace API. Overall the 98 * following is an example &file_operations structure:: 99 * 100 * static const example_drm_fops = { 101 * .owner = THIS_MODULE, 102 * .open = drm_open, 103 * .release = drm_release, 104 * .unlocked_ioctl = drm_ioctl, 105 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n 106 * .poll = drm_poll, 107 * .read = drm_read, 108 * .mmap = drm_gem_mmap, 109 * }; 110 * 111 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for 112 * DMA based drivers there is the DEFINE_DRM_GEM_DMA_FOPS() macro to make this 113 * simpler. 114 * 115 * The driver's &file_operations must be stored in &drm_driver.fops. 116 * 117 * For driver-private IOCTL handling see the more detailed discussion in 118 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`. 119 */ 120 121 /** 122 * drm_file_alloc - allocate file context 123 * @minor: minor to allocate on 124 * 125 * This allocates a new DRM file context. It is not linked into any context and 126 * can be used by the caller freely. Note that the context keeps a pointer to 127 * @minor, so it must be freed before @minor is. 128 * 129 * RETURNS: 130 * Pointer to newly allocated context, ERR_PTR on failure. 131 */ 132 struct drm_file *drm_file_alloc(struct drm_minor *minor) 133 { 134 static atomic64_t ident = ATOMIC64_INIT(0); 135 struct drm_device *dev = minor->dev; 136 struct drm_file *file; 137 int ret; 138 139 file = kzalloc_obj(*file); 140 if (!file) 141 return ERR_PTR(-ENOMEM); 142 143 /* Get a unique identifier for fdinfo: */ 144 file->client_id = atomic64_inc_return(&ident); 145 rcu_assign_pointer(file->pid, get_pid(task_tgid(current))); 146 file->minor = minor; 147 148 /* for compatibility root is always authenticated */ 149 file->authenticated = capable(CAP_SYS_ADMIN); 150 151 INIT_LIST_HEAD(&file->lhead); 152 INIT_LIST_HEAD(&file->fbs); 153 mutex_init(&file->fbs_lock); 154 INIT_LIST_HEAD(&file->blobs); 155 INIT_LIST_HEAD(&file->pending_event_list); 156 INIT_LIST_HEAD(&file->event_list); 157 init_waitqueue_head(&file->event_wait); 158 file->event_space = 4096; /* set aside 4k for event buffer */ 159 160 spin_lock_init(&file->master_lookup_lock); 161 mutex_init(&file->event_read_lock); 162 mutex_init(&file->client_name_lock); 163 164 if (drm_core_check_feature(dev, DRIVER_GEM)) 165 drm_gem_open(dev, file); 166 167 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) 168 drm_syncobj_open(file); 169 170 drm_prime_init_file_private(&file->prime); 171 172 if (!drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL)) 173 drm_debugfs_clients_add(file); 174 175 if (dev->driver->open) { 176 ret = dev->driver->open(dev, file); 177 if (ret < 0) 178 goto out_prime_destroy; 179 } 180 181 return file; 182 183 out_prime_destroy: 184 drm_prime_destroy_file_private(&file->prime); 185 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) 186 drm_syncobj_release(file); 187 if (drm_core_check_feature(dev, DRIVER_GEM)) 188 drm_gem_release(dev, file); 189 190 if (!drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL)) 191 drm_debugfs_clients_remove(file); 192 193 put_pid(rcu_access_pointer(file->pid)); 194 kfree(file); 195 196 return ERR_PTR(ret); 197 } 198 199 static void drm_events_release(struct drm_file *file_priv) 200 { 201 struct drm_device *dev = file_priv->minor->dev; 202 struct drm_pending_event *e, *et; 203 unsigned long flags; 204 205 spin_lock_irqsave(&dev->event_lock, flags); 206 207 /* Unlink pending events */ 208 list_for_each_entry_safe(e, et, &file_priv->pending_event_list, 209 pending_link) { 210 list_del(&e->pending_link); 211 e->file_priv = NULL; 212 } 213 214 /* Remove unconsumed events */ 215 list_for_each_entry_safe(e, et, &file_priv->event_list, link) { 216 list_del(&e->link); 217 kfree(e); 218 } 219 220 spin_unlock_irqrestore(&dev->event_lock, flags); 221 } 222 223 /** 224 * drm_file_free - free file context 225 * @file: context to free, or NULL 226 * 227 * This destroys and deallocates a DRM file context previously allocated via 228 * drm_file_alloc(). The caller must make sure to unlink it from any contexts 229 * before calling this. 230 * 231 * If NULL is passed, this is a no-op. 232 */ 233 void drm_file_free(struct drm_file *file) 234 { 235 struct drm_device *dev; 236 int idx; 237 238 if (!file) 239 return; 240 241 dev = file->minor->dev; 242 243 drm_dbg_core(dev, "comm=\"%s\", pid=%d, dev=0x%lx, open_count=%d\n", 244 current->comm, task_pid_nr(current), 245 (long)old_encode_dev(file->minor->kdev->devt), 246 atomic_read(&dev->open_count)); 247 248 if (!drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL)) 249 drm_debugfs_clients_remove(file); 250 251 drm_events_release(file); 252 253 if (drm_core_check_feature(dev, DRIVER_MODESET) && 254 drm_dev_enter(dev, &idx)) { 255 drm_fb_release(file); 256 drm_property_destroy_user_blobs(dev, file); 257 drm_dev_exit(idx); 258 } 259 260 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ)) 261 drm_syncobj_release(file); 262 263 if (drm_core_check_feature(dev, DRIVER_GEM)) 264 drm_gem_release(dev, file); 265 266 if (drm_is_primary_client(file)) 267 drm_master_release(file); 268 269 if (dev->driver->postclose) 270 dev->driver->postclose(dev, file); 271 272 drm_prime_destroy_file_private(&file->prime); 273 274 WARN_ON(!list_empty(&file->event_list)); 275 276 put_pid(rcu_access_pointer(file->pid)); 277 278 mutex_destroy(&file->client_name_lock); 279 kfree(file->client_name); 280 281 kfree(file); 282 } 283 284 static void drm_close_helper(struct file *filp) 285 { 286 struct drm_file *file_priv = filp->private_data; 287 struct drm_device *dev = file_priv->minor->dev; 288 289 mutex_lock(&dev->filelist_mutex); 290 list_del(&file_priv->lhead); 291 mutex_unlock(&dev->filelist_mutex); 292 293 drm_file_free(file_priv); 294 } 295 296 /* 297 * Check whether DRI will run on this CPU. 298 * 299 * \return non-zero if the DRI will run on this CPU, or zero otherwise. 300 */ 301 static int drm_cpu_valid(void) 302 { 303 #if defined(__sparc__) && !defined(__sparc_v9__) 304 return 0; /* No cmpxchg before v9 sparc. */ 305 #endif 306 return 1; 307 } 308 309 /* 310 * Called whenever a process opens a drm node 311 * 312 * \param filp file pointer. 313 * \param minor acquired minor-object. 314 * \return zero on success or a negative number on failure. 315 * 316 * Creates and initializes a drm_file structure for the file private data in \p 317 * filp and add it into the double linked list in \p dev. 318 */ 319 int drm_open_helper(struct file *filp, struct drm_minor *minor) 320 { 321 struct drm_device *dev = minor->dev; 322 struct drm_file *priv; 323 int ret; 324 325 if (filp->f_flags & O_EXCL) 326 return -EBUSY; /* No exclusive opens */ 327 if (!drm_cpu_valid()) 328 return -EINVAL; 329 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && 330 dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF) 331 return -EINVAL; 332 if (WARN_ON_ONCE(!(filp->f_op->fop_flags & FOP_UNSIGNED_OFFSET))) 333 return -EINVAL; 334 335 drm_dbg_core(dev, "comm=\"%s\", pid=%d, minor=%d\n", 336 current->comm, task_pid_nr(current), minor->index); 337 338 priv = drm_file_alloc(minor); 339 if (IS_ERR(priv)) 340 return PTR_ERR(priv); 341 342 if (drm_is_primary_client(priv)) { 343 ret = drm_master_open(priv); 344 if (ret) { 345 drm_file_free(priv); 346 return ret; 347 } 348 } 349 350 filp->private_data = priv; 351 priv->filp = filp; 352 353 mutex_lock(&dev->filelist_mutex); 354 list_add(&priv->lhead, &dev->filelist); 355 mutex_unlock(&dev->filelist_mutex); 356 357 return 0; 358 } 359 360 /** 361 * drm_open - open method for DRM file 362 * @inode: device inode 363 * @filp: file pointer. 364 * 365 * This function must be used by drivers as their &file_operations.open method. 366 * It looks up the correct DRM device and instantiates all the per-file 367 * resources for it. It also calls the &drm_driver.open driver callback. 368 * 369 * RETURNS: 370 * 0 on success or negative errno value on failure. 371 */ 372 int drm_open(struct inode *inode, struct file *filp) 373 { 374 struct drm_device *dev; 375 struct drm_minor *minor; 376 int retcode; 377 378 minor = drm_minor_acquire(&drm_minors_xa, iminor(inode)); 379 if (IS_ERR(minor)) 380 return PTR_ERR(minor); 381 382 dev = minor->dev; 383 if (drm_dev_needs_global_mutex(dev)) 384 mutex_lock(&drm_global_mutex); 385 386 atomic_fetch_inc(&dev->open_count); 387 388 /* share address_space across all char-devs of a single device */ 389 filp->f_mapping = dev->anon_inode->i_mapping; 390 391 retcode = drm_open_helper(filp, minor); 392 if (retcode) 393 goto err_undo; 394 395 if (drm_dev_needs_global_mutex(dev)) 396 mutex_unlock(&drm_global_mutex); 397 398 return 0; 399 400 err_undo: 401 atomic_dec(&dev->open_count); 402 if (drm_dev_needs_global_mutex(dev)) 403 mutex_unlock(&drm_global_mutex); 404 drm_minor_release(minor); 405 return retcode; 406 } 407 EXPORT_SYMBOL(drm_open); 408 409 static void drm_lastclose(struct drm_device *dev) 410 { 411 drm_client_dev_restore(dev, false); 412 413 if (dev_is_pci(dev->dev)) 414 vga_switcheroo_process_delayed_switch(); 415 } 416 417 /** 418 * drm_release - release method for DRM file 419 * @inode: device inode 420 * @filp: file pointer. 421 * 422 * This function must be used by drivers as their &file_operations.release 423 * method. It frees any resources associated with the open file. If this 424 * is the last open file for the DRM device, it also restores the active 425 * in-kernel DRM client. 426 * 427 * RETURNS: 428 * Always succeeds and returns 0. 429 */ 430 int drm_release(struct inode *inode, struct file *filp) 431 { 432 struct drm_file *file_priv = filp->private_data; 433 struct drm_minor *minor = file_priv->minor; 434 struct drm_device *dev = minor->dev; 435 436 if (drm_dev_needs_global_mutex(dev)) 437 mutex_lock(&drm_global_mutex); 438 439 drm_dbg_core(dev, "open_count = %d\n", atomic_read(&dev->open_count)); 440 441 drm_close_helper(filp); 442 443 if (atomic_dec_and_test(&dev->open_count)) 444 drm_lastclose(dev); 445 446 if (drm_dev_needs_global_mutex(dev)) 447 mutex_unlock(&drm_global_mutex); 448 449 drm_minor_release(minor); 450 451 return 0; 452 } 453 EXPORT_SYMBOL(drm_release); 454 455 void drm_file_update_pid(struct drm_file *filp) 456 { 457 struct drm_device *dev; 458 struct pid *pid, *old; 459 460 /* 461 * Master nodes need to keep the original ownership in order for 462 * drm_master_check_perm to keep working correctly. (See comment in 463 * drm_auth.c.) 464 */ 465 if (filp->was_master) 466 return; 467 468 pid = task_tgid(current); 469 470 /* 471 * Quick unlocked check since the model is a single handover followed by 472 * exclusive repeated use. 473 */ 474 if (pid == rcu_access_pointer(filp->pid)) 475 return; 476 477 dev = filp->minor->dev; 478 mutex_lock(&dev->filelist_mutex); 479 get_pid(pid); 480 old = rcu_replace_pointer(filp->pid, pid, 1); 481 mutex_unlock(&dev->filelist_mutex); 482 483 synchronize_rcu(); 484 put_pid(old); 485 } 486 487 /** 488 * drm_release_noglobal - release method for DRM file 489 * @inode: device inode 490 * @filp: file pointer. 491 * 492 * This function may be used by drivers as their &file_operations.release 493 * method. It frees any resources associated with the open file prior to taking 494 * the drm_global_mutex. If this is the last open file for the DRM device, it 495 * then restores the active in-kernel DRM client. 496 * 497 * RETURNS: 498 * Always succeeds and returns 0. 499 */ 500 int drm_release_noglobal(struct inode *inode, struct file *filp) 501 { 502 struct drm_file *file_priv = filp->private_data; 503 struct drm_minor *minor = file_priv->minor; 504 struct drm_device *dev = minor->dev; 505 506 drm_close_helper(filp); 507 508 if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) { 509 drm_lastclose(dev); 510 mutex_unlock(&drm_global_mutex); 511 } 512 513 drm_minor_release(minor); 514 515 return 0; 516 } 517 EXPORT_SYMBOL(drm_release_noglobal); 518 519 /** 520 * drm_read - read method for DRM file 521 * @filp: file pointer 522 * @buffer: userspace destination pointer for the read 523 * @count: count in bytes to read 524 * @offset: offset to read 525 * 526 * This function must be used by drivers as their &file_operations.read 527 * method if they use DRM events for asynchronous signalling to userspace. 528 * Since events are used by the KMS API for vblank and page flip completion this 529 * means all modern display drivers must use it. 530 * 531 * @offset is ignored, DRM events are read like a pipe. Polling support is 532 * provided by drm_poll(). 533 * 534 * This function will only ever read a full event. Therefore userspace must 535 * supply a big enough buffer to fit any event to ensure forward progress. Since 536 * the maximum event space is currently 4K it's recommended to just use that for 537 * safety. 538 * 539 * RETURNS: 540 * Number of bytes read (always aligned to full events, and can be 0) or a 541 * negative error code on failure. 542 */ 543 ssize_t drm_read(struct file *filp, char __user *buffer, 544 size_t count, loff_t *offset) 545 { 546 struct drm_file *file_priv = filp->private_data; 547 struct drm_device *dev = file_priv->minor->dev; 548 ssize_t ret; 549 550 ret = mutex_lock_interruptible(&file_priv->event_read_lock); 551 if (ret) 552 return ret; 553 554 for (;;) { 555 struct drm_pending_event *e = NULL; 556 557 spin_lock_irq(&dev->event_lock); 558 if (!list_empty(&file_priv->event_list)) { 559 e = list_first_entry(&file_priv->event_list, 560 struct drm_pending_event, link); 561 file_priv->event_space += e->event->length; 562 list_del(&e->link); 563 } 564 spin_unlock_irq(&dev->event_lock); 565 566 if (e == NULL) { 567 if (ret) 568 break; 569 570 if (filp->f_flags & O_NONBLOCK) { 571 ret = -EAGAIN; 572 break; 573 } 574 575 mutex_unlock(&file_priv->event_read_lock); 576 ret = wait_event_interruptible(file_priv->event_wait, 577 !list_empty(&file_priv->event_list)); 578 if (ret >= 0) 579 ret = mutex_lock_interruptible(&file_priv->event_read_lock); 580 if (ret) 581 return ret; 582 } else { 583 unsigned length = e->event->length; 584 585 if (length > count - ret) { 586 put_back_event: 587 spin_lock_irq(&dev->event_lock); 588 file_priv->event_space -= length; 589 list_add(&e->link, &file_priv->event_list); 590 spin_unlock_irq(&dev->event_lock); 591 wake_up_interruptible_poll(&file_priv->event_wait, 592 EPOLLIN | EPOLLRDNORM); 593 break; 594 } 595 596 if (copy_to_user(buffer + ret, e->event, length)) { 597 if (ret == 0) 598 ret = -EFAULT; 599 goto put_back_event; 600 } 601 602 ret += length; 603 kfree(e); 604 } 605 } 606 mutex_unlock(&file_priv->event_read_lock); 607 608 return ret; 609 } 610 EXPORT_SYMBOL(drm_read); 611 612 /** 613 * drm_poll - poll method for DRM file 614 * @filp: file pointer 615 * @wait: poll waiter table 616 * 617 * This function must be used by drivers as their &file_operations.read method 618 * if they use DRM events for asynchronous signalling to userspace. Since 619 * events are used by the KMS API for vblank and page flip completion this means 620 * all modern display drivers must use it. 621 * 622 * See also drm_read(). 623 * 624 * RETURNS: 625 * Mask of POLL flags indicating the current status of the file. 626 */ 627 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait) 628 { 629 struct drm_file *file_priv = filp->private_data; 630 __poll_t mask = 0; 631 632 poll_wait(filp, &file_priv->event_wait, wait); 633 634 if (!list_empty(&file_priv->event_list)) 635 mask |= EPOLLIN | EPOLLRDNORM; 636 637 return mask; 638 } 639 EXPORT_SYMBOL(drm_poll); 640 641 /** 642 * drm_event_reserve_init_locked - init a DRM event and reserve space for it 643 * @dev: DRM device 644 * @file_priv: DRM file private data 645 * @p: tracking structure for the pending event 646 * @e: actual event data to deliver to userspace 647 * 648 * This function prepares the passed in event for eventual delivery. If the event 649 * doesn't get delivered (because the IOCTL fails later on, before queuing up 650 * anything) then the even must be cancelled and freed using 651 * drm_event_cancel_free(). Successfully initialized events should be sent out 652 * using drm_send_event() or drm_send_event_locked() to signal completion of the 653 * asynchronous event to userspace. 654 * 655 * If callers embedded @p into a larger structure it must be allocated with 656 * kmalloc and @p must be the first member element. 657 * 658 * This is the locked version of drm_event_reserve_init() for callers which 659 * already hold &drm_device.event_lock. 660 * 661 * RETURNS: 662 * 0 on success or a negative error code on failure. 663 */ 664 int drm_event_reserve_init_locked(struct drm_device *dev, 665 struct drm_file *file_priv, 666 struct drm_pending_event *p, 667 struct drm_event *e) 668 { 669 if (file_priv->event_space < e->length) 670 return -ENOMEM; 671 672 file_priv->event_space -= e->length; 673 674 p->event = e; 675 list_add(&p->pending_link, &file_priv->pending_event_list); 676 p->file_priv = file_priv; 677 678 return 0; 679 } 680 EXPORT_SYMBOL(drm_event_reserve_init_locked); 681 682 /** 683 * drm_event_reserve_init - init a DRM event and reserve space for it 684 * @dev: DRM device 685 * @file_priv: DRM file private data 686 * @p: tracking structure for the pending event 687 * @e: actual event data to deliver to userspace 688 * 689 * This function prepares the passed in event for eventual delivery. If the event 690 * doesn't get delivered (because the IOCTL fails later on, before queuing up 691 * anything) then the even must be cancelled and freed using 692 * drm_event_cancel_free(). Successfully initialized events should be sent out 693 * using drm_send_event() or drm_send_event_locked() to signal completion of the 694 * asynchronous event to userspace. 695 * 696 * If callers embedded @p into a larger structure it must be allocated with 697 * kmalloc and @p must be the first member element. 698 * 699 * Callers which already hold &drm_device.event_lock should use 700 * drm_event_reserve_init_locked() instead. 701 * 702 * RETURNS: 703 * 0 on success or a negative error code on failure. 704 */ 705 int drm_event_reserve_init(struct drm_device *dev, 706 struct drm_file *file_priv, 707 struct drm_pending_event *p, 708 struct drm_event *e) 709 { 710 unsigned long flags; 711 int ret; 712 713 spin_lock_irqsave(&dev->event_lock, flags); 714 ret = drm_event_reserve_init_locked(dev, file_priv, p, e); 715 spin_unlock_irqrestore(&dev->event_lock, flags); 716 717 return ret; 718 } 719 EXPORT_SYMBOL(drm_event_reserve_init); 720 721 /** 722 * drm_event_cancel_free - free a DRM event and release its space 723 * @dev: DRM device 724 * @p: tracking structure for the pending event 725 * 726 * This function frees the event @p initialized with drm_event_reserve_init() 727 * and releases any allocated space. It is used to cancel an event when the 728 * nonblocking operation could not be submitted and needed to be aborted. 729 */ 730 void drm_event_cancel_free(struct drm_device *dev, 731 struct drm_pending_event *p) 732 { 733 unsigned long flags; 734 735 spin_lock_irqsave(&dev->event_lock, flags); 736 if (p->file_priv) { 737 p->file_priv->event_space += p->event->length; 738 list_del(&p->pending_link); 739 } 740 spin_unlock_irqrestore(&dev->event_lock, flags); 741 742 if (p->fence) 743 dma_fence_put(p->fence); 744 745 kfree(p); 746 } 747 EXPORT_SYMBOL(drm_event_cancel_free); 748 749 static void drm_send_event_helper(struct drm_device *dev, 750 struct drm_pending_event *e, ktime_t timestamp) 751 { 752 assert_spin_locked(&dev->event_lock); 753 754 if (e->completion) { 755 complete_all(e->completion); 756 e->completion_release(e->completion); 757 e->completion = NULL; 758 } 759 760 if (e->fence) { 761 if (timestamp) 762 dma_fence_signal_timestamp(e->fence, timestamp); 763 else 764 dma_fence_signal(e->fence); 765 dma_fence_put(e->fence); 766 } 767 768 if (!e->file_priv) { 769 kfree(e); 770 return; 771 } 772 773 list_del(&e->pending_link); 774 list_add_tail(&e->link, 775 &e->file_priv->event_list); 776 wake_up_interruptible_poll(&e->file_priv->event_wait, 777 EPOLLIN | EPOLLRDNORM); 778 } 779 780 /** 781 * drm_send_event_timestamp_locked - send DRM event to file descriptor 782 * @dev: DRM device 783 * @e: DRM event to deliver 784 * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC 785 * time domain 786 * 787 * This function sends the event @e, initialized with drm_event_reserve_init(), 788 * to its associated userspace DRM file. Callers must already hold 789 * &drm_device.event_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_timestamp_locked(struct drm_device *dev, 797 struct drm_pending_event *e, ktime_t timestamp) 798 { 799 drm_send_event_helper(dev, e, timestamp); 800 } 801 EXPORT_SYMBOL(drm_send_event_timestamp_locked); 802 803 /** 804 * drm_send_event_locked - send DRM event to file descriptor 805 * @dev: DRM device 806 * @e: DRM event to deliver 807 * 808 * This function sends the event @e, initialized with drm_event_reserve_init(), 809 * to its associated userspace DRM file. Callers must already hold 810 * &drm_device.event_lock, see drm_send_event() for the unlocked version. 811 * 812 * Note that the core will take care of unlinking and disarming events when the 813 * corresponding DRM file is closed. Drivers need not worry about whether the 814 * DRM file for this event still exists and can call this function upon 815 * completion of the asynchronous work unconditionally. 816 */ 817 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e) 818 { 819 drm_send_event_helper(dev, e, 0); 820 } 821 EXPORT_SYMBOL(drm_send_event_locked); 822 823 /** 824 * drm_send_event - send DRM event to file descriptor 825 * @dev: DRM device 826 * @e: DRM event to deliver 827 * 828 * This function sends the event @e, initialized with drm_event_reserve_init(), 829 * to its associated userspace DRM file. This function acquires 830 * &drm_device.event_lock, see drm_send_event_locked() for callers which already 831 * hold this lock. 832 * 833 * Note that the core will take care of unlinking and disarming events when the 834 * corresponding DRM file is closed. Drivers need not worry about whether the 835 * DRM file for this event still exists and can call this function upon 836 * completion of the asynchronous work unconditionally. 837 */ 838 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) 839 { 840 unsigned long irqflags; 841 842 spin_lock_irqsave(&dev->event_lock, irqflags); 843 drm_send_event_helper(dev, e, 0); 844 spin_unlock_irqrestore(&dev->event_lock, irqflags); 845 } 846 EXPORT_SYMBOL(drm_send_event); 847 848 void drm_fdinfo_print_size(struct drm_printer *p, 849 const char *prefix, 850 const char *stat, 851 const char *region, 852 u64 sz) 853 { 854 const char *units[] = {"", " KiB", " MiB"}; 855 unsigned u; 856 857 for (u = 0; u < ARRAY_SIZE(units) - 1; u++) { 858 if (sz == 0 || !IS_ALIGNED(sz, SZ_1K)) 859 break; 860 sz = div_u64(sz, SZ_1K); 861 } 862 863 drm_printf(p, "%s-%s-%s:\t%llu%s\n", 864 prefix, stat, region, sz, units[u]); 865 } 866 EXPORT_SYMBOL(drm_fdinfo_print_size); 867 868 int drm_memory_stats_is_zero(const struct drm_memory_stats *stats) 869 { 870 return (stats->shared == 0 && 871 stats->private == 0 && 872 stats->resident == 0 && 873 stats->purgeable == 0 && 874 stats->active == 0); 875 } 876 EXPORT_SYMBOL(drm_memory_stats_is_zero); 877 878 /** 879 * drm_print_memory_stats - A helper to print memory stats 880 * @p: The printer to print output to 881 * @stats: The collected memory stats 882 * @supported_status: Bitmask of optional stats which are available 883 * @region: The memory region 884 * 885 */ 886 void drm_print_memory_stats(struct drm_printer *p, 887 const struct drm_memory_stats *stats, 888 enum drm_gem_object_status supported_status, 889 const char *region) 890 { 891 const char *prefix = "drm"; 892 893 drm_fdinfo_print_size(p, prefix, "total", region, 894 stats->private + stats->shared); 895 drm_fdinfo_print_size(p, prefix, "shared", region, stats->shared); 896 897 if (supported_status & DRM_GEM_OBJECT_ACTIVE) 898 drm_fdinfo_print_size(p, prefix, "active", region, stats->active); 899 900 if (supported_status & DRM_GEM_OBJECT_RESIDENT) 901 drm_fdinfo_print_size(p, prefix, "resident", region, 902 stats->resident); 903 904 if (supported_status & DRM_GEM_OBJECT_PURGEABLE) 905 drm_fdinfo_print_size(p, prefix, "purgeable", region, 906 stats->purgeable); 907 } 908 EXPORT_SYMBOL(drm_print_memory_stats); 909 910 /** 911 * drm_show_memory_stats - Helper to collect and show standard fdinfo memory stats 912 * @p: the printer to print output to 913 * @file: the DRM file 914 * 915 * Helper to iterate over GEM objects with a handle allocated in the specified 916 * file. 917 */ 918 void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file) 919 { 920 struct drm_gem_object *obj; 921 struct drm_memory_stats status = {}; 922 enum drm_gem_object_status supported_status = 0; 923 int id; 924 925 spin_lock(&file->table_lock); 926 idr_for_each_entry (&file->object_idr, obj, id) { 927 enum drm_gem_object_status s = 0; 928 size_t add_size = (obj->funcs && obj->funcs->rss) ? 929 obj->funcs->rss(obj) : obj->size; 930 931 if (obj->funcs && obj->funcs->status) { 932 s = obj->funcs->status(obj); 933 supported_status |= s; 934 } 935 936 if (drm_gem_object_is_shared_for_memory_stats(obj)) 937 status.shared += obj->size; 938 else 939 status.private += obj->size; 940 941 if (s & DRM_GEM_OBJECT_RESIDENT) { 942 status.resident += add_size; 943 } else { 944 /* If already purged or not yet backed by pages, don't 945 * count it as purgeable: 946 */ 947 s &= ~DRM_GEM_OBJECT_PURGEABLE; 948 } 949 950 if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) { 951 status.active += add_size; 952 supported_status |= DRM_GEM_OBJECT_ACTIVE; 953 954 /* If still active, don't count as purgeable: */ 955 s &= ~DRM_GEM_OBJECT_PURGEABLE; 956 } 957 958 if (s & DRM_GEM_OBJECT_PURGEABLE) 959 status.purgeable += add_size; 960 } 961 spin_unlock(&file->table_lock); 962 963 drm_print_memory_stats(p, &status, supported_status, "memory"); 964 } 965 EXPORT_SYMBOL(drm_show_memory_stats); 966 967 /** 968 * drm_show_fdinfo - helper for drm file fops 969 * @m: output stream 970 * @f: the device file instance 971 * 972 * Helper to implement fdinfo, for userspace to query usage stats, etc, of a 973 * process using the GPU. See also &drm_driver.show_fdinfo. 974 * 975 * For text output format description please see Documentation/gpu/drm-usage-stats.rst 976 */ 977 void drm_show_fdinfo(struct seq_file *m, struct file *f) 978 { 979 struct drm_file *file = f->private_data; 980 struct drm_device *dev = file->minor->dev; 981 struct drm_printer p = drm_seq_file_printer(m); 982 int idx; 983 984 if (!drm_dev_enter(dev, &idx)) 985 return; 986 987 drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name); 988 drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id); 989 990 if (dev_is_pci(dev->dev)) { 991 struct pci_dev *pdev = to_pci_dev(dev->dev); 992 993 drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n", 994 pci_domain_nr(pdev->bus), pdev->bus->number, 995 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); 996 } 997 998 mutex_lock(&file->client_name_lock); 999 if (file->client_name) 1000 drm_printf(&p, "drm-client-name:\t%s\n", file->client_name); 1001 mutex_unlock(&file->client_name_lock); 1002 1003 if (dev->driver->show_fdinfo) 1004 dev->driver->show_fdinfo(&p, file); 1005 1006 drm_dev_exit(idx); 1007 } 1008 EXPORT_SYMBOL(drm_show_fdinfo); 1009 1010 /** 1011 * drm_file_err - log process name, pid and client_name associated with a drm_file 1012 * @file_priv: context of interest for process name and pid 1013 * @fmt: printf() like format string 1014 * 1015 * Helper function for clients which needs to log process details such 1016 * as name and pid etc along with user logs. 1017 */ 1018 void drm_file_err(struct drm_file *file_priv, const char *fmt, ...) 1019 { 1020 va_list args; 1021 struct va_format vaf; 1022 struct pid *pid; 1023 struct task_struct *task; 1024 struct drm_device *dev = file_priv->minor->dev; 1025 1026 va_start(args, fmt); 1027 vaf.fmt = fmt; 1028 vaf.va = &args; 1029 1030 mutex_lock(&file_priv->client_name_lock); 1031 rcu_read_lock(); 1032 pid = rcu_dereference(file_priv->pid); 1033 task = pid_task(pid, PIDTYPE_TGID); 1034 1035 drm_err(dev, "comm: %s pid: %d client-id:%llu client: %s ... %pV", 1036 task ? task->comm : "Unset", 1037 task ? task->pid : 0, file_priv->client_id, 1038 file_priv->client_name ?: "Unset", &vaf); 1039 1040 va_end(args); 1041 rcu_read_unlock(); 1042 mutex_unlock(&file_priv->client_name_lock); 1043 } 1044 EXPORT_SYMBOL(drm_file_err); 1045 1046 /** 1047 * mock_drm_getfile - Create a new struct file for the drm device 1048 * @minor: drm minor to wrap (e.g. #drm_device.primary) 1049 * @flags: file creation mode (O_RDWR etc) 1050 * 1051 * This create a new struct file that wraps a DRM file context around a 1052 * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without 1053 * invoking userspace. The struct file may be operated on using its f_op 1054 * (the drm_device.driver.fops) to mimick userspace operations, or be supplied 1055 * to userspace facing functions as an internal/anonymous client. 1056 * 1057 * RETURNS: 1058 * Pointer to newly created struct file, ERR_PTR on failure. 1059 */ 1060 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags) 1061 { 1062 struct drm_device *dev = minor->dev; 1063 struct drm_file *priv; 1064 struct file *file; 1065 1066 priv = drm_file_alloc(minor); 1067 if (IS_ERR(priv)) 1068 return ERR_CAST(priv); 1069 1070 file = anon_inode_getfile("drm", dev->driver->fops, priv, flags); 1071 if (IS_ERR(file)) { 1072 drm_file_free(priv); 1073 return file; 1074 } 1075 1076 /* Everyone shares a single global address space */ 1077 file->f_mapping = dev->anon_inode->i_mapping; 1078 1079 drm_dev_get(dev); 1080 priv->filp = file; 1081 1082 return file; 1083 } 1084 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile); 1085