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> 3995cf9264STom Lendacky #include <linux/mem_encrypt.h> 40fb4b4927SKuo-Hsin Yang #include <linux/pagevec.h> 410500c04eSSam Ravnborg 421c535876SSam Ravnborg #include <drm/drm.h> 430500c04eSSam Ravnborg #include <drm/drm_device.h> 440500c04eSSam Ravnborg #include <drm/drm_drv.h> 450500c04eSSam Ravnborg #include <drm/drm_file.h> 46d9fc9413SDaniel Vetter #include <drm/drm_gem.h> 47641b9103SDaniel Vetter #include <drm/drm_managed.h> 4845d58b40SNoralf Trønnes #include <drm/drm_print.h> 490500c04eSSam Ravnborg #include <drm/drm_vma_manager.h> 500500c04eSSam Ravnborg 5167d0ec4eSDaniel Vetter #include "drm_internal.h" 52673a394bSEric Anholt 53673a394bSEric Anholt /** @file drm_gem.c 54673a394bSEric Anholt * 55673a394bSEric Anholt * This file provides some of the base ioctls and library routines for 56673a394bSEric Anholt * the graphics memory manager implemented by each device driver. 57673a394bSEric Anholt * 58673a394bSEric Anholt * Because various devices have different requirements in terms of 59673a394bSEric Anholt * synchronization and migration strategies, implementing that is left up to 60673a394bSEric Anholt * the driver, and all that the general API provides should be generic -- 61673a394bSEric Anholt * allocating objects, reading/writing data with the cpu, freeing objects. 62673a394bSEric Anholt * Even there, platform-dependent optimizations for reading/writing data with 63673a394bSEric Anholt * the CPU mean we'll likely hook those out to driver-specific calls. However, 64673a394bSEric Anholt * the DRI2 implementation wants to have at least allocate/mmap be generic. 65673a394bSEric Anholt * 66673a394bSEric Anholt * The goal was to have swap-backed object allocation managed through 67673a394bSEric Anholt * struct file. However, file descriptors as handles to a struct file have 68673a394bSEric Anholt * two major failings: 69673a394bSEric Anholt * - Process limits prevent more than 1024 or so being used at a time by 70673a394bSEric Anholt * default. 71673a394bSEric Anholt * - Inability to allocate high fds will aggravate the X Server's select() 72673a394bSEric Anholt * handling, and likely that of many GL client applications as well. 73673a394bSEric Anholt * 74673a394bSEric Anholt * This led to a plan of using our own integer IDs (called handles, following 75673a394bSEric Anholt * DRM terminology) to mimic fds, and implement the fd syscalls we need as 76673a394bSEric Anholt * ioctls. The objects themselves will still include the struct file so 77673a394bSEric Anholt * that we can transition to fds if the required kernel infrastructure shows 78673a394bSEric Anholt * up at a later date, and as our interface with shmfs for memory allocation. 79673a394bSEric Anholt */ 80673a394bSEric Anholt 81641b9103SDaniel Vetter static void 82641b9103SDaniel Vetter drm_gem_init_release(struct drm_device *dev, void *ptr) 83641b9103SDaniel Vetter { 84641b9103SDaniel Vetter drm_vma_offset_manager_destroy(dev->vma_offset_manager); 85641b9103SDaniel Vetter } 86641b9103SDaniel Vetter 87673a394bSEric Anholt /** 8889d61fc0SDaniel Vetter * drm_gem_init - Initialize the GEM device fields 8989d61fc0SDaniel Vetter * @dev: drm_devic structure to initialize 90673a394bSEric Anholt */ 91673a394bSEric Anholt int 92673a394bSEric Anholt drm_gem_init(struct drm_device *dev) 93673a394bSEric Anholt { 94b04a5906SDaniel Vetter struct drm_vma_offset_manager *vma_offset_manager; 95a2c0a97bSJesse Barnes 96cd4f013fSDaniel Vetter mutex_init(&dev->object_name_lock); 97e86584c5SChris Wilson idr_init_base(&dev->object_name_idr, 1); 98a2c0a97bSJesse Barnes 99641b9103SDaniel Vetter vma_offset_manager = drmm_kzalloc(dev, sizeof(*vma_offset_manager), 100641b9103SDaniel Vetter 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 111641b9103SDaniel Vetter return drmm_add_action(dev, drm_gem_init_release, NULL); 112a2c0a97bSJesse Barnes } 113a2c0a97bSJesse Barnes 114673a394bSEric Anholt /** 11589d61fc0SDaniel Vetter * drm_gem_object_init - initialize an allocated shmem-backed GEM object 11689d61fc0SDaniel Vetter * @dev: drm_device the object should be initialized for 11789d61fc0SDaniel Vetter * @obj: drm_gem_object to initialize 11889d61fc0SDaniel Vetter * @size: object size 11989d61fc0SDaniel Vetter * 12062cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with 1211d397043SDaniel Vetter * shmfs backing store. 1221d397043SDaniel Vetter */ 1231d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev, 1241d397043SDaniel Vetter struct drm_gem_object *obj, size_t size) 1251d397043SDaniel Vetter { 12689c8233fSDavid Herrmann struct file *filp; 1271d397043SDaniel Vetter 1286ab11a26SDaniel Vetter drm_gem_private_object_init(dev, obj, size); 1296ab11a26SDaniel Vetter 13089c8233fSDavid Herrmann filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); 13189c8233fSDavid Herrmann if (IS_ERR(filp)) 13289c8233fSDavid Herrmann return PTR_ERR(filp); 1331d397043SDaniel Vetter 13489c8233fSDavid Herrmann obj->filp = filp; 1351d397043SDaniel Vetter 1361d397043SDaniel Vetter return 0; 1371d397043SDaniel Vetter } 1381d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init); 1391d397043SDaniel Vetter 1401d397043SDaniel Vetter /** 1412a5706a3SLaurent Pinchart * drm_gem_private_object_init - initialize an allocated private GEM object 14289d61fc0SDaniel Vetter * @dev: drm_device the object should be initialized for 14389d61fc0SDaniel Vetter * @obj: drm_gem_object to initialize 14489d61fc0SDaniel Vetter * @size: object size 14589d61fc0SDaniel Vetter * 14662cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with 14762cb7011SAlan Cox * no GEM provided backing store. Instead the caller is responsible for 14862cb7011SAlan Cox * backing the object and handling it. 14962cb7011SAlan Cox */ 15089c8233fSDavid Herrmann void drm_gem_private_object_init(struct drm_device *dev, 15162cb7011SAlan Cox struct drm_gem_object *obj, size_t size) 15262cb7011SAlan Cox { 15362cb7011SAlan Cox BUG_ON((size & (PAGE_SIZE - 1)) != 0); 15462cb7011SAlan Cox 15562cb7011SAlan Cox obj->dev = dev; 15662cb7011SAlan Cox obj->filp = NULL; 15762cb7011SAlan Cox 15862cb7011SAlan Cox kref_init(&obj->refcount); 159a8e11d1cSDaniel Vetter obj->handle_count = 0; 16062cb7011SAlan Cox obj->size = size; 16152791eeeSChristian König dma_resv_init(&obj->_resv); 1621ba62714SRob Herring if (!obj->resv) 1631ba62714SRob Herring obj->resv = &obj->_resv; 1641ba62714SRob Herring 16588d7ebe5SDavid Herrmann drm_vma_node_reset(&obj->vma_node); 16662cb7011SAlan Cox } 16762cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init); 16862cb7011SAlan Cox 1690ff926c7SDave Airlie static void 1700ff926c7SDave Airlie drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp) 1710ff926c7SDave Airlie { 172319c933cSDaniel Vetter /* 173319c933cSDaniel Vetter * Note: obj->dma_buf can't disappear as long as we still hold a 174319c933cSDaniel Vetter * handle reference in obj->handle_count. 175319c933cSDaniel Vetter */ 176d0b2c533SDaniel Vetter mutex_lock(&filp->prime.lock); 177319c933cSDaniel Vetter if (obj->dma_buf) { 178d0b2c533SDaniel Vetter drm_prime_remove_buf_handle_locked(&filp->prime, 179319c933cSDaniel Vetter obj->dma_buf); 1800ff926c7SDave Airlie } 181d0b2c533SDaniel Vetter mutex_unlock(&filp->prime.lock); 1820ff926c7SDave Airlie } 1830ff926c7SDave Airlie 18436da5908SDaniel Vetter /** 185c6a84325SThierry Reding * drm_gem_object_handle_free - release resources bound to userspace handles 18689d61fc0SDaniel Vetter * @obj: GEM object to clean up. 18789d61fc0SDaniel Vetter * 18836da5908SDaniel Vetter * Called after the last handle to the object has been closed 18936da5908SDaniel Vetter * 19036da5908SDaniel Vetter * Removes any name for the object. Note that this must be 19136da5908SDaniel Vetter * called before drm_gem_object_free or we'll be touching 19236da5908SDaniel Vetter * freed memory 19336da5908SDaniel Vetter */ 19436da5908SDaniel Vetter static void drm_gem_object_handle_free(struct drm_gem_object *obj) 19536da5908SDaniel Vetter { 19636da5908SDaniel Vetter struct drm_device *dev = obj->dev; 19736da5908SDaniel Vetter 19836da5908SDaniel Vetter /* Remove any name for this object */ 19936da5908SDaniel Vetter if (obj->name) { 20036da5908SDaniel Vetter idr_remove(&dev->object_name_idr, obj->name); 20136da5908SDaniel Vetter obj->name = 0; 202a8e11d1cSDaniel Vetter } 20336da5908SDaniel Vetter } 20436da5908SDaniel Vetter 205319c933cSDaniel Vetter static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj) 206319c933cSDaniel Vetter { 207319c933cSDaniel Vetter /* Unbreak the reference cycle if we have an exported dma_buf. */ 208319c933cSDaniel Vetter if (obj->dma_buf) { 209319c933cSDaniel Vetter dma_buf_put(obj->dma_buf); 210319c933cSDaniel Vetter obj->dma_buf = NULL; 211319c933cSDaniel Vetter } 212319c933cSDaniel Vetter } 213319c933cSDaniel Vetter 214becee2a5SDaniel Vetter static void 215e6b62714SThierry Reding drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj) 21636da5908SDaniel Vetter { 21798a8883aSChris Wilson struct drm_device *dev = obj->dev; 21898a8883aSChris Wilson bool final = false; 21998a8883aSChris Wilson 2206afe6929SChris Wilson if (WARN_ON(READ_ONCE(obj->handle_count) == 0)) 22136da5908SDaniel Vetter return; 22236da5908SDaniel Vetter 22336da5908SDaniel Vetter /* 22436da5908SDaniel Vetter * Must bump handle count first as this may be the last 22536da5908SDaniel Vetter * ref, in which case the object would disappear before we 22636da5908SDaniel Vetter * checked for a name 22736da5908SDaniel Vetter */ 22836da5908SDaniel Vetter 22998a8883aSChris Wilson mutex_lock(&dev->object_name_lock); 230319c933cSDaniel Vetter if (--obj->handle_count == 0) { 23136da5908SDaniel Vetter drm_gem_object_handle_free(obj); 232319c933cSDaniel Vetter drm_gem_object_exported_dma_buf_free(obj); 23398a8883aSChris Wilson final = true; 234319c933cSDaniel Vetter } 23598a8883aSChris Wilson mutex_unlock(&dev->object_name_lock); 236a8e11d1cSDaniel Vetter 23798a8883aSChris Wilson if (final) 238*be6ee102SEmil Velikov drm_gem_object_put(obj); 23936da5908SDaniel Vetter } 24036da5908SDaniel Vetter 2418815b23aSChris Wilson /* 2428815b23aSChris Wilson * Called at device or object close to release the file's 2438815b23aSChris Wilson * handle references on objects. 2448815b23aSChris Wilson */ 2458815b23aSChris Wilson static int 2468815b23aSChris Wilson drm_gem_object_release_handle(int id, void *ptr, void *data) 2478815b23aSChris Wilson { 2488815b23aSChris Wilson struct drm_file *file_priv = data; 2498815b23aSChris Wilson struct drm_gem_object *obj = ptr; 2508815b23aSChris Wilson struct drm_device *dev = obj->dev; 2518815b23aSChris Wilson 252b39b5394SNoralf Trønnes if (obj->funcs && obj->funcs->close) 253b39b5394SNoralf Trønnes obj->funcs->close(obj, file_priv); 254b39b5394SNoralf Trønnes else if (dev->driver->gem_close_object) 255d0a133f7SChris Wilson dev->driver->gem_close_object(obj, file_priv); 256d0a133f7SChris Wilson 2578815b23aSChris Wilson drm_gem_remove_prime_handles(obj, file_priv); 258d9a1f0b4SDavid Herrmann drm_vma_node_revoke(&obj->vma_node, file_priv); 2598815b23aSChris Wilson 260e6b62714SThierry Reding drm_gem_object_handle_put_unlocked(obj); 2618815b23aSChris Wilson 2628815b23aSChris Wilson return 0; 2638815b23aSChris Wilson } 2648815b23aSChris Wilson 265673a394bSEric Anholt /** 26689d61fc0SDaniel Vetter * drm_gem_handle_delete - deletes the given file-private handle 26789d61fc0SDaniel Vetter * @filp: drm file-private structure to use for the handle look up 26889d61fc0SDaniel Vetter * @handle: userspace handle to delete 26989d61fc0SDaniel Vetter * 270df2e0900SDaniel Vetter * Removes the GEM handle from the @filp lookup table which has been added with 271df2e0900SDaniel Vetter * drm_gem_handle_create(). If this is the last handle also cleans up linked 272df2e0900SDaniel Vetter * resources like GEM names. 273673a394bSEric Anholt */ 274ff72145bSDave Airlie int 275a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle) 276673a394bSEric Anholt { 277673a394bSEric Anholt struct drm_gem_object *obj; 278673a394bSEric Anholt 279673a394bSEric Anholt spin_lock(&filp->table_lock); 280673a394bSEric Anholt 281673a394bSEric Anholt /* Check if we currently have a reference on the object */ 282f6cd7daeSChris Wilson obj = idr_replace(&filp->object_idr, NULL, handle); 283673a394bSEric Anholt spin_unlock(&filp->table_lock); 284f6cd7daeSChris Wilson if (IS_ERR_OR_NULL(obj)) 285673a394bSEric Anholt return -EINVAL; 286673a394bSEric Anholt 287f6cd7daeSChris Wilson /* Release driver's reference and decrement refcount. */ 288f6cd7daeSChris Wilson drm_gem_object_release_handle(handle, obj, filp); 289f6cd7daeSChris Wilson 290f6cd7daeSChris Wilson /* And finally make the handle available for future allocations. */ 291f6cd7daeSChris Wilson spin_lock(&filp->table_lock); 292673a394bSEric Anholt idr_remove(&filp->object_idr, handle); 293673a394bSEric Anholt spin_unlock(&filp->table_lock); 294673a394bSEric Anholt 295673a394bSEric Anholt return 0; 296673a394bSEric Anholt } 297ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete); 298673a394bSEric Anholt 299673a394bSEric Anholt /** 300abd4e745SRob Herring * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object 301db611527SNoralf Trønnes * @file: drm file-private structure containing the gem object 302db611527SNoralf Trønnes * @dev: corresponding drm_device 303db611527SNoralf Trønnes * @handle: gem object handle 304db611527SNoralf Trønnes * @offset: return location for the fake mmap offset 305db611527SNoralf Trønnes * 306db611527SNoralf Trønnes * This implements the &drm_driver.dumb_map_offset kms driver callback for 307db611527SNoralf Trønnes * drivers which use gem to manage their backing storage. 308db611527SNoralf Trønnes * 309db611527SNoralf Trønnes * Returns: 310db611527SNoralf Trønnes * 0 on success or a negative error code on failure. 311db611527SNoralf Trønnes */ 312abd4e745SRob Herring int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, 313db611527SNoralf Trønnes u32 handle, u64 *offset) 314db611527SNoralf Trønnes { 315db611527SNoralf Trønnes struct drm_gem_object *obj; 316db611527SNoralf Trønnes int ret; 317db611527SNoralf Trønnes 318db611527SNoralf Trønnes obj = drm_gem_object_lookup(file, handle); 319db611527SNoralf Trønnes if (!obj) 320db611527SNoralf Trønnes return -ENOENT; 321db611527SNoralf Trønnes 32290378e58SNoralf Trønnes /* Don't allow imported objects to be mapped */ 32390378e58SNoralf Trønnes if (obj->import_attach) { 32490378e58SNoralf Trønnes ret = -EINVAL; 32590378e58SNoralf Trønnes goto out; 32690378e58SNoralf Trønnes } 32790378e58SNoralf Trønnes 328db611527SNoralf Trønnes ret = drm_gem_create_mmap_offset(obj); 329db611527SNoralf Trønnes if (ret) 330db611527SNoralf Trønnes goto out; 331db611527SNoralf Trønnes 332db611527SNoralf Trønnes *offset = drm_vma_node_offset_addr(&obj->vma_node); 333db611527SNoralf Trønnes out: 334*be6ee102SEmil Velikov drm_gem_object_put(obj); 335db611527SNoralf Trønnes 336db611527SNoralf Trønnes return ret; 337db611527SNoralf Trønnes } 338abd4e745SRob Herring EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset); 339db611527SNoralf Trønnes 340db611527SNoralf Trønnes /** 34143387b37SDaniel Vetter * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers 34289d61fc0SDaniel Vetter * @file: drm file-private structure to remove the dumb handle from 34389d61fc0SDaniel Vetter * @dev: corresponding drm_device 34489d61fc0SDaniel Vetter * @handle: the dumb handle to remove 34543387b37SDaniel Vetter * 346940eba2dSDaniel Vetter * This implements the &drm_driver.dumb_destroy kms driver callback for drivers 347940eba2dSDaniel Vetter * which use gem to manage their backing storage. 34843387b37SDaniel Vetter */ 34943387b37SDaniel Vetter int drm_gem_dumb_destroy(struct drm_file *file, 35043387b37SDaniel Vetter struct drm_device *dev, 35143387b37SDaniel Vetter uint32_t handle) 35243387b37SDaniel Vetter { 35343387b37SDaniel Vetter return drm_gem_handle_delete(file, handle); 35443387b37SDaniel Vetter } 35543387b37SDaniel Vetter EXPORT_SYMBOL(drm_gem_dumb_destroy); 35643387b37SDaniel Vetter 35743387b37SDaniel Vetter /** 35820228c44SDaniel Vetter * drm_gem_handle_create_tail - internal functions to create a handle 35989d61fc0SDaniel Vetter * @file_priv: drm file-private structure to register the handle for 36089d61fc0SDaniel Vetter * @obj: object to register 3618bf8180fSThierry Reding * @handlep: pointer to return the created handle to the caller 36220228c44SDaniel Vetter * 363940eba2dSDaniel Vetter * This expects the &drm_device.object_name_lock to be held already and will 364940eba2dSDaniel Vetter * drop it before returning. Used to avoid races in establishing new handles 365940eba2dSDaniel Vetter * when importing an object from either an flink name or a dma-buf. 366df2e0900SDaniel Vetter * 367df2e0900SDaniel Vetter * Handles must be release again through drm_gem_handle_delete(). This is done 368df2e0900SDaniel Vetter * when userspace closes @file_priv for all attached handles, or through the 369df2e0900SDaniel Vetter * GEM_CLOSE ioctl for individual handles. 370673a394bSEric Anholt */ 371673a394bSEric Anholt int 37220228c44SDaniel Vetter drm_gem_handle_create_tail(struct drm_file *file_priv, 373673a394bSEric Anholt struct drm_gem_object *obj, 374a1a2d1d3SPekka Paalanen u32 *handlep) 375673a394bSEric Anholt { 376304eda32SBen Skeggs struct drm_device *dev = obj->dev; 3779649399eSChris Wilson u32 handle; 378673a394bSEric Anholt int ret; 379673a394bSEric Anholt 38020228c44SDaniel Vetter WARN_ON(!mutex_is_locked(&dev->object_name_lock)); 38198a8883aSChris Wilson if (obj->handle_count++ == 0) 382e6b62714SThierry Reding drm_gem_object_get(obj); 38320228c44SDaniel Vetter 384673a394bSEric Anholt /* 3852e928815STejun Heo * Get the user-visible handle using idr. Preload and perform 3862e928815STejun Heo * allocation under our spinlock. 387673a394bSEric Anholt */ 3882e928815STejun Heo idr_preload(GFP_KERNEL); 389673a394bSEric Anholt spin_lock(&file_priv->table_lock); 3902e928815STejun Heo 3912e928815STejun Heo ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT); 39298a8883aSChris Wilson 393673a394bSEric Anholt spin_unlock(&file_priv->table_lock); 3942e928815STejun Heo idr_preload_end(); 39598a8883aSChris Wilson 396cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock); 3976984128dSChris Wilson if (ret < 0) 3986984128dSChris Wilson goto err_unref; 3996984128dSChris Wilson 4009649399eSChris Wilson handle = ret; 401673a394bSEric Anholt 402d9a1f0b4SDavid Herrmann ret = drm_vma_node_allow(&obj->vma_node, file_priv); 4036984128dSChris Wilson if (ret) 4046984128dSChris Wilson goto err_remove; 405304eda32SBen Skeggs 406b39b5394SNoralf Trønnes if (obj->funcs && obj->funcs->open) { 407b39b5394SNoralf Trønnes ret = obj->funcs->open(obj, file_priv); 408b39b5394SNoralf Trønnes if (ret) 409b39b5394SNoralf Trønnes goto err_revoke; 410b39b5394SNoralf Trønnes } else if (dev->driver->gem_open_object) { 411304eda32SBen Skeggs ret = dev->driver->gem_open_object(obj, file_priv); 4126984128dSChris Wilson if (ret) 4136984128dSChris Wilson goto err_revoke; 414304eda32SBen Skeggs } 415304eda32SBen Skeggs 4169649399eSChris Wilson *handlep = handle; 417673a394bSEric Anholt return 0; 4186984128dSChris Wilson 4196984128dSChris Wilson err_revoke: 420d9a1f0b4SDavid Herrmann drm_vma_node_revoke(&obj->vma_node, file_priv); 4216984128dSChris Wilson err_remove: 4226984128dSChris Wilson spin_lock(&file_priv->table_lock); 4239649399eSChris Wilson idr_remove(&file_priv->object_idr, handle); 4246984128dSChris Wilson spin_unlock(&file_priv->table_lock); 4256984128dSChris Wilson err_unref: 426e6b62714SThierry Reding drm_gem_object_handle_put_unlocked(obj); 4276984128dSChris Wilson return ret; 428673a394bSEric Anholt } 42920228c44SDaniel Vetter 43020228c44SDaniel Vetter /** 4318bf8180fSThierry Reding * drm_gem_handle_create - create a gem handle for an object 43289d61fc0SDaniel Vetter * @file_priv: drm file-private structure to register the handle for 43389d61fc0SDaniel Vetter * @obj: object to register 43482c0ef94SIgor Matheus Andrade Torrente * @handlep: pointer to return the created handle to the caller 43589d61fc0SDaniel Vetter * 43639031176SDaniel Vetter * Create a handle for this object. This adds a handle reference to the object, 43739031176SDaniel Vetter * which includes a regular reference count. Callers will likely want to 43839031176SDaniel Vetter * dereference the object afterwards. 43939031176SDaniel Vetter * 44039031176SDaniel Vetter * Since this publishes @obj to userspace it must be fully set up by this point, 44139031176SDaniel Vetter * drivers must call this last in their buffer object creation callbacks. 44220228c44SDaniel Vetter */ 4438bf8180fSThierry Reding int drm_gem_handle_create(struct drm_file *file_priv, 44420228c44SDaniel Vetter struct drm_gem_object *obj, 44520228c44SDaniel Vetter u32 *handlep) 44620228c44SDaniel Vetter { 44720228c44SDaniel Vetter mutex_lock(&obj->dev->object_name_lock); 44820228c44SDaniel Vetter 44920228c44SDaniel Vetter return drm_gem_handle_create_tail(file_priv, obj, handlep); 45020228c44SDaniel Vetter } 451673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create); 452673a394bSEric Anholt 45375ef8b3bSRob Clark 45475ef8b3bSRob Clark /** 45575ef8b3bSRob Clark * drm_gem_free_mmap_offset - release a fake mmap offset for an object 45675ef8b3bSRob Clark * @obj: obj in question 45775ef8b3bSRob Clark * 45875ef8b3bSRob Clark * This routine frees fake offsets allocated by drm_gem_create_mmap_offset(). 459f74418a4SDaniel Vetter * 460f74418a4SDaniel Vetter * Note that drm_gem_object_release() already calls this function, so drivers 461f74418a4SDaniel Vetter * don't have to take care of releasing the mmap offset themselves when freeing 462f74418a4SDaniel Vetter * the GEM object. 46375ef8b3bSRob Clark */ 46475ef8b3bSRob Clark void 46575ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj) 46675ef8b3bSRob Clark { 46775ef8b3bSRob Clark struct drm_device *dev = obj->dev; 46875ef8b3bSRob Clark 469b04a5906SDaniel Vetter drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node); 47075ef8b3bSRob Clark } 47175ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset); 47275ef8b3bSRob Clark 47375ef8b3bSRob Clark /** 474367bbd49SRob Clark * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object 475367bbd49SRob Clark * @obj: obj in question 476367bbd49SRob Clark * @size: the virtual size 477367bbd49SRob Clark * 478367bbd49SRob Clark * GEM memory mapping works by handing back to userspace a fake mmap offset 479367bbd49SRob Clark * it can use in a subsequent mmap(2) call. The DRM core code then looks 480367bbd49SRob Clark * up the object based on the offset and sets up the various memory mapping 481367bbd49SRob Clark * structures. 482367bbd49SRob Clark * 483367bbd49SRob Clark * This routine allocates and attaches a fake offset for @obj, in cases where 484940eba2dSDaniel Vetter * the virtual size differs from the physical size (ie. &drm_gem_object.size). 485940eba2dSDaniel Vetter * Otherwise just use drm_gem_create_mmap_offset(). 486f74418a4SDaniel Vetter * 487f74418a4SDaniel Vetter * This function is idempotent and handles an already allocated mmap offset 488f74418a4SDaniel Vetter * transparently. Drivers do not need to check for this case. 489367bbd49SRob Clark */ 490367bbd49SRob Clark int 491367bbd49SRob Clark drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size) 492367bbd49SRob Clark { 493367bbd49SRob Clark struct drm_device *dev = obj->dev; 494367bbd49SRob Clark 495b04a5906SDaniel Vetter return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node, 496367bbd49SRob Clark size / PAGE_SIZE); 497367bbd49SRob Clark } 498367bbd49SRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset_size); 499367bbd49SRob Clark 500367bbd49SRob Clark /** 50175ef8b3bSRob Clark * drm_gem_create_mmap_offset - create a fake mmap offset for an object 50275ef8b3bSRob Clark * @obj: obj in question 50375ef8b3bSRob Clark * 50475ef8b3bSRob Clark * GEM memory mapping works by handing back to userspace a fake mmap offset 50575ef8b3bSRob Clark * it can use in a subsequent mmap(2) call. The DRM core code then looks 50675ef8b3bSRob Clark * up the object based on the offset and sets up the various memory mapping 50775ef8b3bSRob Clark * structures. 50875ef8b3bSRob Clark * 50975ef8b3bSRob Clark * This routine allocates and attaches a fake offset for @obj. 510f74418a4SDaniel Vetter * 511f74418a4SDaniel Vetter * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release 512f74418a4SDaniel Vetter * the fake offset again. 51375ef8b3bSRob Clark */ 514367bbd49SRob Clark int drm_gem_create_mmap_offset(struct drm_gem_object *obj) 51575ef8b3bSRob Clark { 516367bbd49SRob Clark return drm_gem_create_mmap_offset_size(obj, obj->size); 51775ef8b3bSRob Clark } 51875ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset); 51975ef8b3bSRob Clark 520fb4b4927SKuo-Hsin Yang /* 521fb4b4927SKuo-Hsin Yang * Move pages to appropriate lru and release the pagevec, decrementing the 522fb4b4927SKuo-Hsin Yang * ref count of those pages. 523fb4b4927SKuo-Hsin Yang */ 524fb4b4927SKuo-Hsin Yang static void drm_gem_check_release_pagevec(struct pagevec *pvec) 525fb4b4927SKuo-Hsin Yang { 526fb4b4927SKuo-Hsin Yang check_move_unevictable_pages(pvec); 527fb4b4927SKuo-Hsin Yang __pagevec_release(pvec); 528fb4b4927SKuo-Hsin Yang cond_resched(); 529fb4b4927SKuo-Hsin Yang } 530fb4b4927SKuo-Hsin Yang 531bcc5c9d5SRob Clark /** 532bcc5c9d5SRob Clark * drm_gem_get_pages - helper to allocate backing pages for a GEM object 533bcc5c9d5SRob Clark * from shmem 534bcc5c9d5SRob Clark * @obj: obj in question 5350cdbe8acSDavid Herrmann * 5360cdbe8acSDavid Herrmann * This reads the page-array of the shmem-backing storage of the given gem 5370cdbe8acSDavid Herrmann * object. An array of pages is returned. If a page is not allocated or 5380cdbe8acSDavid Herrmann * swapped-out, this will allocate/swap-in the required pages. Note that the 5390cdbe8acSDavid Herrmann * whole object is covered by the page-array and pinned in memory. 5400cdbe8acSDavid Herrmann * 5410cdbe8acSDavid Herrmann * Use drm_gem_put_pages() to release the array and unpin all pages. 5420cdbe8acSDavid Herrmann * 5430cdbe8acSDavid Herrmann * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()). 5440cdbe8acSDavid Herrmann * If you require other GFP-masks, you have to do those allocations yourself. 5450cdbe8acSDavid Herrmann * 5460cdbe8acSDavid Herrmann * Note that you are not allowed to change gfp-zones during runtime. That is, 5470cdbe8acSDavid Herrmann * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as 5480cdbe8acSDavid Herrmann * set during initialization. If you have special zone constraints, set them 5495b9fbfffSJordan Crouse * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care 5500cdbe8acSDavid Herrmann * to keep pages in the required zone during swap-in. 551bcc5c9d5SRob Clark */ 5520cdbe8acSDavid Herrmann struct page **drm_gem_get_pages(struct drm_gem_object *obj) 553bcc5c9d5SRob Clark { 554bcc5c9d5SRob Clark struct address_space *mapping; 555bcc5c9d5SRob Clark struct page *p, **pages; 556fb4b4927SKuo-Hsin Yang struct pagevec pvec; 557bcc5c9d5SRob Clark int i, npages; 558bcc5c9d5SRob Clark 559bcc5c9d5SRob Clark /* This is the shared memory object that backs the GEM resource */ 56093c76a3dSAl Viro mapping = obj->filp->f_mapping; 561bcc5c9d5SRob Clark 562bcc5c9d5SRob Clark /* We already BUG_ON() for non-page-aligned sizes in 563bcc5c9d5SRob Clark * drm_gem_object_init(), so we should never hit this unless 564bcc5c9d5SRob Clark * driver author is doing something really wrong: 565bcc5c9d5SRob Clark */ 566bcc5c9d5SRob Clark WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 567bcc5c9d5SRob Clark 568bcc5c9d5SRob Clark npages = obj->size >> PAGE_SHIFT; 569bcc5c9d5SRob Clark 5702098105eSMichal Hocko pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 571bcc5c9d5SRob Clark if (pages == NULL) 572bcc5c9d5SRob Clark return ERR_PTR(-ENOMEM); 573bcc5c9d5SRob Clark 574fb4b4927SKuo-Hsin Yang mapping_set_unevictable(mapping); 575fb4b4927SKuo-Hsin Yang 576bcc5c9d5SRob Clark for (i = 0; i < npages; i++) { 5770cdbe8acSDavid Herrmann p = shmem_read_mapping_page(mapping, i); 578bcc5c9d5SRob Clark if (IS_ERR(p)) 579bcc5c9d5SRob Clark goto fail; 580bcc5c9d5SRob Clark pages[i] = p; 581bcc5c9d5SRob Clark 5822123000bSDavid Herrmann /* Make sure shmem keeps __GFP_DMA32 allocated pages in the 5832123000bSDavid Herrmann * correct region during swapin. Note that this requires 5842123000bSDavid Herrmann * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping) 5852123000bSDavid Herrmann * so shmem can relocate pages during swapin if required. 586bcc5c9d5SRob Clark */ 587c62d2555SMichal Hocko BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) && 588bcc5c9d5SRob Clark (page_to_pfn(p) >= 0x00100000UL)); 589bcc5c9d5SRob Clark } 590bcc5c9d5SRob Clark 591bcc5c9d5SRob Clark return pages; 592bcc5c9d5SRob Clark 593bcc5c9d5SRob Clark fail: 594fb4b4927SKuo-Hsin Yang mapping_clear_unevictable(mapping); 595fb4b4927SKuo-Hsin Yang pagevec_init(&pvec); 596fb4b4927SKuo-Hsin Yang while (i--) { 597fb4b4927SKuo-Hsin Yang if (!pagevec_add(&pvec, pages[i])) 598fb4b4927SKuo-Hsin Yang drm_gem_check_release_pagevec(&pvec); 599fb4b4927SKuo-Hsin Yang } 600fb4b4927SKuo-Hsin Yang if (pagevec_count(&pvec)) 601fb4b4927SKuo-Hsin Yang drm_gem_check_release_pagevec(&pvec); 602bcc5c9d5SRob Clark 6032098105eSMichal Hocko kvfree(pages); 604bcc5c9d5SRob Clark return ERR_CAST(p); 605bcc5c9d5SRob Clark } 606bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_get_pages); 607bcc5c9d5SRob Clark 608bcc5c9d5SRob Clark /** 609bcc5c9d5SRob Clark * drm_gem_put_pages - helper to free backing pages for a GEM object 610bcc5c9d5SRob Clark * @obj: obj in question 611bcc5c9d5SRob Clark * @pages: pages to free 612bcc5c9d5SRob Clark * @dirty: if true, pages will be marked as dirty 613bcc5c9d5SRob Clark * @accessed: if true, the pages will be marked as accessed 614bcc5c9d5SRob Clark */ 615bcc5c9d5SRob Clark void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, 616bcc5c9d5SRob Clark bool dirty, bool accessed) 617bcc5c9d5SRob Clark { 618bcc5c9d5SRob Clark int i, npages; 619fb4b4927SKuo-Hsin Yang struct address_space *mapping; 620fb4b4927SKuo-Hsin Yang struct pagevec pvec; 621fb4b4927SKuo-Hsin Yang 622fb4b4927SKuo-Hsin Yang mapping = file_inode(obj->filp)->i_mapping; 623fb4b4927SKuo-Hsin Yang mapping_clear_unevictable(mapping); 624bcc5c9d5SRob Clark 625bcc5c9d5SRob Clark /* We already BUG_ON() for non-page-aligned sizes in 626bcc5c9d5SRob Clark * drm_gem_object_init(), so we should never hit this unless 627bcc5c9d5SRob Clark * driver author is doing something really wrong: 628bcc5c9d5SRob Clark */ 629bcc5c9d5SRob Clark WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 630bcc5c9d5SRob Clark 631bcc5c9d5SRob Clark npages = obj->size >> PAGE_SHIFT; 632bcc5c9d5SRob Clark 633fb4b4927SKuo-Hsin Yang pagevec_init(&pvec); 634bcc5c9d5SRob Clark for (i = 0; i < npages; i++) { 635930a4024SRob Herring if (!pages[i]) 636930a4024SRob Herring continue; 637930a4024SRob Herring 638bcc5c9d5SRob Clark if (dirty) 639bcc5c9d5SRob Clark set_page_dirty(pages[i]); 640bcc5c9d5SRob Clark 641bcc5c9d5SRob Clark if (accessed) 642bcc5c9d5SRob Clark mark_page_accessed(pages[i]); 643bcc5c9d5SRob Clark 644bcc5c9d5SRob Clark /* Undo the reference we took when populating the table */ 645fb4b4927SKuo-Hsin Yang if (!pagevec_add(&pvec, pages[i])) 646fb4b4927SKuo-Hsin Yang drm_gem_check_release_pagevec(&pvec); 647bcc5c9d5SRob Clark } 648fb4b4927SKuo-Hsin Yang if (pagevec_count(&pvec)) 649fb4b4927SKuo-Hsin Yang drm_gem_check_release_pagevec(&pvec); 650bcc5c9d5SRob Clark 6512098105eSMichal Hocko kvfree(pages); 652bcc5c9d5SRob Clark } 653bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_put_pages); 654bcc5c9d5SRob Clark 655c117aa4dSRob Herring static int objects_lookup(struct drm_file *filp, u32 *handle, int count, 656c117aa4dSRob Herring struct drm_gem_object **objs) 657c117aa4dSRob Herring { 658c117aa4dSRob Herring int i, ret = 0; 659c117aa4dSRob Herring struct drm_gem_object *obj; 660c117aa4dSRob Herring 661c117aa4dSRob Herring spin_lock(&filp->table_lock); 662c117aa4dSRob Herring 663c117aa4dSRob Herring for (i = 0; i < count; i++) { 664c117aa4dSRob Herring /* Check if we currently have a reference on the object */ 665c117aa4dSRob Herring obj = idr_find(&filp->object_idr, handle[i]); 666c117aa4dSRob Herring if (!obj) { 667c117aa4dSRob Herring ret = -ENOENT; 668c117aa4dSRob Herring break; 669c117aa4dSRob Herring } 670c117aa4dSRob Herring drm_gem_object_get(obj); 671c117aa4dSRob Herring objs[i] = obj; 672c117aa4dSRob Herring } 673c117aa4dSRob Herring spin_unlock(&filp->table_lock); 674c117aa4dSRob Herring 675c117aa4dSRob Herring return ret; 676c117aa4dSRob Herring } 677c117aa4dSRob Herring 678c117aa4dSRob Herring /** 679c117aa4dSRob Herring * drm_gem_objects_lookup - look up GEM objects from an array of handles 680c117aa4dSRob Herring * @filp: DRM file private date 681c117aa4dSRob Herring * @bo_handles: user pointer to array of userspace handle 682c117aa4dSRob Herring * @count: size of handle array 683c117aa4dSRob Herring * @objs_out: returned pointer to array of drm_gem_object pointers 684c117aa4dSRob Herring * 685c117aa4dSRob Herring * Takes an array of userspace handles and returns a newly allocated array of 686c117aa4dSRob Herring * GEM objects. 687c117aa4dSRob Herring * 688c117aa4dSRob Herring * For a single handle lookup, use drm_gem_object_lookup(). 689c117aa4dSRob Herring * 690c117aa4dSRob Herring * Returns: 691c117aa4dSRob Herring * 692c117aa4dSRob Herring * @objs filled in with GEM object pointers. Returned GEM objects need to be 693*be6ee102SEmil Velikov * released with drm_gem_object_put(). -ENOENT is returned on a lookup 694c117aa4dSRob Herring * failure. 0 is returned on success. 695c117aa4dSRob Herring * 696c117aa4dSRob Herring */ 697c117aa4dSRob Herring int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, 698c117aa4dSRob Herring int count, struct drm_gem_object ***objs_out) 699c117aa4dSRob Herring { 700c117aa4dSRob Herring int ret; 701c117aa4dSRob Herring u32 *handles; 702c117aa4dSRob Herring struct drm_gem_object **objs; 703c117aa4dSRob Herring 704c117aa4dSRob Herring if (!count) 705c117aa4dSRob Herring return 0; 706c117aa4dSRob Herring 707c117aa4dSRob Herring objs = kvmalloc_array(count, sizeof(struct drm_gem_object *), 708c117aa4dSRob Herring GFP_KERNEL | __GFP_ZERO); 709c117aa4dSRob Herring if (!objs) 710c117aa4dSRob Herring return -ENOMEM; 711c117aa4dSRob Herring 712ec0bb482SDan Carpenter *objs_out = objs; 713ec0bb482SDan Carpenter 714c117aa4dSRob Herring handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL); 715c117aa4dSRob Herring if (!handles) { 716c117aa4dSRob Herring ret = -ENOMEM; 717c117aa4dSRob Herring goto out; 718c117aa4dSRob Herring } 719c117aa4dSRob Herring 720c117aa4dSRob Herring if (copy_from_user(handles, bo_handles, count * sizeof(u32))) { 721c117aa4dSRob Herring ret = -EFAULT; 722c117aa4dSRob Herring DRM_DEBUG("Failed to copy in GEM handles\n"); 723c117aa4dSRob Herring goto out; 724c117aa4dSRob Herring } 725c117aa4dSRob Herring 726c117aa4dSRob Herring ret = objects_lookup(filp, handles, count, objs); 727c117aa4dSRob Herring out: 728c117aa4dSRob Herring kvfree(handles); 729c117aa4dSRob Herring return ret; 730c117aa4dSRob Herring 731c117aa4dSRob Herring } 732c117aa4dSRob Herring EXPORT_SYMBOL(drm_gem_objects_lookup); 733c117aa4dSRob Herring 734df2e0900SDaniel Vetter /** 7351e55a53aSMatt Roper * drm_gem_object_lookup - look up a GEM object from its handle 736df2e0900SDaniel Vetter * @filp: DRM file private date 737df2e0900SDaniel Vetter * @handle: userspace handle 738df2e0900SDaniel Vetter * 739df2e0900SDaniel Vetter * Returns: 740df2e0900SDaniel Vetter * 741df2e0900SDaniel Vetter * A reference to the object named by the handle if such exists on @filp, NULL 742df2e0900SDaniel Vetter * otherwise. 743c117aa4dSRob Herring * 744c117aa4dSRob Herring * If looking up an array of handles, use drm_gem_objects_lookup(). 745df2e0900SDaniel Vetter */ 746673a394bSEric Anholt struct drm_gem_object * 747a8ad0bd8SChris Wilson drm_gem_object_lookup(struct drm_file *filp, u32 handle) 748673a394bSEric Anholt { 749c117aa4dSRob Herring struct drm_gem_object *obj = NULL; 750673a394bSEric Anholt 751c117aa4dSRob Herring objects_lookup(filp, &handle, 1, &obj); 752673a394bSEric Anholt return obj; 753673a394bSEric Anholt } 754673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup); 755673a394bSEric Anholt 756673a394bSEric Anholt /** 75752791eeeSChristian König * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects 7581ba62714SRob Herring * shared and/or exclusive fences. 7591ba62714SRob Herring * @filep: DRM file private date 7601ba62714SRob Herring * @handle: userspace handle 7611ba62714SRob Herring * @wait_all: if true, wait on all fences, else wait on just exclusive fence 7621ba62714SRob Herring * @timeout: timeout value in jiffies or zero to return immediately 7631ba62714SRob Herring * 7641ba62714SRob Herring * Returns: 7651ba62714SRob Herring * 7661ba62714SRob Herring * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or 7671ba62714SRob Herring * greater than 0 on success. 7681ba62714SRob Herring */ 76952791eeeSChristian König long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle, 7701ba62714SRob Herring bool wait_all, unsigned long timeout) 7711ba62714SRob Herring { 7721ba62714SRob Herring long ret; 7731ba62714SRob Herring struct drm_gem_object *obj; 7741ba62714SRob Herring 7751ba62714SRob Herring obj = drm_gem_object_lookup(filep, handle); 7761ba62714SRob Herring if (!obj) { 7771ba62714SRob Herring DRM_DEBUG("Failed to look up GEM BO %d\n", handle); 7781ba62714SRob Herring return -EINVAL; 7791ba62714SRob Herring } 7801ba62714SRob Herring 78152791eeeSChristian König ret = dma_resv_wait_timeout_rcu(obj->resv, wait_all, 7821ba62714SRob Herring true, timeout); 7831ba62714SRob Herring if (ret == 0) 7841ba62714SRob Herring ret = -ETIME; 7851ba62714SRob Herring else if (ret > 0) 7861ba62714SRob Herring ret = 0; 7871ba62714SRob Herring 788*be6ee102SEmil Velikov drm_gem_object_put(obj); 7891ba62714SRob Herring 7901ba62714SRob Herring return ret; 7911ba62714SRob Herring } 79252791eeeSChristian König EXPORT_SYMBOL(drm_gem_dma_resv_wait); 7931ba62714SRob Herring 7941ba62714SRob Herring /** 79589d61fc0SDaniel Vetter * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl 79689d61fc0SDaniel Vetter * @dev: drm_device 79789d61fc0SDaniel Vetter * @data: ioctl data 79889d61fc0SDaniel Vetter * @file_priv: drm file-private structure 79989d61fc0SDaniel Vetter * 800673a394bSEric Anholt * Releases the handle to an mm object. 801673a394bSEric Anholt */ 802673a394bSEric Anholt int 803673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data, 804673a394bSEric Anholt struct drm_file *file_priv) 805673a394bSEric Anholt { 806673a394bSEric Anholt struct drm_gem_close *args = data; 807673a394bSEric Anholt int ret; 808673a394bSEric Anholt 8091bcecfacSAndrzej Hajda if (!drm_core_check_feature(dev, DRIVER_GEM)) 81069fdf420SChris Wilson return -EOPNOTSUPP; 811673a394bSEric Anholt 812673a394bSEric Anholt ret = drm_gem_handle_delete(file_priv, args->handle); 813673a394bSEric Anholt 814673a394bSEric Anholt return ret; 815673a394bSEric Anholt } 816673a394bSEric Anholt 817673a394bSEric Anholt /** 81889d61fc0SDaniel Vetter * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl 81989d61fc0SDaniel Vetter * @dev: drm_device 82089d61fc0SDaniel Vetter * @data: ioctl data 82189d61fc0SDaniel Vetter * @file_priv: drm file-private structure 82289d61fc0SDaniel Vetter * 823673a394bSEric Anholt * Create a global name for an object, returning the name. 824673a394bSEric Anholt * 825673a394bSEric Anholt * Note that the name does not hold a reference; when the object 826673a394bSEric Anholt * is freed, the name goes away. 827673a394bSEric Anholt */ 828673a394bSEric Anholt int 829673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data, 830673a394bSEric Anholt struct drm_file *file_priv) 831673a394bSEric Anholt { 832673a394bSEric Anholt struct drm_gem_flink *args = data; 833673a394bSEric Anholt struct drm_gem_object *obj; 834673a394bSEric Anholt int ret; 835673a394bSEric Anholt 8361bcecfacSAndrzej Hajda if (!drm_core_check_feature(dev, DRIVER_GEM)) 83769fdf420SChris Wilson return -EOPNOTSUPP; 838673a394bSEric Anholt 839a8ad0bd8SChris Wilson obj = drm_gem_object_lookup(file_priv, args->handle); 840673a394bSEric Anholt if (obj == NULL) 841bf79cb91SChris Wilson return -ENOENT; 842673a394bSEric Anholt 843cd4f013fSDaniel Vetter mutex_lock(&dev->object_name_lock); 844a8e11d1cSDaniel Vetter /* prevent races with concurrent gem_close. */ 845a8e11d1cSDaniel Vetter if (obj->handle_count == 0) { 846a8e11d1cSDaniel Vetter ret = -ENOENT; 847a8e11d1cSDaniel Vetter goto err; 848a8e11d1cSDaniel Vetter } 849a8e11d1cSDaniel Vetter 8508d59bae5SChris Wilson if (!obj->name) { 8510f646425SChris Wilson ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL); 8522e928815STejun Heo if (ret < 0) 8533e49c4f4SChris Wilson goto err; 8542e07fb22SYoungJun Cho 8552e07fb22SYoungJun Cho obj->name = ret; 8568d59bae5SChris Wilson } 8573e49c4f4SChris Wilson 8582e07fb22SYoungJun Cho args->name = (uint64_t) obj->name; 8592e07fb22SYoungJun Cho ret = 0; 8602e07fb22SYoungJun Cho 8613e49c4f4SChris Wilson err: 862cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock); 863*be6ee102SEmil Velikov drm_gem_object_put(obj); 8643e49c4f4SChris Wilson return ret; 865673a394bSEric Anholt } 866673a394bSEric Anholt 867673a394bSEric Anholt /** 86889d61fc0SDaniel Vetter * drm_gem_open - implementation of the GEM_OPEN ioctl 86989d61fc0SDaniel Vetter * @dev: drm_device 87089d61fc0SDaniel Vetter * @data: ioctl data 87189d61fc0SDaniel Vetter * @file_priv: drm file-private structure 87289d61fc0SDaniel Vetter * 873673a394bSEric Anholt * Open an object using the global name, returning a handle and the size. 874673a394bSEric Anholt * 875673a394bSEric Anholt * This handle (of course) holds a reference to the object, so the object 876673a394bSEric Anholt * will not go away until the handle is deleted. 877673a394bSEric Anholt */ 878673a394bSEric Anholt int 879673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data, 880673a394bSEric Anholt struct drm_file *file_priv) 881673a394bSEric Anholt { 882673a394bSEric Anholt struct drm_gem_open *args = data; 883673a394bSEric Anholt struct drm_gem_object *obj; 884673a394bSEric Anholt int ret; 885a1a2d1d3SPekka Paalanen u32 handle; 886673a394bSEric Anholt 8871bcecfacSAndrzej Hajda if (!drm_core_check_feature(dev, DRIVER_GEM)) 88869fdf420SChris Wilson return -EOPNOTSUPP; 889673a394bSEric Anholt 890cd4f013fSDaniel Vetter mutex_lock(&dev->object_name_lock); 891673a394bSEric Anholt obj = idr_find(&dev->object_name_idr, (int) args->name); 89220228c44SDaniel Vetter if (obj) { 893e6b62714SThierry Reding drm_gem_object_get(obj); 89420228c44SDaniel Vetter } else { 895cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock); 896673a394bSEric Anholt return -ENOENT; 89720228c44SDaniel Vetter } 898673a394bSEric Anholt 89920228c44SDaniel Vetter /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */ 90020228c44SDaniel Vetter ret = drm_gem_handle_create_tail(file_priv, obj, &handle); 901*be6ee102SEmil Velikov drm_gem_object_put(obj); 902673a394bSEric Anholt if (ret) 903673a394bSEric Anholt return ret; 904673a394bSEric Anholt 905673a394bSEric Anholt args->handle = handle; 906673a394bSEric Anholt args->size = obj->size; 907673a394bSEric Anholt 908673a394bSEric Anholt return 0; 909673a394bSEric Anholt } 910673a394bSEric Anholt 911673a394bSEric Anholt /** 91289d61fc0SDaniel Vetter * gem_gem_open - initalizes GEM file-private structures at devnode open time 91389d61fc0SDaniel Vetter * @dev: drm_device which is being opened by userspace 91489d61fc0SDaniel Vetter * @file_private: drm file-private structure to set up 91589d61fc0SDaniel Vetter * 916673a394bSEric Anholt * Called at device open time, sets up the structure for handling refcounting 917673a394bSEric Anholt * of mm objects. 918673a394bSEric Anholt */ 919673a394bSEric Anholt void 920673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 921673a394bSEric Anholt { 922e86584c5SChris Wilson idr_init_base(&file_private->object_idr, 1); 923673a394bSEric Anholt spin_lock_init(&file_private->table_lock); 924673a394bSEric Anholt } 925673a394bSEric Anholt 926673a394bSEric Anholt /** 92789d61fc0SDaniel Vetter * drm_gem_release - release file-private GEM resources 92889d61fc0SDaniel Vetter * @dev: drm_device which is being closed by userspace 92989d61fc0SDaniel Vetter * @file_private: drm file-private structure to clean up 93089d61fc0SDaniel Vetter * 931673a394bSEric Anholt * Called at close time when the filp is going away. 932673a394bSEric Anholt * 933673a394bSEric Anholt * Releases any remaining references on objects by this filp. 934673a394bSEric Anholt */ 935673a394bSEric Anholt void 936673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 937673a394bSEric Anholt { 938673a394bSEric Anholt idr_for_each(&file_private->object_idr, 939304eda32SBen Skeggs &drm_gem_object_release_handle, file_private); 940673a394bSEric Anholt idr_destroy(&file_private->object_idr); 941673a394bSEric Anholt } 942673a394bSEric Anholt 943f74418a4SDaniel Vetter /** 944f74418a4SDaniel Vetter * drm_gem_object_release - release GEM buffer object resources 945f74418a4SDaniel Vetter * @obj: GEM buffer object 946f74418a4SDaniel Vetter * 947f74418a4SDaniel Vetter * This releases any structures and resources used by @obj and is the invers of 948f74418a4SDaniel Vetter * drm_gem_object_init(). 949f74418a4SDaniel Vetter */ 950fd632aa3SDaniel Vetter void 951fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj) 952c3ae90c0SLuca Barbieri { 953319c933cSDaniel Vetter WARN_ON(obj->dma_buf); 954319c933cSDaniel Vetter 95562cb7011SAlan Cox if (obj->filp) 956c3ae90c0SLuca Barbieri fput(obj->filp); 95777472347SDavid Herrmann 95852791eeeSChristian König dma_resv_fini(&obj->_resv); 95977472347SDavid Herrmann drm_gem_free_mmap_offset(obj); 960c3ae90c0SLuca Barbieri } 961fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release); 962c3ae90c0SLuca Barbieri 963673a394bSEric Anholt /** 96489d61fc0SDaniel Vetter * drm_gem_object_free - free a GEM object 96589d61fc0SDaniel Vetter * @kref: kref of the object to free 96689d61fc0SDaniel Vetter * 967673a394bSEric Anholt * Called after the last reference to the object has been lost. 968673a394bSEric Anholt * 969673a394bSEric Anholt * Frees the object 970673a394bSEric Anholt */ 971673a394bSEric Anholt void 972673a394bSEric Anholt drm_gem_object_free(struct kref *kref) 973673a394bSEric Anholt { 9746ff774bdSDaniel Vetter struct drm_gem_object *obj = 9756ff774bdSDaniel Vetter container_of(kref, struct drm_gem_object, refcount); 976673a394bSEric Anholt struct drm_device *dev = obj->dev; 977673a394bSEric Anholt 9781a9458aeSEmil Velikov if (obj->funcs) 979b39b5394SNoralf Trønnes obj->funcs->free(obj); 9801a9458aeSEmil Velikov else if (dev->driver->gem_free_object_unlocked) 9816d3e7fddSDaniel Vetter dev->driver->gem_free_object_unlocked(obj); 9826d3e7fddSDaniel Vetter } 983673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free); 984673a394bSEric Anholt 985df2e0900SDaniel Vetter /** 986eecd7fd8SEmil Velikov * drm_gem_object_put_locked - release a GEM buffer object reference 9879f0ba539SDaniel Vetter * @obj: GEM buffer object 9889f0ba539SDaniel Vetter * 989940eba2dSDaniel Vetter * This releases a reference to @obj. Callers must hold the 990940eba2dSDaniel Vetter * &drm_device.struct_mutex lock when calling this function, even when the 991940eba2dSDaniel Vetter * driver doesn't use &drm_device.struct_mutex for anything. 9929f0ba539SDaniel Vetter * 9939f0ba539SDaniel Vetter * For drivers not encumbered with legacy locking use 994*be6ee102SEmil Velikov * drm_gem_object_put() instead. 9959f0ba539SDaniel Vetter */ 9969f0ba539SDaniel Vetter void 997eecd7fd8SEmil Velikov drm_gem_object_put_locked(struct drm_gem_object *obj) 9989f0ba539SDaniel Vetter { 9999f0ba539SDaniel Vetter if (obj) { 10009f0ba539SDaniel Vetter WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); 10019f0ba539SDaniel Vetter 10029f0ba539SDaniel Vetter kref_put(&obj->refcount, drm_gem_object_free); 10039f0ba539SDaniel Vetter } 10049f0ba539SDaniel Vetter } 1005eecd7fd8SEmil Velikov EXPORT_SYMBOL(drm_gem_object_put_locked); 10069f0ba539SDaniel Vetter 10079f0ba539SDaniel Vetter /** 1008df2e0900SDaniel Vetter * drm_gem_vm_open - vma->ops->open implementation for GEM 1009df2e0900SDaniel Vetter * @vma: VM area structure 1010df2e0900SDaniel Vetter * 1011df2e0900SDaniel Vetter * This function implements the #vm_operations_struct open() callback for GEM 1012df2e0900SDaniel Vetter * drivers. This must be used together with drm_gem_vm_close(). 1013df2e0900SDaniel Vetter */ 1014ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma) 1015ab00b3e5SJesse Barnes { 1016ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 1017ab00b3e5SJesse Barnes 1018e6b62714SThierry Reding drm_gem_object_get(obj); 1019ab00b3e5SJesse Barnes } 1020ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open); 1021ab00b3e5SJesse Barnes 1022df2e0900SDaniel Vetter /** 1023df2e0900SDaniel Vetter * drm_gem_vm_close - vma->ops->close implementation for GEM 1024df2e0900SDaniel Vetter * @vma: VM area structure 1025df2e0900SDaniel Vetter * 1026df2e0900SDaniel Vetter * This function implements the #vm_operations_struct close() callback for GEM 1027df2e0900SDaniel Vetter * drivers. This must be used together with drm_gem_vm_open(). 1028df2e0900SDaniel Vetter */ 1029ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma) 1030ab00b3e5SJesse Barnes { 1031ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 1032ab00b3e5SJesse Barnes 1033*be6ee102SEmil Velikov drm_gem_object_put(obj); 1034ab00b3e5SJesse Barnes } 1035ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close); 1036ab00b3e5SJesse Barnes 10371c5aafa6SLaurent Pinchart /** 10381c5aafa6SLaurent Pinchart * drm_gem_mmap_obj - memory map a GEM object 10391c5aafa6SLaurent Pinchart * @obj: the GEM object to map 10401c5aafa6SLaurent Pinchart * @obj_size: the object size to be mapped, in bytes 10411c5aafa6SLaurent Pinchart * @vma: VMA for the area to be mapped 10421c5aafa6SLaurent Pinchart * 10431c5aafa6SLaurent Pinchart * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops 10441c5aafa6SLaurent Pinchart * provided by the driver. Depending on their requirements, drivers can either 10451c5aafa6SLaurent Pinchart * provide a fault handler in their gem_vm_ops (in which case any accesses to 10461c5aafa6SLaurent Pinchart * the object will be trapped, to perform migration, GTT binding, surface 10471c5aafa6SLaurent Pinchart * register allocation, or performance monitoring), or mmap the buffer memory 10481c5aafa6SLaurent Pinchart * synchronously after calling drm_gem_mmap_obj. 10491c5aafa6SLaurent Pinchart * 10501c5aafa6SLaurent Pinchart * This function is mainly intended to implement the DMABUF mmap operation, when 10511c5aafa6SLaurent Pinchart * the GEM object is not looked up based on its fake offset. To implement the 10521c5aafa6SLaurent Pinchart * DRM mmap operation, drivers should use the drm_gem_mmap() function. 10531c5aafa6SLaurent Pinchart * 1054ca481c9bSDavid Herrmann * drm_gem_mmap_obj() assumes the user is granted access to the buffer while 1055ca481c9bSDavid Herrmann * drm_gem_mmap() prevents unprivileged users from mapping random objects. So 1056ca481c9bSDavid Herrmann * callers must verify access restrictions before calling this helper. 1057ca481c9bSDavid Herrmann * 10581c5aafa6SLaurent Pinchart * Return 0 or success or -EINVAL if the object size is smaller than the VMA 10591c5aafa6SLaurent Pinchart * size, or if no gem_vm_ops are provided. 10601c5aafa6SLaurent Pinchart */ 10611c5aafa6SLaurent Pinchart int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size, 10621c5aafa6SLaurent Pinchart struct vm_area_struct *vma) 10631c5aafa6SLaurent Pinchart { 10641c5aafa6SLaurent Pinchart struct drm_device *dev = obj->dev; 1065c40069cbSGerd Hoffmann int ret; 10661c5aafa6SLaurent Pinchart 10671c5aafa6SLaurent Pinchart /* Check for valid size. */ 10681c5aafa6SLaurent Pinchart if (obj_size < vma->vm_end - vma->vm_start) 10691c5aafa6SLaurent Pinchart return -EINVAL; 10701c5aafa6SLaurent Pinchart 10711c5aafa6SLaurent Pinchart /* Take a ref for this mapping of the object, so that the fault 10721c5aafa6SLaurent Pinchart * handler can dereference the mmap offset's pointer to the object. 10731c5aafa6SLaurent Pinchart * This reference is cleaned up by the corresponding vm_close 10741c5aafa6SLaurent Pinchart * (which should happen whether the vma was created by this call, or 10751c5aafa6SLaurent Pinchart * by a vm_open due to mremap or partial unmap or whatever). 10761c5aafa6SLaurent Pinchart */ 1077e6b62714SThierry Reding drm_gem_object_get(obj); 10781c5aafa6SLaurent Pinchart 10799786b65bSGerd Hoffmann if (obj->funcs && obj->funcs->mmap) { 10809786b65bSGerd Hoffmann ret = obj->funcs->mmap(obj, vma); 10819786b65bSGerd Hoffmann if (ret) { 1082*be6ee102SEmil Velikov drm_gem_object_put(obj); 10839786b65bSGerd Hoffmann return ret; 10849786b65bSGerd Hoffmann } 10859786b65bSGerd Hoffmann WARN_ON(!(vma->vm_flags & VM_DONTEXPAND)); 10869786b65bSGerd Hoffmann } else { 10879786b65bSGerd Hoffmann if (obj->funcs && obj->funcs->vm_ops) 10889786b65bSGerd Hoffmann vma->vm_ops = obj->funcs->vm_ops; 10899786b65bSGerd Hoffmann else if (dev->driver->gem_vm_ops) 10909786b65bSGerd Hoffmann vma->vm_ops = dev->driver->gem_vm_ops; 10919786b65bSGerd Hoffmann else { 1092*be6ee102SEmil Velikov drm_gem_object_put(obj); 10939786b65bSGerd Hoffmann return -EINVAL; 10949786b65bSGerd Hoffmann } 10959786b65bSGerd Hoffmann 10969786b65bSGerd Hoffmann vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 10979786b65bSGerd Hoffmann vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 10989786b65bSGerd Hoffmann vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); 10999786b65bSGerd Hoffmann } 11009786b65bSGerd Hoffmann 11019786b65bSGerd Hoffmann vma->vm_private_data = obj; 11029786b65bSGerd Hoffmann 11031c5aafa6SLaurent Pinchart return 0; 11041c5aafa6SLaurent Pinchart } 11051c5aafa6SLaurent Pinchart EXPORT_SYMBOL(drm_gem_mmap_obj); 1106ab00b3e5SJesse Barnes 1107a2c0a97bSJesse Barnes /** 1108a2c0a97bSJesse Barnes * drm_gem_mmap - memory map routine for GEM objects 1109a2c0a97bSJesse Barnes * @filp: DRM file pointer 1110a2c0a97bSJesse Barnes * @vma: VMA for the area to be mapped 1111a2c0a97bSJesse Barnes * 1112a2c0a97bSJesse Barnes * If a driver supports GEM object mapping, mmap calls on the DRM file 1113a2c0a97bSJesse Barnes * descriptor will end up here. 1114a2c0a97bSJesse Barnes * 11151c5aafa6SLaurent Pinchart * Look up the GEM object based on the offset passed in (vma->vm_pgoff will 1116a2c0a97bSJesse Barnes * contain the fake offset we created when the GTT map ioctl was called on 11171c5aafa6SLaurent Pinchart * the object) and map it with a call to drm_gem_mmap_obj(). 1118ca481c9bSDavid Herrmann * 1119ca481c9bSDavid Herrmann * If the caller is not granted access to the buffer object, the mmap will fail 1120ca481c9bSDavid Herrmann * with EACCES. Please see the vma manager for more information. 1121a2c0a97bSJesse Barnes */ 1122a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 1123a2c0a97bSJesse Barnes { 1124a2c0a97bSJesse Barnes struct drm_file *priv = filp->private_data; 1125a2c0a97bSJesse Barnes struct drm_device *dev = priv->minor->dev; 11262225cfe4SDaniel Vetter struct drm_gem_object *obj = NULL; 11270de23977SDavid Herrmann struct drm_vma_offset_node *node; 1128a8469aa8SDavid Herrmann int ret; 1129a2c0a97bSJesse Barnes 1130c07dcd61SDaniel Vetter if (drm_dev_is_unplugged(dev)) 11312c07a21dSDave Airlie return -ENODEV; 11322c07a21dSDave Airlie 11332225cfe4SDaniel Vetter drm_vma_offset_lock_lookup(dev->vma_offset_manager); 11342225cfe4SDaniel Vetter node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager, 1135b04a5906SDaniel Vetter vma->vm_pgoff, 11360de23977SDavid Herrmann vma_pages(vma)); 11372225cfe4SDaniel Vetter if (likely(node)) { 11382225cfe4SDaniel Vetter obj = container_of(node, struct drm_gem_object, vma_node); 11392225cfe4SDaniel Vetter /* 11402225cfe4SDaniel Vetter * When the object is being freed, after it hits 0-refcnt it 11412225cfe4SDaniel Vetter * proceeds to tear down the object. In the process it will 11422225cfe4SDaniel Vetter * attempt to remove the VMA offset and so acquire this 11432225cfe4SDaniel Vetter * mgr->vm_lock. Therefore if we find an object with a 0-refcnt 11442225cfe4SDaniel Vetter * that matches our range, we know it is in the process of being 11452225cfe4SDaniel Vetter * destroyed and will be freed as soon as we release the lock - 11462225cfe4SDaniel Vetter * so we have to check for the 0-refcnted object and treat it as 11472225cfe4SDaniel Vetter * invalid. 11482225cfe4SDaniel Vetter */ 11492225cfe4SDaniel Vetter if (!kref_get_unless_zero(&obj->refcount)) 11502225cfe4SDaniel Vetter obj = NULL; 11512225cfe4SDaniel Vetter } 11522225cfe4SDaniel Vetter drm_vma_offset_unlock_lookup(dev->vma_offset_manager); 11532225cfe4SDaniel Vetter 11542225cfe4SDaniel Vetter if (!obj) 1155197633b9SDaniel Vetter return -EINVAL; 11562225cfe4SDaniel Vetter 1157d9a1f0b4SDavid Herrmann if (!drm_vma_node_is_allowed(node, priv)) { 1158*be6ee102SEmil Velikov drm_gem_object_put(obj); 1159ca481c9bSDavid Herrmann return -EACCES; 1160a2c0a97bSJesse Barnes } 1161a2c0a97bSJesse Barnes 11623e977ac6SChris Wilson if (node->readonly) { 11633e977ac6SChris Wilson if (vma->vm_flags & VM_WRITE) { 1164*be6ee102SEmil Velikov drm_gem_object_put(obj); 11653e977ac6SChris Wilson return -EINVAL; 11663e977ac6SChris Wilson } 11673e977ac6SChris Wilson 11683e977ac6SChris Wilson vma->vm_flags &= ~VM_MAYWRITE; 11693e977ac6SChris Wilson } 11703e977ac6SChris Wilson 11712225cfe4SDaniel Vetter ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, 11722225cfe4SDaniel Vetter vma); 1173a2c0a97bSJesse Barnes 1174*be6ee102SEmil Velikov drm_gem_object_put(obj); 1175a2c0a97bSJesse Barnes 1176a2c0a97bSJesse Barnes return ret; 1177a2c0a97bSJesse Barnes } 1178a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap); 117945d58b40SNoralf Trønnes 118045d58b40SNoralf Trønnes void drm_gem_print_info(struct drm_printer *p, unsigned int indent, 118145d58b40SNoralf Trønnes const struct drm_gem_object *obj) 118245d58b40SNoralf Trønnes { 118345d58b40SNoralf Trønnes drm_printf_indent(p, indent, "name=%d\n", obj->name); 118445d58b40SNoralf Trønnes drm_printf_indent(p, indent, "refcount=%u\n", 118545d58b40SNoralf Trønnes kref_read(&obj->refcount)); 118645d58b40SNoralf Trønnes drm_printf_indent(p, indent, "start=%08lx\n", 118745d58b40SNoralf Trønnes drm_vma_node_start(&obj->vma_node)); 118845d58b40SNoralf Trønnes drm_printf_indent(p, indent, "size=%zu\n", obj->size); 118945d58b40SNoralf Trønnes drm_printf_indent(p, indent, "imported=%s\n", 119045d58b40SNoralf Trønnes obj->import_attach ? "yes" : "no"); 119145d58b40SNoralf Trønnes 1192b39b5394SNoralf Trønnes if (obj->funcs && obj->funcs->print_info) 1193b39b5394SNoralf Trønnes obj->funcs->print_info(p, indent, obj); 1194b39b5394SNoralf Trønnes else if (obj->dev->driver->gem_print_info) 119545d58b40SNoralf Trønnes obj->dev->driver->gem_print_info(p, indent, obj); 119645d58b40SNoralf Trønnes } 1197b39b5394SNoralf Trønnes 1198b39b5394SNoralf Trønnes int drm_gem_pin(struct drm_gem_object *obj) 1199b39b5394SNoralf Trønnes { 1200b39b5394SNoralf Trønnes if (obj->funcs && obj->funcs->pin) 1201b39b5394SNoralf Trønnes return obj->funcs->pin(obj); 1202b39b5394SNoralf Trønnes else if (obj->dev->driver->gem_prime_pin) 1203b39b5394SNoralf Trønnes return obj->dev->driver->gem_prime_pin(obj); 1204b39b5394SNoralf Trønnes else 1205b39b5394SNoralf Trønnes return 0; 1206b39b5394SNoralf Trønnes } 1207b39b5394SNoralf Trønnes 1208b39b5394SNoralf Trønnes void drm_gem_unpin(struct drm_gem_object *obj) 1209b39b5394SNoralf Trønnes { 1210b39b5394SNoralf Trønnes if (obj->funcs && obj->funcs->unpin) 1211b39b5394SNoralf Trønnes obj->funcs->unpin(obj); 1212b39b5394SNoralf Trønnes else if (obj->dev->driver->gem_prime_unpin) 1213b39b5394SNoralf Trønnes obj->dev->driver->gem_prime_unpin(obj); 1214b39b5394SNoralf Trønnes } 1215b39b5394SNoralf Trønnes 1216b39b5394SNoralf Trønnes void *drm_gem_vmap(struct drm_gem_object *obj) 1217b39b5394SNoralf Trønnes { 1218b39b5394SNoralf Trønnes void *vaddr; 1219b39b5394SNoralf Trønnes 1220b39b5394SNoralf Trønnes if (obj->funcs && obj->funcs->vmap) 1221b39b5394SNoralf Trønnes vaddr = obj->funcs->vmap(obj); 1222b39b5394SNoralf Trønnes else if (obj->dev->driver->gem_prime_vmap) 1223b39b5394SNoralf Trønnes vaddr = obj->dev->driver->gem_prime_vmap(obj); 1224b39b5394SNoralf Trønnes else 1225b39b5394SNoralf Trønnes vaddr = ERR_PTR(-EOPNOTSUPP); 1226b39b5394SNoralf Trønnes 1227b39b5394SNoralf Trønnes if (!vaddr) 1228b39b5394SNoralf Trønnes vaddr = ERR_PTR(-ENOMEM); 1229b39b5394SNoralf Trønnes 1230b39b5394SNoralf Trønnes return vaddr; 1231b39b5394SNoralf Trønnes } 1232b39b5394SNoralf Trønnes 1233b39b5394SNoralf Trønnes void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr) 1234b39b5394SNoralf Trønnes { 1235b39b5394SNoralf Trønnes if (!vaddr) 1236b39b5394SNoralf Trønnes return; 1237b39b5394SNoralf Trønnes 1238b39b5394SNoralf Trønnes if (obj->funcs && obj->funcs->vunmap) 1239b39b5394SNoralf Trønnes obj->funcs->vunmap(obj, vaddr); 1240b39b5394SNoralf Trønnes else if (obj->dev->driver->gem_prime_vunmap) 1241b39b5394SNoralf Trønnes obj->dev->driver->gem_prime_vunmap(obj, vaddr); 1242b39b5394SNoralf Trønnes } 12437edc3e3bSEric Anholt 12447edc3e3bSEric Anholt /** 12457edc3e3bSEric Anholt * drm_gem_lock_reservations - Sets up the ww context and acquires 12467edc3e3bSEric Anholt * the lock on an array of GEM objects. 12477edc3e3bSEric Anholt * 12487edc3e3bSEric Anholt * Once you've locked your reservations, you'll want to set up space 12497edc3e3bSEric Anholt * for your shared fences (if applicable), submit your job, then 12507edc3e3bSEric Anholt * drm_gem_unlock_reservations(). 12517edc3e3bSEric Anholt * 12527edc3e3bSEric Anholt * @objs: drm_gem_objects to lock 12537edc3e3bSEric Anholt * @count: Number of objects in @objs 12547edc3e3bSEric Anholt * @acquire_ctx: struct ww_acquire_ctx that will be initialized as 12557edc3e3bSEric Anholt * part of tracking this set of locked reservations. 12567edc3e3bSEric Anholt */ 12577edc3e3bSEric Anholt int 12587edc3e3bSEric Anholt drm_gem_lock_reservations(struct drm_gem_object **objs, int count, 12597edc3e3bSEric Anholt struct ww_acquire_ctx *acquire_ctx) 12607edc3e3bSEric Anholt { 12617edc3e3bSEric Anholt int contended = -1; 12627edc3e3bSEric Anholt int i, ret; 12637edc3e3bSEric Anholt 12647edc3e3bSEric Anholt ww_acquire_init(acquire_ctx, &reservation_ww_class); 12657edc3e3bSEric Anholt 12667edc3e3bSEric Anholt retry: 12677edc3e3bSEric Anholt if (contended != -1) { 12687edc3e3bSEric Anholt struct drm_gem_object *obj = objs[contended]; 12697edc3e3bSEric Anholt 127052791eeeSChristian König ret = dma_resv_lock_slow_interruptible(obj->resv, 12717edc3e3bSEric Anholt acquire_ctx); 12727edc3e3bSEric Anholt if (ret) { 12737edc3e3bSEric Anholt ww_acquire_done(acquire_ctx); 12747edc3e3bSEric Anholt return ret; 12757edc3e3bSEric Anholt } 12767edc3e3bSEric Anholt } 12777edc3e3bSEric Anholt 12787edc3e3bSEric Anholt for (i = 0; i < count; i++) { 12797edc3e3bSEric Anholt if (i == contended) 12807edc3e3bSEric Anholt continue; 12817edc3e3bSEric Anholt 128252791eeeSChristian König ret = dma_resv_lock_interruptible(objs[i]->resv, 12837edc3e3bSEric Anholt acquire_ctx); 12847edc3e3bSEric Anholt if (ret) { 12857edc3e3bSEric Anholt int j; 12867edc3e3bSEric Anholt 12877edc3e3bSEric Anholt for (j = 0; j < i; j++) 128852791eeeSChristian König dma_resv_unlock(objs[j]->resv); 12897edc3e3bSEric Anholt 12907edc3e3bSEric Anholt if (contended != -1 && contended >= i) 129152791eeeSChristian König dma_resv_unlock(objs[contended]->resv); 12927edc3e3bSEric Anholt 12937edc3e3bSEric Anholt if (ret == -EDEADLK) { 12947edc3e3bSEric Anholt contended = i; 12957edc3e3bSEric Anholt goto retry; 12967edc3e3bSEric Anholt } 12977edc3e3bSEric Anholt 12987edc3e3bSEric Anholt ww_acquire_done(acquire_ctx); 12997edc3e3bSEric Anholt return ret; 13007edc3e3bSEric Anholt } 13017edc3e3bSEric Anholt } 13027edc3e3bSEric Anholt 13037edc3e3bSEric Anholt ww_acquire_done(acquire_ctx); 13047edc3e3bSEric Anholt 13057edc3e3bSEric Anholt return 0; 13067edc3e3bSEric Anholt } 13077edc3e3bSEric Anholt EXPORT_SYMBOL(drm_gem_lock_reservations); 13087edc3e3bSEric Anholt 13097edc3e3bSEric Anholt void 13107edc3e3bSEric Anholt drm_gem_unlock_reservations(struct drm_gem_object **objs, int count, 13117edc3e3bSEric Anholt struct ww_acquire_ctx *acquire_ctx) 13127edc3e3bSEric Anholt { 13137edc3e3bSEric Anholt int i; 13147edc3e3bSEric Anholt 13157edc3e3bSEric Anholt for (i = 0; i < count; i++) 131652791eeeSChristian König dma_resv_unlock(objs[i]->resv); 13177edc3e3bSEric Anholt 13187edc3e3bSEric Anholt ww_acquire_fini(acquire_ctx); 13197edc3e3bSEric Anholt } 13207edc3e3bSEric Anholt EXPORT_SYMBOL(drm_gem_unlock_reservations); 13215d5a179dSEric Anholt 13225d5a179dSEric Anholt /** 13235d5a179dSEric Anholt * drm_gem_fence_array_add - Adds the fence to an array of fences to be 13245d5a179dSEric Anholt * waited on, deduplicating fences from the same context. 13255d5a179dSEric Anholt * 1326761e473fSSean Paul * @fence_array: array of dma_fence * for the job to block on. 1327761e473fSSean Paul * @fence: the dma_fence to add to the list of dependencies. 13285d5a179dSEric Anholt * 13295d5a179dSEric Anholt * Returns: 13305d5a179dSEric Anholt * 0 on success, or an error on failing to expand the array. 13315d5a179dSEric Anholt */ 13325d5a179dSEric Anholt int drm_gem_fence_array_add(struct xarray *fence_array, 13335d5a179dSEric Anholt struct dma_fence *fence) 13345d5a179dSEric Anholt { 13355d5a179dSEric Anholt struct dma_fence *entry; 13365d5a179dSEric Anholt unsigned long index; 13375d5a179dSEric Anholt u32 id = 0; 13385d5a179dSEric Anholt int ret; 13395d5a179dSEric Anholt 13405d5a179dSEric Anholt if (!fence) 13415d5a179dSEric Anholt return 0; 13425d5a179dSEric Anholt 13435d5a179dSEric Anholt /* Deduplicate if we already depend on a fence from the same context. 13445d5a179dSEric Anholt * This lets the size of the array of deps scale with the number of 13455d5a179dSEric Anholt * engines involved, rather than the number of BOs. 13465d5a179dSEric Anholt */ 13475d5a179dSEric Anholt xa_for_each(fence_array, index, entry) { 13485d5a179dSEric Anholt if (entry->context != fence->context) 13495d5a179dSEric Anholt continue; 13505d5a179dSEric Anholt 13515d5a179dSEric Anholt if (dma_fence_is_later(fence, entry)) { 13525d5a179dSEric Anholt dma_fence_put(entry); 13535d5a179dSEric Anholt xa_store(fence_array, index, fence, GFP_KERNEL); 13545d5a179dSEric Anholt } else { 13555d5a179dSEric Anholt dma_fence_put(fence); 13565d5a179dSEric Anholt } 13575d5a179dSEric Anholt return 0; 13585d5a179dSEric Anholt } 13595d5a179dSEric Anholt 13605d5a179dSEric Anholt ret = xa_alloc(fence_array, &id, fence, xa_limit_32b, GFP_KERNEL); 13615d5a179dSEric Anholt if (ret != 0) 13625d5a179dSEric Anholt dma_fence_put(fence); 13635d5a179dSEric Anholt 13645d5a179dSEric Anholt return ret; 13655d5a179dSEric Anholt } 13665d5a179dSEric Anholt EXPORT_SYMBOL(drm_gem_fence_array_add); 13675d5a179dSEric Anholt 13685d5a179dSEric Anholt /** 13695d5a179dSEric Anholt * drm_gem_fence_array_add_implicit - Adds the implicit dependencies tracked 13705d5a179dSEric Anholt * in the GEM object's reservation object to an array of dma_fences for use in 13715d5a179dSEric Anholt * scheduling a rendering job. 13725d5a179dSEric Anholt * 13735d5a179dSEric Anholt * This should be called after drm_gem_lock_reservations() on your array of 13745d5a179dSEric Anholt * GEM objects used in the job but before updating the reservations with your 13755d5a179dSEric Anholt * own fences. 13765d5a179dSEric Anholt * 1377761e473fSSean Paul * @fence_array: array of dma_fence * for the job to block on. 1378761e473fSSean Paul * @obj: the gem object to add new dependencies from. 1379761e473fSSean Paul * @write: whether the job might write the object (so we need to depend on 13805d5a179dSEric Anholt * shared fences in the reservation object). 13815d5a179dSEric Anholt */ 13825d5a179dSEric Anholt int drm_gem_fence_array_add_implicit(struct xarray *fence_array, 13835d5a179dSEric Anholt struct drm_gem_object *obj, 13845d5a179dSEric Anholt bool write) 13855d5a179dSEric Anholt { 13865d5a179dSEric Anholt int ret; 13875d5a179dSEric Anholt struct dma_fence **fences; 13885d5a179dSEric Anholt unsigned int i, fence_count; 13895d5a179dSEric Anholt 13905d5a179dSEric Anholt if (!write) { 13915d5a179dSEric Anholt struct dma_fence *fence = 139252791eeeSChristian König dma_resv_get_excl_rcu(obj->resv); 13935d5a179dSEric Anholt 13945d5a179dSEric Anholt return drm_gem_fence_array_add(fence_array, fence); 13955d5a179dSEric Anholt } 13965d5a179dSEric Anholt 139752791eeeSChristian König ret = dma_resv_get_fences_rcu(obj->resv, NULL, 13985d5a179dSEric Anholt &fence_count, &fences); 13995d5a179dSEric Anholt if (ret || !fence_count) 14005d5a179dSEric Anholt return ret; 14015d5a179dSEric Anholt 14025d5a179dSEric Anholt for (i = 0; i < fence_count; i++) { 14035d5a179dSEric Anholt ret = drm_gem_fence_array_add(fence_array, fences[i]); 14045d5a179dSEric Anholt if (ret) 14055d5a179dSEric Anholt break; 14065d5a179dSEric Anholt } 14075d5a179dSEric Anholt 14085d5a179dSEric Anholt for (; i < fence_count; i++) 14095d5a179dSEric Anholt dma_fence_put(fences[i]); 14105d5a179dSEric Anholt kfree(fences); 14115d5a179dSEric Anholt return ret; 14125d5a179dSEric Anholt } 14135d5a179dSEric Anholt EXPORT_SYMBOL(drm_gem_fence_array_add_implicit); 1414