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> 41*67d0ec4eSDaniel Vetter #include "drm_internal.h" 42673a394bSEric Anholt 43673a394bSEric Anholt /** @file drm_gem.c 44673a394bSEric Anholt * 45673a394bSEric Anholt * This file provides some of the base ioctls and library routines for 46673a394bSEric Anholt * the graphics memory manager implemented by each device driver. 47673a394bSEric Anholt * 48673a394bSEric Anholt * Because various devices have different requirements in terms of 49673a394bSEric Anholt * synchronization and migration strategies, implementing that is left up to 50673a394bSEric Anholt * the driver, and all that the general API provides should be generic -- 51673a394bSEric Anholt * allocating objects, reading/writing data with the cpu, freeing objects. 52673a394bSEric Anholt * Even there, platform-dependent optimizations for reading/writing data with 53673a394bSEric Anholt * the CPU mean we'll likely hook those out to driver-specific calls. However, 54673a394bSEric Anholt * the DRI2 implementation wants to have at least allocate/mmap be generic. 55673a394bSEric Anholt * 56673a394bSEric Anholt * The goal was to have swap-backed object allocation managed through 57673a394bSEric Anholt * struct file. However, file descriptors as handles to a struct file have 58673a394bSEric Anholt * two major failings: 59673a394bSEric Anholt * - Process limits prevent more than 1024 or so being used at a time by 60673a394bSEric Anholt * default. 61673a394bSEric Anholt * - Inability to allocate high fds will aggravate the X Server's select() 62673a394bSEric Anholt * handling, and likely that of many GL client applications as well. 63673a394bSEric Anholt * 64673a394bSEric Anholt * This led to a plan of using our own integer IDs (called handles, following 65673a394bSEric Anholt * DRM terminology) to mimic fds, and implement the fd syscalls we need as 66673a394bSEric Anholt * ioctls. The objects themselves will still include the struct file so 67673a394bSEric Anholt * that we can transition to fds if the required kernel infrastructure shows 68673a394bSEric Anholt * up at a later date, and as our interface with shmfs for memory allocation. 69673a394bSEric Anholt */ 70673a394bSEric Anholt 71a2c0a97bSJesse Barnes /* 72a2c0a97bSJesse Barnes * We make up offsets for buffer objects so we can recognize them at 73a2c0a97bSJesse Barnes * mmap time. 74a2c0a97bSJesse Barnes */ 7505269a3aSJordan Crouse 7605269a3aSJordan Crouse /* pgoff in mmap is an unsigned long, so we need to make sure that 7705269a3aSJordan Crouse * the faked up offset will fit 7805269a3aSJordan Crouse */ 7905269a3aSJordan Crouse 8005269a3aSJordan Crouse #if BITS_PER_LONG == 64 81a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) 82a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16) 8305269a3aSJordan Crouse #else 8405269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1) 8505269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16) 8605269a3aSJordan Crouse #endif 87a2c0a97bSJesse Barnes 88673a394bSEric Anholt /** 8989d61fc0SDaniel Vetter * drm_gem_init - Initialize the GEM device fields 9089d61fc0SDaniel Vetter * @dev: drm_devic structure to initialize 91673a394bSEric Anholt */ 92673a394bSEric Anholt int 93673a394bSEric Anholt drm_gem_init(struct drm_device *dev) 94673a394bSEric Anholt { 95b04a5906SDaniel Vetter struct drm_vma_offset_manager *vma_offset_manager; 96a2c0a97bSJesse Barnes 97cd4f013fSDaniel Vetter mutex_init(&dev->object_name_lock); 98673a394bSEric Anholt idr_init(&dev->object_name_idr); 99a2c0a97bSJesse Barnes 100b04a5906SDaniel Vetter vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL); 101b04a5906SDaniel Vetter if (!vma_offset_manager) { 102a2c0a97bSJesse Barnes DRM_ERROR("out of memory\n"); 103a2c0a97bSJesse Barnes return -ENOMEM; 104a2c0a97bSJesse Barnes } 105a2c0a97bSJesse Barnes 106b04a5906SDaniel Vetter dev->vma_offset_manager = vma_offset_manager; 107b04a5906SDaniel Vetter drm_vma_offset_manager_init(vma_offset_manager, 1080de23977SDavid Herrmann DRM_FILE_PAGE_OFFSET_START, 10977ef8bbcSDavid Herrmann DRM_FILE_PAGE_OFFSET_SIZE); 110a2c0a97bSJesse Barnes 111673a394bSEric Anholt return 0; 112673a394bSEric Anholt } 113673a394bSEric Anholt 114a2c0a97bSJesse Barnes void 115a2c0a97bSJesse Barnes drm_gem_destroy(struct drm_device *dev) 116a2c0a97bSJesse Barnes { 117a2c0a97bSJesse Barnes 118b04a5906SDaniel Vetter drm_vma_offset_manager_destroy(dev->vma_offset_manager); 119b04a5906SDaniel Vetter kfree(dev->vma_offset_manager); 120b04a5906SDaniel Vetter dev->vma_offset_manager = NULL; 121a2c0a97bSJesse Barnes } 122a2c0a97bSJesse Barnes 123673a394bSEric Anholt /** 12489d61fc0SDaniel Vetter * drm_gem_object_init - initialize an allocated shmem-backed GEM object 12589d61fc0SDaniel Vetter * @dev: drm_device the object should be initialized for 12689d61fc0SDaniel Vetter * @obj: drm_gem_object to initialize 12789d61fc0SDaniel Vetter * @size: object size 12889d61fc0SDaniel Vetter * 12962cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with 1301d397043SDaniel Vetter * shmfs backing store. 1311d397043SDaniel Vetter */ 1321d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev, 1331d397043SDaniel Vetter struct drm_gem_object *obj, size_t size) 1341d397043SDaniel Vetter { 13589c8233fSDavid Herrmann struct file *filp; 1361d397043SDaniel Vetter 1376ab11a26SDaniel Vetter drm_gem_private_object_init(dev, obj, size); 1386ab11a26SDaniel Vetter 13989c8233fSDavid Herrmann filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); 14089c8233fSDavid Herrmann if (IS_ERR(filp)) 14189c8233fSDavid Herrmann return PTR_ERR(filp); 1421d397043SDaniel Vetter 14389c8233fSDavid Herrmann obj->filp = filp; 1441d397043SDaniel Vetter 1451d397043SDaniel Vetter return 0; 1461d397043SDaniel Vetter } 1471d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init); 1481d397043SDaniel Vetter 1491d397043SDaniel Vetter /** 15089d61fc0SDaniel Vetter * drm_gem_object_init - initialize an allocated private GEM object 15189d61fc0SDaniel Vetter * @dev: drm_device the object should be initialized for 15289d61fc0SDaniel Vetter * @obj: drm_gem_object to initialize 15389d61fc0SDaniel Vetter * @size: object size 15489d61fc0SDaniel Vetter * 15562cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with 15662cb7011SAlan Cox * no GEM provided backing store. Instead the caller is responsible for 15762cb7011SAlan Cox * backing the object and handling it. 15862cb7011SAlan Cox */ 15989c8233fSDavid Herrmann void drm_gem_private_object_init(struct drm_device *dev, 16062cb7011SAlan Cox struct drm_gem_object *obj, size_t size) 16162cb7011SAlan Cox { 16262cb7011SAlan Cox BUG_ON((size & (PAGE_SIZE - 1)) != 0); 16362cb7011SAlan Cox 16462cb7011SAlan Cox obj->dev = dev; 16562cb7011SAlan Cox obj->filp = NULL; 16662cb7011SAlan Cox 16762cb7011SAlan Cox kref_init(&obj->refcount); 168a8e11d1cSDaniel Vetter obj->handle_count = 0; 16962cb7011SAlan Cox obj->size = size; 17088d7ebe5SDavid Herrmann drm_vma_node_reset(&obj->vma_node); 17162cb7011SAlan Cox } 17262cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init); 17362cb7011SAlan Cox 1740ff926c7SDave Airlie static void 1750ff926c7SDave Airlie drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp) 1760ff926c7SDave Airlie { 177319c933cSDaniel Vetter /* 178319c933cSDaniel Vetter * Note: obj->dma_buf can't disappear as long as we still hold a 179319c933cSDaniel Vetter * handle reference in obj->handle_count. 180319c933cSDaniel Vetter */ 181d0b2c533SDaniel Vetter mutex_lock(&filp->prime.lock); 182319c933cSDaniel Vetter if (obj->dma_buf) { 183d0b2c533SDaniel Vetter drm_prime_remove_buf_handle_locked(&filp->prime, 184319c933cSDaniel Vetter obj->dma_buf); 1850ff926c7SDave Airlie } 186d0b2c533SDaniel Vetter mutex_unlock(&filp->prime.lock); 1870ff926c7SDave Airlie } 1880ff926c7SDave Airlie 18936da5908SDaniel Vetter /** 19089d61fc0SDaniel Vetter * drm_gem_object_free - release resources bound to userspace handles 19189d61fc0SDaniel Vetter * @obj: GEM object to clean up. 19289d61fc0SDaniel Vetter * 19336da5908SDaniel Vetter * Called after the last handle to the object has been closed 19436da5908SDaniel Vetter * 19536da5908SDaniel Vetter * Removes any name for the object. Note that this must be 19636da5908SDaniel Vetter * called before drm_gem_object_free or we'll be touching 19736da5908SDaniel Vetter * freed memory 19836da5908SDaniel Vetter */ 19936da5908SDaniel Vetter static void drm_gem_object_handle_free(struct drm_gem_object *obj) 20036da5908SDaniel Vetter { 20136da5908SDaniel Vetter struct drm_device *dev = obj->dev; 20236da5908SDaniel Vetter 20336da5908SDaniel Vetter /* Remove any name for this object */ 20436da5908SDaniel Vetter if (obj->name) { 20536da5908SDaniel Vetter idr_remove(&dev->object_name_idr, obj->name); 20636da5908SDaniel Vetter obj->name = 0; 207a8e11d1cSDaniel Vetter } 20836da5908SDaniel Vetter } 20936da5908SDaniel Vetter 210319c933cSDaniel Vetter static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj) 211319c933cSDaniel Vetter { 212319c933cSDaniel Vetter /* Unbreak the reference cycle if we have an exported dma_buf. */ 213319c933cSDaniel Vetter if (obj->dma_buf) { 214319c933cSDaniel Vetter dma_buf_put(obj->dma_buf); 215319c933cSDaniel Vetter obj->dma_buf = NULL; 216319c933cSDaniel Vetter } 217319c933cSDaniel Vetter } 218319c933cSDaniel Vetter 219becee2a5SDaniel Vetter static void 22036da5908SDaniel Vetter drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj) 22136da5908SDaniel Vetter { 222a8e11d1cSDaniel Vetter if (WARN_ON(obj->handle_count == 0)) 22336da5908SDaniel Vetter return; 22436da5908SDaniel Vetter 22536da5908SDaniel Vetter /* 22636da5908SDaniel Vetter * Must bump handle count first as this may be the last 22736da5908SDaniel Vetter * ref, in which case the object would disappear before we 22836da5908SDaniel Vetter * checked for a name 22936da5908SDaniel Vetter */ 23036da5908SDaniel Vetter 231cd4f013fSDaniel Vetter mutex_lock(&obj->dev->object_name_lock); 232319c933cSDaniel Vetter if (--obj->handle_count == 0) { 23336da5908SDaniel Vetter drm_gem_object_handle_free(obj); 234319c933cSDaniel Vetter drm_gem_object_exported_dma_buf_free(obj); 235319c933cSDaniel Vetter } 236cd4f013fSDaniel Vetter mutex_unlock(&obj->dev->object_name_lock); 237a8e11d1cSDaniel Vetter 23836da5908SDaniel Vetter drm_gem_object_unreference_unlocked(obj); 23936da5908SDaniel Vetter } 24036da5908SDaniel Vetter 241673a394bSEric Anholt /** 24289d61fc0SDaniel Vetter * drm_gem_handle_delete - deletes the given file-private handle 24389d61fc0SDaniel Vetter * @filp: drm file-private structure to use for the handle look up 24489d61fc0SDaniel Vetter * @handle: userspace handle to delete 24589d61fc0SDaniel Vetter * 24689d61fc0SDaniel Vetter * Removes the GEM handle from the @filp lookup table and if this is the last 24789d61fc0SDaniel Vetter * handle also cleans up linked resources like GEM names. 248673a394bSEric Anholt */ 249ff72145bSDave Airlie int 250a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle) 251673a394bSEric Anholt { 252673a394bSEric Anholt struct drm_device *dev; 253673a394bSEric Anholt struct drm_gem_object *obj; 254673a394bSEric Anholt 255673a394bSEric Anholt /* This is gross. The idr system doesn't let us try a delete and 256673a394bSEric Anholt * return an error code. It just spews if you fail at deleting. 257673a394bSEric Anholt * So, we have to grab a lock around finding the object and then 258673a394bSEric Anholt * doing the delete on it and dropping the refcount, or the user 259673a394bSEric Anholt * could race us to double-decrement the refcount and cause a 260673a394bSEric Anholt * use-after-free later. Given the frequency of our handle lookups, 261673a394bSEric Anholt * we may want to use ida for number allocation and a hash table 262673a394bSEric Anholt * for the pointers, anyway. 263673a394bSEric Anholt */ 264673a394bSEric Anholt spin_lock(&filp->table_lock); 265673a394bSEric Anholt 266673a394bSEric Anholt /* Check if we currently have a reference on the object */ 267673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 268673a394bSEric Anholt if (obj == NULL) { 269673a394bSEric Anholt spin_unlock(&filp->table_lock); 270673a394bSEric Anholt return -EINVAL; 271673a394bSEric Anholt } 272673a394bSEric Anholt dev = obj->dev; 273673a394bSEric Anholt 274673a394bSEric Anholt /* Release reference and decrement refcount. */ 275673a394bSEric Anholt idr_remove(&filp->object_idr, handle); 276673a394bSEric Anholt spin_unlock(&filp->table_lock); 277673a394bSEric Anholt 2789c784855SThierry Reding if (drm_core_check_feature(dev, DRIVER_PRIME)) 2790ff926c7SDave Airlie drm_gem_remove_prime_handles(obj, filp); 280ca481c9bSDavid Herrmann drm_vma_node_revoke(&obj->vma_node, filp->filp); 2813248877eSDave Airlie 282304eda32SBen Skeggs if (dev->driver->gem_close_object) 283304eda32SBen Skeggs dev->driver->gem_close_object(obj, filp); 284bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 285673a394bSEric Anholt 286673a394bSEric Anholt return 0; 287673a394bSEric Anholt } 288ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete); 289673a394bSEric Anholt 290673a394bSEric Anholt /** 29143387b37SDaniel Vetter * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers 29289d61fc0SDaniel Vetter * @file: drm file-private structure to remove the dumb handle from 29389d61fc0SDaniel Vetter * @dev: corresponding drm_device 29489d61fc0SDaniel Vetter * @handle: the dumb handle to remove 29543387b37SDaniel Vetter * 29643387b37SDaniel Vetter * This implements the ->dumb_destroy kms driver callback for drivers which use 29743387b37SDaniel Vetter * gem to manage their backing storage. 29843387b37SDaniel Vetter */ 29943387b37SDaniel Vetter int drm_gem_dumb_destroy(struct drm_file *file, 30043387b37SDaniel Vetter struct drm_device *dev, 30143387b37SDaniel Vetter uint32_t handle) 30243387b37SDaniel Vetter { 30343387b37SDaniel Vetter return drm_gem_handle_delete(file, handle); 30443387b37SDaniel Vetter } 30543387b37SDaniel Vetter EXPORT_SYMBOL(drm_gem_dumb_destroy); 30643387b37SDaniel Vetter 30743387b37SDaniel Vetter /** 30820228c44SDaniel Vetter * drm_gem_handle_create_tail - internal functions to create a handle 30989d61fc0SDaniel Vetter * @file_priv: drm file-private structure to register the handle for 31089d61fc0SDaniel Vetter * @obj: object to register 31189d61fc0SDaniel Vetter * @handlep: pionter to return the created handle to the caller 31220228c44SDaniel Vetter * 31320228c44SDaniel Vetter * This expects the dev->object_name_lock to be held already and will drop it 31420228c44SDaniel Vetter * before returning. Used to avoid races in establishing new handles when 31520228c44SDaniel Vetter * importing an object from either an flink name or a dma-buf. 316673a394bSEric Anholt */ 317673a394bSEric Anholt int 31820228c44SDaniel Vetter drm_gem_handle_create_tail(struct drm_file *file_priv, 319673a394bSEric Anholt struct drm_gem_object *obj, 320a1a2d1d3SPekka Paalanen u32 *handlep) 321673a394bSEric Anholt { 322304eda32SBen Skeggs struct drm_device *dev = obj->dev; 323673a394bSEric Anholt int ret; 324673a394bSEric Anholt 32520228c44SDaniel Vetter WARN_ON(!mutex_is_locked(&dev->object_name_lock)); 32620228c44SDaniel Vetter 327673a394bSEric Anholt /* 3282e928815STejun Heo * Get the user-visible handle using idr. Preload and perform 3292e928815STejun Heo * allocation under our spinlock. 330673a394bSEric Anholt */ 3312e928815STejun Heo idr_preload(GFP_KERNEL); 332673a394bSEric Anholt spin_lock(&file_priv->table_lock); 3332e928815STejun Heo 3342e928815STejun Heo ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT); 335a8e11d1cSDaniel Vetter drm_gem_object_reference(obj); 336a8e11d1cSDaniel Vetter obj->handle_count++; 337673a394bSEric Anholt spin_unlock(&file_priv->table_lock); 3382e928815STejun Heo idr_preload_end(); 339cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock); 340a8e11d1cSDaniel Vetter if (ret < 0) { 341a8e11d1cSDaniel Vetter drm_gem_object_handle_unreference_unlocked(obj); 342673a394bSEric Anholt return ret; 343a8e11d1cSDaniel Vetter } 3442e928815STejun Heo *handlep = ret; 345673a394bSEric Anholt 346ca481c9bSDavid Herrmann ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp); 347ca481c9bSDavid Herrmann if (ret) { 348ca481c9bSDavid Herrmann drm_gem_handle_delete(file_priv, *handlep); 349ca481c9bSDavid Herrmann return ret; 350ca481c9bSDavid Herrmann } 351304eda32SBen Skeggs 352304eda32SBen Skeggs if (dev->driver->gem_open_object) { 353304eda32SBen Skeggs ret = dev->driver->gem_open_object(obj, file_priv); 354304eda32SBen Skeggs if (ret) { 355304eda32SBen Skeggs drm_gem_handle_delete(file_priv, *handlep); 356304eda32SBen Skeggs return ret; 357304eda32SBen Skeggs } 358304eda32SBen Skeggs } 359304eda32SBen Skeggs 360673a394bSEric Anholt return 0; 361673a394bSEric Anholt } 36220228c44SDaniel Vetter 36320228c44SDaniel Vetter /** 36489d61fc0SDaniel Vetter * gem_handle_create - create a gem handle for an object 36589d61fc0SDaniel Vetter * @file_priv: drm file-private structure to register the handle for 36689d61fc0SDaniel Vetter * @obj: object to register 36789d61fc0SDaniel Vetter * @handlep: pionter to return the created handle to the caller 36889d61fc0SDaniel Vetter * 36920228c44SDaniel Vetter * Create a handle for this object. This adds a handle reference 37020228c44SDaniel Vetter * to the object, which includes a regular reference count. Callers 37120228c44SDaniel Vetter * will likely want to dereference the object afterwards. 37220228c44SDaniel Vetter */ 37320228c44SDaniel Vetter int 37420228c44SDaniel Vetter drm_gem_handle_create(struct drm_file *file_priv, 37520228c44SDaniel Vetter struct drm_gem_object *obj, 37620228c44SDaniel Vetter u32 *handlep) 37720228c44SDaniel Vetter { 37820228c44SDaniel Vetter mutex_lock(&obj->dev->object_name_lock); 37920228c44SDaniel Vetter 38020228c44SDaniel Vetter return drm_gem_handle_create_tail(file_priv, obj, handlep); 38120228c44SDaniel Vetter } 382673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create); 383673a394bSEric Anholt 38475ef8b3bSRob Clark 38575ef8b3bSRob Clark /** 38675ef8b3bSRob Clark * drm_gem_free_mmap_offset - release a fake mmap offset for an object 38775ef8b3bSRob Clark * @obj: obj in question 38875ef8b3bSRob Clark * 38975ef8b3bSRob Clark * This routine frees fake offsets allocated by drm_gem_create_mmap_offset(). 39075ef8b3bSRob Clark */ 39175ef8b3bSRob Clark void 39275ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj) 39375ef8b3bSRob Clark { 39475ef8b3bSRob Clark struct drm_device *dev = obj->dev; 39575ef8b3bSRob Clark 396b04a5906SDaniel Vetter drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node); 39775ef8b3bSRob Clark } 39875ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset); 39975ef8b3bSRob Clark 40075ef8b3bSRob Clark /** 401367bbd49SRob Clark * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object 402367bbd49SRob Clark * @obj: obj in question 403367bbd49SRob Clark * @size: the virtual size 404367bbd49SRob Clark * 405367bbd49SRob Clark * GEM memory mapping works by handing back to userspace a fake mmap offset 406367bbd49SRob Clark * it can use in a subsequent mmap(2) call. The DRM core code then looks 407367bbd49SRob Clark * up the object based on the offset and sets up the various memory mapping 408367bbd49SRob Clark * structures. 409367bbd49SRob Clark * 410367bbd49SRob Clark * This routine allocates and attaches a fake offset for @obj, in cases where 411367bbd49SRob Clark * the virtual size differs from the physical size (ie. obj->size). Otherwise 412367bbd49SRob Clark * just use drm_gem_create_mmap_offset(). 413367bbd49SRob Clark */ 414367bbd49SRob Clark int 415367bbd49SRob Clark drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size) 416367bbd49SRob Clark { 417367bbd49SRob Clark struct drm_device *dev = obj->dev; 418367bbd49SRob Clark 419b04a5906SDaniel Vetter return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node, 420367bbd49SRob Clark size / PAGE_SIZE); 421367bbd49SRob Clark } 422367bbd49SRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset_size); 423367bbd49SRob Clark 424367bbd49SRob Clark /** 42575ef8b3bSRob Clark * drm_gem_create_mmap_offset - create a fake mmap offset for an object 42675ef8b3bSRob Clark * @obj: obj in question 42775ef8b3bSRob Clark * 42875ef8b3bSRob Clark * GEM memory mapping works by handing back to userspace a fake mmap offset 42975ef8b3bSRob Clark * it can use in a subsequent mmap(2) call. The DRM core code then looks 43075ef8b3bSRob Clark * up the object based on the offset and sets up the various memory mapping 43175ef8b3bSRob Clark * structures. 43275ef8b3bSRob Clark * 43375ef8b3bSRob Clark * This routine allocates and attaches a fake offset for @obj. 43475ef8b3bSRob Clark */ 435367bbd49SRob Clark int drm_gem_create_mmap_offset(struct drm_gem_object *obj) 43675ef8b3bSRob Clark { 437367bbd49SRob Clark return drm_gem_create_mmap_offset_size(obj, obj->size); 43875ef8b3bSRob Clark } 43975ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset); 44075ef8b3bSRob Clark 441bcc5c9d5SRob Clark /** 442bcc5c9d5SRob Clark * drm_gem_get_pages - helper to allocate backing pages for a GEM object 443bcc5c9d5SRob Clark * from shmem 444bcc5c9d5SRob Clark * @obj: obj in question 4450cdbe8acSDavid Herrmann * 4460cdbe8acSDavid Herrmann * This reads the page-array of the shmem-backing storage of the given gem 4470cdbe8acSDavid Herrmann * object. An array of pages is returned. If a page is not allocated or 4480cdbe8acSDavid Herrmann * swapped-out, this will allocate/swap-in the required pages. Note that the 4490cdbe8acSDavid Herrmann * whole object is covered by the page-array and pinned in memory. 4500cdbe8acSDavid Herrmann * 4510cdbe8acSDavid Herrmann * Use drm_gem_put_pages() to release the array and unpin all pages. 4520cdbe8acSDavid Herrmann * 4530cdbe8acSDavid Herrmann * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()). 4540cdbe8acSDavid Herrmann * If you require other GFP-masks, you have to do those allocations yourself. 4550cdbe8acSDavid Herrmann * 4560cdbe8acSDavid Herrmann * Note that you are not allowed to change gfp-zones during runtime. That is, 4570cdbe8acSDavid Herrmann * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as 4580cdbe8acSDavid Herrmann * set during initialization. If you have special zone constraints, set them 4590cdbe8acSDavid Herrmann * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care 4600cdbe8acSDavid Herrmann * to keep pages in the required zone during swap-in. 461bcc5c9d5SRob Clark */ 4620cdbe8acSDavid Herrmann struct page **drm_gem_get_pages(struct drm_gem_object *obj) 463bcc5c9d5SRob Clark { 464bcc5c9d5SRob Clark struct address_space *mapping; 465bcc5c9d5SRob Clark struct page *p, **pages; 466bcc5c9d5SRob Clark int i, npages; 467bcc5c9d5SRob Clark 468bcc5c9d5SRob Clark /* This is the shared memory object that backs the GEM resource */ 4690cdbe8acSDavid Herrmann mapping = file_inode(obj->filp)->i_mapping; 470bcc5c9d5SRob Clark 471bcc5c9d5SRob Clark /* We already BUG_ON() for non-page-aligned sizes in 472bcc5c9d5SRob Clark * drm_gem_object_init(), so we should never hit this unless 473bcc5c9d5SRob Clark * driver author is doing something really wrong: 474bcc5c9d5SRob Clark */ 475bcc5c9d5SRob Clark WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 476bcc5c9d5SRob Clark 477bcc5c9d5SRob Clark npages = obj->size >> PAGE_SHIFT; 478bcc5c9d5SRob Clark 479bcc5c9d5SRob Clark pages = drm_malloc_ab(npages, sizeof(struct page *)); 480bcc5c9d5SRob Clark if (pages == NULL) 481bcc5c9d5SRob Clark return ERR_PTR(-ENOMEM); 482bcc5c9d5SRob Clark 483bcc5c9d5SRob Clark for (i = 0; i < npages; i++) { 4840cdbe8acSDavid Herrmann p = shmem_read_mapping_page(mapping, i); 485bcc5c9d5SRob Clark if (IS_ERR(p)) 486bcc5c9d5SRob Clark goto fail; 487bcc5c9d5SRob Clark pages[i] = p; 488bcc5c9d5SRob Clark 4892123000bSDavid Herrmann /* Make sure shmem keeps __GFP_DMA32 allocated pages in the 4902123000bSDavid Herrmann * correct region during swapin. Note that this requires 4912123000bSDavid Herrmann * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping) 4922123000bSDavid Herrmann * so shmem can relocate pages during swapin if required. 493bcc5c9d5SRob Clark */ 4940cdbe8acSDavid Herrmann BUG_ON((mapping_gfp_mask(mapping) & __GFP_DMA32) && 495bcc5c9d5SRob Clark (page_to_pfn(p) >= 0x00100000UL)); 496bcc5c9d5SRob Clark } 497bcc5c9d5SRob Clark 498bcc5c9d5SRob Clark return pages; 499bcc5c9d5SRob Clark 500bcc5c9d5SRob Clark fail: 501bcc5c9d5SRob Clark while (i--) 502bcc5c9d5SRob Clark page_cache_release(pages[i]); 503bcc5c9d5SRob Clark 504bcc5c9d5SRob Clark drm_free_large(pages); 505bcc5c9d5SRob Clark return ERR_CAST(p); 506bcc5c9d5SRob Clark } 507bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_get_pages); 508bcc5c9d5SRob Clark 509bcc5c9d5SRob Clark /** 510bcc5c9d5SRob Clark * drm_gem_put_pages - helper to free backing pages for a GEM object 511bcc5c9d5SRob Clark * @obj: obj in question 512bcc5c9d5SRob Clark * @pages: pages to free 513bcc5c9d5SRob Clark * @dirty: if true, pages will be marked as dirty 514bcc5c9d5SRob Clark * @accessed: if true, the pages will be marked as accessed 515bcc5c9d5SRob Clark */ 516bcc5c9d5SRob Clark void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, 517bcc5c9d5SRob Clark bool dirty, bool accessed) 518bcc5c9d5SRob Clark { 519bcc5c9d5SRob Clark int i, npages; 520bcc5c9d5SRob Clark 521bcc5c9d5SRob Clark /* We already BUG_ON() for non-page-aligned sizes in 522bcc5c9d5SRob Clark * drm_gem_object_init(), so we should never hit this unless 523bcc5c9d5SRob Clark * driver author is doing something really wrong: 524bcc5c9d5SRob Clark */ 525bcc5c9d5SRob Clark WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 526bcc5c9d5SRob Clark 527bcc5c9d5SRob Clark npages = obj->size >> PAGE_SHIFT; 528bcc5c9d5SRob Clark 529bcc5c9d5SRob Clark for (i = 0; i < npages; i++) { 530bcc5c9d5SRob Clark if (dirty) 531bcc5c9d5SRob Clark set_page_dirty(pages[i]); 532bcc5c9d5SRob Clark 533bcc5c9d5SRob Clark if (accessed) 534bcc5c9d5SRob Clark mark_page_accessed(pages[i]); 535bcc5c9d5SRob Clark 536bcc5c9d5SRob Clark /* Undo the reference we took when populating the table */ 537bcc5c9d5SRob Clark page_cache_release(pages[i]); 538bcc5c9d5SRob Clark } 539bcc5c9d5SRob Clark 540bcc5c9d5SRob Clark drm_free_large(pages); 541bcc5c9d5SRob Clark } 542bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_put_pages); 543bcc5c9d5SRob Clark 544673a394bSEric Anholt /** Returns a reference to the object named by the handle. */ 545673a394bSEric Anholt struct drm_gem_object * 546673a394bSEric Anholt drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, 547a1a2d1d3SPekka Paalanen u32 handle) 548673a394bSEric Anholt { 549673a394bSEric Anholt struct drm_gem_object *obj; 550673a394bSEric Anholt 551673a394bSEric Anholt spin_lock(&filp->table_lock); 552673a394bSEric Anholt 553673a394bSEric Anholt /* Check if we currently have a reference on the object */ 554673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 555673a394bSEric Anholt if (obj == NULL) { 556673a394bSEric Anholt spin_unlock(&filp->table_lock); 557673a394bSEric Anholt return NULL; 558673a394bSEric Anholt } 559673a394bSEric Anholt 560673a394bSEric Anholt drm_gem_object_reference(obj); 561673a394bSEric Anholt 562673a394bSEric Anholt spin_unlock(&filp->table_lock); 563673a394bSEric Anholt 564673a394bSEric Anholt return obj; 565673a394bSEric Anholt } 566673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup); 567673a394bSEric Anholt 568673a394bSEric Anholt /** 56989d61fc0SDaniel Vetter * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl 57089d61fc0SDaniel Vetter * @dev: drm_device 57189d61fc0SDaniel Vetter * @data: ioctl data 57289d61fc0SDaniel Vetter * @file_priv: drm file-private structure 57389d61fc0SDaniel Vetter * 574673a394bSEric Anholt * Releases the handle to an mm object. 575673a394bSEric Anholt */ 576673a394bSEric Anholt int 577673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data, 578673a394bSEric Anholt struct drm_file *file_priv) 579673a394bSEric Anholt { 580673a394bSEric Anholt struct drm_gem_close *args = data; 581673a394bSEric Anholt int ret; 582673a394bSEric Anholt 583673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 584673a394bSEric Anholt return -ENODEV; 585673a394bSEric Anholt 586673a394bSEric Anholt ret = drm_gem_handle_delete(file_priv, args->handle); 587673a394bSEric Anholt 588673a394bSEric Anholt return ret; 589673a394bSEric Anholt } 590673a394bSEric Anholt 591673a394bSEric Anholt /** 59289d61fc0SDaniel Vetter * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl 59389d61fc0SDaniel Vetter * @dev: drm_device 59489d61fc0SDaniel Vetter * @data: ioctl data 59589d61fc0SDaniel Vetter * @file_priv: drm file-private structure 59689d61fc0SDaniel Vetter * 597673a394bSEric Anholt * Create a global name for an object, returning the name. 598673a394bSEric Anholt * 599673a394bSEric Anholt * Note that the name does not hold a reference; when the object 600673a394bSEric Anholt * is freed, the name goes away. 601673a394bSEric Anholt */ 602673a394bSEric Anholt int 603673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data, 604673a394bSEric Anholt struct drm_file *file_priv) 605673a394bSEric Anholt { 606673a394bSEric Anholt struct drm_gem_flink *args = data; 607673a394bSEric Anholt struct drm_gem_object *obj; 608673a394bSEric Anholt int ret; 609673a394bSEric Anholt 610673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 611673a394bSEric Anholt return -ENODEV; 612673a394bSEric Anholt 613673a394bSEric Anholt obj = drm_gem_object_lookup(dev, file_priv, args->handle); 614673a394bSEric Anholt if (obj == NULL) 615bf79cb91SChris Wilson return -ENOENT; 616673a394bSEric Anholt 617cd4f013fSDaniel Vetter mutex_lock(&dev->object_name_lock); 6182e928815STejun Heo idr_preload(GFP_KERNEL); 619a8e11d1cSDaniel Vetter /* prevent races with concurrent gem_close. */ 620a8e11d1cSDaniel Vetter if (obj->handle_count == 0) { 621a8e11d1cSDaniel Vetter ret = -ENOENT; 622a8e11d1cSDaniel Vetter goto err; 623a8e11d1cSDaniel Vetter } 624a8e11d1cSDaniel Vetter 6258d59bae5SChris Wilson if (!obj->name) { 6262e928815STejun Heo ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT); 6272e928815STejun Heo if (ret < 0) 6283e49c4f4SChris Wilson goto err; 6292e07fb22SYoungJun Cho 6302e07fb22SYoungJun Cho obj->name = ret; 6318d59bae5SChris Wilson } 6323e49c4f4SChris Wilson 6332e07fb22SYoungJun Cho args->name = (uint64_t) obj->name; 6342e07fb22SYoungJun Cho ret = 0; 6352e07fb22SYoungJun Cho 6363e49c4f4SChris Wilson err: 6372e07fb22SYoungJun Cho idr_preload_end(); 638cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock); 639bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 6403e49c4f4SChris Wilson return ret; 641673a394bSEric Anholt } 642673a394bSEric Anholt 643673a394bSEric Anholt /** 64489d61fc0SDaniel Vetter * drm_gem_open - implementation of the GEM_OPEN ioctl 64589d61fc0SDaniel Vetter * @dev: drm_device 64689d61fc0SDaniel Vetter * @data: ioctl data 64789d61fc0SDaniel Vetter * @file_priv: drm file-private structure 64889d61fc0SDaniel Vetter * 649673a394bSEric Anholt * Open an object using the global name, returning a handle and the size. 650673a394bSEric Anholt * 651673a394bSEric Anholt * This handle (of course) holds a reference to the object, so the object 652673a394bSEric Anholt * will not go away until the handle is deleted. 653673a394bSEric Anholt */ 654673a394bSEric Anholt int 655673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data, 656673a394bSEric Anholt struct drm_file *file_priv) 657673a394bSEric Anholt { 658673a394bSEric Anholt struct drm_gem_open *args = data; 659673a394bSEric Anholt struct drm_gem_object *obj; 660673a394bSEric Anholt int ret; 661a1a2d1d3SPekka Paalanen u32 handle; 662673a394bSEric Anholt 663673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 664673a394bSEric Anholt return -ENODEV; 665673a394bSEric Anholt 666cd4f013fSDaniel Vetter mutex_lock(&dev->object_name_lock); 667673a394bSEric Anholt obj = idr_find(&dev->object_name_idr, (int) args->name); 66820228c44SDaniel Vetter if (obj) { 669673a394bSEric Anholt drm_gem_object_reference(obj); 67020228c44SDaniel Vetter } else { 671cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock); 672673a394bSEric Anholt return -ENOENT; 67320228c44SDaniel Vetter } 674673a394bSEric Anholt 67520228c44SDaniel Vetter /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */ 67620228c44SDaniel Vetter ret = drm_gem_handle_create_tail(file_priv, obj, &handle); 677bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 678673a394bSEric Anholt if (ret) 679673a394bSEric Anholt return ret; 680673a394bSEric Anholt 681673a394bSEric Anholt args->handle = handle; 682673a394bSEric Anholt args->size = obj->size; 683673a394bSEric Anholt 684673a394bSEric Anholt return 0; 685673a394bSEric Anholt } 686673a394bSEric Anholt 687673a394bSEric Anholt /** 68889d61fc0SDaniel Vetter * gem_gem_open - initalizes GEM file-private structures at devnode open time 68989d61fc0SDaniel Vetter * @dev: drm_device which is being opened by userspace 69089d61fc0SDaniel Vetter * @file_private: drm file-private structure to set up 69189d61fc0SDaniel Vetter * 692673a394bSEric Anholt * Called at device open time, sets up the structure for handling refcounting 693673a394bSEric Anholt * of mm objects. 694673a394bSEric Anholt */ 695673a394bSEric Anholt void 696673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 697673a394bSEric Anholt { 698673a394bSEric Anholt idr_init(&file_private->object_idr); 699673a394bSEric Anholt spin_lock_init(&file_private->table_lock); 700673a394bSEric Anholt } 701673a394bSEric Anholt 70289d61fc0SDaniel Vetter /* 703673a394bSEric Anholt * Called at device close to release the file's 704673a394bSEric Anholt * handle references on objects. 705673a394bSEric Anholt */ 706673a394bSEric Anholt static int 707673a394bSEric Anholt drm_gem_object_release_handle(int id, void *ptr, void *data) 708673a394bSEric Anholt { 709304eda32SBen Skeggs struct drm_file *file_priv = data; 710673a394bSEric Anholt struct drm_gem_object *obj = ptr; 711304eda32SBen Skeggs struct drm_device *dev = obj->dev; 712304eda32SBen Skeggs 7139c784855SThierry Reding if (drm_core_check_feature(dev, DRIVER_PRIME)) 7140ff926c7SDave Airlie drm_gem_remove_prime_handles(obj, file_priv); 715ca481c9bSDavid Herrmann drm_vma_node_revoke(&obj->vma_node, file_priv->filp); 7163248877eSDave Airlie 717304eda32SBen Skeggs if (dev->driver->gem_close_object) 718304eda32SBen Skeggs dev->driver->gem_close_object(obj, file_priv); 719673a394bSEric Anholt 720bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 721673a394bSEric Anholt 722673a394bSEric Anholt return 0; 723673a394bSEric Anholt } 724673a394bSEric Anholt 725673a394bSEric Anholt /** 72689d61fc0SDaniel Vetter * drm_gem_release - release file-private GEM resources 72789d61fc0SDaniel Vetter * @dev: drm_device which is being closed by userspace 72889d61fc0SDaniel Vetter * @file_private: drm file-private structure to clean up 72989d61fc0SDaniel Vetter * 730673a394bSEric Anholt * Called at close time when the filp is going away. 731673a394bSEric Anholt * 732673a394bSEric Anholt * Releases any remaining references on objects by this filp. 733673a394bSEric Anholt */ 734673a394bSEric Anholt void 735673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 736673a394bSEric Anholt { 737673a394bSEric Anholt idr_for_each(&file_private->object_idr, 738304eda32SBen Skeggs &drm_gem_object_release_handle, file_private); 739673a394bSEric Anholt idr_destroy(&file_private->object_idr); 740673a394bSEric Anholt } 741673a394bSEric Anholt 742fd632aa3SDaniel Vetter void 743fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj) 744c3ae90c0SLuca Barbieri { 745319c933cSDaniel Vetter WARN_ON(obj->dma_buf); 746319c933cSDaniel Vetter 74762cb7011SAlan Cox if (obj->filp) 748c3ae90c0SLuca Barbieri fput(obj->filp); 74977472347SDavid Herrmann 75077472347SDavid Herrmann drm_gem_free_mmap_offset(obj); 751c3ae90c0SLuca Barbieri } 752fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release); 753c3ae90c0SLuca Barbieri 754673a394bSEric Anholt /** 75589d61fc0SDaniel Vetter * drm_gem_object_free - free a GEM object 75689d61fc0SDaniel Vetter * @kref: kref of the object to free 75789d61fc0SDaniel Vetter * 758673a394bSEric Anholt * Called after the last reference to the object has been lost. 759c3ae90c0SLuca Barbieri * Must be called holding struct_ mutex 760673a394bSEric Anholt * 761673a394bSEric Anholt * Frees the object 762673a394bSEric Anholt */ 763673a394bSEric Anholt void 764673a394bSEric Anholt drm_gem_object_free(struct kref *kref) 765673a394bSEric Anholt { 766673a394bSEric Anholt struct drm_gem_object *obj = (struct drm_gem_object *) kref; 767673a394bSEric Anholt struct drm_device *dev = obj->dev; 768673a394bSEric Anholt 769673a394bSEric Anholt BUG_ON(!mutex_is_locked(&dev->struct_mutex)); 770673a394bSEric Anholt 771673a394bSEric Anholt if (dev->driver->gem_free_object != NULL) 772673a394bSEric Anholt dev->driver->gem_free_object(obj); 773673a394bSEric Anholt } 774673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free); 775673a394bSEric Anholt 776ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma) 777ab00b3e5SJesse Barnes { 778ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 779ab00b3e5SJesse Barnes 780ab00b3e5SJesse Barnes drm_gem_object_reference(obj); 78131dfbc93SChris Wilson 78231dfbc93SChris Wilson mutex_lock(&obj->dev->struct_mutex); 783b06d66beSRob Clark drm_vm_open_locked(obj->dev, vma); 78431dfbc93SChris Wilson mutex_unlock(&obj->dev->struct_mutex); 785ab00b3e5SJesse Barnes } 786ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open); 787ab00b3e5SJesse Barnes 788ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma) 789ab00b3e5SJesse Barnes { 790ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 791b74ad5aeSChris Wilson struct drm_device *dev = obj->dev; 792ab00b3e5SJesse Barnes 793b74ad5aeSChris Wilson mutex_lock(&dev->struct_mutex); 794b06d66beSRob Clark drm_vm_close_locked(obj->dev, vma); 79531dfbc93SChris Wilson drm_gem_object_unreference(obj); 796b74ad5aeSChris Wilson mutex_unlock(&dev->struct_mutex); 797ab00b3e5SJesse Barnes } 798ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close); 799ab00b3e5SJesse Barnes 8001c5aafa6SLaurent Pinchart /** 8011c5aafa6SLaurent Pinchart * drm_gem_mmap_obj - memory map a GEM object 8021c5aafa6SLaurent Pinchart * @obj: the GEM object to map 8031c5aafa6SLaurent Pinchart * @obj_size: the object size to be mapped, in bytes 8041c5aafa6SLaurent Pinchart * @vma: VMA for the area to be mapped 8051c5aafa6SLaurent Pinchart * 8061c5aafa6SLaurent Pinchart * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops 8071c5aafa6SLaurent Pinchart * provided by the driver. Depending on their requirements, drivers can either 8081c5aafa6SLaurent Pinchart * provide a fault handler in their gem_vm_ops (in which case any accesses to 8091c5aafa6SLaurent Pinchart * the object will be trapped, to perform migration, GTT binding, surface 8101c5aafa6SLaurent Pinchart * register allocation, or performance monitoring), or mmap the buffer memory 8111c5aafa6SLaurent Pinchart * synchronously after calling drm_gem_mmap_obj. 8121c5aafa6SLaurent Pinchart * 8131c5aafa6SLaurent Pinchart * This function is mainly intended to implement the DMABUF mmap operation, when 8141c5aafa6SLaurent Pinchart * the GEM object is not looked up based on its fake offset. To implement the 8151c5aafa6SLaurent Pinchart * DRM mmap operation, drivers should use the drm_gem_mmap() function. 8161c5aafa6SLaurent Pinchart * 817ca481c9bSDavid Herrmann * drm_gem_mmap_obj() assumes the user is granted access to the buffer while 818ca481c9bSDavid Herrmann * drm_gem_mmap() prevents unprivileged users from mapping random objects. So 819ca481c9bSDavid Herrmann * callers must verify access restrictions before calling this helper. 820ca481c9bSDavid Herrmann * 8214368dd84SYoungJun Cho * NOTE: This function has to be protected with dev->struct_mutex 8224368dd84SYoungJun Cho * 8231c5aafa6SLaurent Pinchart * Return 0 or success or -EINVAL if the object size is smaller than the VMA 8241c5aafa6SLaurent Pinchart * size, or if no gem_vm_ops are provided. 8251c5aafa6SLaurent Pinchart */ 8261c5aafa6SLaurent Pinchart int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size, 8271c5aafa6SLaurent Pinchart struct vm_area_struct *vma) 8281c5aafa6SLaurent Pinchart { 8291c5aafa6SLaurent Pinchart struct drm_device *dev = obj->dev; 8301c5aafa6SLaurent Pinchart 8314368dd84SYoungJun Cho lockdep_assert_held(&dev->struct_mutex); 8324368dd84SYoungJun Cho 8331c5aafa6SLaurent Pinchart /* Check for valid size. */ 8341c5aafa6SLaurent Pinchart if (obj_size < vma->vm_end - vma->vm_start) 8351c5aafa6SLaurent Pinchart return -EINVAL; 8361c5aafa6SLaurent Pinchart 8371c5aafa6SLaurent Pinchart if (!dev->driver->gem_vm_ops) 8381c5aafa6SLaurent Pinchart return -EINVAL; 8391c5aafa6SLaurent Pinchart 8401c5aafa6SLaurent Pinchart vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 8411c5aafa6SLaurent Pinchart vma->vm_ops = dev->driver->gem_vm_ops; 8421c5aafa6SLaurent Pinchart vma->vm_private_data = obj; 8431c5aafa6SLaurent Pinchart vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 8441c5aafa6SLaurent Pinchart 8451c5aafa6SLaurent Pinchart /* Take a ref for this mapping of the object, so that the fault 8461c5aafa6SLaurent Pinchart * handler can dereference the mmap offset's pointer to the object. 8471c5aafa6SLaurent Pinchart * This reference is cleaned up by the corresponding vm_close 8481c5aafa6SLaurent Pinchart * (which should happen whether the vma was created by this call, or 8491c5aafa6SLaurent Pinchart * by a vm_open due to mremap or partial unmap or whatever). 8501c5aafa6SLaurent Pinchart */ 8511c5aafa6SLaurent Pinchart drm_gem_object_reference(obj); 8521c5aafa6SLaurent Pinchart 8531c5aafa6SLaurent Pinchart drm_vm_open_locked(dev, vma); 8541c5aafa6SLaurent Pinchart return 0; 8551c5aafa6SLaurent Pinchart } 8561c5aafa6SLaurent Pinchart EXPORT_SYMBOL(drm_gem_mmap_obj); 857ab00b3e5SJesse Barnes 858a2c0a97bSJesse Barnes /** 859a2c0a97bSJesse Barnes * drm_gem_mmap - memory map routine for GEM objects 860a2c0a97bSJesse Barnes * @filp: DRM file pointer 861a2c0a97bSJesse Barnes * @vma: VMA for the area to be mapped 862a2c0a97bSJesse Barnes * 863a2c0a97bSJesse Barnes * If a driver supports GEM object mapping, mmap calls on the DRM file 864a2c0a97bSJesse Barnes * descriptor will end up here. 865a2c0a97bSJesse Barnes * 8661c5aafa6SLaurent Pinchart * Look up the GEM object based on the offset passed in (vma->vm_pgoff will 867a2c0a97bSJesse Barnes * contain the fake offset we created when the GTT map ioctl was called on 8681c5aafa6SLaurent Pinchart * the object) and map it with a call to drm_gem_mmap_obj(). 869ca481c9bSDavid Herrmann * 870ca481c9bSDavid Herrmann * If the caller is not granted access to the buffer object, the mmap will fail 871ca481c9bSDavid Herrmann * with EACCES. Please see the vma manager for more information. 872a2c0a97bSJesse Barnes */ 873a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 874a2c0a97bSJesse Barnes { 875a2c0a97bSJesse Barnes struct drm_file *priv = filp->private_data; 876a2c0a97bSJesse Barnes struct drm_device *dev = priv->minor->dev; 8770de23977SDavid Herrmann struct drm_gem_object *obj; 8780de23977SDavid Herrmann struct drm_vma_offset_node *node; 879a8469aa8SDavid Herrmann int ret; 880a2c0a97bSJesse Barnes 8812c07a21dSDave Airlie if (drm_device_is_unplugged(dev)) 8822c07a21dSDave Airlie return -ENODEV; 8832c07a21dSDave Airlie 884a2c0a97bSJesse Barnes mutex_lock(&dev->struct_mutex); 885a2c0a97bSJesse Barnes 886b04a5906SDaniel Vetter node = drm_vma_offset_exact_lookup(dev->vma_offset_manager, 887b04a5906SDaniel Vetter vma->vm_pgoff, 8880de23977SDavid Herrmann vma_pages(vma)); 8890de23977SDavid Herrmann if (!node) { 890a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 891a2c0a97bSJesse Barnes return drm_mmap(filp, vma); 892ca481c9bSDavid Herrmann } else if (!drm_vma_node_is_allowed(node, filp)) { 893ca481c9bSDavid Herrmann mutex_unlock(&dev->struct_mutex); 894ca481c9bSDavid Herrmann return -EACCES; 895a2c0a97bSJesse Barnes } 896a2c0a97bSJesse Barnes 8970de23977SDavid Herrmann obj = container_of(node, struct drm_gem_object, vma_node); 898aed2c03cSDavid Herrmann ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, vma); 899a2c0a97bSJesse Barnes 900a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 901a2c0a97bSJesse Barnes 902a2c0a97bSJesse Barnes return ret; 903a2c0a97bSJesse Barnes } 904a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap); 905