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> 37*5949eac4SHugh Dickins #include <linux/shmem_fs.h> 38673a394bSEric Anholt #include "drmP.h" 39673a394bSEric Anholt 40673a394bSEric Anholt /** @file drm_gem.c 41673a394bSEric Anholt * 42673a394bSEric Anholt * This file provides some of the base ioctls and library routines for 43673a394bSEric Anholt * the graphics memory manager implemented by each device driver. 44673a394bSEric Anholt * 45673a394bSEric Anholt * Because various devices have different requirements in terms of 46673a394bSEric Anholt * synchronization and migration strategies, implementing that is left up to 47673a394bSEric Anholt * the driver, and all that the general API provides should be generic -- 48673a394bSEric Anholt * allocating objects, reading/writing data with the cpu, freeing objects. 49673a394bSEric Anholt * Even there, platform-dependent optimizations for reading/writing data with 50673a394bSEric Anholt * the CPU mean we'll likely hook those out to driver-specific calls. However, 51673a394bSEric Anholt * the DRI2 implementation wants to have at least allocate/mmap be generic. 52673a394bSEric Anholt * 53673a394bSEric Anholt * The goal was to have swap-backed object allocation managed through 54673a394bSEric Anholt * struct file. However, file descriptors as handles to a struct file have 55673a394bSEric Anholt * two major failings: 56673a394bSEric Anholt * - Process limits prevent more than 1024 or so being used at a time by 57673a394bSEric Anholt * default. 58673a394bSEric Anholt * - Inability to allocate high fds will aggravate the X Server's select() 59673a394bSEric Anholt * handling, and likely that of many GL client applications as well. 60673a394bSEric Anholt * 61673a394bSEric Anholt * This led to a plan of using our own integer IDs (called handles, following 62673a394bSEric Anholt * DRM terminology) to mimic fds, and implement the fd syscalls we need as 63673a394bSEric Anholt * ioctls. The objects themselves will still include the struct file so 64673a394bSEric Anholt * that we can transition to fds if the required kernel infrastructure shows 65673a394bSEric Anholt * up at a later date, and as our interface with shmfs for memory allocation. 66673a394bSEric Anholt */ 67673a394bSEric Anholt 68a2c0a97bSJesse Barnes /* 69a2c0a97bSJesse Barnes * We make up offsets for buffer objects so we can recognize them at 70a2c0a97bSJesse Barnes * mmap time. 71a2c0a97bSJesse Barnes */ 7205269a3aSJordan Crouse 7305269a3aSJordan Crouse /* pgoff in mmap is an unsigned long, so we need to make sure that 7405269a3aSJordan Crouse * the faked up offset will fit 7505269a3aSJordan Crouse */ 7605269a3aSJordan Crouse 7705269a3aSJordan Crouse #if BITS_PER_LONG == 64 78a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) 79a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16) 8005269a3aSJordan Crouse #else 8105269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1) 8205269a3aSJordan Crouse #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16) 8305269a3aSJordan Crouse #endif 84a2c0a97bSJesse Barnes 85673a394bSEric Anholt /** 86673a394bSEric Anholt * Initialize the GEM device fields 87673a394bSEric Anholt */ 88673a394bSEric Anholt 89673a394bSEric Anholt int 90673a394bSEric Anholt drm_gem_init(struct drm_device *dev) 91673a394bSEric Anholt { 92a2c0a97bSJesse Barnes struct drm_gem_mm *mm; 93a2c0a97bSJesse Barnes 94673a394bSEric Anholt spin_lock_init(&dev->object_name_lock); 95673a394bSEric Anholt idr_init(&dev->object_name_idr); 96a2c0a97bSJesse Barnes 979a298b2aSEric Anholt mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL); 98a2c0a97bSJesse Barnes if (!mm) { 99a2c0a97bSJesse Barnes DRM_ERROR("out of memory\n"); 100a2c0a97bSJesse Barnes return -ENOMEM; 101a2c0a97bSJesse Barnes } 102a2c0a97bSJesse Barnes 103a2c0a97bSJesse Barnes dev->mm_private = mm; 104a2c0a97bSJesse Barnes 1054cb81ac2SChris Wilson if (drm_ht_create(&mm->offset_hash, 12)) { 1069a298b2aSEric Anholt kfree(mm); 107a2c0a97bSJesse Barnes return -ENOMEM; 108a2c0a97bSJesse Barnes } 109a2c0a97bSJesse Barnes 110a2c0a97bSJesse Barnes if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START, 111a2c0a97bSJesse Barnes DRM_FILE_PAGE_OFFSET_SIZE)) { 112a2c0a97bSJesse Barnes drm_ht_remove(&mm->offset_hash); 1139a298b2aSEric Anholt kfree(mm); 114a2c0a97bSJesse Barnes return -ENOMEM; 115a2c0a97bSJesse Barnes } 116a2c0a97bSJesse Barnes 117673a394bSEric Anholt return 0; 118673a394bSEric Anholt } 119673a394bSEric Anholt 120a2c0a97bSJesse Barnes void 121a2c0a97bSJesse Barnes drm_gem_destroy(struct drm_device *dev) 122a2c0a97bSJesse Barnes { 123a2c0a97bSJesse Barnes struct drm_gem_mm *mm = dev->mm_private; 124a2c0a97bSJesse Barnes 125a2c0a97bSJesse Barnes drm_mm_takedown(&mm->offset_manager); 126a2c0a97bSJesse Barnes drm_ht_remove(&mm->offset_hash); 1279a298b2aSEric Anholt kfree(mm); 128a2c0a97bSJesse Barnes dev->mm_private = NULL; 129a2c0a97bSJesse Barnes } 130a2c0a97bSJesse Barnes 131673a394bSEric Anholt /** 1321d397043SDaniel Vetter * Initialize an already allocate GEM object of the specified size with 1331d397043SDaniel Vetter * shmfs backing store. 1341d397043SDaniel Vetter */ 1351d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev, 1361d397043SDaniel Vetter struct drm_gem_object *obj, size_t size) 1371d397043SDaniel Vetter { 1381d397043SDaniel Vetter BUG_ON((size & (PAGE_SIZE - 1)) != 0); 1391d397043SDaniel Vetter 1401d397043SDaniel Vetter obj->dev = dev; 1411d397043SDaniel Vetter obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); 1421d397043SDaniel Vetter if (IS_ERR(obj->filp)) 1431d397043SDaniel Vetter return -ENOMEM; 1441d397043SDaniel Vetter 1451d397043SDaniel Vetter kref_init(&obj->refcount); 14629d08b3eSDave Airlie atomic_set(&obj->handle_count, 0); 1471d397043SDaniel Vetter obj->size = size; 1481d397043SDaniel Vetter 1491d397043SDaniel Vetter return 0; 1501d397043SDaniel Vetter } 1511d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init); 1521d397043SDaniel Vetter 1531d397043SDaniel Vetter /** 154673a394bSEric Anholt * Allocate a GEM object of the specified size with shmfs backing store 155673a394bSEric Anholt */ 156673a394bSEric Anholt struct drm_gem_object * 157673a394bSEric Anholt drm_gem_object_alloc(struct drm_device *dev, size_t size) 158673a394bSEric Anholt { 159673a394bSEric Anholt struct drm_gem_object *obj; 160673a394bSEric Anholt 161b798b1feSRobert P. J. Day obj = kzalloc(sizeof(*obj), GFP_KERNEL); 162845792d9SJiri Slaby if (!obj) 163845792d9SJiri Slaby goto free; 164673a394bSEric Anholt 1651d397043SDaniel Vetter if (drm_gem_object_init(dev, obj, size) != 0) 166845792d9SJiri Slaby goto free; 167673a394bSEric Anholt 168673a394bSEric Anholt if (dev->driver->gem_init_object != NULL && 169673a394bSEric Anholt dev->driver->gem_init_object(obj) != 0) { 170845792d9SJiri Slaby goto fput; 171673a394bSEric Anholt } 172673a394bSEric Anholt return obj; 173845792d9SJiri Slaby fput: 1741d397043SDaniel Vetter /* Object_init mangles the global counters - readjust them. */ 175845792d9SJiri Slaby fput(obj->filp); 176845792d9SJiri Slaby free: 177845792d9SJiri Slaby kfree(obj); 178845792d9SJiri Slaby return NULL; 179673a394bSEric Anholt } 180673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_alloc); 181673a394bSEric Anholt 182673a394bSEric Anholt /** 183673a394bSEric Anholt * Removes the mapping from handle to filp for this object. 184673a394bSEric Anholt */ 185ff72145bSDave Airlie int 186a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle) 187673a394bSEric Anholt { 188673a394bSEric Anholt struct drm_device *dev; 189673a394bSEric Anholt struct drm_gem_object *obj; 190673a394bSEric Anholt 191673a394bSEric Anholt /* This is gross. The idr system doesn't let us try a delete and 192673a394bSEric Anholt * return an error code. It just spews if you fail at deleting. 193673a394bSEric Anholt * So, we have to grab a lock around finding the object and then 194673a394bSEric Anholt * doing the delete on it and dropping the refcount, or the user 195673a394bSEric Anholt * could race us to double-decrement the refcount and cause a 196673a394bSEric Anholt * use-after-free later. Given the frequency of our handle lookups, 197673a394bSEric Anholt * we may want to use ida for number allocation and a hash table 198673a394bSEric Anholt * for the pointers, anyway. 199673a394bSEric Anholt */ 200673a394bSEric Anholt spin_lock(&filp->table_lock); 201673a394bSEric Anholt 202673a394bSEric Anholt /* Check if we currently have a reference on the object */ 203673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 204673a394bSEric Anholt if (obj == NULL) { 205673a394bSEric Anholt spin_unlock(&filp->table_lock); 206673a394bSEric Anholt return -EINVAL; 207673a394bSEric Anholt } 208673a394bSEric Anholt dev = obj->dev; 209673a394bSEric Anholt 210673a394bSEric Anholt /* Release reference and decrement refcount. */ 211673a394bSEric Anholt idr_remove(&filp->object_idr, handle); 212673a394bSEric Anholt spin_unlock(&filp->table_lock); 213673a394bSEric Anholt 214bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 215673a394bSEric Anholt 216673a394bSEric Anholt return 0; 217673a394bSEric Anholt } 218ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete); 219673a394bSEric Anholt 220673a394bSEric Anholt /** 221673a394bSEric Anholt * Create a handle for this object. This adds a handle reference 222673a394bSEric Anholt * to the object, which includes a regular reference count. Callers 223673a394bSEric Anholt * will likely want to dereference the object afterwards. 224673a394bSEric Anholt */ 225673a394bSEric Anholt int 226673a394bSEric Anholt drm_gem_handle_create(struct drm_file *file_priv, 227673a394bSEric Anholt struct drm_gem_object *obj, 228a1a2d1d3SPekka Paalanen u32 *handlep) 229673a394bSEric Anholt { 230673a394bSEric Anholt int ret; 231673a394bSEric Anholt 232673a394bSEric Anholt /* 233673a394bSEric Anholt * Get the user-visible handle using idr. 234673a394bSEric Anholt */ 235673a394bSEric Anholt again: 236673a394bSEric Anholt /* ensure there is space available to allocate a handle */ 237673a394bSEric Anholt if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) 238673a394bSEric Anholt return -ENOMEM; 239673a394bSEric Anholt 240673a394bSEric Anholt /* do the allocation under our spinlock */ 241673a394bSEric Anholt spin_lock(&file_priv->table_lock); 242a1a2d1d3SPekka Paalanen ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep); 243673a394bSEric Anholt spin_unlock(&file_priv->table_lock); 244673a394bSEric Anholt if (ret == -EAGAIN) 245673a394bSEric Anholt goto again; 246673a394bSEric Anholt 247673a394bSEric Anholt if (ret != 0) 248673a394bSEric Anholt return ret; 249673a394bSEric Anholt 250673a394bSEric Anholt drm_gem_object_handle_reference(obj); 251673a394bSEric Anholt return 0; 252673a394bSEric Anholt } 253673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create); 254673a394bSEric Anholt 255673a394bSEric Anholt /** Returns a reference to the object named by the handle. */ 256673a394bSEric Anholt struct drm_gem_object * 257673a394bSEric Anholt drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, 258a1a2d1d3SPekka Paalanen u32 handle) 259673a394bSEric Anholt { 260673a394bSEric Anholt struct drm_gem_object *obj; 261673a394bSEric Anholt 262673a394bSEric Anholt spin_lock(&filp->table_lock); 263673a394bSEric Anholt 264673a394bSEric Anholt /* Check if we currently have a reference on the object */ 265673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 266673a394bSEric Anholt if (obj == NULL) { 267673a394bSEric Anholt spin_unlock(&filp->table_lock); 268673a394bSEric Anholt return NULL; 269673a394bSEric Anholt } 270673a394bSEric Anholt 271673a394bSEric Anholt drm_gem_object_reference(obj); 272673a394bSEric Anholt 273673a394bSEric Anholt spin_unlock(&filp->table_lock); 274673a394bSEric Anholt 275673a394bSEric Anholt return obj; 276673a394bSEric Anholt } 277673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup); 278673a394bSEric Anholt 279673a394bSEric Anholt /** 280673a394bSEric Anholt * Releases the handle to an mm object. 281673a394bSEric Anholt */ 282673a394bSEric Anholt int 283673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data, 284673a394bSEric Anholt struct drm_file *file_priv) 285673a394bSEric Anholt { 286673a394bSEric Anholt struct drm_gem_close *args = data; 287673a394bSEric Anholt int ret; 288673a394bSEric Anholt 289673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 290673a394bSEric Anholt return -ENODEV; 291673a394bSEric Anholt 292673a394bSEric Anholt ret = drm_gem_handle_delete(file_priv, args->handle); 293673a394bSEric Anholt 294673a394bSEric Anholt return ret; 295673a394bSEric Anholt } 296673a394bSEric Anholt 297673a394bSEric Anholt /** 298673a394bSEric Anholt * Create a global name for an object, returning the name. 299673a394bSEric Anholt * 300673a394bSEric Anholt * Note that the name does not hold a reference; when the object 301673a394bSEric Anholt * is freed, the name goes away. 302673a394bSEric Anholt */ 303673a394bSEric Anholt int 304673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data, 305673a394bSEric Anholt struct drm_file *file_priv) 306673a394bSEric Anholt { 307673a394bSEric Anholt struct drm_gem_flink *args = data; 308673a394bSEric Anholt struct drm_gem_object *obj; 309673a394bSEric Anholt int ret; 310673a394bSEric Anholt 311673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 312673a394bSEric Anholt return -ENODEV; 313673a394bSEric Anholt 314673a394bSEric Anholt obj = drm_gem_object_lookup(dev, file_priv, args->handle); 315673a394bSEric Anholt if (obj == NULL) 316bf79cb91SChris Wilson return -ENOENT; 317673a394bSEric Anholt 318673a394bSEric Anholt again: 3193e49c4f4SChris Wilson if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) { 3203e49c4f4SChris Wilson ret = -ENOMEM; 3213e49c4f4SChris Wilson goto err; 3223e49c4f4SChris Wilson } 323673a394bSEric Anholt 324673a394bSEric Anholt spin_lock(&dev->object_name_lock); 3258d59bae5SChris Wilson if (!obj->name) { 326673a394bSEric Anholt ret = idr_get_new_above(&dev->object_name_idr, obj, 1, 327673a394bSEric Anholt &obj->name); 3288d59bae5SChris Wilson args->name = (uint64_t) obj->name; 329673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 3308d59bae5SChris Wilson 331673a394bSEric Anholt if (ret == -EAGAIN) 332673a394bSEric Anholt goto again; 333673a394bSEric Anholt 3343e49c4f4SChris Wilson if (ret != 0) 3353e49c4f4SChris Wilson goto err; 336673a394bSEric Anholt 3378d59bae5SChris Wilson /* Allocate a reference for the name table. */ 3388d59bae5SChris Wilson drm_gem_object_reference(obj); 3398d59bae5SChris Wilson } else { 340673a394bSEric Anholt args->name = (uint64_t) obj->name; 3418d59bae5SChris Wilson spin_unlock(&dev->object_name_lock); 3428d59bae5SChris Wilson ret = 0; 3438d59bae5SChris Wilson } 3443e49c4f4SChris Wilson 3453e49c4f4SChris Wilson err: 346bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 3473e49c4f4SChris Wilson return ret; 348673a394bSEric Anholt } 349673a394bSEric Anholt 350673a394bSEric Anholt /** 351673a394bSEric Anholt * Open an object using the global name, returning a handle and the size. 352673a394bSEric Anholt * 353673a394bSEric Anholt * This handle (of course) holds a reference to the object, so the object 354673a394bSEric Anholt * will not go away until the handle is deleted. 355673a394bSEric Anholt */ 356673a394bSEric Anholt int 357673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data, 358673a394bSEric Anholt struct drm_file *file_priv) 359673a394bSEric Anholt { 360673a394bSEric Anholt struct drm_gem_open *args = data; 361673a394bSEric Anholt struct drm_gem_object *obj; 362673a394bSEric Anholt int ret; 363a1a2d1d3SPekka Paalanen u32 handle; 364673a394bSEric Anholt 365673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 366673a394bSEric Anholt return -ENODEV; 367673a394bSEric Anholt 368673a394bSEric Anholt spin_lock(&dev->object_name_lock); 369673a394bSEric Anholt obj = idr_find(&dev->object_name_idr, (int) args->name); 370673a394bSEric Anholt if (obj) 371673a394bSEric Anholt drm_gem_object_reference(obj); 372673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 373673a394bSEric Anholt if (!obj) 374673a394bSEric Anholt return -ENOENT; 375673a394bSEric Anholt 376673a394bSEric Anholt ret = drm_gem_handle_create(file_priv, obj, &handle); 377bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 378673a394bSEric Anholt if (ret) 379673a394bSEric Anholt return ret; 380673a394bSEric Anholt 381673a394bSEric Anholt args->handle = handle; 382673a394bSEric Anholt args->size = obj->size; 383673a394bSEric Anholt 384673a394bSEric Anholt return 0; 385673a394bSEric Anholt } 386673a394bSEric Anholt 387673a394bSEric Anholt /** 388673a394bSEric Anholt * Called at device open time, sets up the structure for handling refcounting 389673a394bSEric Anholt * of mm objects. 390673a394bSEric Anholt */ 391673a394bSEric Anholt void 392673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 393673a394bSEric Anholt { 394673a394bSEric Anholt idr_init(&file_private->object_idr); 395673a394bSEric Anholt spin_lock_init(&file_private->table_lock); 396673a394bSEric Anholt } 397673a394bSEric Anholt 398673a394bSEric Anholt /** 399673a394bSEric Anholt * Called at device close to release the file's 400673a394bSEric Anholt * handle references on objects. 401673a394bSEric Anholt */ 402673a394bSEric Anholt static int 403673a394bSEric Anholt drm_gem_object_release_handle(int id, void *ptr, void *data) 404673a394bSEric Anholt { 405673a394bSEric Anholt struct drm_gem_object *obj = ptr; 406673a394bSEric Anholt 407bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 408673a394bSEric Anholt 409673a394bSEric Anholt return 0; 410673a394bSEric Anholt } 411673a394bSEric Anholt 412673a394bSEric Anholt /** 413673a394bSEric Anholt * Called at close time when the filp is going away. 414673a394bSEric Anholt * 415673a394bSEric Anholt * Releases any remaining references on objects by this filp. 416673a394bSEric Anholt */ 417673a394bSEric Anholt void 418673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 419673a394bSEric Anholt { 420673a394bSEric Anholt idr_for_each(&file_private->object_idr, 421673a394bSEric Anholt &drm_gem_object_release_handle, NULL); 422673a394bSEric Anholt 423ddd3d069SChris Wilson idr_remove_all(&file_private->object_idr); 424673a394bSEric Anholt idr_destroy(&file_private->object_idr); 425673a394bSEric Anholt } 426673a394bSEric Anholt 427fd632aa3SDaniel Vetter void 428fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj) 429c3ae90c0SLuca Barbieri { 430c3ae90c0SLuca Barbieri fput(obj->filp); 431c3ae90c0SLuca Barbieri } 432fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release); 433c3ae90c0SLuca Barbieri 434673a394bSEric Anholt /** 435673a394bSEric Anholt * Called after the last reference to the object has been lost. 436c3ae90c0SLuca Barbieri * Must be called holding struct_ mutex 437673a394bSEric Anholt * 438673a394bSEric Anholt * Frees the object 439673a394bSEric Anholt */ 440673a394bSEric Anholt void 441673a394bSEric Anholt drm_gem_object_free(struct kref *kref) 442673a394bSEric Anholt { 443673a394bSEric Anholt struct drm_gem_object *obj = (struct drm_gem_object *) kref; 444673a394bSEric Anholt struct drm_device *dev = obj->dev; 445673a394bSEric Anholt 446673a394bSEric Anholt BUG_ON(!mutex_is_locked(&dev->struct_mutex)); 447673a394bSEric Anholt 448673a394bSEric Anholt if (dev->driver->gem_free_object != NULL) 449673a394bSEric Anholt dev->driver->gem_free_object(obj); 450673a394bSEric Anholt } 451673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free); 452673a394bSEric Anholt 453c3ae90c0SLuca Barbieri static void drm_gem_object_ref_bug(struct kref *list_kref) 454c3ae90c0SLuca Barbieri { 455c3ae90c0SLuca Barbieri BUG(); 456c3ae90c0SLuca Barbieri } 457c3ae90c0SLuca Barbieri 458c3ae90c0SLuca Barbieri /** 459673a394bSEric Anholt * Called after the last handle to the object has been closed 460673a394bSEric Anholt * 461673a394bSEric Anholt * Removes any name for the object. Note that this must be 462673a394bSEric Anholt * called before drm_gem_object_free or we'll be touching 463673a394bSEric Anholt * freed memory 464673a394bSEric Anholt */ 46529d08b3eSDave Airlie void drm_gem_object_handle_free(struct drm_gem_object *obj) 466673a394bSEric Anholt { 467673a394bSEric Anholt struct drm_device *dev = obj->dev; 468673a394bSEric Anholt 469673a394bSEric Anholt /* Remove any name for this object */ 470673a394bSEric Anholt spin_lock(&dev->object_name_lock); 471673a394bSEric Anholt if (obj->name) { 472673a394bSEric Anholt idr_remove(&dev->object_name_idr, obj->name); 4738d59bae5SChris Wilson obj->name = 0; 474673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 475673a394bSEric Anholt /* 476673a394bSEric Anholt * The object name held a reference to this object, drop 477673a394bSEric Anholt * that now. 478c3ae90c0SLuca Barbieri * 479c3ae90c0SLuca Barbieri * This cannot be the last reference, since the handle holds one too. 480673a394bSEric Anholt */ 481c3ae90c0SLuca Barbieri kref_put(&obj->refcount, drm_gem_object_ref_bug); 482673a394bSEric Anholt } else 483673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 484673a394bSEric Anholt 485673a394bSEric Anholt } 486673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_handle_free); 487673a394bSEric Anholt 488ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma) 489ab00b3e5SJesse Barnes { 490ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 491ab00b3e5SJesse Barnes 492ab00b3e5SJesse Barnes drm_gem_object_reference(obj); 49331dfbc93SChris Wilson 49431dfbc93SChris Wilson mutex_lock(&obj->dev->struct_mutex); 49531dfbc93SChris Wilson drm_vm_open_locked(vma); 49631dfbc93SChris Wilson mutex_unlock(&obj->dev->struct_mutex); 497ab00b3e5SJesse Barnes } 498ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open); 499ab00b3e5SJesse Barnes 500ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma) 501ab00b3e5SJesse Barnes { 502ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 503b74ad5aeSChris Wilson struct drm_device *dev = obj->dev; 504ab00b3e5SJesse Barnes 505b74ad5aeSChris Wilson mutex_lock(&dev->struct_mutex); 50631dfbc93SChris Wilson drm_vm_close_locked(vma); 50731dfbc93SChris Wilson drm_gem_object_unreference(obj); 508b74ad5aeSChris Wilson mutex_unlock(&dev->struct_mutex); 509ab00b3e5SJesse Barnes } 510ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close); 511ab00b3e5SJesse Barnes 512ab00b3e5SJesse Barnes 513a2c0a97bSJesse Barnes /** 514a2c0a97bSJesse Barnes * drm_gem_mmap - memory map routine for GEM objects 515a2c0a97bSJesse Barnes * @filp: DRM file pointer 516a2c0a97bSJesse Barnes * @vma: VMA for the area to be mapped 517a2c0a97bSJesse Barnes * 518a2c0a97bSJesse Barnes * If a driver supports GEM object mapping, mmap calls on the DRM file 519a2c0a97bSJesse Barnes * descriptor will end up here. 520a2c0a97bSJesse Barnes * 521a2c0a97bSJesse Barnes * If we find the object based on the offset passed in (vma->vm_pgoff will 522a2c0a97bSJesse Barnes * contain the fake offset we created when the GTT map ioctl was called on 523a2c0a97bSJesse Barnes * the object), we set up the driver fault handler so that any accesses 524a2c0a97bSJesse Barnes * to the object can be trapped, to perform migration, GTT binding, surface 525a2c0a97bSJesse Barnes * register allocation, or performance monitoring. 526a2c0a97bSJesse Barnes */ 527a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 528a2c0a97bSJesse Barnes { 529a2c0a97bSJesse Barnes struct drm_file *priv = filp->private_data; 530a2c0a97bSJesse Barnes struct drm_device *dev = priv->minor->dev; 531a2c0a97bSJesse Barnes struct drm_gem_mm *mm = dev->mm_private; 532f77d390cSBenjamin Herrenschmidt struct drm_local_map *map = NULL; 533a2c0a97bSJesse Barnes struct drm_gem_object *obj; 534a2c0a97bSJesse Barnes struct drm_hash_item *hash; 535a2c0a97bSJesse Barnes int ret = 0; 536a2c0a97bSJesse Barnes 537a2c0a97bSJesse Barnes mutex_lock(&dev->struct_mutex); 538a2c0a97bSJesse Barnes 539a2c0a97bSJesse Barnes if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) { 540a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 541a2c0a97bSJesse Barnes return drm_mmap(filp, vma); 542a2c0a97bSJesse Barnes } 543a2c0a97bSJesse Barnes 544a2c0a97bSJesse Barnes map = drm_hash_entry(hash, struct drm_map_list, hash)->map; 545a2c0a97bSJesse Barnes if (!map || 546a2c0a97bSJesse Barnes ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) { 547a2c0a97bSJesse Barnes ret = -EPERM; 548a2c0a97bSJesse Barnes goto out_unlock; 549a2c0a97bSJesse Barnes } 550a2c0a97bSJesse Barnes 551a2c0a97bSJesse Barnes /* Check for valid size. */ 552a2c0a97bSJesse Barnes if (map->size < vma->vm_end - vma->vm_start) { 553a2c0a97bSJesse Barnes ret = -EINVAL; 554a2c0a97bSJesse Barnes goto out_unlock; 555a2c0a97bSJesse Barnes } 556a2c0a97bSJesse Barnes 557a2c0a97bSJesse Barnes obj = map->handle; 558a2c0a97bSJesse Barnes if (!obj->dev->driver->gem_vm_ops) { 559a2c0a97bSJesse Barnes ret = -EINVAL; 560a2c0a97bSJesse Barnes goto out_unlock; 561a2c0a97bSJesse Barnes } 562a2c0a97bSJesse Barnes 563a2c0a97bSJesse Barnes vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND; 564a2c0a97bSJesse Barnes vma->vm_ops = obj->dev->driver->gem_vm_ops; 565a2c0a97bSJesse Barnes vma->vm_private_data = map->handle; 56679cc304fSJeremy Fitzhardinge vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 567a2c0a97bSJesse Barnes 568ab00b3e5SJesse Barnes /* Take a ref for this mapping of the object, so that the fault 569ab00b3e5SJesse Barnes * handler can dereference the mmap offset's pointer to the object. 570ab00b3e5SJesse Barnes * This reference is cleaned up by the corresponding vm_close 571ab00b3e5SJesse Barnes * (which should happen whether the vma was created by this call, or 572ab00b3e5SJesse Barnes * by a vm_open due to mremap or partial unmap or whatever). 573ab00b3e5SJesse Barnes */ 574ab00b3e5SJesse Barnes drm_gem_object_reference(obj); 575ab00b3e5SJesse Barnes 576a2c0a97bSJesse Barnes vma->vm_file = filp; /* Needed for drm_vm_open() */ 577a2c0a97bSJesse Barnes drm_vm_open_locked(vma); 578a2c0a97bSJesse Barnes 579a2c0a97bSJesse Barnes out_unlock: 580a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 581a2c0a97bSJesse Barnes 582a2c0a97bSJesse Barnes return ret; 583a2c0a97bSJesse Barnes } 584a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap); 585