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> 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 /** 132*62cb7011SAlan Cox * Initialize an already allocated 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 /** 154*62cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with 155*62cb7011SAlan Cox * no GEM provided backing store. Instead the caller is responsible for 156*62cb7011SAlan Cox * backing the object and handling it. 157*62cb7011SAlan Cox */ 158*62cb7011SAlan Cox int drm_gem_private_object_init(struct drm_device *dev, 159*62cb7011SAlan Cox struct drm_gem_object *obj, size_t size) 160*62cb7011SAlan Cox { 161*62cb7011SAlan Cox BUG_ON((size & (PAGE_SIZE - 1)) != 0); 162*62cb7011SAlan Cox 163*62cb7011SAlan Cox obj->dev = dev; 164*62cb7011SAlan Cox obj->filp = NULL; 165*62cb7011SAlan Cox 166*62cb7011SAlan Cox kref_init(&obj->refcount); 167*62cb7011SAlan Cox atomic_set(&obj->handle_count, 0); 168*62cb7011SAlan Cox obj->size = size; 169*62cb7011SAlan Cox 170*62cb7011SAlan Cox return 0; 171*62cb7011SAlan Cox } 172*62cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init); 173*62cb7011SAlan Cox 174*62cb7011SAlan Cox /** 175673a394bSEric Anholt * Allocate a GEM object of the specified size with shmfs backing store 176673a394bSEric Anholt */ 177673a394bSEric Anholt struct drm_gem_object * 178673a394bSEric Anholt drm_gem_object_alloc(struct drm_device *dev, size_t size) 179673a394bSEric Anholt { 180673a394bSEric Anholt struct drm_gem_object *obj; 181673a394bSEric Anholt 182b798b1feSRobert P. J. Day obj = kzalloc(sizeof(*obj), GFP_KERNEL); 183845792d9SJiri Slaby if (!obj) 184845792d9SJiri Slaby goto free; 185673a394bSEric Anholt 1861d397043SDaniel Vetter if (drm_gem_object_init(dev, obj, size) != 0) 187845792d9SJiri Slaby goto free; 188673a394bSEric Anholt 189673a394bSEric Anholt if (dev->driver->gem_init_object != NULL && 190673a394bSEric Anholt dev->driver->gem_init_object(obj) != 0) { 191845792d9SJiri Slaby goto fput; 192673a394bSEric Anholt } 193673a394bSEric Anholt return obj; 194845792d9SJiri Slaby fput: 1951d397043SDaniel Vetter /* Object_init mangles the global counters - readjust them. */ 196845792d9SJiri Slaby fput(obj->filp); 197845792d9SJiri Slaby free: 198845792d9SJiri Slaby kfree(obj); 199845792d9SJiri Slaby return NULL; 200673a394bSEric Anholt } 201673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_alloc); 202673a394bSEric Anholt 203673a394bSEric Anholt /** 204673a394bSEric Anholt * Removes the mapping from handle to filp for this object. 205673a394bSEric Anholt */ 206ff72145bSDave Airlie int 207a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle) 208673a394bSEric Anholt { 209673a394bSEric Anholt struct drm_device *dev; 210673a394bSEric Anholt struct drm_gem_object *obj; 211673a394bSEric Anholt 212673a394bSEric Anholt /* This is gross. The idr system doesn't let us try a delete and 213673a394bSEric Anholt * return an error code. It just spews if you fail at deleting. 214673a394bSEric Anholt * So, we have to grab a lock around finding the object and then 215673a394bSEric Anholt * doing the delete on it and dropping the refcount, or the user 216673a394bSEric Anholt * could race us to double-decrement the refcount and cause a 217673a394bSEric Anholt * use-after-free later. Given the frequency of our handle lookups, 218673a394bSEric Anholt * we may want to use ida for number allocation and a hash table 219673a394bSEric Anholt * for the pointers, anyway. 220673a394bSEric Anholt */ 221673a394bSEric Anholt spin_lock(&filp->table_lock); 222673a394bSEric Anholt 223673a394bSEric Anholt /* Check if we currently have a reference on the object */ 224673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 225673a394bSEric Anholt if (obj == NULL) { 226673a394bSEric Anholt spin_unlock(&filp->table_lock); 227673a394bSEric Anholt return -EINVAL; 228673a394bSEric Anholt } 229673a394bSEric Anholt dev = obj->dev; 230673a394bSEric Anholt 231673a394bSEric Anholt /* Release reference and decrement refcount. */ 232673a394bSEric Anholt idr_remove(&filp->object_idr, handle); 233673a394bSEric Anholt spin_unlock(&filp->table_lock); 234673a394bSEric Anholt 235304eda32SBen Skeggs if (dev->driver->gem_close_object) 236304eda32SBen Skeggs dev->driver->gem_close_object(obj, filp); 237bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 238673a394bSEric Anholt 239673a394bSEric Anholt return 0; 240673a394bSEric Anholt } 241ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete); 242673a394bSEric Anholt 243673a394bSEric Anholt /** 244673a394bSEric Anholt * Create a handle for this object. This adds a handle reference 245673a394bSEric Anholt * to the object, which includes a regular reference count. Callers 246673a394bSEric Anholt * will likely want to dereference the object afterwards. 247673a394bSEric Anholt */ 248673a394bSEric Anholt int 249673a394bSEric Anholt drm_gem_handle_create(struct drm_file *file_priv, 250673a394bSEric Anholt struct drm_gem_object *obj, 251a1a2d1d3SPekka Paalanen u32 *handlep) 252673a394bSEric Anholt { 253304eda32SBen Skeggs struct drm_device *dev = obj->dev; 254673a394bSEric Anholt int ret; 255673a394bSEric Anholt 256673a394bSEric Anholt /* 257673a394bSEric Anholt * Get the user-visible handle using idr. 258673a394bSEric Anholt */ 259673a394bSEric Anholt again: 260673a394bSEric Anholt /* ensure there is space available to allocate a handle */ 261673a394bSEric Anholt if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) 262673a394bSEric Anholt return -ENOMEM; 263673a394bSEric Anholt 264673a394bSEric Anholt /* do the allocation under our spinlock */ 265673a394bSEric Anholt spin_lock(&file_priv->table_lock); 266a1a2d1d3SPekka Paalanen ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep); 267673a394bSEric Anholt spin_unlock(&file_priv->table_lock); 268673a394bSEric Anholt if (ret == -EAGAIN) 269673a394bSEric Anholt goto again; 270673a394bSEric Anholt 271673a394bSEric Anholt if (ret != 0) 272673a394bSEric Anholt return ret; 273673a394bSEric Anholt 274673a394bSEric Anholt drm_gem_object_handle_reference(obj); 275304eda32SBen Skeggs 276304eda32SBen Skeggs if (dev->driver->gem_open_object) { 277304eda32SBen Skeggs ret = dev->driver->gem_open_object(obj, file_priv); 278304eda32SBen Skeggs if (ret) { 279304eda32SBen Skeggs drm_gem_handle_delete(file_priv, *handlep); 280304eda32SBen Skeggs return ret; 281304eda32SBen Skeggs } 282304eda32SBen Skeggs } 283304eda32SBen Skeggs 284673a394bSEric Anholt return 0; 285673a394bSEric Anholt } 286673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create); 287673a394bSEric Anholt 288673a394bSEric Anholt /** Returns a reference to the object named by the handle. */ 289673a394bSEric Anholt struct drm_gem_object * 290673a394bSEric Anholt drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, 291a1a2d1d3SPekka Paalanen u32 handle) 292673a394bSEric Anholt { 293673a394bSEric Anholt struct drm_gem_object *obj; 294673a394bSEric Anholt 295673a394bSEric Anholt spin_lock(&filp->table_lock); 296673a394bSEric Anholt 297673a394bSEric Anholt /* Check if we currently have a reference on the object */ 298673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 299673a394bSEric Anholt if (obj == NULL) { 300673a394bSEric Anholt spin_unlock(&filp->table_lock); 301673a394bSEric Anholt return NULL; 302673a394bSEric Anholt } 303673a394bSEric Anholt 304673a394bSEric Anholt drm_gem_object_reference(obj); 305673a394bSEric Anholt 306673a394bSEric Anholt spin_unlock(&filp->table_lock); 307673a394bSEric Anholt 308673a394bSEric Anholt return obj; 309673a394bSEric Anholt } 310673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup); 311673a394bSEric Anholt 312673a394bSEric Anholt /** 313673a394bSEric Anholt * Releases the handle to an mm object. 314673a394bSEric Anholt */ 315673a394bSEric Anholt int 316673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data, 317673a394bSEric Anholt struct drm_file *file_priv) 318673a394bSEric Anholt { 319673a394bSEric Anholt struct drm_gem_close *args = data; 320673a394bSEric Anholt int ret; 321673a394bSEric Anholt 322673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 323673a394bSEric Anholt return -ENODEV; 324673a394bSEric Anholt 325673a394bSEric Anholt ret = drm_gem_handle_delete(file_priv, args->handle); 326673a394bSEric Anholt 327673a394bSEric Anholt return ret; 328673a394bSEric Anholt } 329673a394bSEric Anholt 330673a394bSEric Anholt /** 331673a394bSEric Anholt * Create a global name for an object, returning the name. 332673a394bSEric Anholt * 333673a394bSEric Anholt * Note that the name does not hold a reference; when the object 334673a394bSEric Anholt * is freed, the name goes away. 335673a394bSEric Anholt */ 336673a394bSEric Anholt int 337673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data, 338673a394bSEric Anholt struct drm_file *file_priv) 339673a394bSEric Anholt { 340673a394bSEric Anholt struct drm_gem_flink *args = data; 341673a394bSEric Anholt struct drm_gem_object *obj; 342673a394bSEric Anholt int ret; 343673a394bSEric Anholt 344673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 345673a394bSEric Anholt return -ENODEV; 346673a394bSEric Anholt 347673a394bSEric Anholt obj = drm_gem_object_lookup(dev, file_priv, args->handle); 348673a394bSEric Anholt if (obj == NULL) 349bf79cb91SChris Wilson return -ENOENT; 350673a394bSEric Anholt 351673a394bSEric Anholt again: 3523e49c4f4SChris Wilson if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) { 3533e49c4f4SChris Wilson ret = -ENOMEM; 3543e49c4f4SChris Wilson goto err; 3553e49c4f4SChris Wilson } 356673a394bSEric Anholt 357673a394bSEric Anholt spin_lock(&dev->object_name_lock); 3588d59bae5SChris Wilson if (!obj->name) { 359673a394bSEric Anholt ret = idr_get_new_above(&dev->object_name_idr, obj, 1, 360673a394bSEric Anholt &obj->name); 3618d59bae5SChris Wilson args->name = (uint64_t) obj->name; 362673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 3638d59bae5SChris Wilson 364673a394bSEric Anholt if (ret == -EAGAIN) 365673a394bSEric Anholt goto again; 366673a394bSEric Anholt 3673e49c4f4SChris Wilson if (ret != 0) 3683e49c4f4SChris Wilson goto err; 369673a394bSEric Anholt 3708d59bae5SChris Wilson /* Allocate a reference for the name table. */ 3718d59bae5SChris Wilson drm_gem_object_reference(obj); 3728d59bae5SChris Wilson } else { 373673a394bSEric Anholt args->name = (uint64_t) obj->name; 3748d59bae5SChris Wilson spin_unlock(&dev->object_name_lock); 3758d59bae5SChris Wilson ret = 0; 3768d59bae5SChris Wilson } 3773e49c4f4SChris Wilson 3783e49c4f4SChris Wilson err: 379bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 3803e49c4f4SChris Wilson return ret; 381673a394bSEric Anholt } 382673a394bSEric Anholt 383673a394bSEric Anholt /** 384673a394bSEric Anholt * Open an object using the global name, returning a handle and the size. 385673a394bSEric Anholt * 386673a394bSEric Anholt * This handle (of course) holds a reference to the object, so the object 387673a394bSEric Anholt * will not go away until the handle is deleted. 388673a394bSEric Anholt */ 389673a394bSEric Anholt int 390673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data, 391673a394bSEric Anholt struct drm_file *file_priv) 392673a394bSEric Anholt { 393673a394bSEric Anholt struct drm_gem_open *args = data; 394673a394bSEric Anholt struct drm_gem_object *obj; 395673a394bSEric Anholt int ret; 396a1a2d1d3SPekka Paalanen u32 handle; 397673a394bSEric Anholt 398673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 399673a394bSEric Anholt return -ENODEV; 400673a394bSEric Anholt 401673a394bSEric Anholt spin_lock(&dev->object_name_lock); 402673a394bSEric Anholt obj = idr_find(&dev->object_name_idr, (int) args->name); 403673a394bSEric Anholt if (obj) 404673a394bSEric Anholt drm_gem_object_reference(obj); 405673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 406673a394bSEric Anholt if (!obj) 407673a394bSEric Anholt return -ENOENT; 408673a394bSEric Anholt 409673a394bSEric Anholt ret = drm_gem_handle_create(file_priv, obj, &handle); 410bc9025bdSLuca Barbieri drm_gem_object_unreference_unlocked(obj); 411673a394bSEric Anholt if (ret) 412673a394bSEric Anholt return ret; 413673a394bSEric Anholt 414673a394bSEric Anholt args->handle = handle; 415673a394bSEric Anholt args->size = obj->size; 416673a394bSEric Anholt 417673a394bSEric Anholt return 0; 418673a394bSEric Anholt } 419673a394bSEric Anholt 420673a394bSEric Anholt /** 421673a394bSEric Anholt * Called at device open time, sets up the structure for handling refcounting 422673a394bSEric Anholt * of mm objects. 423673a394bSEric Anholt */ 424673a394bSEric Anholt void 425673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 426673a394bSEric Anholt { 427673a394bSEric Anholt idr_init(&file_private->object_idr); 428673a394bSEric Anholt spin_lock_init(&file_private->table_lock); 429673a394bSEric Anholt } 430673a394bSEric Anholt 431673a394bSEric Anholt /** 432673a394bSEric Anholt * Called at device close to release the file's 433673a394bSEric Anholt * handle references on objects. 434673a394bSEric Anholt */ 435673a394bSEric Anholt static int 436673a394bSEric Anholt drm_gem_object_release_handle(int id, void *ptr, void *data) 437673a394bSEric Anholt { 438304eda32SBen Skeggs struct drm_file *file_priv = data; 439673a394bSEric Anholt struct drm_gem_object *obj = ptr; 440304eda32SBen Skeggs struct drm_device *dev = obj->dev; 441304eda32SBen Skeggs 442304eda32SBen Skeggs if (dev->driver->gem_close_object) 443304eda32SBen Skeggs dev->driver->gem_close_object(obj, file_priv); 444673a394bSEric Anholt 445bc9025bdSLuca Barbieri drm_gem_object_handle_unreference_unlocked(obj); 446673a394bSEric Anholt 447673a394bSEric Anholt return 0; 448673a394bSEric Anholt } 449673a394bSEric Anholt 450673a394bSEric Anholt /** 451673a394bSEric Anholt * Called at close time when the filp is going away. 452673a394bSEric Anholt * 453673a394bSEric Anholt * Releases any remaining references on objects by this filp. 454673a394bSEric Anholt */ 455673a394bSEric Anholt void 456673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 457673a394bSEric Anholt { 458673a394bSEric Anholt idr_for_each(&file_private->object_idr, 459304eda32SBen Skeggs &drm_gem_object_release_handle, file_private); 460673a394bSEric Anholt 461ddd3d069SChris Wilson idr_remove_all(&file_private->object_idr); 462673a394bSEric Anholt idr_destroy(&file_private->object_idr); 463673a394bSEric Anholt } 464673a394bSEric Anholt 465fd632aa3SDaniel Vetter void 466fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj) 467c3ae90c0SLuca Barbieri { 468*62cb7011SAlan Cox if (obj->filp) 469c3ae90c0SLuca Barbieri fput(obj->filp); 470c3ae90c0SLuca Barbieri } 471fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release); 472c3ae90c0SLuca Barbieri 473673a394bSEric Anholt /** 474673a394bSEric Anholt * Called after the last reference to the object has been lost. 475c3ae90c0SLuca Barbieri * Must be called holding struct_ mutex 476673a394bSEric Anholt * 477673a394bSEric Anholt * Frees the object 478673a394bSEric Anholt */ 479673a394bSEric Anholt void 480673a394bSEric Anholt drm_gem_object_free(struct kref *kref) 481673a394bSEric Anholt { 482673a394bSEric Anholt struct drm_gem_object *obj = (struct drm_gem_object *) kref; 483673a394bSEric Anholt struct drm_device *dev = obj->dev; 484673a394bSEric Anholt 485673a394bSEric Anholt BUG_ON(!mutex_is_locked(&dev->struct_mutex)); 486673a394bSEric Anholt 487673a394bSEric Anholt if (dev->driver->gem_free_object != NULL) 488673a394bSEric Anholt dev->driver->gem_free_object(obj); 489673a394bSEric Anholt } 490673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free); 491673a394bSEric Anholt 492c3ae90c0SLuca Barbieri static void drm_gem_object_ref_bug(struct kref *list_kref) 493c3ae90c0SLuca Barbieri { 494c3ae90c0SLuca Barbieri BUG(); 495c3ae90c0SLuca Barbieri } 496c3ae90c0SLuca Barbieri 497c3ae90c0SLuca Barbieri /** 498673a394bSEric Anholt * Called after the last handle to the object has been closed 499673a394bSEric Anholt * 500673a394bSEric Anholt * Removes any name for the object. Note that this must be 501673a394bSEric Anholt * called before drm_gem_object_free or we'll be touching 502673a394bSEric Anholt * freed memory 503673a394bSEric Anholt */ 50429d08b3eSDave Airlie void drm_gem_object_handle_free(struct drm_gem_object *obj) 505673a394bSEric Anholt { 506673a394bSEric Anholt struct drm_device *dev = obj->dev; 507673a394bSEric Anholt 508673a394bSEric Anholt /* Remove any name for this object */ 509673a394bSEric Anholt spin_lock(&dev->object_name_lock); 510673a394bSEric Anholt if (obj->name) { 511673a394bSEric Anholt idr_remove(&dev->object_name_idr, obj->name); 5128d59bae5SChris Wilson obj->name = 0; 513673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 514673a394bSEric Anholt /* 515673a394bSEric Anholt * The object name held a reference to this object, drop 516673a394bSEric Anholt * that now. 517c3ae90c0SLuca Barbieri * 518c3ae90c0SLuca Barbieri * This cannot be the last reference, since the handle holds one too. 519673a394bSEric Anholt */ 520c3ae90c0SLuca Barbieri kref_put(&obj->refcount, drm_gem_object_ref_bug); 521673a394bSEric Anholt } else 522673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 523673a394bSEric Anholt 524673a394bSEric Anholt } 525673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_handle_free); 526673a394bSEric Anholt 527ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma) 528ab00b3e5SJesse Barnes { 529ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 530ab00b3e5SJesse Barnes 531ab00b3e5SJesse Barnes drm_gem_object_reference(obj); 53231dfbc93SChris Wilson 53331dfbc93SChris Wilson mutex_lock(&obj->dev->struct_mutex); 53431dfbc93SChris Wilson drm_vm_open_locked(vma); 53531dfbc93SChris Wilson mutex_unlock(&obj->dev->struct_mutex); 536ab00b3e5SJesse Barnes } 537ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open); 538ab00b3e5SJesse Barnes 539ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma) 540ab00b3e5SJesse Barnes { 541ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 542b74ad5aeSChris Wilson struct drm_device *dev = obj->dev; 543ab00b3e5SJesse Barnes 544b74ad5aeSChris Wilson mutex_lock(&dev->struct_mutex); 54531dfbc93SChris Wilson drm_vm_close_locked(vma); 54631dfbc93SChris Wilson drm_gem_object_unreference(obj); 547b74ad5aeSChris Wilson mutex_unlock(&dev->struct_mutex); 548ab00b3e5SJesse Barnes } 549ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close); 550ab00b3e5SJesse Barnes 551ab00b3e5SJesse Barnes 552a2c0a97bSJesse Barnes /** 553a2c0a97bSJesse Barnes * drm_gem_mmap - memory map routine for GEM objects 554a2c0a97bSJesse Barnes * @filp: DRM file pointer 555a2c0a97bSJesse Barnes * @vma: VMA for the area to be mapped 556a2c0a97bSJesse Barnes * 557a2c0a97bSJesse Barnes * If a driver supports GEM object mapping, mmap calls on the DRM file 558a2c0a97bSJesse Barnes * descriptor will end up here. 559a2c0a97bSJesse Barnes * 560a2c0a97bSJesse Barnes * If we find the object based on the offset passed in (vma->vm_pgoff will 561a2c0a97bSJesse Barnes * contain the fake offset we created when the GTT map ioctl was called on 562a2c0a97bSJesse Barnes * the object), we set up the driver fault handler so that any accesses 563a2c0a97bSJesse Barnes * to the object can be trapped, to perform migration, GTT binding, surface 564a2c0a97bSJesse Barnes * register allocation, or performance monitoring. 565a2c0a97bSJesse Barnes */ 566a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 567a2c0a97bSJesse Barnes { 568a2c0a97bSJesse Barnes struct drm_file *priv = filp->private_data; 569a2c0a97bSJesse Barnes struct drm_device *dev = priv->minor->dev; 570a2c0a97bSJesse Barnes struct drm_gem_mm *mm = dev->mm_private; 571f77d390cSBenjamin Herrenschmidt struct drm_local_map *map = NULL; 572a2c0a97bSJesse Barnes struct drm_gem_object *obj; 573a2c0a97bSJesse Barnes struct drm_hash_item *hash; 574a2c0a97bSJesse Barnes int ret = 0; 575a2c0a97bSJesse Barnes 576a2c0a97bSJesse Barnes mutex_lock(&dev->struct_mutex); 577a2c0a97bSJesse Barnes 578a2c0a97bSJesse Barnes if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) { 579a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 580a2c0a97bSJesse Barnes return drm_mmap(filp, vma); 581a2c0a97bSJesse Barnes } 582a2c0a97bSJesse Barnes 583a2c0a97bSJesse Barnes map = drm_hash_entry(hash, struct drm_map_list, hash)->map; 584a2c0a97bSJesse Barnes if (!map || 585a2c0a97bSJesse Barnes ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) { 586a2c0a97bSJesse Barnes ret = -EPERM; 587a2c0a97bSJesse Barnes goto out_unlock; 588a2c0a97bSJesse Barnes } 589a2c0a97bSJesse Barnes 590a2c0a97bSJesse Barnes /* Check for valid size. */ 591a2c0a97bSJesse Barnes if (map->size < vma->vm_end - vma->vm_start) { 592a2c0a97bSJesse Barnes ret = -EINVAL; 593a2c0a97bSJesse Barnes goto out_unlock; 594a2c0a97bSJesse Barnes } 595a2c0a97bSJesse Barnes 596a2c0a97bSJesse Barnes obj = map->handle; 597a2c0a97bSJesse Barnes if (!obj->dev->driver->gem_vm_ops) { 598a2c0a97bSJesse Barnes ret = -EINVAL; 599a2c0a97bSJesse Barnes goto out_unlock; 600a2c0a97bSJesse Barnes } 601a2c0a97bSJesse Barnes 602a2c0a97bSJesse Barnes vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND; 603a2c0a97bSJesse Barnes vma->vm_ops = obj->dev->driver->gem_vm_ops; 604a2c0a97bSJesse Barnes vma->vm_private_data = map->handle; 60579cc304fSJeremy Fitzhardinge vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 606a2c0a97bSJesse Barnes 607ab00b3e5SJesse Barnes /* Take a ref for this mapping of the object, so that the fault 608ab00b3e5SJesse Barnes * handler can dereference the mmap offset's pointer to the object. 609ab00b3e5SJesse Barnes * This reference is cleaned up by the corresponding vm_close 610ab00b3e5SJesse Barnes * (which should happen whether the vma was created by this call, or 611ab00b3e5SJesse Barnes * by a vm_open due to mremap or partial unmap or whatever). 612ab00b3e5SJesse Barnes */ 613ab00b3e5SJesse Barnes drm_gem_object_reference(obj); 614ab00b3e5SJesse Barnes 615a2c0a97bSJesse Barnes vma->vm_file = filp; /* Needed for drm_vm_open() */ 616a2c0a97bSJesse Barnes drm_vm_open_locked(vma); 617a2c0a97bSJesse Barnes 618a2c0a97bSJesse Barnes out_unlock: 619a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 620a2c0a97bSJesse Barnes 621a2c0a97bSJesse Barnes return ret; 622a2c0a97bSJesse Barnes } 623a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap); 624