1673a394bSEric Anholt /* 2673a394bSEric Anholt * Copyright © 2008 Intel Corporation 3673a394bSEric Anholt * 4673a394bSEric Anholt * Permission is hereby granted, free of charge, to any person obtaining a 5673a394bSEric Anholt * copy of this software and associated documentation files (the "Software"), 6673a394bSEric Anholt * to deal in the Software without restriction, including without limitation 7673a394bSEric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8673a394bSEric Anholt * and/or sell copies of the Software, and to permit persons to whom the 9673a394bSEric Anholt * Software is furnished to do so, subject to the following conditions: 10673a394bSEric Anholt * 11673a394bSEric Anholt * The above copyright notice and this permission notice (including the next 12673a394bSEric Anholt * paragraph) shall be included in all copies or substantial portions of the 13673a394bSEric Anholt * Software. 14673a394bSEric Anholt * 15673a394bSEric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16673a394bSEric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17673a394bSEric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18673a394bSEric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19673a394bSEric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20673a394bSEric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21673a394bSEric Anholt * IN THE SOFTWARE. 22673a394bSEric Anholt * 23673a394bSEric Anholt * Authors: 24673a394bSEric Anholt * Eric Anholt <eric@anholt.net> 25673a394bSEric Anholt * 26673a394bSEric Anholt */ 27673a394bSEric Anholt 28673a394bSEric Anholt #include <linux/types.h> 29673a394bSEric Anholt #include <linux/slab.h> 30673a394bSEric Anholt #include <linux/mm.h> 31673a394bSEric Anholt #include <linux/uaccess.h> 32673a394bSEric Anholt #include <linux/fs.h> 33673a394bSEric Anholt #include <linux/file.h> 34673a394bSEric Anholt #include <linux/module.h> 35673a394bSEric Anholt #include <linux/mman.h> 36673a394bSEric Anholt #include <linux/pagemap.h> 375949eac4SHugh Dickins #include <linux/shmem_fs.h> 383248877eSDave Airlie #include <linux/dma-buf.h> 39760285e7SDavid Howells #include <drm/drmP.h> 400de23977SDavid Herrmann #include <drm/drm_vma_manager.h> 41d9fc9413SDaniel Vetter #include <drm/drm_gem.h> 4267d0ec4eSDaniel Vetter #include "drm_internal.h" 43673a394bSEric Anholt 44673a394bSEric Anholt /** @file drm_gem.c 45673a394bSEric Anholt * 46673a394bSEric Anholt * This file provides some of the base ioctls and library routines for 47673a394bSEric Anholt * the graphics memory manager implemented by each device driver. 48673a394bSEric Anholt * 49673a394bSEric Anholt * Because various devices have different requirements in terms of 50673a394bSEric Anholt * synchronization and migration strategies, implementing that is left up to 51673a394bSEric Anholt * the driver, and all that the general API provides should be generic -- 52673a394bSEric Anholt * allocating objects, reading/writing data with the cpu, freeing objects. 53673a394bSEric Anholt * Even there, platform-dependent optimizations for reading/writing data with 54673a394bSEric Anholt * the CPU mean we'll likely hook those out to driver-specific calls. However, 55673a394bSEric Anholt * the DRI2 implementation wants to have at least allocate/mmap be generic. 56673a394bSEric Anholt * 57673a394bSEric Anholt * The goal was to have swap-backed object allocation managed through 58673a394bSEric Anholt * struct file. However, file descriptors as handles to a struct file have 59673a394bSEric Anholt * two major failings: 60673a394bSEric Anholt * - Process limits prevent more than 1024 or so being used at a time by 61673a394bSEric Anholt * default. 62673a394bSEric Anholt * - Inability to allocate high fds will aggravate the X Server's select() 63673a394bSEric Anholt * handling, and likely that of many GL client applications as well. 64673a394bSEric Anholt * 65673a394bSEric Anholt * This led to a plan of using our own integer IDs (called handles, following 66673a394bSEric Anholt * DRM terminology) to mimic fds, and implement the fd syscalls we need as 67673a394bSEric Anholt * ioctls. The objects themselves will still include the struct file so 68673a394bSEric Anholt * that we can transition to fds if the required kernel infrastructure shows 69673a394bSEric Anholt * up at a later date, and as our interface with shmfs for memory allocation. 70673a394bSEric Anholt */ 71673a394bSEric Anholt 72a2c0a97bSJesse Barnes /* 73a2c0a97bSJesse Barnes * We make up offsets for buffer objects so we can recognize them at 74a2c0a97bSJesse Barnes * mmap time. 75a2c0a97bSJesse Barnes */ 7605269a3aSJordan Crouse 7705269a3aSJordan Crouse /* pgoff in mmap is an unsigned long, so we need to make sure that 7805269a3aSJordan Crouse * the faked up offset will fit 7905269a3aSJordan Crouse */ 8005269a3aSJordan Crouse 8105269a3aSJordan Crouse #if BITS_PER_LONG == 64 82a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) 83a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16) 8405269a3aSJordan Crouse #else 8505269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1) 8605269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16) 8705269a3aSJordan Crouse #endif 88a2c0a97bSJesse Barnes 89673a394bSEric Anholt /** 9089d61fc0SDaniel Vetter * drm_gem_init - Initialize the GEM device fields 9189d61fc0SDaniel Vetter * @dev: drm_devic structure to initialize 92673a394bSEric Anholt */ 93673a394bSEric Anholt int 94673a394bSEric Anholt drm_gem_init(struct drm_device *dev) 95673a394bSEric Anholt { 96b04a5906SDaniel Vetter struct drm_vma_offset_manager *vma_offset_manager; 97a2c0a97bSJesse Barnes 98cd4f013fSDaniel Vetter mutex_init(&dev->object_name_lock); 99673a394bSEric Anholt idr_init(&dev->object_name_idr); 100a2c0a97bSJesse Barnes 101b04a5906SDaniel Vetter vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL); 102b04a5906SDaniel Vetter if (!vma_offset_manager) { 103a2c0a97bSJesse Barnes DRM_ERROR("out of memory\n"); 104a2c0a97bSJesse Barnes return -ENOMEM; 105a2c0a97bSJesse Barnes } 106a2c0a97bSJesse Barnes 107b04a5906SDaniel Vetter dev->vma_offset_manager = vma_offset_manager; 108b04a5906SDaniel Vetter drm_vma_offset_manager_init(vma_offset_manager, 1090de23977SDavid Herrmann DRM_FILE_PAGE_OFFSET_START, 11077ef8bbcSDavid Herrmann DRM_FILE_PAGE_OFFSET_SIZE); 111a2c0a97bSJesse Barnes 112673a394bSEric Anholt return 0; 113673a394bSEric Anholt } 114673a394bSEric Anholt 115a2c0a97bSJesse Barnes void 116a2c0a97bSJesse Barnes drm_gem_destroy(struct drm_device *dev) 117a2c0a97bSJesse Barnes { 118a2c0a97bSJesse Barnes 119b04a5906SDaniel Vetter drm_vma_offset_manager_destroy(dev->vma_offset_manager); 120b04a5906SDaniel Vetter kfree(dev->vma_offset_manager); 121b04a5906SDaniel Vetter dev->vma_offset_manager = NULL; 122a2c0a97bSJesse Barnes } 123a2c0a97bSJesse Barnes 124673a394bSEric Anholt /** 12589d61fc0SDaniel Vetter * drm_gem_object_init - initialize an allocated shmem-backed GEM object 12689d61fc0SDaniel Vetter * @dev: drm_device the object should be initialized for 12789d61fc0SDaniel Vetter * @obj: drm_gem_object to initialize 12889d61fc0SDaniel Vetter * @size: object size 12989d61fc0SDaniel Vetter * 13062cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with 1311d397043SDaniel Vetter * shmfs backing store. 1321d397043SDaniel Vetter */ 1331d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev, 1341d397043SDaniel Vetter struct drm_gem_object *obj, size_t size) 1351d397043SDaniel Vetter { 13689c8233fSDavid Herrmann struct file *filp; 1371d397043SDaniel Vetter 1386ab11a26SDaniel Vetter drm_gem_private_object_init(dev, obj, size); 1396ab11a26SDaniel Vetter 14089c8233fSDavid Herrmann filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); 14189c8233fSDavid Herrmann if (IS_ERR(filp)) 14289c8233fSDavid Herrmann return PTR_ERR(filp); 1431d397043SDaniel Vetter 14489c8233fSDavid Herrmann obj->filp = filp; 1451d397043SDaniel Vetter 1461d397043SDaniel Vetter return 0; 1471d397043SDaniel Vetter } 1481d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init); 1491d397043SDaniel Vetter 1501d397043SDaniel Vetter /** 1512a5706a3SLaurent Pinchart * drm_gem_private_object_init - initialize an allocated private GEM object 15289d61fc0SDaniel Vetter * @dev: drm_device the object should be initialized for 15389d61fc0SDaniel Vetter * @obj: drm_gem_object to initialize 15489d61fc0SDaniel Vetter * @size: object size 15589d61fc0SDaniel Vetter * 15662cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with 15762cb7011SAlan Cox * no GEM provided backing store. Instead the caller is responsible for 15862cb7011SAlan Cox * backing the object and handling it. 15962cb7011SAlan Cox */ 16089c8233fSDavid Herrmann void drm_gem_private_object_init(struct drm_device *dev, 16162cb7011SAlan Cox struct drm_gem_object *obj, size_t size) 16262cb7011SAlan Cox { 16362cb7011SAlan Cox BUG_ON((size & (PAGE_SIZE - 1)) != 0); 16462cb7011SAlan Cox 16562cb7011SAlan Cox obj->dev = dev; 16662cb7011SAlan Cox obj->filp = NULL; 16762cb7011SAlan Cox 16862cb7011SAlan Cox kref_init(&obj->refcount); 169a8e11d1cSDaniel Vetter obj->handle_count = 0; 17062cb7011SAlan Cox obj->size = size; 17188d7ebe5SDavid Herrmann drm_vma_node_reset(&obj->vma_node); 17262cb7011SAlan Cox } 17362cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init); 17462cb7011SAlan Cox 1750ff926c7SDave Airlie static void 1760ff926c7SDave Airlie drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp) 1770ff926c7SDave Airlie { 178319c933cSDaniel Vetter /* 179319c933cSDaniel Vetter * Note: obj->dma_buf can't disappear as long as we still hold a 180319c933cSDaniel Vetter * handle reference in obj->handle_count. 181319c933cSDaniel Vetter */ 182d0b2c533SDaniel Vetter mutex_lock(&filp->prime.lock); 183319c933cSDaniel Vetter if (obj->dma_buf) { 184d0b2c533SDaniel Vetter drm_prime_remove_buf_handle_locked(&filp->prime, 185319c933cSDaniel Vetter obj->dma_buf); 1860ff926c7SDave Airlie } 187d0b2c533SDaniel Vetter mutex_unlock(&filp->prime.lock); 1880ff926c7SDave Airlie } 1890ff926c7SDave Airlie 19036da5908SDaniel Vetter /** 191c6a84325SThierry Reding * drm_gem_object_handle_free - release resources bound to userspace handles 19289d61fc0SDaniel Vetter * @obj: GEM object to clean up. 19389d61fc0SDaniel Vetter * 19436da5908SDaniel Vetter * Called after the last handle to the object has been closed 19536da5908SDaniel Vetter * 19636da5908SDaniel Vetter * Removes any name for the object. Note that this must be 19736da5908SDaniel Vetter * called before drm_gem_object_free or we'll be touching 19836da5908SDaniel Vetter * freed memory 19936da5908SDaniel Vetter */ 20036da5908SDaniel Vetter static void drm_gem_object_handle_free(struct drm_gem_object *obj) 20136da5908SDaniel Vetter { 20236da5908SDaniel Vetter struct drm_device *dev = obj->dev; 20336da5908SDaniel Vetter 20436da5908SDaniel Vetter /* Remove any name for this object */ 20536da5908SDaniel Vetter if (obj->name) { 20636da5908SDaniel Vetter idr_remove(&dev->object_name_idr, obj->name); 20736da5908SDaniel Vetter obj->name = 0; 208a8e11d1cSDaniel Vetter } 20936da5908SDaniel Vetter } 21036da5908SDaniel Vetter 211319c933cSDaniel Vetter static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj) 212319c933cSDaniel Vetter { 213319c933cSDaniel Vetter /* Unbreak the reference cycle if we have an exported dma_buf. */ 214319c933cSDaniel Vetter if (obj->dma_buf) { 215319c933cSDaniel Vetter dma_buf_put(obj->dma_buf); 216319c933cSDaniel Vetter obj->dma_buf = NULL; 217319c933cSDaniel Vetter } 218319c933cSDaniel Vetter } 219319c933cSDaniel Vetter 220becee2a5SDaniel Vetter static void 22136da5908SDaniel Vetter drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj) 22236da5908SDaniel Vetter { 223a8e11d1cSDaniel Vetter if (WARN_ON(obj->handle_count == 0)) 22436da5908SDaniel Vetter return; 22536da5908SDaniel Vetter 22636da5908SDaniel Vetter /* 22736da5908SDaniel Vetter * Must bump handle count first as this may be the last 22836da5908SDaniel Vetter * ref, in which case the object would disappear before we 22936da5908SDaniel Vetter * checked for a name 23036da5908SDaniel Vetter */ 23136da5908SDaniel Vetter 232cd4f013fSDaniel Vetter mutex_lock(&obj->dev->object_name_lock); 233319c933cSDaniel Vetter if (--obj->handle_count == 0) { 23436da5908SDaniel Vetter drm_gem_object_handle_free(obj); 235319c933cSDaniel Vetter drm_gem_object_exported_dma_buf_free(obj); 236319c933cSDaniel Vetter } 237cd4f013fSDaniel Vetter mutex_unlock(&obj->dev->object_name_lock); 238a8e11d1cSDaniel Vetter 23936da5908SDaniel Vetter drm_gem_object_unreference_unlocked(obj); 24036da5908SDaniel Vetter } 24136da5908SDaniel Vetter 242673a394bSEric Anholt /** 24389d61fc0SDaniel Vetter * drm_gem_handle_delete - deletes the given file-private handle 24489d61fc0SDaniel Vetter * @filp: drm file-private structure to use for the handle look up 24589d61fc0SDaniel Vetter * @handle: userspace handle to delete 24689d61fc0SDaniel Vetter * 247df2e0900SDaniel Vetter * Removes the GEM handle from the @filp lookup table which has been added with 248df2e0900SDaniel Vetter * drm_gem_handle_create(). If this is the last handle also cleans up linked 249df2e0900SDaniel Vetter * resources like GEM names. 250673a394bSEric Anholt */ 251ff72145bSDave Airlie int 252a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle) 253673a394bSEric Anholt { 254673a394bSEric Anholt struct drm_device *dev; 255673a394bSEric Anholt struct drm_gem_object *obj; 256673a394bSEric Anholt 257673a394bSEric Anholt /* This is gross. The idr system doesn't let us try a delete and 258673a394bSEric Anholt * return an error code. It just spews if you fail at deleting. 259673a394bSEric Anholt * So, we have to grab a lock around finding the object and then 260673a394bSEric Anholt * doing the delete on it and dropping the refcount, or the user 261673a394bSEric Anholt * could race us to double-decrement the refcount and cause a 262673a394bSEric Anholt * use-after-free later. Given the frequency of our handle lookups, 263673a394bSEric Anholt * we may want to use ida for number allocation and a hash table 264673a394bSEric Anholt * for the pointers, anyway. 265673a394bSEric Anholt */ 266673a394bSEric Anholt spin_lock(&filp->table_lock); 267673a394bSEric Anholt 268673a394bSEric Anholt /* Check if we currently have a reference on the object */ 269673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 270673a394bSEric Anholt if (obj == NULL) { 271673a394bSEric Anholt spin_unlock(&filp->table_lock); 272673a394bSEric Anholt return -EINVAL; 273673a394bSEric Anholt } 274673a394bSEric Anholt dev = obj->dev; 275673a394bSEric Anholt 276673a394bSEric Anholt /* Release reference and decrement refcount. */ 277673a394bSEric Anholt idr_remove(&filp->object_idr, handle); 278673a394bSEric Anholt spin_unlock(&filp->table_lock); 279673a394bSEric Anholt 2809c784855SThierry Reding if (drm_core_check_feature(dev, DRIVER_PRIME)) 2810ff926c7SDave Airlie drm_gem_remove_prime_handles(obj, filp); 282ca481c9bSDavid Herrmann drm_vma_node_revoke(&obj->vma_node, filp->filp); 2833248877eSDave Airlie 284304eda32SBen Skeggs if (dev->driver->gem_close_object) 285304eda32SBen Skeggs dev->driver->gem_close_object(obj, filp); 286bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 287673a394bSEric Anholt 288673a394bSEric Anholt return 0; 289673a394bSEric Anholt } 290ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete); 291673a394bSEric Anholt 292673a394bSEric Anholt /** 29343387b37SDaniel Vetter * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers 29489d61fc0SDaniel Vetter * @file: drm file-private structure to remove the dumb handle from 29589d61fc0SDaniel Vetter * @dev: corresponding drm_device 29689d61fc0SDaniel Vetter * @handle: the dumb handle to remove 29743387b37SDaniel Vetter * 29843387b37SDaniel Vetter * This implements the ->dumb_destroy kms driver callback for drivers which use 29943387b37SDaniel Vetter * gem to manage their backing storage. 30043387b37SDaniel Vetter */ 30143387b37SDaniel Vetter int drm_gem_dumb_destroy(struct drm_file *file, 30243387b37SDaniel Vetter struct drm_device *dev, 30343387b37SDaniel Vetter uint32_t handle) 30443387b37SDaniel Vetter { 30543387b37SDaniel Vetter return drm_gem_handle_delete(file, handle); 30643387b37SDaniel Vetter } 30743387b37SDaniel Vetter EXPORT_SYMBOL(drm_gem_dumb_destroy); 30843387b37SDaniel Vetter 30943387b37SDaniel Vetter /** 31020228c44SDaniel Vetter * drm_gem_handle_create_tail - internal functions to create a handle 31189d61fc0SDaniel Vetter * @file_priv: drm file-private structure to register the handle for 31289d61fc0SDaniel Vetter * @obj: object to register 3138bf8180fSThierry Reding * @handlep: pointer to return the created handle to the caller 31420228c44SDaniel Vetter * 31520228c44SDaniel Vetter * This expects the dev->object_name_lock to be held already and will drop it 31620228c44SDaniel Vetter * before returning. Used to avoid races in establishing new handles when 31720228c44SDaniel Vetter * importing an object from either an flink name or a dma-buf. 318df2e0900SDaniel Vetter * 319df2e0900SDaniel Vetter * Handles must be release again through drm_gem_handle_delete(). This is done 320df2e0900SDaniel Vetter * when userspace closes @file_priv for all attached handles, or through the 321df2e0900SDaniel Vetter * GEM_CLOSE ioctl for individual handles. 322673a394bSEric Anholt */ 323673a394bSEric Anholt int 32420228c44SDaniel Vetter drm_gem_handle_create_tail(struct drm_file *file_priv, 325673a394bSEric Anholt struct drm_gem_object *obj, 326a1a2d1d3SPekka Paalanen u32 *handlep) 327673a394bSEric Anholt { 328304eda32SBen Skeggs struct drm_device *dev = obj->dev; 329673a394bSEric Anholt int ret; 330673a394bSEric Anholt 33120228c44SDaniel Vetter WARN_ON(!mutex_is_locked(&dev->object_name_lock)); 33220228c44SDaniel Vetter 333673a394bSEric Anholt /* 3342e928815STejun Heo * Get the user-visible handle using idr. Preload and perform 3352e928815STejun Heo * allocation under our spinlock. 336673a394bSEric Anholt */ 3372e928815STejun Heo idr_preload(GFP_KERNEL); 338673a394bSEric Anholt spin_lock(&file_priv->table_lock); 3392e928815STejun Heo 3402e928815STejun Heo ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT); 341a8e11d1cSDaniel Vetter drm_gem_object_reference(obj); 342a8e11d1cSDaniel Vetter obj->handle_count++; 343673a394bSEric Anholt spin_unlock(&file_priv->table_lock); 3442e928815STejun Heo idr_preload_end(); 345cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock); 346*6984128dSChris Wilson if (ret < 0) 347*6984128dSChris Wilson goto err_unref; 348*6984128dSChris Wilson 3492e928815STejun Heo *handlep = ret; 350673a394bSEric Anholt 351ca481c9bSDavid Herrmann ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp); 352*6984128dSChris Wilson if (ret) 353*6984128dSChris Wilson goto err_remove; 354304eda32SBen Skeggs 355304eda32SBen Skeggs if (dev->driver->gem_open_object) { 356304eda32SBen Skeggs ret = dev->driver->gem_open_object(obj, file_priv); 357*6984128dSChris Wilson if (ret) 358*6984128dSChris Wilson goto err_revoke; 359304eda32SBen Skeggs } 360304eda32SBen Skeggs 361673a394bSEric Anholt return 0; 362*6984128dSChris Wilson 363*6984128dSChris Wilson err_revoke: 364*6984128dSChris Wilson drm_vma_node_revoke(&obj->vma_node, file_priv->filp); 365*6984128dSChris Wilson err_remove: 366*6984128dSChris Wilson spin_lock(&file_priv->table_lock); 367*6984128dSChris Wilson idr_remove(&file_priv->object_idr, *handlep); 368*6984128dSChris Wilson spin_unlock(&file_priv->table_lock); 369*6984128dSChris Wilson err_unref: 370*6984128dSChris Wilson drm_gem_object_handle_unreference_unlocked(obj); 371*6984128dSChris Wilson return ret; 372673a394bSEric Anholt } 37320228c44SDaniel Vetter 37420228c44SDaniel Vetter /** 3758bf8180fSThierry Reding * drm_gem_handle_create - create a gem handle for an object 37689d61fc0SDaniel Vetter * @file_priv: drm file-private structure to register the handle for 37789d61fc0SDaniel Vetter * @obj: object to register 37889d61fc0SDaniel Vetter * @handlep: pionter to return the created handle to the caller 37989d61fc0SDaniel Vetter * 38020228c44SDaniel Vetter * Create a handle for this object. This adds a handle reference 38120228c44SDaniel Vetter * to the object, which includes a regular reference count. Callers 38220228c44SDaniel Vetter * will likely want to dereference the object afterwards. 38320228c44SDaniel Vetter */ 3848bf8180fSThierry Reding int drm_gem_handle_create(struct drm_file *file_priv, 38520228c44SDaniel Vetter struct drm_gem_object *obj, 38620228c44SDaniel Vetter u32 *handlep) 38720228c44SDaniel Vetter { 38820228c44SDaniel Vetter mutex_lock(&obj->dev->object_name_lock); 38920228c44SDaniel Vetter 39020228c44SDaniel Vetter return drm_gem_handle_create_tail(file_priv, obj, handlep); 39120228c44SDaniel Vetter } 392673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create); 393673a394bSEric Anholt 39475ef8b3bSRob Clark 39575ef8b3bSRob Clark /** 39675ef8b3bSRob Clark * drm_gem_free_mmap_offset - release a fake mmap offset for an object 39775ef8b3bSRob Clark * @obj: obj in question 39875ef8b3bSRob Clark * 39975ef8b3bSRob Clark * This routine frees fake offsets allocated by drm_gem_create_mmap_offset(). 40075ef8b3bSRob Clark */ 40175ef8b3bSRob Clark void 40275ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj) 40375ef8b3bSRob Clark { 40475ef8b3bSRob Clark struct drm_device *dev = obj->dev; 40575ef8b3bSRob Clark 406b04a5906SDaniel Vetter drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node); 40775ef8b3bSRob Clark } 40875ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset); 40975ef8b3bSRob Clark 41075ef8b3bSRob Clark /** 411367bbd49SRob Clark * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object 412367bbd49SRob Clark * @obj: obj in question 413367bbd49SRob Clark * @size: the virtual size 414367bbd49SRob Clark * 415367bbd49SRob Clark * GEM memory mapping works by handing back to userspace a fake mmap offset 416367bbd49SRob Clark * it can use in a subsequent mmap(2) call. The DRM core code then looks 417367bbd49SRob Clark * up the object based on the offset and sets up the various memory mapping 418367bbd49SRob Clark * structures. 419367bbd49SRob Clark * 420367bbd49SRob Clark * This routine allocates and attaches a fake offset for @obj, in cases where 421367bbd49SRob Clark * the virtual size differs from the physical size (ie. obj->size). Otherwise 422367bbd49SRob Clark * just use drm_gem_create_mmap_offset(). 423367bbd49SRob Clark */ 424367bbd49SRob Clark int 425367bbd49SRob Clark drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size) 426367bbd49SRob Clark { 427367bbd49SRob Clark struct drm_device *dev = obj->dev; 428367bbd49SRob Clark 429b04a5906SDaniel Vetter return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node, 430367bbd49SRob Clark size / PAGE_SIZE); 431367bbd49SRob Clark } 432367bbd49SRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset_size); 433367bbd49SRob Clark 434367bbd49SRob Clark /** 43575ef8b3bSRob Clark * drm_gem_create_mmap_offset - create a fake mmap offset for an object 43675ef8b3bSRob Clark * @obj: obj in question 43775ef8b3bSRob Clark * 43875ef8b3bSRob Clark * GEM memory mapping works by handing back to userspace a fake mmap offset 43975ef8b3bSRob Clark * it can use in a subsequent mmap(2) call. The DRM core code then looks 44075ef8b3bSRob Clark * up the object based on the offset and sets up the various memory mapping 44175ef8b3bSRob Clark * structures. 44275ef8b3bSRob Clark * 44375ef8b3bSRob Clark * This routine allocates and attaches a fake offset for @obj. 44475ef8b3bSRob Clark */ 445367bbd49SRob Clark int drm_gem_create_mmap_offset(struct drm_gem_object *obj) 44675ef8b3bSRob Clark { 447367bbd49SRob Clark return drm_gem_create_mmap_offset_size(obj, obj->size); 44875ef8b3bSRob Clark } 44975ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset); 45075ef8b3bSRob Clark 451bcc5c9d5SRob Clark /** 452bcc5c9d5SRob Clark * drm_gem_get_pages - helper to allocate backing pages for a GEM object 453bcc5c9d5SRob Clark * from shmem 454bcc5c9d5SRob Clark * @obj: obj in question 4550cdbe8acSDavid Herrmann * 4560cdbe8acSDavid Herrmann * This reads the page-array of the shmem-backing storage of the given gem 4570cdbe8acSDavid Herrmann * object. An array of pages is returned. If a page is not allocated or 4580cdbe8acSDavid Herrmann * swapped-out, this will allocate/swap-in the required pages. Note that the 4590cdbe8acSDavid Herrmann * whole object is covered by the page-array and pinned in memory. 4600cdbe8acSDavid Herrmann * 4610cdbe8acSDavid Herrmann * Use drm_gem_put_pages() to release the array and unpin all pages. 4620cdbe8acSDavid Herrmann * 4630cdbe8acSDavid Herrmann * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()). 4640cdbe8acSDavid Herrmann * If you require other GFP-masks, you have to do those allocations yourself. 4650cdbe8acSDavid Herrmann * 4660cdbe8acSDavid Herrmann * Note that you are not allowed to change gfp-zones during runtime. That is, 4670cdbe8acSDavid Herrmann * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as 4680cdbe8acSDavid Herrmann * set during initialization. If you have special zone constraints, set them 4690cdbe8acSDavid Herrmann * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care 4700cdbe8acSDavid Herrmann * to keep pages in the required zone during swap-in. 471bcc5c9d5SRob Clark */ 4720cdbe8acSDavid Herrmann struct page **drm_gem_get_pages(struct drm_gem_object *obj) 473bcc5c9d5SRob Clark { 474bcc5c9d5SRob Clark struct address_space *mapping; 475bcc5c9d5SRob Clark struct page *p, **pages; 476bcc5c9d5SRob Clark int i, npages; 477bcc5c9d5SRob Clark 478bcc5c9d5SRob Clark /* This is the shared memory object that backs the GEM resource */ 4790cdbe8acSDavid Herrmann mapping = file_inode(obj->filp)->i_mapping; 480bcc5c9d5SRob Clark 481bcc5c9d5SRob Clark /* We already BUG_ON() for non-page-aligned sizes in 482bcc5c9d5SRob Clark * drm_gem_object_init(), so we should never hit this unless 483bcc5c9d5SRob Clark * driver author is doing something really wrong: 484bcc5c9d5SRob Clark */ 485bcc5c9d5SRob Clark WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 486bcc5c9d5SRob Clark 487bcc5c9d5SRob Clark npages = obj->size >> PAGE_SHIFT; 488bcc5c9d5SRob Clark 489bcc5c9d5SRob Clark pages = drm_malloc_ab(npages, sizeof(struct page *)); 490bcc5c9d5SRob Clark if (pages == NULL) 491bcc5c9d5SRob Clark return ERR_PTR(-ENOMEM); 492bcc5c9d5SRob Clark 493bcc5c9d5SRob Clark for (i = 0; i < npages; i++) { 4940cdbe8acSDavid Herrmann p = shmem_read_mapping_page(mapping, i); 495bcc5c9d5SRob Clark if (IS_ERR(p)) 496bcc5c9d5SRob Clark goto fail; 497bcc5c9d5SRob Clark pages[i] = p; 498bcc5c9d5SRob Clark 4992123000bSDavid Herrmann /* Make sure shmem keeps __GFP_DMA32 allocated pages in the 5002123000bSDavid Herrmann * correct region during swapin. Note that this requires 5012123000bSDavid Herrmann * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping) 5022123000bSDavid Herrmann * so shmem can relocate pages during swapin if required. 503bcc5c9d5SRob Clark */ 504c62d2555SMichal Hocko BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) && 505bcc5c9d5SRob Clark (page_to_pfn(p) >= 0x00100000UL)); 506bcc5c9d5SRob Clark } 507bcc5c9d5SRob Clark 508bcc5c9d5SRob Clark return pages; 509bcc5c9d5SRob Clark 510bcc5c9d5SRob Clark fail: 511bcc5c9d5SRob Clark while (i--) 512bcc5c9d5SRob Clark page_cache_release(pages[i]); 513bcc5c9d5SRob Clark 514bcc5c9d5SRob Clark drm_free_large(pages); 515bcc5c9d5SRob Clark return ERR_CAST(p); 516bcc5c9d5SRob Clark } 517bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_get_pages); 518bcc5c9d5SRob Clark 519bcc5c9d5SRob Clark /** 520bcc5c9d5SRob Clark * drm_gem_put_pages - helper to free backing pages for a GEM object 521bcc5c9d5SRob Clark * @obj: obj in question 522bcc5c9d5SRob Clark * @pages: pages to free 523bcc5c9d5SRob Clark * @dirty: if true, pages will be marked as dirty 524bcc5c9d5SRob Clark * @accessed: if true, the pages will be marked as accessed 525bcc5c9d5SRob Clark */ 526bcc5c9d5SRob Clark void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, 527bcc5c9d5SRob Clark bool dirty, bool accessed) 528bcc5c9d5SRob Clark { 529bcc5c9d5SRob Clark int i, npages; 530bcc5c9d5SRob Clark 531bcc5c9d5SRob Clark /* We already BUG_ON() for non-page-aligned sizes in 532bcc5c9d5SRob Clark * drm_gem_object_init(), so we should never hit this unless 533bcc5c9d5SRob Clark * driver author is doing something really wrong: 534bcc5c9d5SRob Clark */ 535bcc5c9d5SRob Clark WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 536bcc5c9d5SRob Clark 537bcc5c9d5SRob Clark npages = obj->size >> PAGE_SHIFT; 538bcc5c9d5SRob Clark 539bcc5c9d5SRob Clark for (i = 0; i < npages; i++) { 540bcc5c9d5SRob Clark if (dirty) 541bcc5c9d5SRob Clark set_page_dirty(pages[i]); 542bcc5c9d5SRob Clark 543bcc5c9d5SRob Clark if (accessed) 544bcc5c9d5SRob Clark mark_page_accessed(pages[i]); 545bcc5c9d5SRob Clark 546bcc5c9d5SRob Clark /* Undo the reference we took when populating the table */ 547bcc5c9d5SRob Clark page_cache_release(pages[i]); 548bcc5c9d5SRob Clark } 549bcc5c9d5SRob Clark 550bcc5c9d5SRob Clark drm_free_large(pages); 551bcc5c9d5SRob Clark } 552bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_put_pages); 553bcc5c9d5SRob Clark 554df2e0900SDaniel Vetter /** 555df2e0900SDaniel Vetter * drm_gem_object_lookup - look up a GEM object from it's handle 556df2e0900SDaniel Vetter * @dev: DRM device 557df2e0900SDaniel Vetter * @filp: DRM file private date 558df2e0900SDaniel Vetter * @handle: userspace handle 559df2e0900SDaniel Vetter * 560df2e0900SDaniel Vetter * Returns: 561df2e0900SDaniel Vetter * 562df2e0900SDaniel Vetter * A reference to the object named by the handle if such exists on @filp, NULL 563df2e0900SDaniel Vetter * otherwise. 564df2e0900SDaniel Vetter */ 565673a394bSEric Anholt struct drm_gem_object * 566673a394bSEric Anholt drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, 567a1a2d1d3SPekka Paalanen u32 handle) 568673a394bSEric Anholt { 569673a394bSEric Anholt struct drm_gem_object *obj; 570673a394bSEric Anholt 571673a394bSEric Anholt spin_lock(&filp->table_lock); 572673a394bSEric Anholt 573673a394bSEric Anholt /* Check if we currently have a reference on the object */ 574673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 575673a394bSEric Anholt if (obj == NULL) { 576673a394bSEric Anholt spin_unlock(&filp->table_lock); 577673a394bSEric Anholt return NULL; 578673a394bSEric Anholt } 579673a394bSEric Anholt 580673a394bSEric Anholt drm_gem_object_reference(obj); 581673a394bSEric Anholt 582673a394bSEric Anholt spin_unlock(&filp->table_lock); 583673a394bSEric Anholt 584673a394bSEric Anholt return obj; 585673a394bSEric Anholt } 586673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup); 587673a394bSEric Anholt 588673a394bSEric Anholt /** 58989d61fc0SDaniel Vetter * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl 59089d61fc0SDaniel Vetter * @dev: drm_device 59189d61fc0SDaniel Vetter * @data: ioctl data 59289d61fc0SDaniel Vetter * @file_priv: drm file-private structure 59389d61fc0SDaniel Vetter * 594673a394bSEric Anholt * Releases the handle to an mm object. 595673a394bSEric Anholt */ 596673a394bSEric Anholt int 597673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data, 598673a394bSEric Anholt struct drm_file *file_priv) 599673a394bSEric Anholt { 600673a394bSEric Anholt struct drm_gem_close *args = data; 601673a394bSEric Anholt int ret; 602673a394bSEric Anholt 6031bcecfacSAndrzej Hajda if (!drm_core_check_feature(dev, DRIVER_GEM)) 604673a394bSEric Anholt return -ENODEV; 605673a394bSEric Anholt 606673a394bSEric Anholt ret = drm_gem_handle_delete(file_priv, args->handle); 607673a394bSEric Anholt 608673a394bSEric Anholt return ret; 609673a394bSEric Anholt } 610673a394bSEric Anholt 611673a394bSEric Anholt /** 61289d61fc0SDaniel Vetter * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl 61389d61fc0SDaniel Vetter * @dev: drm_device 61489d61fc0SDaniel Vetter * @data: ioctl data 61589d61fc0SDaniel Vetter * @file_priv: drm file-private structure 61689d61fc0SDaniel Vetter * 617673a394bSEric Anholt * Create a global name for an object, returning the name. 618673a394bSEric Anholt * 619673a394bSEric Anholt * Note that the name does not hold a reference; when the object 620673a394bSEric Anholt * is freed, the name goes away. 621673a394bSEric Anholt */ 622673a394bSEric Anholt int 623673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data, 624673a394bSEric Anholt struct drm_file *file_priv) 625673a394bSEric Anholt { 626673a394bSEric Anholt struct drm_gem_flink *args = data; 627673a394bSEric Anholt struct drm_gem_object *obj; 628673a394bSEric Anholt int ret; 629673a394bSEric Anholt 6301bcecfacSAndrzej Hajda if (!drm_core_check_feature(dev, DRIVER_GEM)) 631673a394bSEric Anholt return -ENODEV; 632673a394bSEric Anholt 633673a394bSEric Anholt obj = drm_gem_object_lookup(dev, file_priv, args->handle); 634673a394bSEric Anholt if (obj == NULL) 635bf79cb91SChris Wilson return -ENOENT; 636673a394bSEric Anholt 637cd4f013fSDaniel Vetter mutex_lock(&dev->object_name_lock); 6382e928815STejun Heo idr_preload(GFP_KERNEL); 639a8e11d1cSDaniel Vetter /* prevent races with concurrent gem_close. */ 640a8e11d1cSDaniel Vetter if (obj->handle_count == 0) { 641a8e11d1cSDaniel Vetter ret = -ENOENT; 642a8e11d1cSDaniel Vetter goto err; 643a8e11d1cSDaniel Vetter } 644a8e11d1cSDaniel Vetter 6458d59bae5SChris Wilson if (!obj->name) { 6462e928815STejun Heo ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT); 6472e928815STejun Heo if (ret < 0) 6483e49c4f4SChris Wilson goto err; 6492e07fb22SYoungJun Cho 6502e07fb22SYoungJun Cho obj->name = ret; 6518d59bae5SChris Wilson } 6523e49c4f4SChris Wilson 6532e07fb22SYoungJun Cho args->name = (uint64_t) obj->name; 6542e07fb22SYoungJun Cho ret = 0; 6552e07fb22SYoungJun Cho 6563e49c4f4SChris Wilson err: 6572e07fb22SYoungJun Cho idr_preload_end(); 658cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock); 659bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 6603e49c4f4SChris Wilson return ret; 661673a394bSEric Anholt } 662673a394bSEric Anholt 663673a394bSEric Anholt /** 66489d61fc0SDaniel Vetter * drm_gem_open - implementation of the GEM_OPEN ioctl 66589d61fc0SDaniel Vetter * @dev: drm_device 66689d61fc0SDaniel Vetter * @data: ioctl data 66789d61fc0SDaniel Vetter * @file_priv: drm file-private structure 66889d61fc0SDaniel Vetter * 669673a394bSEric Anholt * Open an object using the global name, returning a handle and the size. 670673a394bSEric Anholt * 671673a394bSEric Anholt * This handle (of course) holds a reference to the object, so the object 672673a394bSEric Anholt * will not go away until the handle is deleted. 673673a394bSEric Anholt */ 674673a394bSEric Anholt int 675673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data, 676673a394bSEric Anholt struct drm_file *file_priv) 677673a394bSEric Anholt { 678673a394bSEric Anholt struct drm_gem_open *args = data; 679673a394bSEric Anholt struct drm_gem_object *obj; 680673a394bSEric Anholt int ret; 681a1a2d1d3SPekka Paalanen u32 handle; 682673a394bSEric Anholt 6831bcecfacSAndrzej Hajda if (!drm_core_check_feature(dev, DRIVER_GEM)) 684673a394bSEric Anholt return -ENODEV; 685673a394bSEric Anholt 686cd4f013fSDaniel Vetter mutex_lock(&dev->object_name_lock); 687673a394bSEric Anholt obj = idr_find(&dev->object_name_idr, (int) args->name); 68820228c44SDaniel Vetter if (obj) { 689673a394bSEric Anholt drm_gem_object_reference(obj); 69020228c44SDaniel Vetter } else { 691cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock); 692673a394bSEric Anholt return -ENOENT; 69320228c44SDaniel Vetter } 694673a394bSEric Anholt 69520228c44SDaniel Vetter /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */ 69620228c44SDaniel Vetter ret = drm_gem_handle_create_tail(file_priv, obj, &handle); 697bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 698673a394bSEric Anholt if (ret) 699673a394bSEric Anholt return ret; 700673a394bSEric Anholt 701673a394bSEric Anholt args->handle = handle; 702673a394bSEric Anholt args->size = obj->size; 703673a394bSEric Anholt 704673a394bSEric Anholt return 0; 705673a394bSEric Anholt } 706673a394bSEric Anholt 707673a394bSEric Anholt /** 70889d61fc0SDaniel Vetter * gem_gem_open - initalizes GEM file-private structures at devnode open time 70989d61fc0SDaniel Vetter * @dev: drm_device which is being opened by userspace 71089d61fc0SDaniel Vetter * @file_private: drm file-private structure to set up 71189d61fc0SDaniel Vetter * 712673a394bSEric Anholt * Called at device open time, sets up the structure for handling refcounting 713673a394bSEric Anholt * of mm objects. 714673a394bSEric Anholt */ 715673a394bSEric Anholt void 716673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 717673a394bSEric Anholt { 718673a394bSEric Anholt idr_init(&file_private->object_idr); 719673a394bSEric Anholt spin_lock_init(&file_private->table_lock); 720673a394bSEric Anholt } 721673a394bSEric Anholt 72289d61fc0SDaniel Vetter /* 723673a394bSEric Anholt * Called at device close to release the file's 724673a394bSEric Anholt * handle references on objects. 725673a394bSEric Anholt */ 726673a394bSEric Anholt static int 727673a394bSEric Anholt drm_gem_object_release_handle(int id, void *ptr, void *data) 728673a394bSEric Anholt { 729304eda32SBen Skeggs struct drm_file *file_priv = data; 730673a394bSEric Anholt struct drm_gem_object *obj = ptr; 731304eda32SBen Skeggs struct drm_device *dev = obj->dev; 732304eda32SBen Skeggs 7339c784855SThierry Reding if (drm_core_check_feature(dev, DRIVER_PRIME)) 7340ff926c7SDave Airlie drm_gem_remove_prime_handles(obj, file_priv); 735ca481c9bSDavid Herrmann drm_vma_node_revoke(&obj->vma_node, file_priv->filp); 7363248877eSDave Airlie 737304eda32SBen Skeggs if (dev->driver->gem_close_object) 738304eda32SBen Skeggs dev->driver->gem_close_object(obj, file_priv); 739673a394bSEric Anholt 740bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 741673a394bSEric Anholt 742673a394bSEric Anholt return 0; 743673a394bSEric Anholt } 744673a394bSEric Anholt 745673a394bSEric Anholt /** 74689d61fc0SDaniel Vetter * drm_gem_release - release file-private GEM resources 74789d61fc0SDaniel Vetter * @dev: drm_device which is being closed by userspace 74889d61fc0SDaniel Vetter * @file_private: drm file-private structure to clean up 74989d61fc0SDaniel Vetter * 750673a394bSEric Anholt * Called at close time when the filp is going away. 751673a394bSEric Anholt * 752673a394bSEric Anholt * Releases any remaining references on objects by this filp. 753673a394bSEric Anholt */ 754673a394bSEric Anholt void 755673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 756673a394bSEric Anholt { 757673a394bSEric Anholt idr_for_each(&file_private->object_idr, 758304eda32SBen Skeggs &drm_gem_object_release_handle, file_private); 759673a394bSEric Anholt idr_destroy(&file_private->object_idr); 760673a394bSEric Anholt } 761673a394bSEric Anholt 762fd632aa3SDaniel Vetter void 763fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj) 764c3ae90c0SLuca Barbieri { 765319c933cSDaniel Vetter WARN_ON(obj->dma_buf); 766319c933cSDaniel Vetter 76762cb7011SAlan Cox if (obj->filp) 768c3ae90c0SLuca Barbieri fput(obj->filp); 76977472347SDavid Herrmann 77077472347SDavid Herrmann drm_gem_free_mmap_offset(obj); 771c3ae90c0SLuca Barbieri } 772fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release); 773c3ae90c0SLuca Barbieri 774673a394bSEric Anholt /** 77589d61fc0SDaniel Vetter * drm_gem_object_free - free a GEM object 77689d61fc0SDaniel Vetter * @kref: kref of the object to free 77789d61fc0SDaniel Vetter * 778673a394bSEric Anholt * Called after the last reference to the object has been lost. 779c3ae90c0SLuca Barbieri * Must be called holding struct_ mutex 780673a394bSEric Anholt * 781673a394bSEric Anholt * Frees the object 782673a394bSEric Anholt */ 783673a394bSEric Anholt void 784673a394bSEric Anholt drm_gem_object_free(struct kref *kref) 785673a394bSEric Anholt { 7866ff774bdSDaniel Vetter struct drm_gem_object *obj = 7876ff774bdSDaniel Vetter container_of(kref, struct drm_gem_object, refcount); 788673a394bSEric Anholt struct drm_device *dev = obj->dev; 789673a394bSEric Anholt 7908d77a940SDaniel Vetter WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 791673a394bSEric Anholt 792673a394bSEric Anholt if (dev->driver->gem_free_object != NULL) 793673a394bSEric Anholt dev->driver->gem_free_object(obj); 794673a394bSEric Anholt } 795673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free); 796673a394bSEric Anholt 797df2e0900SDaniel Vetter /** 798df2e0900SDaniel Vetter * drm_gem_vm_open - vma->ops->open implementation for GEM 799df2e0900SDaniel Vetter * @vma: VM area structure 800df2e0900SDaniel Vetter * 801df2e0900SDaniel Vetter * This function implements the #vm_operations_struct open() callback for GEM 802df2e0900SDaniel Vetter * drivers. This must be used together with drm_gem_vm_close(). 803df2e0900SDaniel Vetter */ 804ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma) 805ab00b3e5SJesse Barnes { 806ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 807ab00b3e5SJesse Barnes 808ab00b3e5SJesse Barnes drm_gem_object_reference(obj); 809ab00b3e5SJesse Barnes } 810ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open); 811ab00b3e5SJesse Barnes 812df2e0900SDaniel Vetter /** 813df2e0900SDaniel Vetter * drm_gem_vm_close - vma->ops->close implementation for GEM 814df2e0900SDaniel Vetter * @vma: VM area structure 815df2e0900SDaniel Vetter * 816df2e0900SDaniel Vetter * This function implements the #vm_operations_struct close() callback for GEM 817df2e0900SDaniel Vetter * drivers. This must be used together with drm_gem_vm_open(). 818df2e0900SDaniel Vetter */ 819ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma) 820ab00b3e5SJesse Barnes { 821ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 822ab00b3e5SJesse Barnes 823131e663bSDaniel Vetter drm_gem_object_unreference_unlocked(obj); 824ab00b3e5SJesse Barnes } 825ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close); 826ab00b3e5SJesse Barnes 8271c5aafa6SLaurent Pinchart /** 8281c5aafa6SLaurent Pinchart * drm_gem_mmap_obj - memory map a GEM object 8291c5aafa6SLaurent Pinchart * @obj: the GEM object to map 8301c5aafa6SLaurent Pinchart * @obj_size: the object size to be mapped, in bytes 8311c5aafa6SLaurent Pinchart * @vma: VMA for the area to be mapped 8321c5aafa6SLaurent Pinchart * 8331c5aafa6SLaurent Pinchart * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops 8341c5aafa6SLaurent Pinchart * provided by the driver. Depending on their requirements, drivers can either 8351c5aafa6SLaurent Pinchart * provide a fault handler in their gem_vm_ops (in which case any accesses to 8361c5aafa6SLaurent Pinchart * the object will be trapped, to perform migration, GTT binding, surface 8371c5aafa6SLaurent Pinchart * register allocation, or performance monitoring), or mmap the buffer memory 8381c5aafa6SLaurent Pinchart * synchronously after calling drm_gem_mmap_obj. 8391c5aafa6SLaurent Pinchart * 8401c5aafa6SLaurent Pinchart * This function is mainly intended to implement the DMABUF mmap operation, when 8411c5aafa6SLaurent Pinchart * the GEM object is not looked up based on its fake offset. To implement the 8421c5aafa6SLaurent Pinchart * DRM mmap operation, drivers should use the drm_gem_mmap() function. 8431c5aafa6SLaurent Pinchart * 844ca481c9bSDavid Herrmann * drm_gem_mmap_obj() assumes the user is granted access to the buffer while 845ca481c9bSDavid Herrmann * drm_gem_mmap() prevents unprivileged users from mapping random objects. So 846ca481c9bSDavid Herrmann * callers must verify access restrictions before calling this helper. 847ca481c9bSDavid Herrmann * 8481c5aafa6SLaurent Pinchart * Return 0 or success or -EINVAL if the object size is smaller than the VMA 8491c5aafa6SLaurent Pinchart * size, or if no gem_vm_ops are provided. 8501c5aafa6SLaurent Pinchart */ 8511c5aafa6SLaurent Pinchart int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size, 8521c5aafa6SLaurent Pinchart struct vm_area_struct *vma) 8531c5aafa6SLaurent Pinchart { 8541c5aafa6SLaurent Pinchart struct drm_device *dev = obj->dev; 8551c5aafa6SLaurent Pinchart 8561c5aafa6SLaurent Pinchart /* Check for valid size. */ 8571c5aafa6SLaurent Pinchart if (obj_size < vma->vm_end - vma->vm_start) 8581c5aafa6SLaurent Pinchart return -EINVAL; 8591c5aafa6SLaurent Pinchart 8601c5aafa6SLaurent Pinchart if (!dev->driver->gem_vm_ops) 8611c5aafa6SLaurent Pinchart return -EINVAL; 8621c5aafa6SLaurent Pinchart 8631c5aafa6SLaurent Pinchart vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 8641c5aafa6SLaurent Pinchart vma->vm_ops = dev->driver->gem_vm_ops; 8651c5aafa6SLaurent Pinchart vma->vm_private_data = obj; 8661c5aafa6SLaurent Pinchart vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 8671c5aafa6SLaurent Pinchart 8681c5aafa6SLaurent Pinchart /* Take a ref for this mapping of the object, so that the fault 8691c5aafa6SLaurent Pinchart * handler can dereference the mmap offset's pointer to the object. 8701c5aafa6SLaurent Pinchart * This reference is cleaned up by the corresponding vm_close 8711c5aafa6SLaurent Pinchart * (which should happen whether the vma was created by this call, or 8721c5aafa6SLaurent Pinchart * by a vm_open due to mremap or partial unmap or whatever). 8731c5aafa6SLaurent Pinchart */ 8741c5aafa6SLaurent Pinchart drm_gem_object_reference(obj); 8751c5aafa6SLaurent Pinchart 8761c5aafa6SLaurent Pinchart return 0; 8771c5aafa6SLaurent Pinchart } 8781c5aafa6SLaurent Pinchart EXPORT_SYMBOL(drm_gem_mmap_obj); 879ab00b3e5SJesse Barnes 880a2c0a97bSJesse Barnes /** 881a2c0a97bSJesse Barnes * drm_gem_mmap - memory map routine for GEM objects 882a2c0a97bSJesse Barnes * @filp: DRM file pointer 883a2c0a97bSJesse Barnes * @vma: VMA for the area to be mapped 884a2c0a97bSJesse Barnes * 885a2c0a97bSJesse Barnes * If a driver supports GEM object mapping, mmap calls on the DRM file 886a2c0a97bSJesse Barnes * descriptor will end up here. 887a2c0a97bSJesse Barnes * 8881c5aafa6SLaurent Pinchart * Look up the GEM object based on the offset passed in (vma->vm_pgoff will 889a2c0a97bSJesse Barnes * contain the fake offset we created when the GTT map ioctl was called on 8901c5aafa6SLaurent Pinchart * the object) and map it with a call to drm_gem_mmap_obj(). 891ca481c9bSDavid Herrmann * 892ca481c9bSDavid Herrmann * If the caller is not granted access to the buffer object, the mmap will fail 893ca481c9bSDavid Herrmann * with EACCES. Please see the vma manager for more information. 894a2c0a97bSJesse Barnes */ 895a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 896a2c0a97bSJesse Barnes { 897a2c0a97bSJesse Barnes struct drm_file *priv = filp->private_data; 898a2c0a97bSJesse Barnes struct drm_device *dev = priv->minor->dev; 8992225cfe4SDaniel Vetter struct drm_gem_object *obj = NULL; 9000de23977SDavid Herrmann struct drm_vma_offset_node *node; 901a8469aa8SDavid Herrmann int ret; 902a2c0a97bSJesse Barnes 9032c07a21dSDave Airlie if (drm_device_is_unplugged(dev)) 9042c07a21dSDave Airlie return -ENODEV; 9052c07a21dSDave Airlie 9062225cfe4SDaniel Vetter drm_vma_offset_lock_lookup(dev->vma_offset_manager); 9072225cfe4SDaniel Vetter node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager, 908b04a5906SDaniel Vetter vma->vm_pgoff, 9090de23977SDavid Herrmann vma_pages(vma)); 9102225cfe4SDaniel Vetter if (likely(node)) { 9112225cfe4SDaniel Vetter obj = container_of(node, struct drm_gem_object, vma_node); 9122225cfe4SDaniel Vetter /* 9132225cfe4SDaniel Vetter * When the object is being freed, after it hits 0-refcnt it 9142225cfe4SDaniel Vetter * proceeds to tear down the object. In the process it will 9152225cfe4SDaniel Vetter * attempt to remove the VMA offset and so acquire this 9162225cfe4SDaniel Vetter * mgr->vm_lock. Therefore if we find an object with a 0-refcnt 9172225cfe4SDaniel Vetter * that matches our range, we know it is in the process of being 9182225cfe4SDaniel Vetter * destroyed and will be freed as soon as we release the lock - 9192225cfe4SDaniel Vetter * so we have to check for the 0-refcnted object and treat it as 9202225cfe4SDaniel Vetter * invalid. 9212225cfe4SDaniel Vetter */ 9222225cfe4SDaniel Vetter if (!kref_get_unless_zero(&obj->refcount)) 9232225cfe4SDaniel Vetter obj = NULL; 9242225cfe4SDaniel Vetter } 9252225cfe4SDaniel Vetter drm_vma_offset_unlock_lookup(dev->vma_offset_manager); 9262225cfe4SDaniel Vetter 9272225cfe4SDaniel Vetter if (!obj) 928197633b9SDaniel Vetter return -EINVAL; 9292225cfe4SDaniel Vetter 9302225cfe4SDaniel Vetter if (!drm_vma_node_is_allowed(node, filp)) { 9312225cfe4SDaniel Vetter drm_gem_object_unreference_unlocked(obj); 932ca481c9bSDavid Herrmann return -EACCES; 933a2c0a97bSJesse Barnes } 934a2c0a97bSJesse Barnes 9352225cfe4SDaniel Vetter ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, 9362225cfe4SDaniel Vetter vma); 937a2c0a97bSJesse Barnes 9382225cfe4SDaniel Vetter drm_gem_object_unreference_unlocked(obj); 939a2c0a97bSJesse Barnes 940a2c0a97bSJesse Barnes return ret; 941a2c0a97bSJesse Barnes } 942a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap); 943