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> 40673a394bSEric Anholt 41673a394bSEric Anholt /** @file drm_gem.c 42673a394bSEric Anholt * 43673a394bSEric Anholt * This file provides some of the base ioctls and library routines for 44673a394bSEric Anholt * the graphics memory manager implemented by each device driver. 45673a394bSEric Anholt * 46673a394bSEric Anholt * Because various devices have different requirements in terms of 47673a394bSEric Anholt * synchronization and migration strategies, implementing that is left up to 48673a394bSEric Anholt * the driver, and all that the general API provides should be generic -- 49673a394bSEric Anholt * allocating objects, reading/writing data with the cpu, freeing objects. 50673a394bSEric Anholt * Even there, platform-dependent optimizations for reading/writing data with 51673a394bSEric Anholt * the CPU mean we'll likely hook those out to driver-specific calls. However, 52673a394bSEric Anholt * the DRI2 implementation wants to have at least allocate/mmap be generic. 53673a394bSEric Anholt * 54673a394bSEric Anholt * The goal was to have swap-backed object allocation managed through 55673a394bSEric Anholt * struct file. However, file descriptors as handles to a struct file have 56673a394bSEric Anholt * two major failings: 57673a394bSEric Anholt * - Process limits prevent more than 1024 or so being used at a time by 58673a394bSEric Anholt * default. 59673a394bSEric Anholt * - Inability to allocate high fds will aggravate the X Server's select() 60673a394bSEric Anholt * handling, and likely that of many GL client applications as well. 61673a394bSEric Anholt * 62673a394bSEric Anholt * This led to a plan of using our own integer IDs (called handles, following 63673a394bSEric Anholt * DRM terminology) to mimic fds, and implement the fd syscalls we need as 64673a394bSEric Anholt * ioctls. The objects themselves will still include the struct file so 65673a394bSEric Anholt * that we can transition to fds if the required kernel infrastructure shows 66673a394bSEric Anholt * up at a later date, and as our interface with shmfs for memory allocation. 67673a394bSEric Anholt */ 68673a394bSEric Anholt 69a2c0a97bSJesse Barnes /* 70a2c0a97bSJesse Barnes * We make up offsets for buffer objects so we can recognize them at 71a2c0a97bSJesse Barnes * mmap time. 72a2c0a97bSJesse Barnes */ 7305269a3aSJordan Crouse 7405269a3aSJordan Crouse /* pgoff in mmap is an unsigned long, so we need to make sure that 7505269a3aSJordan Crouse * the faked up offset will fit 7605269a3aSJordan Crouse */ 7705269a3aSJordan Crouse 7805269a3aSJordan Crouse #if BITS_PER_LONG == 64 79a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) 80a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16) 8105269a3aSJordan Crouse #else 8205269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1) 8305269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16) 8405269a3aSJordan Crouse #endif 85a2c0a97bSJesse Barnes 86673a394bSEric Anholt /** 87673a394bSEric Anholt * Initialize the GEM device fields 88673a394bSEric Anholt */ 89673a394bSEric Anholt 90673a394bSEric Anholt int 91673a394bSEric Anholt drm_gem_init(struct drm_device *dev) 92673a394bSEric Anholt { 93a2c0a97bSJesse Barnes struct drm_gem_mm *mm; 94a2c0a97bSJesse Barnes 95673a394bSEric Anholt spin_lock_init(&dev->object_name_lock); 96673a394bSEric Anholt idr_init(&dev->object_name_idr); 97a2c0a97bSJesse Barnes 989a298b2aSEric Anholt mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL); 99a2c0a97bSJesse Barnes if (!mm) { 100a2c0a97bSJesse Barnes DRM_ERROR("out of memory\n"); 101a2c0a97bSJesse Barnes return -ENOMEM; 102a2c0a97bSJesse Barnes } 103a2c0a97bSJesse Barnes 104a2c0a97bSJesse Barnes dev->mm_private = mm; 105a2c0a97bSJesse Barnes 1064cb81ac2SChris Wilson if (drm_ht_create(&mm->offset_hash, 12)) { 1079a298b2aSEric Anholt kfree(mm); 108a2c0a97bSJesse Barnes return -ENOMEM; 109a2c0a97bSJesse Barnes } 110a2c0a97bSJesse Barnes 11177ef8bbcSDavid Herrmann drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START, 11277ef8bbcSDavid Herrmann DRM_FILE_PAGE_OFFSET_SIZE); 113a2c0a97bSJesse Barnes 114673a394bSEric Anholt return 0; 115673a394bSEric Anholt } 116673a394bSEric Anholt 117a2c0a97bSJesse Barnes void 118a2c0a97bSJesse Barnes drm_gem_destroy(struct drm_device *dev) 119a2c0a97bSJesse Barnes { 120a2c0a97bSJesse Barnes struct drm_gem_mm *mm = dev->mm_private; 121a2c0a97bSJesse Barnes 122a2c0a97bSJesse Barnes drm_mm_takedown(&mm->offset_manager); 123a2c0a97bSJesse Barnes drm_ht_remove(&mm->offset_hash); 1249a298b2aSEric Anholt kfree(mm); 125a2c0a97bSJesse Barnes dev->mm_private = NULL; 126a2c0a97bSJesse Barnes } 127a2c0a97bSJesse Barnes 128673a394bSEric Anholt /** 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 { 135*89c8233fSDavid Herrmann struct file *filp; 1361d397043SDaniel Vetter 137*89c8233fSDavid Herrmann filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); 138*89c8233fSDavid Herrmann if (IS_ERR(filp)) 139*89c8233fSDavid Herrmann return PTR_ERR(filp); 1401d397043SDaniel Vetter 141*89c8233fSDavid Herrmann drm_gem_private_object_init(dev, obj, size); 142*89c8233fSDavid Herrmann obj->filp = filp; 1431d397043SDaniel Vetter 1441d397043SDaniel Vetter return 0; 1451d397043SDaniel Vetter } 1461d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init); 1471d397043SDaniel Vetter 1481d397043SDaniel Vetter /** 14962cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with 15062cb7011SAlan Cox * no GEM provided backing store. Instead the caller is responsible for 15162cb7011SAlan Cox * backing the object and handling it. 15262cb7011SAlan Cox */ 153*89c8233fSDavid Herrmann void drm_gem_private_object_init(struct drm_device *dev, 15462cb7011SAlan Cox struct drm_gem_object *obj, size_t size) 15562cb7011SAlan Cox { 15662cb7011SAlan Cox BUG_ON((size & (PAGE_SIZE - 1)) != 0); 15762cb7011SAlan Cox 15862cb7011SAlan Cox obj->dev = dev; 15962cb7011SAlan Cox obj->filp = NULL; 16062cb7011SAlan Cox 16162cb7011SAlan Cox kref_init(&obj->refcount); 16262cb7011SAlan Cox atomic_set(&obj->handle_count, 0); 16362cb7011SAlan Cox obj->size = size; 16462cb7011SAlan Cox } 16562cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init); 16662cb7011SAlan Cox 16762cb7011SAlan Cox /** 168673a394bSEric Anholt * Allocate a GEM object of the specified size with shmfs backing store 169673a394bSEric Anholt */ 170673a394bSEric Anholt struct drm_gem_object * 171673a394bSEric Anholt drm_gem_object_alloc(struct drm_device *dev, size_t size) 172673a394bSEric Anholt { 173673a394bSEric Anholt struct drm_gem_object *obj; 174673a394bSEric Anholt 175b798b1feSRobert P. J. Day obj = kzalloc(sizeof(*obj), GFP_KERNEL); 176845792d9SJiri Slaby if (!obj) 177845792d9SJiri Slaby goto free; 178673a394bSEric Anholt 1791d397043SDaniel Vetter if (drm_gem_object_init(dev, obj, size) != 0) 180845792d9SJiri Slaby goto free; 181673a394bSEric Anholt 182673a394bSEric Anholt if (dev->driver->gem_init_object != NULL && 183673a394bSEric Anholt dev->driver->gem_init_object(obj) != 0) { 184845792d9SJiri Slaby goto fput; 185673a394bSEric Anholt } 186673a394bSEric Anholt return obj; 187845792d9SJiri Slaby fput: 1881d397043SDaniel Vetter /* Object_init mangles the global counters - readjust them. */ 189845792d9SJiri Slaby fput(obj->filp); 190845792d9SJiri Slaby free: 191845792d9SJiri Slaby kfree(obj); 192845792d9SJiri Slaby return NULL; 193673a394bSEric Anholt } 194673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_alloc); 195673a394bSEric Anholt 1960ff926c7SDave Airlie static void 1970ff926c7SDave Airlie drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp) 1980ff926c7SDave Airlie { 1990ff926c7SDave Airlie if (obj->import_attach) { 200219b4733SDave Airlie drm_prime_remove_buf_handle(&filp->prime, 2010ff926c7SDave Airlie obj->import_attach->dmabuf); 2020ff926c7SDave Airlie } 2030ff926c7SDave Airlie if (obj->export_dma_buf) { 204219b4733SDave Airlie drm_prime_remove_buf_handle(&filp->prime, 2050ff926c7SDave Airlie obj->export_dma_buf); 2060ff926c7SDave Airlie } 2070ff926c7SDave Airlie } 2080ff926c7SDave Airlie 209673a394bSEric Anholt /** 210673a394bSEric Anholt * Removes the mapping from handle to filp for this object. 211673a394bSEric Anholt */ 212ff72145bSDave Airlie int 213a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle) 214673a394bSEric Anholt { 215673a394bSEric Anholt struct drm_device *dev; 216673a394bSEric Anholt struct drm_gem_object *obj; 217673a394bSEric Anholt 218673a394bSEric Anholt /* This is gross. The idr system doesn't let us try a delete and 219673a394bSEric Anholt * return an error code. It just spews if you fail at deleting. 220673a394bSEric Anholt * So, we have to grab a lock around finding the object and then 221673a394bSEric Anholt * doing the delete on it and dropping the refcount, or the user 222673a394bSEric Anholt * could race us to double-decrement the refcount and cause a 223673a394bSEric Anholt * use-after-free later. Given the frequency of our handle lookups, 224673a394bSEric Anholt * we may want to use ida for number allocation and a hash table 225673a394bSEric Anholt * for the pointers, anyway. 226673a394bSEric Anholt */ 227673a394bSEric Anholt spin_lock(&filp->table_lock); 228673a394bSEric Anholt 229673a394bSEric Anholt /* Check if we currently have a reference on the object */ 230673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 231673a394bSEric Anholt if (obj == NULL) { 232673a394bSEric Anholt spin_unlock(&filp->table_lock); 233673a394bSEric Anholt return -EINVAL; 234673a394bSEric Anholt } 235673a394bSEric Anholt dev = obj->dev; 236673a394bSEric Anholt 237673a394bSEric Anholt /* Release reference and decrement refcount. */ 238673a394bSEric Anholt idr_remove(&filp->object_idr, handle); 239673a394bSEric Anholt spin_unlock(&filp->table_lock); 240673a394bSEric Anholt 2410ff926c7SDave Airlie drm_gem_remove_prime_handles(obj, filp); 2423248877eSDave Airlie 243304eda32SBen Skeggs if (dev->driver->gem_close_object) 244304eda32SBen Skeggs dev->driver->gem_close_object(obj, filp); 245bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 246673a394bSEric Anholt 247673a394bSEric Anholt return 0; 248673a394bSEric Anholt } 249ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete); 250673a394bSEric Anholt 251673a394bSEric Anholt /** 252673a394bSEric Anholt * Create a handle for this object. This adds a handle reference 253673a394bSEric Anholt * to the object, which includes a regular reference count. Callers 254673a394bSEric Anholt * will likely want to dereference the object afterwards. 255673a394bSEric Anholt */ 256673a394bSEric Anholt int 257673a394bSEric Anholt drm_gem_handle_create(struct drm_file *file_priv, 258673a394bSEric Anholt struct drm_gem_object *obj, 259a1a2d1d3SPekka Paalanen u32 *handlep) 260673a394bSEric Anholt { 261304eda32SBen Skeggs struct drm_device *dev = obj->dev; 262673a394bSEric Anholt int ret; 263673a394bSEric Anholt 264673a394bSEric Anholt /* 2652e928815STejun Heo * Get the user-visible handle using idr. Preload and perform 2662e928815STejun Heo * allocation under our spinlock. 267673a394bSEric Anholt */ 2682e928815STejun Heo idr_preload(GFP_KERNEL); 269673a394bSEric Anholt spin_lock(&file_priv->table_lock); 2702e928815STejun Heo 2712e928815STejun Heo ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT); 2722e928815STejun Heo 273673a394bSEric Anholt spin_unlock(&file_priv->table_lock); 2742e928815STejun Heo idr_preload_end(); 2752e928815STejun Heo if (ret < 0) 276673a394bSEric Anholt return ret; 2772e928815STejun Heo *handlep = ret; 278673a394bSEric Anholt 279673a394bSEric Anholt drm_gem_object_handle_reference(obj); 280304eda32SBen Skeggs 281304eda32SBen Skeggs if (dev->driver->gem_open_object) { 282304eda32SBen Skeggs ret = dev->driver->gem_open_object(obj, file_priv); 283304eda32SBen Skeggs if (ret) { 284304eda32SBen Skeggs drm_gem_handle_delete(file_priv, *handlep); 285304eda32SBen Skeggs return ret; 286304eda32SBen Skeggs } 287304eda32SBen Skeggs } 288304eda32SBen Skeggs 289673a394bSEric Anholt return 0; 290673a394bSEric Anholt } 291673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create); 292673a394bSEric Anholt 29375ef8b3bSRob Clark 29475ef8b3bSRob Clark /** 29575ef8b3bSRob Clark * drm_gem_free_mmap_offset - release a fake mmap offset for an object 29675ef8b3bSRob Clark * @obj: obj in question 29775ef8b3bSRob Clark * 29875ef8b3bSRob Clark * This routine frees fake offsets allocated by drm_gem_create_mmap_offset(). 29975ef8b3bSRob Clark */ 30075ef8b3bSRob Clark void 30175ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj) 30275ef8b3bSRob Clark { 30375ef8b3bSRob Clark struct drm_device *dev = obj->dev; 30475ef8b3bSRob Clark struct drm_gem_mm *mm = dev->mm_private; 30575ef8b3bSRob Clark struct drm_map_list *list = &obj->map_list; 30675ef8b3bSRob Clark 30775ef8b3bSRob Clark drm_ht_remove_item(&mm->offset_hash, &list->hash); 30875ef8b3bSRob Clark drm_mm_put_block(list->file_offset_node); 30975ef8b3bSRob Clark kfree(list->map); 31075ef8b3bSRob Clark list->map = NULL; 31175ef8b3bSRob Clark } 31275ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset); 31375ef8b3bSRob Clark 31475ef8b3bSRob Clark /** 31575ef8b3bSRob Clark * drm_gem_create_mmap_offset - create a fake mmap offset for an object 31675ef8b3bSRob Clark * @obj: obj in question 31775ef8b3bSRob Clark * 31875ef8b3bSRob Clark * GEM memory mapping works by handing back to userspace a fake mmap offset 31975ef8b3bSRob Clark * it can use in a subsequent mmap(2) call. The DRM core code then looks 32075ef8b3bSRob Clark * up the object based on the offset and sets up the various memory mapping 32175ef8b3bSRob Clark * structures. 32275ef8b3bSRob Clark * 32375ef8b3bSRob Clark * This routine allocates and attaches a fake offset for @obj. 32475ef8b3bSRob Clark */ 32575ef8b3bSRob Clark int 32675ef8b3bSRob Clark drm_gem_create_mmap_offset(struct drm_gem_object *obj) 32775ef8b3bSRob Clark { 32875ef8b3bSRob Clark struct drm_device *dev = obj->dev; 32975ef8b3bSRob Clark struct drm_gem_mm *mm = dev->mm_private; 33075ef8b3bSRob Clark struct drm_map_list *list; 33175ef8b3bSRob Clark struct drm_local_map *map; 3324a1b0714SLaurent Pinchart int ret; 33375ef8b3bSRob Clark 33475ef8b3bSRob Clark /* Set the object up for mmap'ing */ 33575ef8b3bSRob Clark list = &obj->map_list; 33675ef8b3bSRob Clark list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL); 33775ef8b3bSRob Clark if (!list->map) 33875ef8b3bSRob Clark return -ENOMEM; 33975ef8b3bSRob Clark 34075ef8b3bSRob Clark map = list->map; 34175ef8b3bSRob Clark map->type = _DRM_GEM; 34275ef8b3bSRob Clark map->size = obj->size; 34375ef8b3bSRob Clark map->handle = obj; 34475ef8b3bSRob Clark 34575ef8b3bSRob Clark /* Get a DRM GEM mmap offset allocated... */ 34675ef8b3bSRob Clark list->file_offset_node = drm_mm_search_free(&mm->offset_manager, 3476b9d89b4SChris Wilson obj->size / PAGE_SIZE, 0, false); 34875ef8b3bSRob Clark 34975ef8b3bSRob Clark if (!list->file_offset_node) { 35075ef8b3bSRob Clark DRM_ERROR("failed to allocate offset for bo %d\n", obj->name); 35175ef8b3bSRob Clark ret = -ENOSPC; 35275ef8b3bSRob Clark goto out_free_list; 35375ef8b3bSRob Clark } 35475ef8b3bSRob Clark 35575ef8b3bSRob Clark list->file_offset_node = drm_mm_get_block(list->file_offset_node, 35675ef8b3bSRob Clark obj->size / PAGE_SIZE, 0); 35775ef8b3bSRob Clark if (!list->file_offset_node) { 35875ef8b3bSRob Clark ret = -ENOMEM; 35975ef8b3bSRob Clark goto out_free_list; 36075ef8b3bSRob Clark } 36175ef8b3bSRob Clark 36275ef8b3bSRob Clark list->hash.key = list->file_offset_node->start; 36375ef8b3bSRob Clark ret = drm_ht_insert_item(&mm->offset_hash, &list->hash); 36475ef8b3bSRob Clark if (ret) { 36575ef8b3bSRob Clark DRM_ERROR("failed to add to map hash\n"); 36675ef8b3bSRob Clark goto out_free_mm; 36775ef8b3bSRob Clark } 36875ef8b3bSRob Clark 36975ef8b3bSRob Clark return 0; 37075ef8b3bSRob Clark 37175ef8b3bSRob Clark out_free_mm: 37275ef8b3bSRob Clark drm_mm_put_block(list->file_offset_node); 37375ef8b3bSRob Clark out_free_list: 37475ef8b3bSRob Clark kfree(list->map); 37575ef8b3bSRob Clark list->map = NULL; 37675ef8b3bSRob Clark 37775ef8b3bSRob Clark return ret; 37875ef8b3bSRob Clark } 37975ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset); 38075ef8b3bSRob Clark 381673a394bSEric Anholt /** Returns a reference to the object named by the handle. */ 382673a394bSEric Anholt struct drm_gem_object * 383673a394bSEric Anholt drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, 384a1a2d1d3SPekka Paalanen u32 handle) 385673a394bSEric Anholt { 386673a394bSEric Anholt struct drm_gem_object *obj; 387673a394bSEric Anholt 388673a394bSEric Anholt spin_lock(&filp->table_lock); 389673a394bSEric Anholt 390673a394bSEric Anholt /* Check if we currently have a reference on the object */ 391673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 392673a394bSEric Anholt if (obj == NULL) { 393673a394bSEric Anholt spin_unlock(&filp->table_lock); 394673a394bSEric Anholt return NULL; 395673a394bSEric Anholt } 396673a394bSEric Anholt 397673a394bSEric Anholt drm_gem_object_reference(obj); 398673a394bSEric Anholt 399673a394bSEric Anholt spin_unlock(&filp->table_lock); 400673a394bSEric Anholt 401673a394bSEric Anholt return obj; 402673a394bSEric Anholt } 403673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup); 404673a394bSEric Anholt 405673a394bSEric Anholt /** 406673a394bSEric Anholt * Releases the handle to an mm object. 407673a394bSEric Anholt */ 408673a394bSEric Anholt int 409673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data, 410673a394bSEric Anholt struct drm_file *file_priv) 411673a394bSEric Anholt { 412673a394bSEric Anholt struct drm_gem_close *args = data; 413673a394bSEric Anholt int ret; 414673a394bSEric Anholt 415673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 416673a394bSEric Anholt return -ENODEV; 417673a394bSEric Anholt 418673a394bSEric Anholt ret = drm_gem_handle_delete(file_priv, args->handle); 419673a394bSEric Anholt 420673a394bSEric Anholt return ret; 421673a394bSEric Anholt } 422673a394bSEric Anholt 423673a394bSEric Anholt /** 424673a394bSEric Anholt * Create a global name for an object, returning the name. 425673a394bSEric Anholt * 426673a394bSEric Anholt * Note that the name does not hold a reference; when the object 427673a394bSEric Anholt * is freed, the name goes away. 428673a394bSEric Anholt */ 429673a394bSEric Anholt int 430673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data, 431673a394bSEric Anholt struct drm_file *file_priv) 432673a394bSEric Anholt { 433673a394bSEric Anholt struct drm_gem_flink *args = data; 434673a394bSEric Anholt struct drm_gem_object *obj; 435673a394bSEric Anholt int ret; 436673a394bSEric Anholt 437673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 438673a394bSEric Anholt return -ENODEV; 439673a394bSEric Anholt 440673a394bSEric Anholt obj = drm_gem_object_lookup(dev, file_priv, args->handle); 441673a394bSEric Anholt if (obj == NULL) 442bf79cb91SChris Wilson return -ENOENT; 443673a394bSEric Anholt 4442e928815STejun Heo idr_preload(GFP_KERNEL); 445673a394bSEric Anholt spin_lock(&dev->object_name_lock); 4468d59bae5SChris Wilson if (!obj->name) { 4472e928815STejun Heo ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT); 4482e928815STejun Heo if (ret < 0) 4493e49c4f4SChris Wilson goto err; 4502e07fb22SYoungJun Cho 4512e07fb22SYoungJun Cho obj->name = ret; 452673a394bSEric Anholt 4538d59bae5SChris Wilson /* Allocate a reference for the name table. */ 4548d59bae5SChris Wilson drm_gem_object_reference(obj); 4558d59bae5SChris Wilson } 4563e49c4f4SChris Wilson 4572e07fb22SYoungJun Cho args->name = (uint64_t) obj->name; 4582e07fb22SYoungJun Cho ret = 0; 4592e07fb22SYoungJun Cho 4603e49c4f4SChris Wilson err: 4612e07fb22SYoungJun Cho spin_unlock(&dev->object_name_lock); 4622e07fb22SYoungJun Cho idr_preload_end(); 463bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 4643e49c4f4SChris Wilson return ret; 465673a394bSEric Anholt } 466673a394bSEric Anholt 467673a394bSEric Anholt /** 468673a394bSEric Anholt * Open an object using the global name, returning a handle and the size. 469673a394bSEric Anholt * 470673a394bSEric Anholt * This handle (of course) holds a reference to the object, so the object 471673a394bSEric Anholt * will not go away until the handle is deleted. 472673a394bSEric Anholt */ 473673a394bSEric Anholt int 474673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data, 475673a394bSEric Anholt struct drm_file *file_priv) 476673a394bSEric Anholt { 477673a394bSEric Anholt struct drm_gem_open *args = data; 478673a394bSEric Anholt struct drm_gem_object *obj; 479673a394bSEric Anholt int ret; 480a1a2d1d3SPekka Paalanen u32 handle; 481673a394bSEric Anholt 482673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 483673a394bSEric Anholt return -ENODEV; 484673a394bSEric Anholt 485673a394bSEric Anholt spin_lock(&dev->object_name_lock); 486673a394bSEric Anholt obj = idr_find(&dev->object_name_idr, (int) args->name); 487673a394bSEric Anholt if (obj) 488673a394bSEric Anholt drm_gem_object_reference(obj); 489673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 490673a394bSEric Anholt if (!obj) 491673a394bSEric Anholt return -ENOENT; 492673a394bSEric Anholt 493673a394bSEric Anholt ret = drm_gem_handle_create(file_priv, obj, &handle); 494bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 495673a394bSEric Anholt if (ret) 496673a394bSEric Anholt return ret; 497673a394bSEric Anholt 498673a394bSEric Anholt args->handle = handle; 499673a394bSEric Anholt args->size = obj->size; 500673a394bSEric Anholt 501673a394bSEric Anholt return 0; 502673a394bSEric Anholt } 503673a394bSEric Anholt 504673a394bSEric Anholt /** 505673a394bSEric Anholt * Called at device open time, sets up the structure for handling refcounting 506673a394bSEric Anholt * of mm objects. 507673a394bSEric Anholt */ 508673a394bSEric Anholt void 509673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 510673a394bSEric Anholt { 511673a394bSEric Anholt idr_init(&file_private->object_idr); 512673a394bSEric Anholt spin_lock_init(&file_private->table_lock); 513673a394bSEric Anholt } 514673a394bSEric Anholt 515673a394bSEric Anholt /** 516673a394bSEric Anholt * Called at device close to release the file's 517673a394bSEric Anholt * handle references on objects. 518673a394bSEric Anholt */ 519673a394bSEric Anholt static int 520673a394bSEric Anholt drm_gem_object_release_handle(int id, void *ptr, void *data) 521673a394bSEric Anholt { 522304eda32SBen Skeggs struct drm_file *file_priv = data; 523673a394bSEric Anholt struct drm_gem_object *obj = ptr; 524304eda32SBen Skeggs struct drm_device *dev = obj->dev; 525304eda32SBen Skeggs 5260ff926c7SDave Airlie drm_gem_remove_prime_handles(obj, file_priv); 5273248877eSDave Airlie 528304eda32SBen Skeggs if (dev->driver->gem_close_object) 529304eda32SBen Skeggs dev->driver->gem_close_object(obj, file_priv); 530673a394bSEric Anholt 531bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 532673a394bSEric Anholt 533673a394bSEric Anholt return 0; 534673a394bSEric Anholt } 535673a394bSEric Anholt 536673a394bSEric Anholt /** 537673a394bSEric Anholt * Called at close time when the filp is going away. 538673a394bSEric Anholt * 539673a394bSEric Anholt * Releases any remaining references on objects by this filp. 540673a394bSEric Anholt */ 541673a394bSEric Anholt void 542673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 543673a394bSEric Anholt { 544673a394bSEric Anholt idr_for_each(&file_private->object_idr, 545304eda32SBen Skeggs &drm_gem_object_release_handle, file_private); 546673a394bSEric Anholt idr_destroy(&file_private->object_idr); 547673a394bSEric Anholt } 548673a394bSEric Anholt 549fd632aa3SDaniel Vetter void 550fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj) 551c3ae90c0SLuca Barbieri { 55262cb7011SAlan Cox if (obj->filp) 553c3ae90c0SLuca Barbieri fput(obj->filp); 554c3ae90c0SLuca Barbieri } 555fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release); 556c3ae90c0SLuca Barbieri 557673a394bSEric Anholt /** 558673a394bSEric Anholt * Called after the last reference to the object has been lost. 559c3ae90c0SLuca Barbieri * Must be called holding struct_ mutex 560673a394bSEric Anholt * 561673a394bSEric Anholt * Frees the object 562673a394bSEric Anholt */ 563673a394bSEric Anholt void 564673a394bSEric Anholt drm_gem_object_free(struct kref *kref) 565673a394bSEric Anholt { 566673a394bSEric Anholt struct drm_gem_object *obj = (struct drm_gem_object *) kref; 567673a394bSEric Anholt struct drm_device *dev = obj->dev; 568673a394bSEric Anholt 569673a394bSEric Anholt BUG_ON(!mutex_is_locked(&dev->struct_mutex)); 570673a394bSEric Anholt 571673a394bSEric Anholt if (dev->driver->gem_free_object != NULL) 572673a394bSEric Anholt dev->driver->gem_free_object(obj); 573673a394bSEric Anholt } 574673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free); 575673a394bSEric Anholt 576c3ae90c0SLuca Barbieri static void drm_gem_object_ref_bug(struct kref *list_kref) 577c3ae90c0SLuca Barbieri { 578c3ae90c0SLuca Barbieri BUG(); 579c3ae90c0SLuca Barbieri } 580c3ae90c0SLuca Barbieri 581c3ae90c0SLuca Barbieri /** 582673a394bSEric Anholt * Called after the last handle to the object has been closed 583673a394bSEric Anholt * 584673a394bSEric Anholt * Removes any name for the object. Note that this must be 585673a394bSEric Anholt * called before drm_gem_object_free or we'll be touching 586673a394bSEric Anholt * freed memory 587673a394bSEric Anholt */ 58829d08b3eSDave Airlie void drm_gem_object_handle_free(struct drm_gem_object *obj) 589673a394bSEric Anholt { 590673a394bSEric Anholt struct drm_device *dev = obj->dev; 591673a394bSEric Anholt 592673a394bSEric Anholt /* Remove any name for this object */ 593673a394bSEric Anholt spin_lock(&dev->object_name_lock); 594673a394bSEric Anholt if (obj->name) { 595673a394bSEric Anholt idr_remove(&dev->object_name_idr, obj->name); 5968d59bae5SChris Wilson obj->name = 0; 597673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 598673a394bSEric Anholt /* 599673a394bSEric Anholt * The object name held a reference to this object, drop 600673a394bSEric Anholt * that now. 601c3ae90c0SLuca Barbieri * 602c3ae90c0SLuca Barbieri * This cannot be the last reference, since the handle holds one too. 603673a394bSEric Anholt */ 604c3ae90c0SLuca Barbieri kref_put(&obj->refcount, drm_gem_object_ref_bug); 605673a394bSEric Anholt } else 606673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 607673a394bSEric Anholt 608673a394bSEric Anholt } 609673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_handle_free); 610673a394bSEric Anholt 611ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma) 612ab00b3e5SJesse Barnes { 613ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 614ab00b3e5SJesse Barnes 615ab00b3e5SJesse Barnes drm_gem_object_reference(obj); 61631dfbc93SChris Wilson 61731dfbc93SChris Wilson mutex_lock(&obj->dev->struct_mutex); 618b06d66beSRob Clark drm_vm_open_locked(obj->dev, vma); 61931dfbc93SChris Wilson mutex_unlock(&obj->dev->struct_mutex); 620ab00b3e5SJesse Barnes } 621ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open); 622ab00b3e5SJesse Barnes 623ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma) 624ab00b3e5SJesse Barnes { 625ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 626b74ad5aeSChris Wilson struct drm_device *dev = obj->dev; 627ab00b3e5SJesse Barnes 628b74ad5aeSChris Wilson mutex_lock(&dev->struct_mutex); 629b06d66beSRob Clark drm_vm_close_locked(obj->dev, vma); 63031dfbc93SChris Wilson drm_gem_object_unreference(obj); 631b74ad5aeSChris Wilson mutex_unlock(&dev->struct_mutex); 632ab00b3e5SJesse Barnes } 633ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close); 634ab00b3e5SJesse Barnes 6351c5aafa6SLaurent Pinchart /** 6361c5aafa6SLaurent Pinchart * drm_gem_mmap_obj - memory map a GEM object 6371c5aafa6SLaurent Pinchart * @obj: the GEM object to map 6381c5aafa6SLaurent Pinchart * @obj_size: the object size to be mapped, in bytes 6391c5aafa6SLaurent Pinchart * @vma: VMA for the area to be mapped 6401c5aafa6SLaurent Pinchart * 6411c5aafa6SLaurent Pinchart * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops 6421c5aafa6SLaurent Pinchart * provided by the driver. Depending on their requirements, drivers can either 6431c5aafa6SLaurent Pinchart * provide a fault handler in their gem_vm_ops (in which case any accesses to 6441c5aafa6SLaurent Pinchart * the object will be trapped, to perform migration, GTT binding, surface 6451c5aafa6SLaurent Pinchart * register allocation, or performance monitoring), or mmap the buffer memory 6461c5aafa6SLaurent Pinchart * synchronously after calling drm_gem_mmap_obj. 6471c5aafa6SLaurent Pinchart * 6481c5aafa6SLaurent Pinchart * This function is mainly intended to implement the DMABUF mmap operation, when 6491c5aafa6SLaurent Pinchart * the GEM object is not looked up based on its fake offset. To implement the 6501c5aafa6SLaurent Pinchart * DRM mmap operation, drivers should use the drm_gem_mmap() function. 6511c5aafa6SLaurent Pinchart * 6524368dd84SYoungJun Cho * NOTE: This function has to be protected with dev->struct_mutex 6534368dd84SYoungJun Cho * 6541c5aafa6SLaurent Pinchart * Return 0 or success or -EINVAL if the object size is smaller than the VMA 6551c5aafa6SLaurent Pinchart * size, or if no gem_vm_ops are provided. 6561c5aafa6SLaurent Pinchart */ 6571c5aafa6SLaurent Pinchart int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size, 6581c5aafa6SLaurent Pinchart struct vm_area_struct *vma) 6591c5aafa6SLaurent Pinchart { 6601c5aafa6SLaurent Pinchart struct drm_device *dev = obj->dev; 6611c5aafa6SLaurent Pinchart 6624368dd84SYoungJun Cho lockdep_assert_held(&dev->struct_mutex); 6634368dd84SYoungJun Cho 6641c5aafa6SLaurent Pinchart /* Check for valid size. */ 6651c5aafa6SLaurent Pinchart if (obj_size < vma->vm_end - vma->vm_start) 6661c5aafa6SLaurent Pinchart return -EINVAL; 6671c5aafa6SLaurent Pinchart 6681c5aafa6SLaurent Pinchart if (!dev->driver->gem_vm_ops) 6691c5aafa6SLaurent Pinchart return -EINVAL; 6701c5aafa6SLaurent Pinchart 6711c5aafa6SLaurent Pinchart vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 6721c5aafa6SLaurent Pinchart vma->vm_ops = dev->driver->gem_vm_ops; 6731c5aafa6SLaurent Pinchart vma->vm_private_data = obj; 6741c5aafa6SLaurent Pinchart vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 6751c5aafa6SLaurent Pinchart 6761c5aafa6SLaurent Pinchart /* Take a ref for this mapping of the object, so that the fault 6771c5aafa6SLaurent Pinchart * handler can dereference the mmap offset's pointer to the object. 6781c5aafa6SLaurent Pinchart * This reference is cleaned up by the corresponding vm_close 6791c5aafa6SLaurent Pinchart * (which should happen whether the vma was created by this call, or 6801c5aafa6SLaurent Pinchart * by a vm_open due to mremap or partial unmap or whatever). 6811c5aafa6SLaurent Pinchart */ 6821c5aafa6SLaurent Pinchart drm_gem_object_reference(obj); 6831c5aafa6SLaurent Pinchart 6841c5aafa6SLaurent Pinchart drm_vm_open_locked(dev, vma); 6851c5aafa6SLaurent Pinchart return 0; 6861c5aafa6SLaurent Pinchart } 6871c5aafa6SLaurent Pinchart EXPORT_SYMBOL(drm_gem_mmap_obj); 688ab00b3e5SJesse Barnes 689a2c0a97bSJesse Barnes /** 690a2c0a97bSJesse Barnes * drm_gem_mmap - memory map routine for GEM objects 691a2c0a97bSJesse Barnes * @filp: DRM file pointer 692a2c0a97bSJesse Barnes * @vma: VMA for the area to be mapped 693a2c0a97bSJesse Barnes * 694a2c0a97bSJesse Barnes * If a driver supports GEM object mapping, mmap calls on the DRM file 695a2c0a97bSJesse Barnes * descriptor will end up here. 696a2c0a97bSJesse Barnes * 6971c5aafa6SLaurent Pinchart * Look up the GEM object based on the offset passed in (vma->vm_pgoff will 698a2c0a97bSJesse Barnes * contain the fake offset we created when the GTT map ioctl was called on 6991c5aafa6SLaurent Pinchart * the object) and map it with a call to drm_gem_mmap_obj(). 700a2c0a97bSJesse Barnes */ 701a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 702a2c0a97bSJesse Barnes { 703a2c0a97bSJesse Barnes struct drm_file *priv = filp->private_data; 704a2c0a97bSJesse Barnes struct drm_device *dev = priv->minor->dev; 705a2c0a97bSJesse Barnes struct drm_gem_mm *mm = dev->mm_private; 706f77d390cSBenjamin Herrenschmidt struct drm_local_map *map = NULL; 707a2c0a97bSJesse Barnes struct drm_hash_item *hash; 708a2c0a97bSJesse Barnes int ret = 0; 709a2c0a97bSJesse Barnes 7102c07a21dSDave Airlie if (drm_device_is_unplugged(dev)) 7112c07a21dSDave Airlie return -ENODEV; 7122c07a21dSDave Airlie 713a2c0a97bSJesse Barnes mutex_lock(&dev->struct_mutex); 714a2c0a97bSJesse Barnes 715a2c0a97bSJesse Barnes if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) { 716a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 717a2c0a97bSJesse Barnes return drm_mmap(filp, vma); 718a2c0a97bSJesse Barnes } 719a2c0a97bSJesse Barnes 720a2c0a97bSJesse Barnes map = drm_hash_entry(hash, struct drm_map_list, hash)->map; 721a2c0a97bSJesse Barnes if (!map || 722a2c0a97bSJesse Barnes ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) { 723a2c0a97bSJesse Barnes ret = -EPERM; 724a2c0a97bSJesse Barnes goto out_unlock; 725a2c0a97bSJesse Barnes } 726a2c0a97bSJesse Barnes 7271c5aafa6SLaurent Pinchart ret = drm_gem_mmap_obj(map->handle, map->size, vma); 728a2c0a97bSJesse Barnes 729a2c0a97bSJesse Barnes out_unlock: 730a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 731a2c0a97bSJesse Barnes 732a2c0a97bSJesse Barnes return ret; 733a2c0a97bSJesse Barnes } 734a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap); 735