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
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
24 * Eric Anholt <eric@anholt.net>
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
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
60 #include <linux/types.h>
61 #include <linux/slab.h>
63 #include <linux/uaccess.h>
65 #include <linux/file.h>
66 #include <linux/module.h>
67 #include <linux/mman.h>
68 #include <linux/pagemap.h>
69 #include <linux/shmem_fs.h>
70 #include <linux/dma-buf.h>
72 #include <drm/drm_vma_manager.h>
73 #include <drm/drm_gem.h>
74 #include "drm_internal.h"
78 struct drm_mm offset_manager; /**< Offset mgmt for buffer objects */
79 struct drm_open_hash offset_hash; /**< User token hash table for maps */
80 struct unrhdr *idxunr;
86 * This file provides some of the base ioctls and library routines for
87 * the graphics memory manager implemented by each device driver.
89 * Because various devices have different requirements in terms of
90 * synchronization and migration strategies, implementing that is left up to
91 * the driver, and all that the general API provides should be generic --
92 * allocating objects, reading/writing data with the cpu, freeing objects.
93 * Even there, platform-dependent optimizations for reading/writing data with
94 * the CPU mean we'll likely hook those out to driver-specific calls. However,
95 * the DRI2 implementation wants to have at least allocate/mmap be generic.
97 * The goal was to have swap-backed object allocation managed through
98 * struct file. However, file descriptors as handles to a struct file have
100 * - Process limits prevent more than 1024 or so being used at a time by
102 * - Inability to allocate high fds will aggravate the X Server's select()
103 * handling, and likely that of many GL client applications as well.
105 * This led to a plan of using our own integer IDs (called handles, following
106 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
107 * ioctls. The objects themselves will still include the struct file so
108 * that we can transition to fds if the required kernel infrastructure shows
109 * up at a later date, and as our interface with shmfs for memory allocation.
113 * We make up offsets for buffer objects so we can recognize them at
117 /* pgoff in mmap is an unsigned long, so we need to make sure that
118 * the faked up offset will fit
121 #if BITS_PER_LONG == 64
122 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
123 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
125 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
126 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
130 * drm_gem_init - Initialize the GEM device fields
131 * @dev: drm_devic structure to initialize
134 drm_gem_init(struct drm_device *dev)
136 struct drm_gem_mm *mm;
137 struct drm_vma_offset_manager *vma_offset_manager;
139 lockinit(&dev->object_name_lock, "objnam", 0, LK_CANRECURSE);
140 idr_init(&dev->object_name_idr);
142 mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
143 vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
144 if (!vma_offset_manager) {
145 DRM_ERROR("out of memory\n");
149 dev->mm_private = mm;
151 if (drm_ht_create(&mm->offset_hash, 12)) {
156 mm->idxunr = new_unrhdr(0, DRM_GEM_MAX_IDX, NULL);
157 drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
158 DRM_FILE_PAGE_OFFSET_SIZE);
160 dev->vma_offset_manager = vma_offset_manager;
161 drm_vma_offset_manager_init(vma_offset_manager,
162 DRM_FILE_PAGE_OFFSET_START,
163 DRM_FILE_PAGE_OFFSET_SIZE);
169 drm_gem_destroy(struct drm_device *dev)
171 struct drm_gem_mm *mm = dev->mm_private;
173 drm_mm_takedown(&mm->offset_manager);
174 drm_ht_remove(&mm->offset_hash);
175 delete_unrhdr(mm->idxunr);
177 dev->mm_private = NULL;
179 drm_vma_offset_manager_destroy(dev->vma_offset_manager);
180 kfree(dev->vma_offset_manager);
181 dev->vma_offset_manager = NULL;
185 * drm_gem_object_init - initialize an allocated shmem-backed GEM object
186 * @dev: drm_device the object should be initialized for
187 * @obj: drm_gem_object to initialize
190 * Initialize an already allocated GEM object of the specified size with
191 * shmfs backing store.
193 int drm_gem_object_init(struct drm_device *dev,
194 struct drm_gem_object *obj, size_t size)
196 struct vm_object *filp;
198 drm_gem_private_object_init(dev, obj, size);
200 filp = default_pager_alloc(NULL, size,
201 VM_PROT_READ | VM_PROT_WRITE, 0);
207 EXPORT_SYMBOL(drm_gem_object_init);
210 * drm_gem_private_object_init - initialize an allocated private GEM object
211 * @dev: drm_device the object should be initialized for
212 * @obj: drm_gem_object to initialize
215 * Initialize an already allocated GEM object of the specified size with
216 * no GEM provided backing store. Instead the caller is responsible for
217 * backing the object and handling it.
219 void drm_gem_private_object_init(struct drm_device *dev,
220 struct drm_gem_object *obj, size_t size)
222 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
227 kref_init(&obj->refcount);
228 obj->handle_count = 0;
230 drm_vma_node_reset(&obj->vma_node);
232 EXPORT_SYMBOL(drm_gem_private_object_init);
235 drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
238 * Note: obj->dma_buf can't disappear as long as we still hold a
239 * handle reference in obj->handle_count.
241 mutex_lock(&filp->prime.lock);
243 drm_prime_remove_buf_handle_locked(&filp->prime,
246 mutex_unlock(&filp->prime.lock);
250 * drm_gem_object_handle_free - release resources bound to userspace handles
251 * @obj: GEM object to clean up.
253 * Called after the last handle to the object has been closed
255 * Removes any name for the object. Note that this must be
256 * called before drm_gem_object_free or we'll be touching
259 static void drm_gem_object_handle_free(struct drm_gem_object *obj)
261 struct drm_device *dev = obj->dev;
263 /* Remove any name for this object */
265 idr_remove(&dev->object_name_idr, obj->name);
270 static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
272 /* Unbreak the reference cycle if we have an exported dma_buf. */
274 dma_buf_put(obj->dma_buf);
280 drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
282 struct drm_device *dev = obj->dev;
285 if (WARN_ON(obj->handle_count == 0))
289 * Must bump handle count first as this may be the last
290 * ref, in which case the object would disappear before we
294 mutex_lock(&dev->object_name_lock);
295 if (--obj->handle_count == 0) {
296 drm_gem_object_handle_free(obj);
297 drm_gem_object_exported_dma_buf_free(obj);
300 mutex_unlock(&dev->object_name_lock);
303 drm_gem_object_put_unlocked(obj);
307 * Called at device or object close to release the file's
308 * handle references on objects.
311 drm_gem_object_release_handle(int id, void *ptr, void *data)
313 struct drm_file *file_priv = data;
314 struct drm_gem_object *obj = ptr;
315 struct drm_device *dev = obj->dev;
317 if (dev->driver->gem_close_object)
318 dev->driver->gem_close_object(obj, file_priv);
320 if (drm_core_check_feature(dev, DRIVER_PRIME))
321 drm_gem_remove_prime_handles(obj, file_priv);
322 drm_vma_node_revoke(&obj->vma_node, file_priv);
324 drm_gem_object_handle_put_unlocked(obj);
330 * drm_gem_handle_delete - deletes the given file-private handle
331 * @filp: drm file-private structure to use for the handle look up
332 * @handle: userspace handle to delete
334 * Removes the GEM handle from the @filp lookup table which has been added with
335 * drm_gem_handle_create(). If this is the last handle also cleans up linked
336 * resources like GEM names.
339 drm_gem_handle_delete(struct drm_file *filp, u32 handle)
341 struct drm_gem_object *obj;
343 /* This is gross. The idr system doesn't let us try a delete and
344 * return an error code. It just spews if you fail at deleting.
345 * So, we have to grab a lock around finding the object and then
346 * doing the delete on it and dropping the refcount, or the user
347 * could race us to double-decrement the refcount and cause a
348 * use-after-free later. Given the frequency of our handle lookups,
349 * we may want to use ida for number allocation and a hash table
350 * for the pointers, anyway.
352 lockmgr(&filp->table_lock, LK_EXCLUSIVE);
354 /* Check if we currently have a reference on the object */
355 obj = idr_replace(&filp->object_idr, NULL, handle);
356 lockmgr(&filp->table_lock, LK_RELEASE);
357 if (IS_ERR_OR_NULL(obj))
360 /* Release driver's reference and decrement refcount. */
361 drm_gem_object_release_handle(handle, obj, filp);
363 /* And finally make the handle available for future allocations. */
364 lockmgr(&filp->table_lock, LK_EXCLUSIVE);
365 idr_remove(&filp->object_idr, handle);
366 lockmgr(&filp->table_lock, LK_RELEASE);
370 EXPORT_SYMBOL(drm_gem_handle_delete);
373 * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
374 * @file: drm file-private structure containing the gem object
375 * @dev: corresponding drm_device
376 * @handle: gem object handle
377 * @offset: return location for the fake mmap offset
379 * This implements the &drm_driver.dumb_map_offset kms driver callback for
380 * drivers which use gem to manage their backing storage.
383 * 0 on success or a negative error code on failure.
385 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
386 u32 handle, u64 *offset)
388 struct drm_gem_object *obj;
391 obj = drm_gem_object_lookup(file, handle);
395 /* Don't allow imported objects to be mapped */
396 if (obj->import_attach) {
401 ret = drm_gem_create_mmap_offset(obj);
405 *offset = drm_vma_node_offset_addr(&obj->vma_node);
407 drm_gem_object_put_unlocked(obj);
411 EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
414 * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
415 * @file: drm file-private structure to remove the dumb handle from
416 * @dev: corresponding drm_device
417 * @handle: the dumb handle to remove
419 * This implements the &drm_driver.dumb_destroy kms driver callback for drivers
420 * which use gem to manage their backing storage.
422 int drm_gem_dumb_destroy(struct drm_file *file,
423 struct drm_device *dev,
426 return drm_gem_handle_delete(file, handle);
428 EXPORT_SYMBOL(drm_gem_dumb_destroy);
431 * drm_gem_handle_create_tail - internal functions to create a handle
432 * @file_priv: drm file-private structure to register the handle for
433 * @obj: object to register
434 * @handlep: pointer to return the created handle to the caller
436 * This expects the &drm_device.object_name_lock to be held already and will
437 * drop it before returning. Used to avoid races in establishing new handles
438 * when importing an object from either an flink name or a dma-buf.
440 * Handles must be release again through drm_gem_handle_delete(). This is done
441 * when userspace closes @file_priv for all attached handles, or through the
442 * GEM_CLOSE ioctl for individual handles.
445 drm_gem_handle_create_tail(struct drm_file *file_priv,
446 struct drm_gem_object *obj,
449 struct drm_device *dev = obj->dev;
453 WARN_ON(!mutex_is_locked(&dev->object_name_lock));
454 if (obj->handle_count++ == 0)
455 drm_gem_object_get(obj);
458 * Get the user-visible handle using idr. Preload and perform
459 * allocation under our spinlock.
461 idr_preload(GFP_KERNEL);
462 lockmgr(&file_priv->table_lock, LK_EXCLUSIVE);
464 ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
466 lockmgr(&file_priv->table_lock, LK_RELEASE);
469 mutex_unlock(&dev->object_name_lock);
475 ret = drm_vma_node_allow(&obj->vma_node, file_priv);
479 if (dev->driver->gem_open_object) {
480 ret = dev->driver->gem_open_object(obj, file_priv);
489 drm_vma_node_revoke(&obj->vma_node, file_priv);
491 lockmgr(&file_priv->table_lock, LK_EXCLUSIVE);
492 idr_remove(&file_priv->object_idr, handle);
493 lockmgr(&file_priv->table_lock, LK_RELEASE);
495 drm_gem_object_handle_put_unlocked(obj);
500 * drm_gem_handle_create - create a gem handle for an object
501 * @file_priv: drm file-private structure to register the handle for
502 * @obj: object to register
503 * @handlep: pionter to return the created handle to the caller
505 * Create a handle for this object. This adds a handle reference
506 * to the object, which includes a regular reference count. Callers
507 * will likely want to dereference the object afterwards.
509 int drm_gem_handle_create(struct drm_file *file_priv,
510 struct drm_gem_object *obj,
513 mutex_lock(&obj->dev->object_name_lock);
515 return drm_gem_handle_create_tail(file_priv, obj, handlep);
517 EXPORT_SYMBOL(drm_gem_handle_create);
520 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
521 * @obj: obj in question
523 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
525 * Note that drm_gem_object_release() already calls this function, so drivers
526 * don't have to take care of releasing the mmap offset themselves when freeing
530 drm_gem_free_mmap_offset(struct drm_gem_object *obj)
532 struct drm_device *dev = obj->dev;
534 struct drm_gem_mm *mm = dev->mm_private;
535 struct drm_hash_item *list;
539 list = &obj->map_list;
541 drm_ht_remove_item(&mm->offset_hash, list);
542 free_unr(mm->idxunr, list->key);
545 drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
547 EXPORT_SYMBOL(drm_gem_free_mmap_offset);
550 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
551 * @obj: obj in question
552 * @size: the virtual size
554 * GEM memory mapping works by handing back to userspace a fake mmap offset
555 * it can use in a subsequent mmap(2) call. The DRM core code then looks
556 * up the object based on the offset and sets up the various memory mapping
559 * This routine allocates and attaches a fake offset for @obj, in cases where
560 * the virtual size differs from the physical size (ie. &drm_gem_object.size).
561 * Otherwise just use drm_gem_create_mmap_offset().
563 * This function is idempotent and handles an already allocated mmap offset
564 * transparently. Drivers do not need to check for this case.
567 drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
569 struct drm_device *dev = obj->dev;
570 struct drm_gem_mm *mm = dev->mm_private;
576 obj->map_list.key = alloc_unr(mm->idxunr);
577 ret = drm_ht_insert_item(&mm->offset_hash, &obj->map_list);
579 DRM_ERROR("failed to add to map hash\n");
580 free_unr(mm->idxunr, obj->map_list.key);
585 return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
588 EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
591 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
592 * @obj: obj in question
594 * GEM memory mapping works by handing back to userspace a fake mmap offset
595 * it can use in a subsequent mmap(2) call. The DRM core code then looks
596 * up the object based on the offset and sets up the various memory mapping
599 * This routine allocates and attaches a fake offset for @obj.
601 * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
602 * the fake offset again.
604 int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
606 return drm_gem_create_mmap_offset_size(obj, obj->size);
608 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
611 * drm_gem_object_lookup - look up a GEM object from it's handle
612 * @filp: DRM file private date
613 * @handle: userspace handle
617 * A reference to the object named by the handle if such exists on @filp, NULL
620 struct drm_gem_object *
621 drm_gem_object_lookup(struct drm_file *filp, u32 handle)
623 struct drm_gem_object *obj;
625 lockmgr(&filp->table_lock, LK_EXCLUSIVE);
627 /* Check if we currently have a reference on the object */
628 obj = idr_find(&filp->object_idr, handle);
630 drm_gem_object_get(obj);
632 lockmgr(&filp->table_lock, LK_RELEASE);
636 EXPORT_SYMBOL(drm_gem_object_lookup);
639 * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
642 * @file_priv: drm file-private structure
644 * Releases the handle to an mm object.
647 drm_gem_close_ioctl(struct drm_device *dev, void *data,
648 struct drm_file *file_priv)
650 struct drm_gem_close *args = data;
653 if (!drm_core_check_feature(dev, DRIVER_GEM))
656 ret = drm_gem_handle_delete(file_priv, args->handle);
662 * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
665 * @file_priv: drm file-private structure
667 * Create a global name for an object, returning the name.
669 * Note that the name does not hold a reference; when the object
670 * is freed, the name goes away.
673 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
674 struct drm_file *file_priv)
676 struct drm_gem_flink *args = data;
677 struct drm_gem_object *obj;
680 if (!drm_core_check_feature(dev, DRIVER_GEM))
683 obj = drm_gem_object_lookup(file_priv, args->handle);
687 mutex_lock(&dev->object_name_lock);
688 /* prevent races with concurrent gem_close. */
689 if (obj->handle_count == 0) {
695 ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
702 args->name = (uint64_t) obj->name;
706 mutex_unlock(&dev->object_name_lock);
707 drm_gem_object_put_unlocked(obj);
712 * drm_gem_open - implementation of the GEM_OPEN ioctl
715 * @file_priv: drm file-private structure
717 * Open an object using the global name, returning a handle and the size.
719 * This handle (of course) holds a reference to the object, so the object
720 * will not go away until the handle is deleted.
723 drm_gem_open_ioctl(struct drm_device *dev, void *data,
724 struct drm_file *file_priv)
726 struct drm_gem_open *args = data;
727 struct drm_gem_object *obj;
731 if (!drm_core_check_feature(dev, DRIVER_GEM))
734 mutex_lock(&dev->object_name_lock);
735 obj = idr_find(&dev->object_name_idr, (int) args->name);
737 drm_gem_object_get(obj);
739 mutex_unlock(&dev->object_name_lock);
743 /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
744 ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
745 drm_gem_object_put_unlocked(obj);
749 args->handle = handle;
750 args->size = obj->size;
756 * gem_gem_open - initalizes GEM file-private structures at devnode open time
757 * @dev: drm_device which is being opened by userspace
758 * @file_private: drm file-private structure to set up
760 * Called at device open time, sets up the structure for handling refcounting
764 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
766 idr_init(&file_private->object_idr);
767 lockinit(&file_private->table_lock, "fptab", 0, 0);
771 * drm_gem_release - release file-private GEM resources
772 * @dev: drm_device which is being closed by userspace
773 * @file_private: drm file-private structure to clean up
775 * Called at close time when the filp is going away.
777 * Releases any remaining references on objects by this filp.
780 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
782 idr_for_each(&file_private->object_idr,
783 &drm_gem_object_release_handle, file_private);
784 idr_destroy(&file_private->object_idr);
788 * drm_gem_object_release - release GEM buffer object resources
789 * @obj: GEM buffer object
791 * This releases any structures and resources used by @obj and is the invers of
792 * drm_gem_object_init().
795 drm_gem_object_release(struct drm_gem_object *obj)
797 WARN_ON(obj->dma_buf);
800 * obj->vm_obj can be NULL for private gem objects.
802 vm_object_deallocate(obj->filp);
804 drm_gem_free_mmap_offset(obj);
806 EXPORT_SYMBOL(drm_gem_object_release);
809 * drm_gem_object_free - free a GEM object
810 * @kref: kref of the object to free
812 * Called after the last reference to the object has been lost.
813 * Must be called holding &drm_device.struct_mutex.
818 drm_gem_object_free(struct kref *kref)
820 struct drm_gem_object *obj =
821 container_of(kref, struct drm_gem_object, refcount);
822 struct drm_device *dev = obj->dev;
824 if (dev->driver->gem_free_object_unlocked) {
825 dev->driver->gem_free_object_unlocked(obj);
826 } else if (dev->driver->gem_free_object) {
827 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
829 dev->driver->gem_free_object(obj);
832 EXPORT_SYMBOL(drm_gem_object_free);
835 * drm_gem_object_put_unlocked - drop a GEM buffer object reference
836 * @obj: GEM buffer object
838 * This releases a reference to @obj. Callers must not hold the
839 * &drm_device.struct_mutex lock when calling this function.
841 * See also __drm_gem_object_put().
844 drm_gem_object_put_unlocked(struct drm_gem_object *obj)
846 struct drm_device *dev;
852 might_lock(&dev->struct_mutex);
854 if (dev->driver->gem_free_object_unlocked)
855 kref_put(&obj->refcount, drm_gem_object_free);
856 else if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
858 mutex_unlock(&dev->struct_mutex);
860 EXPORT_SYMBOL(drm_gem_object_put_unlocked);
863 * drm_gem_object_put - release a GEM buffer object reference
864 * @obj: GEM buffer object
866 * This releases a reference to @obj. Callers must hold the
867 * &drm_device.struct_mutex lock when calling this function, even when the
868 * driver doesn't use &drm_device.struct_mutex for anything.
870 * For drivers not encumbered with legacy locking use
871 * drm_gem_object_put_unlocked() instead.
874 drm_gem_object_put(struct drm_gem_object *obj)
877 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
879 kref_put(&obj->refcount, drm_gem_object_free);
882 EXPORT_SYMBOL(drm_gem_object_put);
885 * drm_gem_vm_open - vma->ops->open implementation for GEM
886 * @vma: VM area structure
888 * This function implements the #vm_operations_struct open() callback for GEM
889 * drivers. This must be used together with drm_gem_vm_close().
891 void drm_gem_vm_open(struct vm_area_struct *vma)
893 struct drm_gem_object *obj = vma->vm_private_data;
895 drm_gem_object_get(obj);
897 EXPORT_SYMBOL(drm_gem_vm_open);
900 * drm_gem_vm_close - vma->ops->close implementation for GEM
901 * @vma: VM area structure
903 * This function implements the #vm_operations_struct close() callback for GEM
904 * drivers. This must be used together with drm_gem_vm_open().
906 void drm_gem_vm_close(struct vm_area_struct *vma)
908 struct drm_gem_object *obj = vma->vm_private_data;
910 drm_gem_object_put_unlocked(obj);
912 EXPORT_SYMBOL(drm_gem_vm_close);
916 * drm_gem_mmap_obj - memory map a GEM object
917 * @obj: the GEM object to map
918 * @obj_size: the object size to be mapped, in bytes
919 * @vma: VMA for the area to be mapped
921 * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
922 * provided by the driver. Depending on their requirements, drivers can either
923 * provide a fault handler in their gem_vm_ops (in which case any accesses to
924 * the object will be trapped, to perform migration, GTT binding, surface
925 * register allocation, or performance monitoring), or mmap the buffer memory
926 * synchronously after calling drm_gem_mmap_obj.
928 * This function is mainly intended to implement the DMABUF mmap operation, when
929 * the GEM object is not looked up based on its fake offset. To implement the
930 * DRM mmap operation, drivers should use the drm_gem_mmap() function.
932 * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
933 * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
934 * callers must verify access restrictions before calling this helper.
936 * Return 0 or success or -EINVAL if the object size is smaller than the VMA
937 * size, or if no gem_vm_ops are provided.
939 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
940 struct vm_area_struct *vma)
942 struct drm_device *dev = obj->dev;
944 /* Check for valid size. */
945 if (obj_size < vma->vm_end - vma->vm_start)
948 if (!dev->driver->gem_vm_ops)
951 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
952 vma->vm_ops = dev->driver->gem_vm_ops;
953 vma->vm_private_data = obj;
954 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
955 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
957 /* Take a ref for this mapping of the object, so that the fault
958 * handler can dereference the mmap offset's pointer to the object.
959 * This reference is cleaned up by the corresponding vm_close
960 * (which should happen whether the vma was created by this call, or
961 * by a vm_open due to mremap or partial unmap or whatever).
963 drm_gem_object_get(obj);
967 EXPORT_SYMBOL(drm_gem_mmap_obj);
970 * drm_gem_mmap - memory map routine for GEM objects
971 * @filp: DRM file pointer
972 * @vma: VMA for the area to be mapped
974 * If a driver supports GEM object mapping, mmap calls on the DRM file
975 * descriptor will end up here.
977 * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
978 * contain the fake offset we created when the GTT map ioctl was called on
979 * the object) and map it with a call to drm_gem_mmap_obj().
981 * If the caller is not granted access to the buffer object, the mmap will fail
982 * with EACCES. Please see the vma manager for more information.
984 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
986 struct drm_file *priv = filp->private_data;
987 struct drm_device *dev = priv->minor->dev;
988 struct drm_gem_object *obj = NULL;
989 struct drm_vma_offset_node *node;
992 if (drm_dev_is_unplugged(dev))
995 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
996 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
1000 obj = container_of(node, struct drm_gem_object, vma_node);
1002 * When the object is being freed, after it hits 0-refcnt it
1003 * proceeds to tear down the object. In the process it will
1004 * attempt to remove the VMA offset and so acquire this
1005 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
1006 * that matches our range, we know it is in the process of being
1007 * destroyed and will be freed as soon as we release the lock -
1008 * so we have to check for the 0-refcnted object and treat it as
1011 if (!kref_get_unless_zero(&obj->refcount))
1014 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
1019 if (!drm_vma_node_is_allowed(node, priv)) {
1020 drm_gem_object_put_unlocked(obj);
1024 ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
1027 drm_gem_object_put_unlocked(obj);
1031 EXPORT_SYMBOL(drm_gem_mmap);
1034 #ifdef __DragonFly__
1035 static struct drm_gem_object *
1036 drm_gem_object_from_offset(struct drm_device *dev, vm_ooffset_t offset)
1038 struct drm_gem_object *obj;
1039 struct drm_gem_mm *mm = dev->mm_private;
1040 struct drm_hash_item *hash;
1042 if ((offset & DRM_GEM_MAPPING_MASK) != DRM_GEM_MAPPING_KEY)
1044 offset &= ~DRM_GEM_MAPPING_KEY;
1046 if (drm_ht_find_item(&mm->offset_hash, DRM_GEM_MAPPING_IDX(offset),
1050 obj = container_of(hash, struct drm_gem_object, map_list);
1055 drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset, vm_size_t size,
1056 struct vm_object **obj_res, int nprot)
1058 struct drm_gem_object *gem_obj;
1059 struct vm_object *vm_obj;
1062 gem_obj = drm_gem_object_from_offset(dev, *offset);
1063 if (gem_obj == NULL) {
1068 drm_gem_object_reference(gem_obj);
1070 vm_obj = cdev_pager_allocate(gem_obj, OBJT_MGTDEVICE,
1071 dev->driver->gem_vm_ops, size, nprot,
1072 DRM_GEM_MAPPING_MAPOFF(*offset), curthread->td_ucred);
1073 if (vm_obj == NULL) {
1074 drm_gem_object_unreference_unlocked(gem_obj);
1077 *offset = DRM_GEM_MAPPING_MAPOFF(*offset);