1 /* 2 * Copyright © 2008 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 * Authors: 24 * Eric Anholt <eric@anholt.net> 25 * 26 */ 27 /*- 28 * Copyright (c) 2011 The FreeBSD Foundation 29 * All rights reserved. 30 * 31 * This software was developed by Konstantin Belousov under sponsorship from 32 * the FreeBSD Foundation. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53 * SUCH DAMAGE. 54 */ 55 56 #include "opt_vm.h" 57 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/limits.h> 61 #include <sys/lock.h> 62 #include <sys/mutex.h> 63 #include <sys/conf.h> 64 65 #include <vm/vm.h> 66 #include <vm/vm_page.h> 67 68 #include <linux/types.h> 69 #include <linux/mm.h> 70 #include <linux/module.h> 71 #include <drm/drmP.h> 72 #include <drm/drm_vma_manager.h> 73 74 /** @file drm_gem.c 75 * 76 * This file provides some of the base ioctls and library routines for 77 * the graphics memory manager implemented by each device driver. 78 * 79 * Because various devices have different requirements in terms of 80 * synchronization and migration strategies, implementing that is left up to 81 * the driver, and all that the general API provides should be generic -- 82 * allocating objects, reading/writing data with the cpu, freeing objects. 83 * Even there, platform-dependent optimizations for reading/writing data with 84 * the CPU mean we'll likely hook those out to driver-specific calls. However, 85 * the DRI2 implementation wants to have at least allocate/mmap be generic. 86 * 87 * The goal was to have swap-backed object allocation managed through 88 * struct file. However, file descriptors as handles to a struct file have 89 * two major failings: 90 * - Process limits prevent more than 1024 or so being used at a time by 91 * default. 92 * - Inability to allocate high fds will aggravate the X Server's select() 93 * handling, and likely that of many GL client applications as well. 94 * 95 * This led to a plan of using our own integer IDs (called handles, following 96 * DRM terminology) to mimic fds, and implement the fd syscalls we need as 97 * ioctls. The objects themselves will still include the struct file so 98 * that we can transition to fds if the required kernel infrastructure shows 99 * up at a later date, and as our interface with shmfs for memory allocation. 100 */ 101 102 /* 103 * We make up offsets for buffer objects so we can recognize them at 104 * mmap time. 105 */ 106 107 /* pgoff in mmap is an unsigned long, so we need to make sure that 108 * the faked up offset will fit 109 */ 110 111 #if BITS_PER_LONG == 64 112 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) 113 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16) 114 #else 115 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1) 116 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16) 117 #endif 118 119 /** 120 * Initialize the GEM device fields 121 */ 122 123 int 124 drm_gem_init(struct drm_device *dev) 125 { 126 struct drm_gem_mm *mm; 127 128 lockinit(&dev->object_name_lock, "objnam", 0, LK_CANRECURSE); 129 idr_init(&dev->object_name_idr); 130 131 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL); 132 if (!mm) { 133 DRM_ERROR("out of memory\n"); 134 return -ENOMEM; 135 } 136 137 dev->mm_private = mm; 138 139 if (drm_ht_create(&mm->offset_hash, 12)) { 140 kfree(mm); 141 return -ENOMEM; 142 } 143 144 mm->idxunr = new_unrhdr(0, DRM_GEM_MAX_IDX, NULL); 145 drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START, 146 DRM_FILE_PAGE_OFFSET_SIZE); 147 drm_vma_offset_manager_init(&mm->vma_manager, 148 DRM_FILE_PAGE_OFFSET_START, 149 DRM_FILE_PAGE_OFFSET_SIZE); 150 return 0; 151 } 152 153 void 154 drm_gem_destroy(struct drm_device *dev) 155 { 156 struct drm_gem_mm *mm = dev->mm_private; 157 158 drm_mm_takedown(&mm->offset_manager); 159 drm_ht_remove(&mm->offset_hash); 160 161 drm_vma_offset_manager_destroy(&mm->vma_manager); 162 delete_unrhdr(mm->idxunr); 163 kfree(mm); 164 dev->mm_private = NULL; 165 } 166 167 /** 168 * Initialize an already allocated GEM object of the specified size with 169 * shmfs backing store. 170 */ 171 int drm_gem_object_init(struct drm_device *dev, 172 struct drm_gem_object *obj, size_t size) 173 { 174 BUG_ON((size & (PAGE_SIZE - 1)) != 0); 175 176 obj->dev = dev; 177 obj->vm_obj = default_pager_alloc(NULL, size, 178 VM_PROT_READ | VM_PROT_WRITE, 0); 179 180 kref_init(&obj->refcount); 181 atomic_set(&obj->handle_count, 0); 182 obj->size = size; 183 184 return 0; 185 } 186 EXPORT_SYMBOL(drm_gem_object_init); 187 188 /** 189 * Initialize an already allocated GEM object of the specified size with 190 * no GEM provided backing store. Instead the caller is responsible for 191 * backing the object and handling it. 192 */ 193 void drm_gem_private_object_init(struct drm_device *dev, 194 struct drm_gem_object *obj, size_t size) 195 { 196 BUG_ON((size & (PAGE_SIZE - 1)) != 0); 197 198 obj->dev = dev; 199 obj->vm_obj = NULL; 200 201 kref_init(&obj->refcount); 202 atomic_set(&obj->handle_count, 0); 203 obj->size = size; 204 drm_vma_node_reset(&obj->vma_node); 205 } 206 EXPORT_SYMBOL(drm_gem_private_object_init); 207 208 static void 209 drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp) 210 { 211 #if 0 212 if (obj->import_attach) { 213 drm_prime_remove_buf_handle(&filp->prime, 214 obj->import_attach->dmabuf); 215 } 216 217 /* 218 * Note: obj->dma_buf can't disappear as long as we still hold a 219 * handle reference in obj->handle_count. 220 */ 221 if (obj->dma_buf) { 222 drm_prime_remove_buf_handle(&filp->prime, 223 obj->dma_buf); 224 } 225 #endif 226 } 227 228 /** 229 * Called after the last handle to the object has been closed 230 * 231 * Removes any name for the object. Note that this must be 232 * called before drm_gem_object_free or we'll be touching 233 * freed memory 234 */ 235 static void drm_gem_object_handle_free(struct drm_gem_object *obj) 236 { 237 struct drm_device *dev = obj->dev; 238 239 /* Remove any name for this object */ 240 if (obj->name) { 241 idr_remove(&dev->object_name_idr, obj->name); 242 obj->name = 0; 243 } 244 } 245 246 #if 0 247 static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj) 248 { 249 /* Unbreak the reference cycle if we have an exported dma_buf. */ 250 if (obj->dma_buf) { 251 dma_buf_put(obj->dma_buf); 252 obj->dma_buf = NULL; 253 } 254 } 255 #endif 256 257 static void 258 drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj) 259 { 260 if (WARN_ON(atomic_read(&obj->handle_count) == 0)) 261 return; 262 263 /* 264 * Must bump handle count first as this may be the last 265 * ref, in which case the object would disappear before we 266 * checked for a name 267 */ 268 269 if (atomic_dec_and_test(&obj->handle_count)) 270 drm_gem_object_handle_free(obj); 271 drm_gem_object_unreference_unlocked(obj); 272 } 273 274 /** 275 * Removes the mapping from handle to filp for this object. 276 */ 277 int 278 drm_gem_handle_delete(struct drm_file *filp, u32 handle) 279 { 280 struct drm_device *dev; 281 struct drm_gem_object *obj; 282 283 /* This is gross. The idr system doesn't let us try a delete and 284 * return an error code. It just spews if you fail at deleting. 285 * So, we have to grab a lock around finding the object and then 286 * doing the delete on it and dropping the refcount, or the user 287 * could race us to double-decrement the refcount and cause a 288 * use-after-free later. Given the frequency of our handle lookups, 289 * we may want to use ida for number allocation and a hash table 290 * for the pointers, anyway. 291 */ 292 lockmgr(&filp->table_lock, LK_EXCLUSIVE); 293 294 /* Check if we currently have a reference on the object */ 295 obj = idr_find(&filp->object_idr, handle); 296 if (obj == NULL) { 297 lockmgr(&filp->table_lock, LK_RELEASE); 298 return -EINVAL; 299 } 300 dev = obj->dev; 301 302 /* Release reference and decrement refcount. */ 303 idr_remove(&filp->object_idr, handle); 304 lockmgr(&filp->table_lock, LK_RELEASE); 305 306 drm_gem_remove_prime_handles(obj, filp); 307 308 if (dev->driver->gem_close_object) 309 dev->driver->gem_close_object(obj, filp); 310 drm_gem_object_handle_unreference_unlocked(obj); 311 312 return 0; 313 } 314 EXPORT_SYMBOL(drm_gem_handle_delete); 315 316 /** 317 * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers 318 * 319 * This implements the ->dumb_destroy kms driver callback for drivers which use 320 * gem to manage their backing storage. 321 */ 322 int drm_gem_dumb_destroy(struct drm_file *file, 323 struct drm_device *dev, 324 uint32_t handle) 325 { 326 return drm_gem_handle_delete(file, handle); 327 } 328 EXPORT_SYMBOL(drm_gem_dumb_destroy); 329 330 /** 331 * Create a handle for this object. This adds a handle reference 332 * to the object, which includes a regular reference count. Callers 333 * will likely want to dereference the object afterwards. 334 */ 335 int 336 drm_gem_handle_create(struct drm_file *file_priv, 337 struct drm_gem_object *obj, 338 u32 *handlep) 339 { 340 struct drm_device *dev = obj->dev; 341 int ret; 342 343 /* 344 * Get the user-visible handle using idr. 345 */ 346 again: 347 /* ensure there is space available to allocate a handle */ 348 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) 349 return -ENOMEM; 350 351 /* do the allocation under our spinlock */ 352 lockmgr(&file_priv->table_lock, LK_EXCLUSIVE); 353 ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep); 354 lockmgr(&file_priv->table_lock, LK_RELEASE); 355 if (ret == -EAGAIN) 356 goto again; 357 else if (ret) 358 return ret; 359 360 drm_gem_object_handle_reference(obj); 361 362 if (dev->driver->gem_open_object) { 363 ret = dev->driver->gem_open_object(obj, file_priv); 364 if (ret) { 365 drm_gem_handle_delete(file_priv, *handlep); 366 return ret; 367 } 368 } 369 370 return 0; 371 } 372 EXPORT_SYMBOL(drm_gem_handle_create); 373 374 375 /** 376 * drm_gem_free_mmap_offset - release a fake mmap offset for an object 377 * @obj: obj in question 378 * 379 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset(). 380 */ 381 void 382 drm_gem_free_mmap_offset(struct drm_gem_object *obj) 383 { 384 struct drm_device *dev = obj->dev; 385 struct drm_gem_mm *mm = dev->mm_private; 386 struct drm_hash_item *list; 387 388 if (!obj->on_map) 389 return; 390 list = &obj->map_list; 391 392 drm_ht_remove_item(&mm->offset_hash, list); 393 free_unr(mm->idxunr, list->key); 394 obj->on_map = false; 395 396 drm_vma_offset_remove(&mm->vma_manager, &obj->vma_node); 397 } 398 EXPORT_SYMBOL(drm_gem_free_mmap_offset); 399 400 /** 401 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object 402 * @obj: obj in question 403 * @size: the virtual size 404 * 405 * GEM memory mapping works by handing back to userspace a fake mmap offset 406 * it can use in a subsequent mmap(2) call. The DRM core code then looks 407 * up the object based on the offset and sets up the various memory mapping 408 * structures. 409 * 410 * This routine allocates and attaches a fake offset for @obj, in cases where 411 * the virtual size differs from the physical size (ie. obj->size). Otherwise 412 * just use drm_gem_create_mmap_offset(). 413 */ 414 int 415 drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size) 416 { 417 struct drm_device *dev = obj->dev; 418 struct drm_gem_mm *mm = dev->mm_private; 419 int ret = 0; 420 421 if (obj->on_map) 422 return (0); 423 424 obj->map_list.key = alloc_unr(mm->idxunr); 425 ret = drm_ht_insert_item(&mm->offset_hash, &obj->map_list); 426 if (ret != 0) { 427 DRM_ERROR("failed to add to map hash\n"); 428 free_unr(mm->idxunr, obj->map_list.key); 429 return (ret); 430 } 431 obj->on_map = true; 432 return 0; 433 434 return drm_vma_offset_add(&mm->vma_manager, &obj->vma_node, 435 size / PAGE_SIZE); 436 } 437 EXPORT_SYMBOL(drm_gem_create_mmap_offset_size); 438 439 /** 440 * drm_gem_create_mmap_offset - create a fake mmap offset for an object 441 * @obj: obj in question 442 * 443 * GEM memory mapping works by handing back to userspace a fake mmap offset 444 * it can use in a subsequent mmap(2) call. The DRM core code then looks 445 * up the object based on the offset and sets up the various memory mapping 446 * structures. 447 * 448 * This routine allocates and attaches a fake offset for @obj. 449 */ 450 int drm_gem_create_mmap_offset(struct drm_gem_object *obj) 451 { 452 return drm_gem_create_mmap_offset_size(obj, obj->size); 453 } 454 EXPORT_SYMBOL(drm_gem_create_mmap_offset); 455 456 /** Returns a reference to the object named by the handle. */ 457 struct drm_gem_object * 458 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, 459 u32 handle) 460 { 461 struct drm_gem_object *obj; 462 463 lockmgr(&filp->table_lock, LK_EXCLUSIVE); 464 465 /* Check if we currently have a reference on the object */ 466 obj = idr_find(&filp->object_idr, handle); 467 if (obj == NULL) { 468 lockmgr(&filp->table_lock, LK_RELEASE); 469 return NULL; 470 } 471 472 drm_gem_object_reference(obj); 473 474 lockmgr(&filp->table_lock, LK_RELEASE); 475 476 return obj; 477 } 478 EXPORT_SYMBOL(drm_gem_object_lookup); 479 480 /** 481 * Releases the handle to an mm object. 482 */ 483 int 484 drm_gem_close_ioctl(struct drm_device *dev, void *data, 485 struct drm_file *file_priv) 486 { 487 struct drm_gem_close *args = data; 488 int ret; 489 490 if (!(dev->driver->driver_features & DRIVER_GEM)) 491 return -ENODEV; 492 493 ret = drm_gem_handle_delete(file_priv, args->handle); 494 495 return ret; 496 } 497 498 /** 499 * Create a global name for an object, returning the name. 500 * 501 * Note that the name does not hold a reference; when the object 502 * is freed, the name goes away. 503 */ 504 int 505 drm_gem_flink_ioctl(struct drm_device *dev, void *data, 506 struct drm_file *file_priv) 507 { 508 struct drm_gem_flink *args = data; 509 struct drm_gem_object *obj; 510 int ret; 511 512 if (!drm_core_check_feature(dev, DRIVER_GEM)) 513 return -ENODEV; 514 515 obj = drm_gem_object_lookup(dev, file_priv, args->handle); 516 if (obj == NULL) 517 return -ENOENT; 518 519 again: 520 if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) { 521 ret = -ENOMEM; 522 goto err; 523 } 524 525 lockmgr(&dev->object_name_lock, LK_EXCLUSIVE); 526 if (!obj->name) { 527 ret = idr_get_new_above(&dev->object_name_idr, obj, 1, 528 &obj->name); 529 args->name = (uint64_t) obj->name; 530 lockmgr(&dev->object_name_lock, LK_RELEASE); 531 532 if (ret == -EAGAIN) 533 goto again; 534 else if (ret) 535 goto err; 536 537 } else { 538 args->name = (uint64_t) obj->name; 539 lockmgr(&dev->object_name_lock, LK_RELEASE); 540 ret = 0; 541 } 542 543 err: 544 drm_gem_object_unreference_unlocked(obj); 545 return ret; 546 } 547 548 /** 549 * Open an object using the global name, returning a handle and the size. 550 * 551 * This handle (of course) holds a reference to the object, so the object 552 * will not go away until the handle is deleted. 553 */ 554 int 555 drm_gem_open_ioctl(struct drm_device *dev, void *data, 556 struct drm_file *file_priv) 557 { 558 struct drm_gem_open *args = data; 559 struct drm_gem_object *obj; 560 int ret; 561 u32 handle; 562 563 if (!(dev->driver->driver_features & DRIVER_GEM)) 564 return -ENODEV; 565 566 lockmgr(&dev->object_name_lock, LK_EXCLUSIVE); 567 obj = idr_find(&dev->object_name_idr, (int) args->name); 568 if (obj) 569 drm_gem_object_reference(obj); 570 lockmgr(&dev->object_name_lock, LK_RELEASE); 571 if (!obj) 572 return -ENOENT; 573 574 ret = drm_gem_handle_create(file_priv, obj, &handle); 575 drm_gem_object_unreference_unlocked(obj); 576 if (ret) 577 return ret; 578 579 args->handle = handle; 580 args->size = obj->size; 581 582 return 0; 583 } 584 585 /** 586 * Called at device open time, sets up the structure for handling refcounting 587 * of mm objects. 588 */ 589 void 590 drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 591 { 592 idr_init(&file_private->object_idr); 593 lockinit(&file_private->table_lock, "fptab", 0, LK_CANRECURSE); 594 } 595 596 /** 597 * Called at device close to release the file's 598 * handle references on objects. 599 */ 600 static int 601 drm_gem_object_release_handle(int id, void *ptr, void *data) 602 { 603 struct drm_file *file_priv = data; 604 struct drm_gem_object *obj = ptr; 605 struct drm_device *dev = obj->dev; 606 607 drm_gem_remove_prime_handles(obj, file_priv); 608 609 if (dev->driver->gem_close_object) 610 dev->driver->gem_close_object(obj, file_priv); 611 612 drm_gem_object_handle_unreference_unlocked(obj); 613 614 return 0; 615 } 616 617 /** 618 * Called at close time when the filp is going away. 619 * 620 * Releases any remaining references on objects by this filp. 621 */ 622 void 623 drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 624 { 625 idr_for_each(&file_private->object_idr, 626 &drm_gem_object_release_handle, file_private); 627 idr_destroy(&file_private->object_idr); 628 } 629 630 void 631 drm_gem_object_release(struct drm_gem_object *obj) 632 { 633 634 /* 635 * obj->vm_obj can be NULL for private gem objects. 636 */ 637 vm_object_deallocate(obj->vm_obj); 638 } 639 EXPORT_SYMBOL(drm_gem_object_release); 640 641 /** 642 * Called after the last reference to the object has been lost. 643 * Must be called holding struct_ mutex 644 * 645 * Frees the object 646 */ 647 void 648 drm_gem_object_free(struct kref *kref) 649 { 650 struct drm_gem_object *obj = (struct drm_gem_object *) kref; 651 struct drm_device *dev = obj->dev; 652 653 BUG_ON(!mutex_is_locked(&dev->struct_mutex)); 654 655 if (dev->driver->gem_free_object != NULL) 656 dev->driver->gem_free_object(obj); 657 } 658 EXPORT_SYMBOL(drm_gem_object_free); 659 660 static struct drm_gem_object * 661 drm_gem_object_from_offset(struct drm_device *dev, vm_ooffset_t offset) 662 { 663 struct drm_gem_object *obj; 664 struct drm_gem_mm *mm = dev->mm_private; 665 struct drm_hash_item *hash; 666 667 if ((offset & DRM_GEM_MAPPING_MASK) != DRM_GEM_MAPPING_KEY) 668 return (NULL); 669 offset &= ~DRM_GEM_MAPPING_KEY; 670 671 if (drm_ht_find_item(&mm->offset_hash, DRM_GEM_MAPPING_IDX(offset), 672 &hash) != 0) { 673 return (NULL); 674 } 675 obj = container_of(hash, struct drm_gem_object, map_list); 676 return (obj); 677 } 678 679 int 680 drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset, vm_size_t size, 681 struct vm_object **obj_res, int nprot) 682 { 683 struct drm_gem_object *gem_obj; 684 struct vm_object *vm_obj; 685 686 DRM_LOCK(dev); 687 gem_obj = drm_gem_object_from_offset(dev, *offset); 688 if (gem_obj == NULL) { 689 DRM_UNLOCK(dev); 690 return (ENODEV); 691 } 692 693 drm_gem_object_reference(gem_obj); 694 DRM_UNLOCK(dev); 695 vm_obj = cdev_pager_allocate(gem_obj, OBJT_MGTDEVICE, 696 dev->driver->gem_pager_ops, size, nprot, 697 DRM_GEM_MAPPING_MAPOFF(*offset), curthread->td_ucred); 698 if (vm_obj == NULL) { 699 drm_gem_object_unreference_unlocked(gem_obj); 700 return (EINVAL); 701 } 702 *offset = DRM_GEM_MAPPING_MAPOFF(*offset); 703 *obj_res = vm_obj; 704 return (0); 705 } 706 707 void 708 drm_gem_pager_dtr(void *handle) 709 { 710 struct drm_gem_object *obj; 711 struct drm_device *dev; 712 713 obj = handle; 714 dev = obj->dev; 715 716 DRM_LOCK(dev); 717 drm_gem_free_mmap_offset(obj); 718 drm_gem_object_unreference(obj); 719 DRM_UNLOCK(dev); 720 } 721