drm/i915: Update to Linux 4.7.10
[dragonfly.git] / sys / dev / drm / drm_gem.c
blob476138a6a46a624ba277685b17975aa658f585e9
1 /*
2 * Copyright © 2008 Intel Corporation
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:
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.
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.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
27 /*-
28 * Copyright (c) 2011 The FreeBSD Foundation
29 * All rights reserved.
31 * This software was developed by Konstantin Belousov under sponsorship from
32 * the FreeBSD Foundation.
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.
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.
56 #include "opt_vm.h"
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>
65 #include <vm/vm.h>
66 #include <vm/vm_page.h>
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 #include <drm/drm_gem.h>
74 #include "drm_internal.h"
76 /** @file drm_gem.c
78 * This file provides some of the base ioctls and library routines for
79 * the graphics memory manager implemented by each device driver.
81 * Because various devices have different requirements in terms of
82 * synchronization and migration strategies, implementing that is left up to
83 * the driver, and all that the general API provides should be generic --
84 * allocating objects, reading/writing data with the cpu, freeing objects.
85 * Even there, platform-dependent optimizations for reading/writing data with
86 * the CPU mean we'll likely hook those out to driver-specific calls. However,
87 * the DRI2 implementation wants to have at least allocate/mmap be generic.
89 * The goal was to have swap-backed object allocation managed through
90 * struct file. However, file descriptors as handles to a struct file have
91 * two major failings:
92 * - Process limits prevent more than 1024 or so being used at a time by
93 * default.
94 * - Inability to allocate high fds will aggravate the X Server's select()
95 * handling, and likely that of many GL client applications as well.
97 * This led to a plan of using our own integer IDs (called handles, following
98 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
99 * ioctls. The objects themselves will still include the struct file so
100 * that we can transition to fds if the required kernel infrastructure shows
101 * up at a later date, and as our interface with shmfs for memory allocation.
105 * We make up offsets for buffer objects so we can recognize them at
106 * mmap time.
109 /* pgoff in mmap is an unsigned long, so we need to make sure that
110 * the faked up offset will fit
113 #if BITS_PER_LONG == 64
114 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
115 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
116 #else
117 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
118 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
119 #endif
122 * drm_gem_init - Initialize the GEM device fields
123 * @dev: drm_devic structure to initialize
126 drm_gem_init(struct drm_device *dev)
128 struct drm_gem_mm *mm;
130 lockinit(&dev->object_name_lock, "objnam", 0, LK_CANRECURSE);
131 idr_init(&dev->object_name_idr);
133 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
134 if (!mm) {
135 DRM_ERROR("out of memory\n");
136 return -ENOMEM;
139 dev->mm_private = mm;
141 if (drm_ht_create(&mm->offset_hash, 12)) {
142 kfree(mm);
143 return -ENOMEM;
146 mm->idxunr = new_unrhdr(0, DRM_GEM_MAX_IDX, NULL);
147 drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
148 DRM_FILE_PAGE_OFFSET_SIZE);
149 drm_vma_offset_manager_init(&mm->vma_manager,
150 DRM_FILE_PAGE_OFFSET_START,
151 DRM_FILE_PAGE_OFFSET_SIZE);
153 return 0;
156 void
157 drm_gem_destroy(struct drm_device *dev)
159 struct drm_gem_mm *mm = dev->mm_private;
161 drm_mm_takedown(&mm->offset_manager);
162 drm_ht_remove(&mm->offset_hash);
164 drm_vma_offset_manager_destroy(&mm->vma_manager);
165 delete_unrhdr(mm->idxunr);
166 kfree(mm);
167 dev->mm_private = NULL;
171 * Initialize an already allocated GEM object of the specified size with
172 * shmfs backing store.
174 int drm_gem_object_init(struct drm_device *dev,
175 struct drm_gem_object *obj, size_t size)
177 drm_gem_private_object_init(dev, obj, size);
179 obj->vm_obj = default_pager_alloc(NULL, size,
180 VM_PROT_READ | VM_PROT_WRITE, 0);
182 return 0;
184 EXPORT_SYMBOL(drm_gem_object_init);
187 * drm_gem_object_init - initialize an allocated private GEM object
188 * @dev: drm_device the object should be initialized for
189 * @obj: drm_gem_object to initialize
190 * @size: object size
192 * Initialize an already allocated GEM object of the specified size with
193 * no GEM provided backing store. Instead the caller is responsible for
194 * backing the object and handling it.
196 void drm_gem_private_object_init(struct drm_device *dev,
197 struct drm_gem_object *obj, size_t size)
199 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
201 obj->dev = dev;
202 obj->vm_obj = NULL;
204 kref_init(&obj->refcount);
205 obj->handle_count = 0;
206 obj->size = size;
207 drm_vma_node_reset(&obj->vma_node);
209 EXPORT_SYMBOL(drm_gem_private_object_init);
211 static void
212 drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
214 #if 0
215 if (obj->import_attach) {
216 drm_prime_remove_buf_handle(&filp->prime,
217 obj->import_attach->dmabuf);
219 if (obj->export_dma_buf) {
220 drm_prime_remove_buf_handle(&filp->prime,
221 obj->export_dma_buf);
223 #endif
226 static void drm_gem_object_ref_bug(struct kref *list_kref)
228 BUG();
232 * drm_gem_object_free - release resources bound to userspace handles
233 * @obj: GEM object to clean up.
235 * Called after the last handle to the object has been closed
237 * Removes any name for the object. Note that this must be
238 * called before drm_gem_object_free or we'll be touching
239 * freed memory
241 static void drm_gem_object_handle_free(struct drm_gem_object *obj)
243 struct drm_device *dev = obj->dev;
245 /* Remove any name for this object */
246 if (obj->name) {
247 idr_remove(&dev->object_name_idr, obj->name);
248 obj->name = 0;
250 * The object name held a reference to this object, drop
251 * that now.
253 * This cannot be the last reference, since the handle holds one too.
255 kref_put(&obj->refcount, drm_gem_object_ref_bug);
259 #if 0
260 static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
262 /* Unbreak the reference cycle if we have an exported dma_buf. */
263 if (obj->dma_buf) {
264 dma_buf_put(obj->dma_buf);
265 obj->dma_buf = NULL;
268 #endif
270 static void
271 drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
273 if (WARN_ON(obj->handle_count == 0))
274 return;
277 * Must bump handle count first as this may be the last
278 * ref, in which case the object would disappear before we
279 * checked for a name
282 mutex_lock(&obj->dev->object_name_lock);
283 if (--obj->handle_count == 0)
284 drm_gem_object_handle_free(obj);
285 mutex_unlock(&obj->dev->object_name_lock);
287 drm_gem_object_unreference_unlocked(obj);
291 * Called at device close to release the file's
292 * handle references on objects.
294 static int
295 drm_gem_object_release_handle(int id, void *ptr, void *data)
297 struct drm_file *file_priv = data;
298 struct drm_gem_object *obj = ptr;
299 struct drm_device *dev = obj->dev;
301 drm_gem_remove_prime_handles(obj, file_priv);
303 if (dev->driver->gem_close_object)
304 dev->driver->gem_close_object(obj, file_priv);
306 drm_gem_object_handle_unreference_unlocked(obj);
308 return 0;
312 * drm_gem_handle_delete - deletes the given file-private handle
313 * @filp: drm file-private structure to use for the handle look up
314 * @handle: userspace handle to delete
316 * Removes the GEM handle from the @filp lookup table which has been added with
317 * drm_gem_handle_create(). If this is the last handle also cleans up linked
318 * resources like GEM names.
321 drm_gem_handle_delete(struct drm_file *filp, u32 handle)
323 struct drm_device *dev;
324 struct drm_gem_object *obj;
326 /* This is gross. The idr system doesn't let us try a delete and
327 * return an error code. It just spews if you fail at deleting.
328 * So, we have to grab a lock around finding the object and then
329 * doing the delete on it and dropping the refcount, or the user
330 * could race us to double-decrement the refcount and cause a
331 * use-after-free later. Given the frequency of our handle lookups,
332 * we may want to use ida for number allocation and a hash table
333 * for the pointers, anyway.
335 lockmgr(&filp->table_lock, LK_EXCLUSIVE);
337 /* Check if we currently have a reference on the object */
338 obj = idr_find(&filp->object_idr, handle);
339 if (obj == NULL) {
340 lockmgr(&filp->table_lock, LK_RELEASE);
341 return -EINVAL;
343 dev = obj->dev;
345 /* Release reference and decrement refcount. */
346 idr_remove(&filp->object_idr, handle);
347 lockmgr(&filp->table_lock, LK_RELEASE);
349 drm_gem_remove_prime_handles(obj, filp);
351 if (dev->driver->gem_close_object)
352 dev->driver->gem_close_object(obj, filp);
353 drm_gem_object_handle_unreference_unlocked(obj);
355 return 0;
357 EXPORT_SYMBOL(drm_gem_handle_delete);
360 * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
361 * @file: drm file-private structure to remove the dumb handle from
362 * @dev: corresponding drm_device
363 * @handle: the dumb handle to remove
365 * This implements the ->dumb_destroy kms driver callback for drivers which use
366 * gem to manage their backing storage.
368 int drm_gem_dumb_destroy(struct drm_file *file,
369 struct drm_device *dev,
370 uint32_t handle)
372 return drm_gem_handle_delete(file, handle);
374 EXPORT_SYMBOL(drm_gem_dumb_destroy);
377 * drm_gem_handle_create_tail - internal functions to create a handle
378 * @file_priv: drm file-private structure to register the handle for
379 * @obj: object to register
380 * @handlep: pointer to return the created handle to the caller
382 * This expects the dev->object_name_lock to be held already and will drop it
383 * before returning. Used to avoid races in establishing new handles when
384 * importing an object from either an flink name or a dma-buf.
386 * Handles must be release again through drm_gem_handle_delete(). This is done
387 * when userspace closes @file_priv for all attached handles, or through the
388 * GEM_CLOSE ioctl for individual handles.
391 drm_gem_handle_create_tail(struct drm_file *file_priv,
392 struct drm_gem_object *obj,
393 u32 *handlep)
395 struct drm_device *dev = obj->dev;
396 int ret;
398 *handlep = 0; /* whack gcc warning */
399 WARN_ON(!mutex_is_locked(&dev->object_name_lock));
402 * Get the user-visible handle using idr. Preload and perform
403 * allocation under our spinlock.
405 idr_preload(GFP_KERNEL);
406 lockmgr(&file_priv->table_lock, LK_EXCLUSIVE);
408 ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
409 drm_gem_object_reference(obj);
410 obj->handle_count++;
411 lockmgr(&file_priv->table_lock, LK_RELEASE);
412 idr_preload_end();
413 mutex_unlock(&dev->object_name_lock);
414 if (ret < 0) {
415 drm_gem_object_handle_unreference_unlocked(obj);
416 return ret;
418 *handlep = ret;
420 #if 0
421 ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
422 if (ret) {
423 drm_gem_handle_delete(file_priv, *handlep);
424 return ret;
426 #endif
428 if (dev->driver->gem_open_object) {
429 ret = dev->driver->gem_open_object(obj, file_priv);
430 if (ret) {
431 drm_gem_handle_delete(file_priv, *handlep);
432 return ret;
436 return 0;
440 * drm_gem_handle_create - create a gem handle for an object
441 * @file_priv: drm file-private structure to register the handle for
442 * @obj: object to register
443 * @handlep: pionter to return the created handle to the caller
445 * Create a handle for this object. This adds a handle reference
446 * to the object, which includes a regular reference count. Callers
447 * will likely want to dereference the object afterwards.
449 int drm_gem_handle_create(struct drm_file *file_priv,
450 struct drm_gem_object *obj,
451 u32 *handlep)
453 mutex_lock(&obj->dev->object_name_lock);
455 return drm_gem_handle_create_tail(file_priv, obj, handlep);
457 EXPORT_SYMBOL(drm_gem_handle_create);
460 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
461 * @obj: obj in question
463 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
465 * Note that drm_gem_object_release() already calls this function, so drivers
466 * don't have to take care of releasing the mmap offset themselves when freeing
467 * the GEM object.
469 void
470 drm_gem_free_mmap_offset(struct drm_gem_object *obj)
472 struct drm_device *dev = obj->dev;
473 struct drm_gem_mm *mm = dev->mm_private;
474 struct drm_hash_item *list;
476 if (!obj->on_map)
477 return;
478 list = &obj->map_list;
480 drm_ht_remove_item(&mm->offset_hash, list);
481 free_unr(mm->idxunr, list->key);
482 obj->on_map = false;
484 drm_vma_offset_remove(&mm->vma_manager, &obj->vma_node);
486 EXPORT_SYMBOL(drm_gem_free_mmap_offset);
489 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
490 * @obj: obj in question
491 * @size: the virtual size
493 * GEM memory mapping works by handing back to userspace a fake mmap offset
494 * it can use in a subsequent mmap(2) call. The DRM core code then looks
495 * up the object based on the offset and sets up the various memory mapping
496 * structures.
498 * This routine allocates and attaches a fake offset for @obj, in cases where
499 * the virtual size differs from the physical size (ie. obj->size). Otherwise
500 * just use drm_gem_create_mmap_offset().
502 * This function is idempotent and handles an already allocated mmap offset
503 * transparently. Drivers do not need to check for this case.
506 drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
508 struct drm_device *dev = obj->dev;
509 struct drm_gem_mm *mm = dev->mm_private;
510 int ret = 0;
512 if (obj->on_map)
513 return (0);
515 obj->map_list.key = alloc_unr(mm->idxunr);
516 ret = drm_ht_insert_item(&mm->offset_hash, &obj->map_list);
517 if (ret != 0) {
518 DRM_ERROR("failed to add to map hash\n");
519 free_unr(mm->idxunr, obj->map_list.key);
520 return (ret);
522 obj->on_map = true;
523 return 0;
525 return drm_vma_offset_add(&mm->vma_manager, &obj->vma_node,
526 size / PAGE_SIZE);
528 EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
531 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
532 * @obj: obj in question
534 * GEM memory mapping works by handing back to userspace a fake mmap offset
535 * it can use in a subsequent mmap(2) call. The DRM core code then looks
536 * up the object based on the offset and sets up the various memory mapping
537 * structures.
539 * This routine allocates and attaches a fake offset for @obj.
541 * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
542 * the fake offset again.
544 int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
546 return drm_gem_create_mmap_offset_size(obj, obj->size);
548 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
551 * drm_gem_object_lookup - look up a GEM object from it's handle
552 * @dev: DRM device
553 * @filp: DRM file private date
554 * @handle: userspace handle
556 * Returns:
558 * A reference to the object named by the handle if such exists on @filp, NULL
559 * otherwise.
561 struct drm_gem_object *
562 drm_gem_object_lookup(struct drm_file *filp, u32 handle)
564 struct drm_gem_object *obj;
566 lockmgr(&filp->table_lock, LK_EXCLUSIVE);
568 /* Check if we currently have a reference on the object */
569 obj = idr_find(&filp->object_idr, handle);
570 if (obj == NULL) {
571 lockmgr(&filp->table_lock, LK_RELEASE);
572 return NULL;
575 drm_gem_object_reference(obj);
577 lockmgr(&filp->table_lock, LK_RELEASE);
579 return obj;
581 EXPORT_SYMBOL(drm_gem_object_lookup);
584 * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
585 * @dev: drm_device
586 * @data: ioctl data
587 * @file_priv: drm file-private structure
589 * Releases the handle to an mm object.
592 drm_gem_close_ioctl(struct drm_device *dev, void *data,
593 struct drm_file *file_priv)
595 struct drm_gem_close *args = data;
596 int ret;
598 if (!drm_core_check_feature(dev, DRIVER_GEM))
599 return -ENODEV;
601 ret = drm_gem_handle_delete(file_priv, args->handle);
603 return ret;
607 * Create a global name for an object, returning the name.
609 * Note that the name does not hold a reference; when the object
610 * is freed, the name goes away.
613 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
614 struct drm_file *file_priv)
616 struct drm_gem_flink *args = data;
617 struct drm_gem_object *obj;
618 int ret;
620 if (!drm_core_check_feature(dev, DRIVER_GEM))
621 return -ENODEV;
623 obj = drm_gem_object_lookup(file_priv, args->handle);
624 if (obj == NULL)
625 return -ENOENT;
627 idr_preload(GFP_KERNEL);
628 lockmgr(&dev->object_name_lock, LK_EXCLUSIVE);
629 /* prevent races with concurrent gem_close. */
630 if (obj->handle_count == 0) {
631 ret = -ENOENT;
632 goto err;
635 if (!obj->name) {
636 ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT);
637 if (ret < 0)
638 goto err;
640 obj->name = ret;
642 /* Allocate a reference for the name table. */
643 drm_gem_object_reference(obj);
646 args->name = (uint64_t) obj->name;
647 ret = 0;
649 err:
650 lockmgr(&dev->object_name_lock, LK_RELEASE);
651 idr_preload_end();
652 drm_gem_object_unreference_unlocked(obj);
653 return ret;
657 * drm_gem_open - implementation of the GEM_OPEN ioctl
658 * @dev: drm_device
659 * @data: ioctl data
660 * @file_priv: drm file-private structure
662 * Open an object using the global name, returning a handle and the size.
664 * This handle (of course) holds a reference to the object, so the object
665 * will not go away until the handle is deleted.
668 drm_gem_open_ioctl(struct drm_device *dev, void *data,
669 struct drm_file *file_priv)
671 struct drm_gem_open *args = data;
672 struct drm_gem_object *obj;
673 int ret;
674 u32 handle;
676 if (!drm_core_check_feature(dev, DRIVER_GEM))
677 return -ENODEV;
679 lockmgr(&dev->object_name_lock, LK_EXCLUSIVE);
680 obj = idr_find(&dev->object_name_idr, (int) args->name);
681 if (obj)
682 drm_gem_object_reference(obj);
683 lockmgr(&dev->object_name_lock, LK_RELEASE);
684 if (!obj)
685 return -ENOENT;
687 ret = drm_gem_handle_create(file_priv, obj, &handle);
688 drm_gem_object_unreference_unlocked(obj);
689 if (ret)
690 return ret;
692 args->handle = handle;
693 args->size = obj->size;
695 return 0;
699 * gem_gem_open - initalizes GEM file-private structures at devnode open time
700 * @dev: drm_device which is being opened by userspace
701 * @file_private: drm file-private structure to set up
703 * Called at device open time, sets up the structure for handling refcounting
704 * of mm objects.
706 void
707 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
709 idr_init(&file_private->object_idr);
710 lockinit(&file_private->table_lock, "fptab", 0, LK_CANRECURSE);
714 * drm_gem_release - release file-private GEM resources
715 * @dev: drm_device which is being closed by userspace
716 * @file_private: drm file-private structure to clean up
718 * Called at close time when the filp is going away.
720 * Releases any remaining references on objects by this filp.
722 void
723 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
725 idr_for_each(&file_private->object_idr,
726 &drm_gem_object_release_handle, file_private);
727 idr_destroy(&file_private->object_idr);
731 * drm_gem_object_release - release GEM buffer object resources
732 * @obj: GEM buffer object
734 * This releases any structures and resources used by @obj and is the invers of
735 * drm_gem_object_init().
737 void
738 drm_gem_object_release(struct drm_gem_object *obj)
742 * obj->vm_obj can be NULL for private gem objects.
744 vm_object_deallocate(obj->vm_obj);
746 EXPORT_SYMBOL(drm_gem_object_release);
749 * drm_gem_object_free - free a GEM object
750 * @kref: kref of the object to free
752 * Called after the last reference to the object has been lost.
753 * Must be called holding struct_ mutex
755 * Frees the object
757 void
758 drm_gem_object_free(struct kref *kref)
760 struct drm_gem_object *obj =
761 container_of(kref, struct drm_gem_object, refcount);
762 struct drm_device *dev = obj->dev;
764 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
766 if (dev->driver->gem_free_object != NULL)
767 dev->driver->gem_free_object(obj);
769 EXPORT_SYMBOL(drm_gem_object_free);
771 static struct drm_gem_object *
772 drm_gem_object_from_offset(struct drm_device *dev, vm_ooffset_t offset)
774 struct drm_gem_object *obj;
775 struct drm_gem_mm *mm = dev->mm_private;
776 struct drm_hash_item *hash;
778 if ((offset & DRM_GEM_MAPPING_MASK) != DRM_GEM_MAPPING_KEY)
779 return (NULL);
780 offset &= ~DRM_GEM_MAPPING_KEY;
782 if (drm_ht_find_item(&mm->offset_hash, DRM_GEM_MAPPING_IDX(offset),
783 &hash) != 0) {
784 return (NULL);
786 obj = container_of(hash, struct drm_gem_object, map_list);
787 return (obj);
791 drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset, vm_size_t size,
792 struct vm_object **obj_res, int nprot)
794 struct drm_gem_object *gem_obj;
795 struct vm_object *vm_obj;
797 DRM_LOCK(dev);
798 gem_obj = drm_gem_object_from_offset(dev, *offset);
799 if (gem_obj == NULL) {
800 DRM_UNLOCK(dev);
801 return (ENODEV);
804 drm_gem_object_reference(gem_obj);
805 DRM_UNLOCK(dev);
806 vm_obj = cdev_pager_allocate(gem_obj, OBJT_MGTDEVICE,
807 dev->driver->gem_pager_ops, size, nprot,
808 DRM_GEM_MAPPING_MAPOFF(*offset), curthread->td_ucred);
809 if (vm_obj == NULL) {
810 drm_gem_object_unreference_unlocked(gem_obj);
811 return (EINVAL);
813 *offset = DRM_GEM_MAPPING_MAPOFF(*offset);
814 *obj_res = vm_obj;
815 return (0);