32c8837199568355ec36af73cce709f00b302ddc
[dragonfly.git] / sys / dev / drm / drm_gem.c
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
489         if (!drm_core_check_feature(dev, DRIVER_GEM))
490                 return (ENODEV);
491
492         return (drm_gem_handle_delete(file_priv, args->handle));
493 }
494
495 /**
496  * Create a global name for an object, returning the name.
497  *
498  * Note that the name does not hold a reference; when the object
499  * is freed, the name goes away.
500  */
501 int
502 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
503                     struct drm_file *file_priv)
504 {
505         struct drm_gem_flink *args = data;
506         struct drm_gem_object *obj;
507         int ret;
508
509         if (!drm_core_check_feature(dev, DRIVER_GEM))
510                 return -ENODEV;
511
512         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
513         if (obj == NULL)
514                 return -ENOENT;
515
516 again:
517         if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
518                 ret = -ENOMEM;
519                 goto err;
520         }
521
522         lockmgr(&dev->object_name_lock, LK_EXCLUSIVE);
523         if (!obj->name) {
524                 ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
525                                         &obj->name);
526                 args->name = (uint64_t) obj->name;
527                 lockmgr(&dev->object_name_lock, LK_RELEASE);
528
529                 if (ret == -EAGAIN)
530                         goto again;
531                 else if (ret)
532                         goto err;
533
534         } else {
535                 args->name = (uint64_t) obj->name;
536                 lockmgr(&dev->object_name_lock, LK_RELEASE);
537                 ret = 0;
538         }
539
540 err:
541         drm_gem_object_unreference_unlocked(obj);
542         return ret;
543 }
544
545 /**
546  * Open an object using the global name, returning a handle and the size.
547  *
548  * This handle (of course) holds a reference to the object, so the object
549  * will not go away until the handle is deleted.
550  */
551 int
552 drm_gem_open_ioctl(struct drm_device *dev, void *data,
553                    struct drm_file *file_priv)
554 {
555         struct drm_gem_open *args = data;
556         struct drm_gem_object *obj;
557         int ret;
558         u32 handle;
559
560         if (!(dev->driver->driver_features & DRIVER_GEM))
561                 return -ENODEV;
562
563         lockmgr(&dev->object_name_lock, LK_EXCLUSIVE);
564         obj = idr_find(&dev->object_name_idr, (int) args->name);
565         if (obj)
566                 drm_gem_object_reference(obj);
567         lockmgr(&dev->object_name_lock, LK_RELEASE);
568         if (!obj)
569                 return -ENOENT;
570
571         ret = drm_gem_handle_create(file_priv, obj, &handle);
572         drm_gem_object_unreference_unlocked(obj);
573         if (ret)
574                 return ret;
575
576         args->handle = handle;
577         args->size = obj->size;
578
579         return 0;
580 }
581
582 /**
583  * Called at device open time, sets up the structure for handling refcounting
584  * of mm objects.
585  */
586 void
587 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
588 {
589         idr_init(&file_private->object_idr);
590         lockinit(&file_private->table_lock, "fptab", 0, LK_CANRECURSE);
591 }
592
593 /**
594  * Called at device close to release the file's
595  * handle references on objects.
596  */
597 static int
598 drm_gem_object_release_handle(int id, void *ptr, void *data)
599 {
600         struct drm_file *file_priv = data;
601         struct drm_gem_object *obj = ptr;
602         struct drm_device *dev = obj->dev;
603
604         drm_gem_remove_prime_handles(obj, file_priv);
605
606         if (dev->driver->gem_close_object)
607                 dev->driver->gem_close_object(obj, file_priv);
608
609         drm_gem_object_handle_unreference_unlocked(obj);
610
611         return 0;
612 }
613
614 /**
615  * Called at close time when the filp is going away.
616  *
617  * Releases any remaining references on objects by this filp.
618  */
619 void
620 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
621 {
622         idr_for_each(&file_private->object_idr,
623                      &drm_gem_object_release_handle, file_private);
624         idr_destroy(&file_private->object_idr);
625 }
626
627 void
628 drm_gem_object_release(struct drm_gem_object *obj)
629 {
630
631         /*
632          * obj->vm_obj can be NULL for private gem objects.
633          */
634         vm_object_deallocate(obj->vm_obj);
635 }
636 EXPORT_SYMBOL(drm_gem_object_release);
637
638 /**
639  * Called after the last reference to the object has been lost.
640  * Must be called holding struct_ mutex
641  *
642  * Frees the object
643  */
644 void
645 drm_gem_object_free(struct kref *kref)
646 {
647         struct drm_gem_object *obj = (struct drm_gem_object *) kref;
648         struct drm_device *dev = obj->dev;
649
650         BUG_ON(!mutex_is_locked(&dev->struct_mutex));
651
652         if (dev->driver->gem_free_object != NULL)
653                 dev->driver->gem_free_object(obj);
654 }
655 EXPORT_SYMBOL(drm_gem_object_free);
656
657 static struct drm_gem_object *
658 drm_gem_object_from_offset(struct drm_device *dev, vm_ooffset_t offset)
659 {
660         struct drm_gem_object *obj;
661         struct drm_gem_mm *mm = dev->mm_private;
662         struct drm_hash_item *hash;
663
664         if ((offset & DRM_GEM_MAPPING_MASK) != DRM_GEM_MAPPING_KEY)
665                 return (NULL);
666         offset &= ~DRM_GEM_MAPPING_KEY;
667
668         if (drm_ht_find_item(&mm->offset_hash, DRM_GEM_MAPPING_IDX(offset),
669             &hash) != 0) {
670                 return (NULL);
671         }
672         obj = container_of(hash, struct drm_gem_object, map_list);
673         return (obj);
674 }
675
676 int
677 drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset, vm_size_t size,
678     struct vm_object **obj_res, int nprot)
679 {
680         struct drm_gem_object *gem_obj;
681         struct vm_object *vm_obj;
682
683         DRM_LOCK(dev);
684         gem_obj = drm_gem_object_from_offset(dev, *offset);
685         if (gem_obj == NULL) {
686                 DRM_UNLOCK(dev);
687                 return (ENODEV);
688         }
689
690         drm_gem_object_reference(gem_obj);
691         DRM_UNLOCK(dev);
692         vm_obj = cdev_pager_allocate(gem_obj, OBJT_MGTDEVICE,
693             dev->driver->gem_pager_ops, size, nprot,
694             DRM_GEM_MAPPING_MAPOFF(*offset), curthread->td_ucred);
695         if (vm_obj == NULL) {
696                 drm_gem_object_unreference_unlocked(gem_obj);
697                 return (EINVAL);
698         }
699         *offset = DRM_GEM_MAPPING_MAPOFF(*offset);
700         *obj_res = vm_obj;
701         return (0);
702 }
703
704 void
705 drm_gem_pager_dtr(void *handle)
706 {
707         struct drm_gem_object *obj;
708         struct drm_device *dev;
709
710         obj = handle;
711         dev = obj->dev;
712
713         DRM_LOCK(dev);
714         drm_gem_free_mmap_offset(obj);
715         drm_gem_object_unreference(obj);
716         DRM_UNLOCK(dev);
717 }