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