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
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/limits.h>
62 #include <sys/mutex.h>
66 #include <vm/vm_page.h>
68 #include <linux/types.h>
70 #include <linux/module.h>
72 #include <drm/drm_vma_manager.h>
73 #include <drm/drm_gem.h>
74 #include "drm_internal.h"
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
92 * - Process limits prevent more than 1024 or so being used at a time by
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
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)
117 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
118 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
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
);
135 DRM_ERROR("out of memory\n");
139 dev
->mm_private
= mm
;
141 if (drm_ht_create(&mm
->offset_hash
, 12)) {
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
);
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
);
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);
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
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);
204 kref_init(&obj
->refcount
);
205 obj
->handle_count
= 0;
207 drm_vma_node_reset(&obj
->vma_node
);
209 EXPORT_SYMBOL(drm_gem_private_object_init
);
212 drm_gem_remove_prime_handles(struct drm_gem_object
*obj
, struct drm_file
*filp
)
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
);
226 static void drm_gem_object_ref_bug(struct kref
*list_kref
)
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
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 */
247 idr_remove(&dev
->object_name_idr
, obj
->name
);
250 * The object name held a reference to this object, drop
253 * This cannot be the last reference, since the handle holds one too.
255 kref_put(&obj
->refcount
, drm_gem_object_ref_bug
);
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. */
264 dma_buf_put(obj
->dma_buf
);
271 drm_gem_object_handle_unreference_unlocked(struct drm_gem_object
*obj
)
273 if (WARN_ON(obj
->handle_count
== 0))
277 * Must bump handle count first as this may be the last
278 * ref, in which case the object would disappear before we
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 * drm_gem_handle_delete - deletes the given file-private handle
292 * @filp: drm file-private structure to use for the handle look up
293 * @handle: userspace handle to delete
295 * Removes the GEM handle from the @filp lookup table and if this is the last
296 * handle also cleans up linked resources like GEM names.
299 drm_gem_handle_delete(struct drm_file
*filp
, u32 handle
)
301 struct drm_device
*dev
;
302 struct drm_gem_object
*obj
;
304 /* This is gross. The idr system doesn't let us try a delete and
305 * return an error code. It just spews if you fail at deleting.
306 * So, we have to grab a lock around finding the object and then
307 * doing the delete on it and dropping the refcount, or the user
308 * could race us to double-decrement the refcount and cause a
309 * use-after-free later. Given the frequency of our handle lookups,
310 * we may want to use ida for number allocation and a hash table
311 * for the pointers, anyway.
313 lockmgr(&filp
->table_lock
, LK_EXCLUSIVE
);
315 /* Check if we currently have a reference on the object */
316 obj
= idr_find(&filp
->object_idr
, handle
);
318 lockmgr(&filp
->table_lock
, LK_RELEASE
);
323 /* Release reference and decrement refcount. */
324 idr_remove(&filp
->object_idr
, handle
);
325 lockmgr(&filp
->table_lock
, LK_RELEASE
);
327 drm_gem_remove_prime_handles(obj
, filp
);
329 if (dev
->driver
->gem_close_object
)
330 dev
->driver
->gem_close_object(obj
, filp
);
331 drm_gem_object_handle_unreference_unlocked(obj
);
335 EXPORT_SYMBOL(drm_gem_handle_delete
);
338 * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
339 * @file: drm file-private structure to remove the dumb handle from
340 * @dev: corresponding drm_device
341 * @handle: the dumb handle to remove
343 * This implements the ->dumb_destroy kms driver callback for drivers which use
344 * gem to manage their backing storage.
346 int drm_gem_dumb_destroy(struct drm_file
*file
,
347 struct drm_device
*dev
,
350 return drm_gem_handle_delete(file
, handle
);
352 EXPORT_SYMBOL(drm_gem_dumb_destroy
);
355 * drm_gem_handle_create_tail - internal functions to create a handle
356 * @file_priv: drm file-private structure to register the handle for
357 * @obj: object to register
358 * @handlep: pointer to return the created handle to the caller
360 * This expects the dev->object_name_lock to be held already and will drop it
361 * before returning. Used to avoid races in establishing new handles when
362 * importing an object from either an flink name or a dma-buf.
365 drm_gem_handle_create_tail(struct drm_file
*file_priv
,
366 struct drm_gem_object
*obj
,
369 struct drm_device
*dev
= obj
->dev
;
372 *handlep
= 0; /* whack gcc warning */
373 WARN_ON(!mutex_is_locked(&dev
->object_name_lock
));
376 * Get the user-visible handle using idr. Preload and perform
377 * allocation under our spinlock.
379 idr_preload(GFP_KERNEL
);
380 lockmgr(&file_priv
->table_lock
, LK_EXCLUSIVE
);
382 ret
= idr_alloc(&file_priv
->object_idr
, obj
, 1, 0, GFP_NOWAIT
);
383 drm_gem_object_reference(obj
);
385 lockmgr(&file_priv
->table_lock
, LK_RELEASE
);
387 mutex_unlock(&dev
->object_name_lock
);
389 drm_gem_object_handle_unreference_unlocked(obj
);
395 ret
= drm_vma_node_allow(&obj
->vma_node
, file_priv
->filp
);
397 drm_gem_handle_delete(file_priv
, *handlep
);
402 if (dev
->driver
->gem_open_object
) {
403 ret
= dev
->driver
->gem_open_object(obj
, file_priv
);
405 drm_gem_handle_delete(file_priv
, *handlep
);
414 * drm_gem_handle_create - create a gem handle for an object
415 * @file_priv: drm file-private structure to register the handle for
416 * @obj: object to register
417 * @handlep: pionter to return the created handle to the caller
419 * Create a handle for this object. This adds a handle reference
420 * to the object, which includes a regular reference count. Callers
421 * will likely want to dereference the object afterwards.
423 int drm_gem_handle_create(struct drm_file
*file_priv
,
424 struct drm_gem_object
*obj
,
427 mutex_lock(&obj
->dev
->object_name_lock
);
429 return drm_gem_handle_create_tail(file_priv
, obj
, handlep
);
431 EXPORT_SYMBOL(drm_gem_handle_create
);
434 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
435 * @obj: obj in question
437 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
440 drm_gem_free_mmap_offset(struct drm_gem_object
*obj
)
442 struct drm_device
*dev
= obj
->dev
;
443 struct drm_gem_mm
*mm
= dev
->mm_private
;
444 struct drm_hash_item
*list
;
448 list
= &obj
->map_list
;
450 drm_ht_remove_item(&mm
->offset_hash
, list
);
451 free_unr(mm
->idxunr
, list
->key
);
454 drm_vma_offset_remove(&mm
->vma_manager
, &obj
->vma_node
);
456 EXPORT_SYMBOL(drm_gem_free_mmap_offset
);
459 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
460 * @obj: obj in question
461 * @size: the virtual size
463 * GEM memory mapping works by handing back to userspace a fake mmap offset
464 * it can use in a subsequent mmap(2) call. The DRM core code then looks
465 * up the object based on the offset and sets up the various memory mapping
468 * This routine allocates and attaches a fake offset for @obj, in cases where
469 * the virtual size differs from the physical size (ie. obj->size). Otherwise
470 * just use drm_gem_create_mmap_offset().
473 drm_gem_create_mmap_offset_size(struct drm_gem_object
*obj
, size_t size
)
475 struct drm_device
*dev
= obj
->dev
;
476 struct drm_gem_mm
*mm
= dev
->mm_private
;
482 obj
->map_list
.key
= alloc_unr(mm
->idxunr
);
483 ret
= drm_ht_insert_item(&mm
->offset_hash
, &obj
->map_list
);
485 DRM_ERROR("failed to add to map hash\n");
486 free_unr(mm
->idxunr
, obj
->map_list
.key
);
492 return drm_vma_offset_add(&mm
->vma_manager
, &obj
->vma_node
,
495 EXPORT_SYMBOL(drm_gem_create_mmap_offset_size
);
498 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
499 * @obj: obj in question
501 * GEM memory mapping works by handing back to userspace a fake mmap offset
502 * it can use in a subsequent mmap(2) call. The DRM core code then looks
503 * up the object based on the offset and sets up the various memory mapping
506 * This routine allocates and attaches a fake offset for @obj.
508 int drm_gem_create_mmap_offset(struct drm_gem_object
*obj
)
510 return drm_gem_create_mmap_offset_size(obj
, obj
->size
);
512 EXPORT_SYMBOL(drm_gem_create_mmap_offset
);
514 /** Returns a reference to the object named by the handle. */
515 struct drm_gem_object
*
516 drm_gem_object_lookup(struct drm_device
*dev
, struct drm_file
*filp
,
519 struct drm_gem_object
*obj
;
521 lockmgr(&filp
->table_lock
, LK_EXCLUSIVE
);
523 /* Check if we currently have a reference on the object */
524 obj
= idr_find(&filp
->object_idr
, handle
);
526 lockmgr(&filp
->table_lock
, LK_RELEASE
);
530 drm_gem_object_reference(obj
);
532 lockmgr(&filp
->table_lock
, LK_RELEASE
);
536 EXPORT_SYMBOL(drm_gem_object_lookup
);
539 * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
542 * @file_priv: drm file-private structure
544 * Releases the handle to an mm object.
547 drm_gem_close_ioctl(struct drm_device
*dev
, void *data
,
548 struct drm_file
*file_priv
)
550 struct drm_gem_close
*args
= data
;
553 if (!drm_core_check_feature(dev
, DRIVER_GEM
))
556 ret
= drm_gem_handle_delete(file_priv
, args
->handle
);
562 * Create a global name for an object, returning the name.
564 * Note that the name does not hold a reference; when the object
565 * is freed, the name goes away.
568 drm_gem_flink_ioctl(struct drm_device
*dev
, void *data
,
569 struct drm_file
*file_priv
)
571 struct drm_gem_flink
*args
= data
;
572 struct drm_gem_object
*obj
;
575 if (!drm_core_check_feature(dev
, DRIVER_GEM
))
578 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
582 idr_preload(GFP_KERNEL
);
583 lockmgr(&dev
->object_name_lock
, LK_EXCLUSIVE
);
584 /* prevent races with concurrent gem_close. */
585 if (obj
->handle_count
== 0) {
591 ret
= idr_alloc(&dev
->object_name_idr
, obj
, 1, 0, GFP_NOWAIT
);
597 /* Allocate a reference for the name table. */
598 drm_gem_object_reference(obj
);
601 args
->name
= (uint64_t) obj
->name
;
605 lockmgr(&dev
->object_name_lock
, LK_RELEASE
);
607 drm_gem_object_unreference_unlocked(obj
);
612 * drm_gem_open - implementation of the GEM_OPEN ioctl
615 * @file_priv: drm file-private structure
617 * Open an object using the global name, returning a handle and the size.
619 * This handle (of course) holds a reference to the object, so the object
620 * will not go away until the handle is deleted.
623 drm_gem_open_ioctl(struct drm_device
*dev
, void *data
,
624 struct drm_file
*file_priv
)
626 struct drm_gem_open
*args
= data
;
627 struct drm_gem_object
*obj
;
631 if (!drm_core_check_feature(dev
, DRIVER_GEM
))
634 lockmgr(&dev
->object_name_lock
, LK_EXCLUSIVE
);
635 obj
= idr_find(&dev
->object_name_idr
, (int) args
->name
);
637 drm_gem_object_reference(obj
);
638 lockmgr(&dev
->object_name_lock
, LK_RELEASE
);
642 ret
= drm_gem_handle_create(file_priv
, obj
, &handle
);
643 drm_gem_object_unreference_unlocked(obj
);
647 args
->handle
= handle
;
648 args
->size
= obj
->size
;
654 * gem_gem_open - initalizes GEM file-private structures at devnode open time
655 * @dev: drm_device which is being opened by userspace
656 * @file_private: drm file-private structure to set up
658 * Called at device open time, sets up the structure for handling refcounting
662 drm_gem_open(struct drm_device
*dev
, struct drm_file
*file_private
)
664 idr_init(&file_private
->object_idr
);
665 lockinit(&file_private
->table_lock
, "fptab", 0, LK_CANRECURSE
);
669 * Called at device close to release the file's
670 * handle references on objects.
673 drm_gem_object_release_handle(int id
, void *ptr
, void *data
)
675 struct drm_file
*file_priv
= data
;
676 struct drm_gem_object
*obj
= ptr
;
677 struct drm_device
*dev
= obj
->dev
;
679 drm_gem_remove_prime_handles(obj
, file_priv
);
681 if (dev
->driver
->gem_close_object
)
682 dev
->driver
->gem_close_object(obj
, file_priv
);
684 drm_gem_object_handle_unreference_unlocked(obj
);
690 * drm_gem_release - release file-private GEM resources
691 * @dev: drm_device which is being closed by userspace
692 * @file_private: drm file-private structure to clean up
694 * Called at close time when the filp is going away.
696 * Releases any remaining references on objects by this filp.
699 drm_gem_release(struct drm_device
*dev
, struct drm_file
*file_private
)
701 idr_for_each(&file_private
->object_idr
,
702 &drm_gem_object_release_handle
, file_private
);
703 idr_destroy(&file_private
->object_idr
);
707 drm_gem_object_release(struct drm_gem_object
*obj
)
711 * obj->vm_obj can be NULL for private gem objects.
713 vm_object_deallocate(obj
->vm_obj
);
715 EXPORT_SYMBOL(drm_gem_object_release
);
718 * drm_gem_object_free - free a GEM object
719 * @kref: kref of the object to free
721 * Called after the last reference to the object has been lost.
722 * Must be called holding struct_ mutex
727 drm_gem_object_free(struct kref
*kref
)
729 struct drm_gem_object
*obj
=
730 container_of(kref
, struct drm_gem_object
, refcount
);
731 struct drm_device
*dev
= obj
->dev
;
733 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
735 if (dev
->driver
->gem_free_object
!= NULL
)
736 dev
->driver
->gem_free_object(obj
);
738 EXPORT_SYMBOL(drm_gem_object_free
);
740 static struct drm_gem_object
*
741 drm_gem_object_from_offset(struct drm_device
*dev
, vm_ooffset_t offset
)
743 struct drm_gem_object
*obj
;
744 struct drm_gem_mm
*mm
= dev
->mm_private
;
745 struct drm_hash_item
*hash
;
747 if ((offset
& DRM_GEM_MAPPING_MASK
) != DRM_GEM_MAPPING_KEY
)
749 offset
&= ~DRM_GEM_MAPPING_KEY
;
751 if (drm_ht_find_item(&mm
->offset_hash
, DRM_GEM_MAPPING_IDX(offset
),
755 obj
= container_of(hash
, struct drm_gem_object
, map_list
);
760 drm_gem_mmap_single(struct drm_device
*dev
, vm_ooffset_t
*offset
, vm_size_t size
,
761 struct vm_object
**obj_res
, int nprot
)
763 struct drm_gem_object
*gem_obj
;
764 struct vm_object
*vm_obj
;
767 gem_obj
= drm_gem_object_from_offset(dev
, *offset
);
768 if (gem_obj
== NULL
) {
773 drm_gem_object_reference(gem_obj
);
775 vm_obj
= cdev_pager_allocate(gem_obj
, OBJT_MGTDEVICE
,
776 dev
->driver
->gem_pager_ops
, size
, nprot
,
777 DRM_GEM_MAPPING_MAPOFF(*offset
), curthread
->td_ucred
);
778 if (vm_obj
== NULL
) {
779 drm_gem_object_unreference_unlocked(gem_obj
);
782 *offset
= DRM_GEM_MAPPING_MAPOFF(*offset
);